HrtoolsCategoryController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Admin\Controllers\Content;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\HrtoolsCategory;
  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 HrtoolsCategoryController 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('HR工具箱分类')
  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('HR工具箱分类')
  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('HR工具箱分类')
  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('HR工具箱分类')
  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 HrtoolsCategory);
  76. $grid->model()->orderBy('list_order', 'desc')->orderBy('created_at', 'desc');
  77. $grid->id('ID');
  78. $grid->category_name('分类名称')->display(function () {
  79. return $this->category_name.' [id:'.$this->id.']';
  80. });
  81. $grid->admin_set('类型')->display(function () {
  82. return $this->admin_set?'系统分类':'自定义分类';
  83. });
  84. $grid->list_order('排序');
  85. $grid->created_at('添加时间');
  86. /*
  87. $grid->updated_at('更新时间');
  88. $grid->tools(function (Grid\Tools $tools) {
  89. $tools->disableRefreshButton();
  90. });
  91. */
  92. $grid->disableFilter();
  93. //新增按钮
  94. if (Admin::user()->can('content_hrtools_category_create')) {
  95. $grid->disableCreateButton(false);
  96. }
  97. //批量删除
  98. if (Admin::user()->can('content_hrtools_category_delete')) {
  99. $grid->tools(function ($tools) {
  100. $tools->batch(function ($batch) {
  101. $batch->disableDelete(false);
  102. });
  103. });
  104. } else {
  105. $grid->disableRowSelector();
  106. }
  107. $grid->actions(function ($actions) {
  108. if (Admin::user()->can('content_hrtools_category_edit')) {
  109. $actions->disableEdit(false);
  110. }
  111. if (Admin::user()->can('content_hrtools_category_delete') && $actions->row['admin_set']!=1) {
  112. $actions->disableDelete(false);
  113. }
  114. });
  115. return $grid;
  116. }
  117. /**
  118. * Make a show builder.
  119. *
  120. * @param mixed $id
  121. * @return Show
  122. */
  123. protected function detail($id)
  124. {
  125. $show = new Show(HrtoolsCategory::findOrFail($id));
  126. $show->id('ID');
  127. $show->category_name('分类名称');
  128. $show->category_img('缩略图')->image();
  129. $show->content('描述')->setEscape(false);
  130. $show->list_order('排序');
  131. $show->admin_set('系统分类')->as(function ($admin_set) {
  132. return $admin_set?'是':'否';
  133. });
  134. $show->created_at('添加时间');
  135. $show->updated_at('更新时间');
  136. /*$show->panel()
  137. ->tools(function ($tools) {
  138. $tools->disableEdit();
  139. $tools->disableDelete();
  140. });*/
  141. return $show;
  142. }
  143. /**
  144. * Make a form builder.
  145. *
  146. * @return Form
  147. */
  148. protected function form()
  149. {
  150. $form = new Form(new HrtoolsCategory);
  151. $form->text('category_name', '分类名称')->rules('required|max:30', array('分类名称不能为空!','max'=>'分类名称长度不能大于30。'))->setWidth(4)->setMustMark();
  152. $form->image('category_img', '缩略图')->uniqueName()->rules('image|mimes:gif,jpeg,bmp,png', array('image'=>'缩略图必须是图片文件。','mimes'=>'缩略图文件格式不正确。'))->setWidth(4);
  153. $form->textarea('content', '描述')->rules('max:100', array('max'=>'描述长度不能大于100。'));
  154. $form->number('list_order', '排序')->min(0)->default(0)->rules('required|max:10', array('required'=>'排序不能为空。','max'=>'排序长度不能大于10。'))->help('(数字越大越靠前)')->setMustMark();
  155. $form->hidden('admin_set')->value(0);
  156. $form->footer(function ($footer) {
  157. $footer->disableViewCheck();
  158. $footer->disableEditingCheck();
  159. $footer->disableCreatingCheck();
  160. $footer->disableReset();
  161. });
  162. $form->tools(function (Form\Tools $tools) {
  163. $tools->disableDelete();
  164. $tools->disableView();
  165. });
  166. return $form;
  167. }
  168. public function destroy($id)
  169. {
  170. $id_arr = explode(',', $id);
  171. $admin_set_cates = HrtoolsCategory::where(array('admin_set'=>'1'))->select('id')->get()->pluck('id')->toArray();
  172. foreach ($id_arr as $k => $v) {
  173. if (in_array($v, $admin_set_cates)) {
  174. unset($id_arr[$k]);
  175. }
  176. }
  177. $filter_id = implode(',', $id_arr);
  178. if ($this->form()->destroy($filter_id)) {
  179. $data = [
  180. 'status' => true,
  181. 'message' => trans('admin.delete_succeeded'),
  182. ];
  183. } else {
  184. $data = [
  185. 'status' => false,
  186. 'message' => trans('admin.delete_failed'),
  187. ];
  188. }
  189. return response()->json($data);
  190. }
  191. }