custom.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-03-15 21:16
  7. */
  8. /**
  9. * @var $this yii\web\View
  10. * @var $model common\models\Options
  11. * @var $settings array
  12. */
  13. use backend\widgets\ActiveForm;
  14. use common\widgets\JsBlock;
  15. use common\libs\Constants;
  16. use yii\helpers\Url;
  17. use backend\widgets\Ueditor;
  18. $this->title = Yii::t('app', 'Custom Setting');
  19. $this->params['breadcrumbs'][] = Yii::t('app', 'Custom Setting');
  20. ?>
  21. <div class="row">
  22. <div class="col-sm-12">
  23. <div class="ibox float-e-margins">
  24. <?=$this->render('/widgets/_ibox-title')?>
  25. <div class="ibox-content">
  26. <?php
  27. $form = ActiveForm::begin();
  28. foreach ($settings as $index => $setting) {
  29. $deleteUrl = Url::to(['custom-delete', 'id' => $setting->id]);
  30. $editUrl = Url::to(['custom-update', 'id' => $setting->id]);
  31. $template = "{label}\n<div class=\"col-sm-8\">{input}\n{error}</div>\n{hint}<div class='col-sm-2'><span class='tips'><i class='fa fa-info-circle'></i> {$setting->tips} <a class='btn-delete' href='{$deleteUrl}' data-confirm='" . Yii::t('app', 'Are you sure you want to delete this item?') . "' title='' data-method='' data-pjax='1'><i style='float: right' class='fa fa-trash-o'></i></a><a href='{$editUrl}' class='btn_edit' title='编辑' data-pjax=''><i style='float: right;margin-right: 10px;' class='fa fa-edit'></i></a> </span></div>";
  32. if ($setting->input_type == Constants::INPUT_UEDITOR) {
  33. echo $form->field($setting, "[$index]value", ['template' => $template])
  34. ->label($setting->name)
  35. ->widget(Ueditor::className(), ['name' => 'value' . $index]);
  36. } else if($setting->input_type == Constants::INPUT_IMG){
  37. echo $form->field($setting,"[$index]value", ['template'=>"{label}\n<div class=\"col-sm-8 image\">{input}<div style='position: relative'>{img}{actions}</div>\n{error}</div>\n{hint}<div class='col-sm-2'><span class='tips'><i class='fa fa-info-circle'></i> {$setting->tips} <a class='btn-delete' href='{$deleteUrl}' title='' data-confirm='' data-method='' data-pjax='1'><i style='float: right' class='fa fa-trash-o'></i></a><a href='{$editUrl}' class='btn_edit' title='编辑' data-pjax=''><i style='float: right;margin-right: 10px;' class='fa fa-edit'></i></a> </span></div>"])
  38. ->label($setting->name)
  39. ->imgInput( ['value' => $setting->value, 'style'=>"max-width:300px;max-height:200px"] );
  40. } else {
  41. if ($setting->input_type == Constants::INPUT_INPUT) {
  42. echo $form->field($setting, "[$index]value", ['template' => $template])
  43. ->label($setting->name)
  44. ->textInput();
  45. } else {
  46. echo $form->field($setting, "[$index]value", ['template' => $template])
  47. ->label($setting->name)
  48. ->textarea();
  49. }
  50. }
  51. ?>
  52. <div class="hr-line-dashed"></div>
  53. <?php
  54. }
  55. ?>
  56. <div class="form-group">
  57. <label class="col-sm-2 control-label"></label>
  58. <div class="col-sm-8">
  59. <a style="float:right;" type="button" class="btn btn-outline btn-default"
  60. id="add"><?= Yii::t('app', 'Add') ?></a>
  61. </div>
  62. </div>
  63. <?= $form->defaultButtons() ?>
  64. <?php ActiveForm::end(); ?>
  65. </div>
  66. </div>
  67. </div>
  68. </div>
  69. <?php JsBlock::begin() ?>
  70. <script>
  71. $(document).ready(function () {
  72. $('#add').click(function () {
  73. $.ajax({
  74. url: "<?=Url::toRoute("setting/custom-create")?>",
  75. success: function (data) {
  76. layer.open({
  77. type: 1,
  78. title: "<?=Yii::t('app', 'Create')?>",
  79. maxmin: true,
  80. shadeClose: true, //点击遮罩关闭层
  81. area: ['70%', '80%'],
  82. content: data,
  83. });
  84. $("form[name=custom]").bind('submit', function () {
  85. var $form = $(this);
  86. $.ajax({
  87. url: $form.attr('action'),
  88. type: "post",
  89. data: $form.serialize(),
  90. beforeSend: function () {
  91. layer.load(2,{
  92. shade: [0.1,'#fff'] //0.1透明度的白色背景
  93. });
  94. },
  95. success: function (data) {
  96. location.href = "<?=Url::toRoute(['custom'])?>";
  97. },
  98. error: function (jqXHR, textStatus, errorThrown) {
  99. layer.alert(jqXHR.responseJSON.message, {
  100. title:tips.error,
  101. btn: [tips.ok],
  102. icon: 2,
  103. skin: 'layer-ext-moon'
  104. })
  105. },
  106. complete: function () {
  107. layer.closeAll("loading");
  108. }
  109. });
  110. return false;
  111. });
  112. $("select#options-input_type").bind('change', onCheckCanTypeInValue).trigger('change');
  113. },
  114. error: function (XMLHttpRequest, textStatus, errorThrown) {
  115. alert("ajax错误," + textStatus + ' : ' + errorThrown);
  116. },
  117. complete: function (XMLHttpRequest, textStatus) {
  118. }
  119. });
  120. return false;
  121. })
  122. $("a.btn_edit").click(function () {
  123. var name = $(this).parents("div.form-group").children("label").html();
  124. $.ajax({
  125. url: $(this).attr('href'),
  126. success: function (data) {
  127. layer.open({
  128. type: 1,
  129. title: '<?=Yii::t('app', 'Update')?> ' + name,
  130. maxmin: true,
  131. shadeClose: true, //点击遮罩关闭层
  132. area: ['70%', '80%'],
  133. content: data,
  134. });
  135. $("form[name=custom]").bind('submit', function () {
  136. var $form = $(this);
  137. $.ajax({
  138. url: $form.attr('action'),
  139. type: "post",
  140. data: $form.serialize(),
  141. beforeSend: function () {
  142. layer.load(2,{
  143. shade: [0.1,'#fff'] //0.1透明度的白色背景
  144. });
  145. },
  146. success: function (data) {
  147. location.href = "<?=Url::toRoute(['custom'])?>";
  148. },
  149. error: function (jqXHR, textStatus, errorThrown) {
  150. layer.alert(jqXHR.responseJSON.message, {
  151. title:tips.error,
  152. btn: [tips.ok],
  153. icon: 2,
  154. skin: 'layer-ext-moon'
  155. })
  156. },
  157. complete: function () {
  158. layer.closeAll("loading");
  159. }
  160. });
  161. return false;
  162. });
  163. $("select#options-input_type").bind('change', onCheckCanTypeInValue).trigger('change');
  164. },
  165. error: function (XMLHttpRequest, textStatus, errorThrown) {
  166. alert("ajax错误," + textStatus + ' : ' + errorThrown);
  167. },
  168. complete: function (XMLHttpRequest, textStatus) {
  169. }
  170. });
  171. return false;
  172. })
  173. });
  174. function onCheckCanTypeInValue() {
  175. var type = $(this).val();
  176. var restrictTypeTips = '<?=Yii::t('app', 'Type restrict, please type in after create')?>';
  177. var input = $("form[name=custom] input#options-value");
  178. if(type != <?=Constants::INPUT_INPUT?> && type != <?=Constants::INPUT_TEXTAREA?>){
  179. if( input.val() == restrictTypeTips ){
  180. input.val(input.attr('oldValue'));
  181. }else{
  182. input.attr('oldValue', input.val());
  183. }
  184. input.val(restrictTypeTips).attr('disabled', true);
  185. }else{
  186. if( input.val() == '<?=Yii::t('app', 'Type restrict, please type in after create')?>' ){
  187. input.val(input.attr('oldValue'));
  188. }else{
  189. input.attr('oldValue', input.val());
  190. }
  191. input.attr('disabled', false);
  192. }
  193. }
  194. </script>
  195. <?php JsBlock::end() ?>