ExplainCategoryController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Admin\Controllers\Content;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\ExplainCategory;
  5. use Encore\Admin\Controllers\HasResourceActions;
  6. use Encore\Admin\Facades\Admin;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Layout\Content;
  10. use Encore\Admin\Show;
  11. class ExplainCategoryController extends Controller
  12. {
  13. use HasResourceActions;
  14. /**
  15. * Index interface.
  16. *
  17. * @param Content $content
  18. * @return Content
  19. */
  20. public function index(Content $content)
  21. {
  22. return $content
  23. ->header('说明页分类')
  24. ->description('系统分类不能删除')
  25. ->body($this->grid());
  26. }
  27. /**
  28. * Show interface.
  29. *
  30. * @param mixed $id
  31. * @param Content $content
  32. * @return Content
  33. */
  34. public function show($id, Content $content)
  35. {
  36. return $content
  37. ->header('说明页分类')
  38. ->description(' ')
  39. ->body($this->detail($id));
  40. }
  41. /**
  42. * Edit interface.
  43. *
  44. * @param mixed $id
  45. * @param Content $content
  46. * @return Content
  47. */
  48. public function edit($id, Content $content)
  49. {
  50. return $content
  51. ->header('说明页分类')
  52. ->description(' ')
  53. ->body($this->form()->edit($id));
  54. }
  55. /**
  56. * Create interface.
  57. *
  58. * @param Content $content
  59. * @return Content
  60. */
  61. public function create(Content $content)
  62. {
  63. return $content
  64. ->header('说明页分类')
  65. ->description(' ')
  66. ->body($this->form());
  67. }
  68. /**
  69. * Make a grid builder.
  70. *
  71. * @return Grid
  72. */
  73. protected function grid()
  74. {
  75. $grid = new Grid(new ExplainCategory);
  76. $grid->model()->orderBy('list_order', 'DESC')->orderBy('created_at', 'DESC');
  77. $grid->category_name('分类名称');
  78. $grid->admin_set('类型')->display(function ($admin_set) {
  79. return $admin_set? "系统分类":"自定义分类";
  80. });
  81. $grid->list_order('排序');
  82. $grid->created_at('添加时间');
  83. $grid->tools(function (Grid\Tools $tools) {
  84. if (Admin::user()->can('content_explain_category_delete')) {
  85. $tools->batch(function ($batch) {
  86. $batch->disableDelete(false);
  87. });
  88. }
  89. });
  90. if (Admin::user()->can('content_explain_category_create')) {
  91. $grid->disableCreateButton(false);
  92. }
  93. $grid->actions(function ($actions) {
  94. if (Admin::user()->can('content_explain_category_edit')) {
  95. $actions->disableEdit(false);
  96. }
  97. if (Admin::user()->can('content_explain_category_delete') && $actions->row['admin_set']!=1) {
  98. $actions->disableDelete(false);
  99. }
  100. });
  101. $grid->disableFilter();
  102. return $grid;
  103. }
  104. /**
  105. * Make a show builder.
  106. *
  107. * @param mixed $id
  108. * @return Show
  109. */
  110. protected function detail($id)
  111. {
  112. $show = new Show(ExplainCategory::findOrFail($id));
  113. $show->id('ID');
  114. $show->category_name('分类名称');
  115. $show->admin_set('是否系统分类');
  116. $show->list_order('排序');
  117. $show->created_at('添加时间');
  118. $show->updated_at('更新时间');
  119. return $show;
  120. }
  121. /**
  122. * Make a form builder.
  123. *
  124. * @return Form
  125. */
  126. protected function form()
  127. {
  128. $form = new Form(new ExplainCategory);
  129. $form->text('category_name', '分类名称')->rules('required|max:10', array('required'=>'分类名称不能为空。','max'=>'分类名称长度不能大于25个字符。'))->setWidth(4)->setMustMark();
  130. $form->number('list_order', '排序')->min(0)->default(0)->rules('required|min:0', array('required'=>'排序不能为空。','min'=>'排序不能小于0'))->help('(数字越大越靠前)');
  131. $form->hidden('admin_set')->value(0);
  132. $form->footer(function ($footer) {
  133. $footer->disableViewCheck();
  134. $footer->disableEditingCheck();
  135. $footer->disableCreatingCheck();
  136. $footer->disableReset();
  137. });
  138. $form->tools(function (Form\Tools $tools) {
  139. $tools->disableDelete();
  140. $tools->disableView();
  141. });
  142. return $form;
  143. }
  144. /*
  145. * 重写批量删除方法
  146. */
  147. public function destroy($id)
  148. {
  149. $id_arr = explode(',', $id);
  150. $admin_set_cates = ExplainCategory::where(array('admin_set'=>'1'))->select('id')->get()->pluck('id')->toArray();
  151. foreach ($id_arr as $k => $v) {
  152. if (in_array($v, $admin_set_cates)) {
  153. unset($id_arr[$k]);
  154. }
  155. }
  156. $filter_id = implode(',', $id_arr);
  157. if ($this->form()->destroy($filter_id)) {
  158. $data = [
  159. 'status' => true,
  160. 'message' => trans('admin.delete_succeeded'),
  161. ];
  162. } else {
  163. $data = [
  164. 'status' => false,
  165. 'message' => trans('admin.delete_failed'),
  166. ];
  167. }
  168. return response()->json($data);
  169. }
  170. }