DistAppearanceVariableController.php 6.2 KB

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