DistAppearanceVariableController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Renderable\DistDistributorTable;
  4. use App\Admin\Repositories\DistAppearance;
  5. use App\Admin\Repositories\DistAppearanceVariable;
  6. use App\Libraries\CommonHelper;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Http\Controllers\AdminController;
  11. use Dcat\Admin\Layout\Content;
  12. use Dcat\Admin\Admin;
  13. use Dcat\Admin\Widgets\Dropdown;
  14. class DistAppearanceVariableController extends AdminController
  15. {
  16. public function index(Content $content)
  17. {
  18. return $content->full()->body($this->grid());
  19. }
  20. /**
  21. * Make a grid builder.
  22. *
  23. * @return Grid
  24. */
  25. protected function grid()
  26. {
  27. $templateCode = isset($_GET['templateCode']) ? $_GET['templateCode'] : 0;
  28. $appearanceId = isset($_GET['appearanceId']) ? intval($_GET['appearanceId']) : 0;
  29. $distId = isset($_GET['distId']) ? intval($_GET['distId']) : 0;
  30. if (empty($appearanceId)) {
  31. die(' <h3>Please select the theme.<h3>');
  32. }
  33. return Grid::make(DistAppearanceVariable::with(['distributor','appearance']), function (Grid $grid) use ($templateCode, $appearanceId, $distId) {
  34. $grid->column('id')->sortable();
  35. $grid->column('distributor.company_name','Distributor Name')->display(function ($company_name) {
  36. if (empty($company_name)) {
  37. return '-';
  38. }
  39. return $company_name;
  40. });
  41. $grid->column('appearance.title','Appearance');
  42. //变量名
  43. $grid->column('variable_name');
  44. //变量类型
  45. $grid->column('variable_type')->using(['1' => 'text', '2' => 'textarea', '3' => 'json']);
  46. //是否是站点变量
  47. $grid->column('templateCode','site variable')->display(function ($templateIds) {
  48. if ($templateIds == 0) {
  49. return 'Yes';
  50. } else {
  51. return 'No';
  52. }
  53. })->label([
  54. '0' => 'success',
  55. '1' => 'default',
  56. ]);;
  57. //搜索
  58. $grid->quickSearch(['variable_name']);
  59. //工具栏
  60. $grid->disableViewButton();
  61. $grid->showQuickEditButton();
  62. $grid->enableDialogCreate();
  63. $grid->disableEditButton();
  64. $addUrl = 'templateCode='. $templateCode . '&appearanceId='. $appearanceId . '&distId='. $distId;
  65. //排序
  66. $grid->model()->where('appearance_id', $appearanceId)->where('dist_id', $distId)->whereIn('template_code', [0, $templateCode])->orderBy("id",'asc');
  67. //增加js 向新增按钮添加参数还有编辑按钮添加参数
  68. CommonHelper::replaceAddEditerUrl('.dialog-create', '.quick-edit', $addUrl);
  69. /*
  70. Admin::script(
  71. <<<JS
  72. var button = $('.dialog-create');
  73. var currentUrl = button.attr('data-url');
  74. if (currentUrl.indexOf('?') === -1) {
  75. button.attr('data-url', currentUrl + '{$addUrl}');
  76. } else {
  77. button.attr('data-url', currentUrl + '{$addUrl}');
  78. }
  79. $('.quick-edit').each(function() {
  80. var currentUrl = $(this).attr('data-url');
  81. if (currentUrl.indexOf('?') === -1) {
  82. $(this).attr('data-url', currentUrl + '{$addUrl}');
  83. } else {
  84. // 如果已经有查询参数,添加 &id=123
  85. $(this).attr('data-url', currentUrl + '{$addUrl}');
  86. }
  87. });
  88. JS
  89. );
  90. */
  91. });
  92. }
  93. /**
  94. * Make a show builder.
  95. *
  96. * @param mixed $id
  97. *
  98. * @return Show
  99. */
  100. // protected function detail($id)
  101. // {
  102. // return Show::make($id, DistAppearanceVariable::with(['distributor','appearance']), function (Show $show) {
  103. // $show->field('id');
  104. // $show->field('distributor.company_name','Distributor Name')->as(function ($company_name) {
  105. // if (empty($company_name)) {
  106. // return 'Site Variable';
  107. // }
  108. // return $company_name;
  109. // });
  110. // $show->field('appearance.title','Appearance Title');
  111. // $show->field('variable_type')->using(['1' => 'text', '2' => 'textarea', '3' => 'json']);
  112. // $show->field('variable_name');
  113. // $show->field('variable_value');
  114. //
  115. // $show->field('created_at');
  116. // $show->field('updated_at');
  117. // });
  118. // }
  119. /**
  120. * Make a form builder.
  121. *
  122. * @return Form
  123. */
  124. protected function form()
  125. {
  126. $templateCode = isset($_GET['templateCode']) ? $_GET['templateCode'] : 0;
  127. $appearanceId = isset($_GET['appearanceId']) ? intval($_GET['appearanceId']) : 0;
  128. $distId = isset($_GET['distId']) ? intval($_GET['distId']) : 0;
  129. return Form::make(new DistAppearanceVariable(), function (Form $form) use ($templateCode, $appearanceId, $distId) {
  130. $form->hidden('appearance_id')->value($appearanceId);
  131. $form->hidden('dist_id')->value($distId);
  132. $form->hidden('template_code')->value($templateCode);
  133. $form->hidden('variable_code');
  134. //编辑时当template_id为0时,site_variable为默认值1,否则为0
  135. $variableDefaul = 0;
  136. if (!$form->isCreating()) {
  137. if ($form->model()->template_id == 0) {
  138. $variableDefaul = 1;
  139. }
  140. }
  141. $form->switch('site_variable')->value($variableDefaul)->width(9,3);
  142. $form->radio('variable_type')->options([
  143. '1'=>'text',
  144. '2'=>'textarea',
  145. '3'=>'json'
  146. ])->width(9,3)->default(1);
  147. $form->text('variable_name')->width(9,3);
  148. $form->textarea('variable_value')->rows(12)->width(9,3);
  149. $form->ignore(['site_variable']);
  150. $form->submitted(function (Form $form) {
  151. if ($form->input('site_variable') == 1) {
  152. $form->template_code = 0;
  153. }
  154. if ($form->isCreating()) {
  155. $form->variable_code = uniqueCode();
  156. }
  157. //检查json格式是否正确
  158. if ($form->input('variable_type') == 3) {
  159. if (!isValidJson($form->input('variable_value'))) {
  160. return $form->response()->error('The JSON format is incorrect');
  161. }
  162. }
  163. });
  164. });
  165. }
  166. }