NoticeCategoryController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace App\Admin\Controllers\Content;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\NoticeCategory;
  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 NoticeCategoryController 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 NoticeCategory);
  76. $grid->model()->orderBy('listorder', 'ASC');
  77. $grid->id('ID');
  78. $grid->category_name('分类名字');
  79. $grid->admin_set('系统分类')->display(function ($admin_set) {
  80. return $admin_set?"是":"否";
  81. });
  82. $grid->listorder('排序');
  83. $grid->created_at('添加时间');
  84. if (Admin::user()->can('content_noticemanager_noticlecategory_create')) {
  85. $grid->disableCreateButton(false);
  86. }
  87. if (Admin::user()->can('content_noticemanager_noticlecategory_delete')) {
  88. $grid->tools(function ($tools) {
  89. $tools->batch(function ($batch) {
  90. $batch->disableDelete(false);
  91. });
  92. });
  93. } else {
  94. $grid->disableRowSelector();
  95. }
  96. $grid->actions(function ($actions) {
  97. if (Admin::user()->can('content_noticemanager_noticlecategory_edit')) {
  98. $actions->disableEdit(false);
  99. }
  100. if (Admin::user()->can('content_noticemanager_noticlecategory_delete') && $actions->row['admin_set']!=1) {
  101. $actions->disableDelete(false);
  102. }
  103. });
  104. $grid->disableFilter();
  105. return $grid;
  106. }
  107. /**
  108. * Make a show builder.
  109. *
  110. * @param mixed $id
  111. * @return Show
  112. */
  113. protected function detail($id)
  114. {
  115. $show = new Show(NoticeCategory::findOrFail($id));
  116. $show->id('ID');
  117. $show->category_name('分类名字');
  118. $show->admin_set('是否系统默认分类')->display(function ($admin_set) {
  119. return $admin_set?"是":"否";
  120. });
  121. $show->listorder('排序');
  122. $show->created_at('Created at');
  123. $show->updated_at('Updated at');
  124. /* $show->panel()
  125. ->tools(function ($tools) {
  126. $tools->disableEdit();
  127. $tools->disableDelete();
  128. });*/
  129. return $show;
  130. }
  131. /**
  132. * Make a form builder.
  133. *
  134. * @return Form
  135. */
  136. protected function form()
  137. {
  138. $form = new Form(new NoticeCategory);
  139. $form->text('category_name', '分类名字')->rules('required|max:10', array('required'=>'分类名称不能为空。','max'=>'分类名称长度不能大于25个字符。'))->setWidth(4)->setMustMark();
  140. $form->number('listorder', '排序')->min(1)->max(255)->default(255)->rules('required', array('required'=>'排序不能为空。'));
  141. $form->hidden('admin_set')->value(0);
  142. $form->footer(function ($footer) {
  143. $footer->disableViewCheck();
  144. $footer->disableEditingCheck();
  145. $footer->disableCreatingCheck();
  146. $footer->disableReset();
  147. });
  148. $form->tools(function (Form\Tools $tools) {
  149. $tools->disableDelete();
  150. $tools->disableView();
  151. });
  152. return $form;
  153. }
  154. /*
  155. * 重写批量删除方法
  156. */
  157. public function destroy($id)
  158. {
  159. $id_arr = explode(',', $id);
  160. $admin_set_cates = NoticeCategory::where(array('admin_set'=>'1'))->select('id')->get()->pluck('id')->toArray();
  161. foreach ($id_arr as $k => $v) {
  162. if (in_array($v, $admin_set_cates)) {
  163. unset($id_arr[$k]);
  164. }
  165. }
  166. $filter_id = implode(',', $id_arr);
  167. if ($this->form()->destroy($filter_id)) {
  168. $data = [
  169. 'status' => true,
  170. 'message' => trans('admin.delete_succeeded'),
  171. ];
  172. } else {
  173. $data = [
  174. 'status' => false,
  175. 'message' => trans('admin.delete_failed'),
  176. ];
  177. }
  178. return response()->json($data);
  179. }
  180. }