HelpCategoryController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace App\Admin\Controllers\Content;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\HelpCategory;
  5. use Encore\Admin\Controllers\HasResourceActions;
  6. use Encore\Admin\Facades\Admin as SysAdmin;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Layout\Column;
  10. use Encore\Admin\Layout\Content;
  11. use Encore\Admin\Layout\Row;
  12. use Encore\Admin\Show;
  13. use Encore\Admin\Tree;
  14. use Encore\Admin\Widgets\Box;
  15. //use Encore\Admin\Grid;
  16. class HelpCategoryController extends Controller
  17. {
  18. use HasResourceActions;
  19. /**
  20. * Index interface.
  21. *
  22. * @param Content $content
  23. * @return Content
  24. */
  25. public function index(Content $content)
  26. {
  27. return $content
  28. ->header(trans('帮助分类'))
  29. ->description(trans('删除顶级分类将会自动删除此分类下的子分类。'))
  30. ->row(function (Row $row) {
  31. if (SysAdmin::user()->can('content_help_category_create')) {
  32. $row->column(6, $this->tree()->render());
  33. $row->column(6, function (Column $column) {
  34. $form = new \Encore\Admin\Widgets\Form();
  35. $form->action(route('help.category.index'));
  36. $cate_option = HelpCategory::where(array('parent_id'=>0))->orderBy('list_order', 'desc')->orderBy('created_at', 'desc')->get()->pluck('category_name', 'id');
  37. $cate_option = $cate_option->toArray();
  38. $cate_option = array('0'=>'顶级分类')+$cate_option;
  39. $form->select('parent_id', '所属分类')->options($cate_option)->default(0)->setMustMark();
  40. $form->text('category_name', '分类名称')->attribute('maxlength', '25')->setMustMark();
  41. $form->number('list_order', '排序')->min(0)->default(0)->help('(数字越大越靠前)')->attribute('maxlength', '10')->setMustMark();
  42. $form->hidden('_token')->default(csrf_token());
  43. $form->disableReset();
  44. $column->append((new Box(trans('admin.new'), $form))->style('success'));
  45. });
  46. } else {
  47. $row->column(12, $this->tree()->render());
  48. }
  49. });
  50. }
  51. protected function tree()
  52. {
  53. return HelpCategory::tree(function (Tree $tree) {
  54. $tree->branch(function ($branch) {
  55. return "{$branch['category_name']} (id:{$branch['id']})";
  56. });
  57. $tree->setView(array(
  58. 'tree' => 'admin::tree',
  59. 'branch' => 'admin.content.tree_branch'
  60. ));
  61. $tree->query(function ($model) {
  62. return $model->OrderBy('list_order', 'desc')->OrderBy('created_at', 'desc');
  63. });
  64. });
  65. }
  66. /**
  67. * Show interface.
  68. *
  69. * @param mixed $id
  70. * @param Content $content
  71. * @return Content
  72. */
  73. public function show($id, Content $content)
  74. {
  75. return $content
  76. ->header('帮助分类')
  77. ->description(' ')
  78. ->body($this->detail($id));
  79. }
  80. /**
  81. * Edit interface.
  82. *
  83. * @param mixed $id
  84. * @param Content $content
  85. * @return Content
  86. */
  87. public function edit($id, Content $content)
  88. {
  89. return $content
  90. ->header('帮助分类')
  91. ->description(' ')
  92. ->body($this->editForm()->edit($id));
  93. }
  94. /**
  95. * Create interface.
  96. *
  97. * @param Content $content
  98. * @return Content
  99. */
  100. public function create(Content $content)
  101. {
  102. return $content
  103. ->header('帮助分类')
  104. ->description(' ')
  105. ->body($this->form());
  106. }
  107. /**
  108. * Make a grid builder.
  109. *
  110. * @return Grid
  111. */
  112. protected function grid()
  113. {
  114. $grid = new Grid(new HelpCategory);
  115. $grid->model()->orderBy('parent_id', 'asc')->orderBy('list_order', 'DESC');
  116. $grid->id('ID');
  117. $grid->category_name('分类名称');
  118. $grid->parent()->category_name('上级分类');
  119. $grid->list_order('排序');
  120. $grid->created_at('添加时间');
  121. $grid->updated_at('更新时间');
  122. $grid->disableFilter();
  123. return $grid;
  124. }
  125. /**
  126. * Make a show builder.
  127. *
  128. * @param mixed $id
  129. * @return Show
  130. */
  131. protected function detail($id)
  132. {
  133. $show = new Show(HelpCategory::findOrFail($id));
  134. $show->id('ID');
  135. $show->category_name('分类名称');
  136. $show->parent()->category_name('上级分类');
  137. $show->list_order('排序');
  138. $show->created_at('添加时间');
  139. $show->updated_at('更新时间');
  140. return $show;
  141. }
  142. /**
  143. * Make a form builder.
  144. *
  145. * @return Form
  146. */
  147. protected function form()
  148. {
  149. $form = new Form(new HelpCategory);
  150. $cate_option = HelpCategory::where(array('parent_id'=>0))->get()->pluck('category_name', 'id');
  151. $cate_option = HelpCategory::selectOptions();
  152. $cate_option[0] ='顶级分类';
  153. $form->select('parent_id', '所属分类')->options($cate_option)->rules('required', array('required'=>'请选择所属分类。'));
  154. $form->text('category_name', '分类名称')->rules('required', array('required'=>'分类名称不能为空。'));
  155. $form->number('list_order', '排序')->min(0)->default(0)
  156. ->attribute('maxlength', '10')->rules('required', array('required'=>'排序不能为空。'))->help('(数字越大越靠前)');
  157. return $form;
  158. }
  159. protected function editForm()
  160. {
  161. $form = new Form(new HelpCategory);
  162. $cate_option = HelpCategory::where(array('parent_id'=>0))->OrderBy('list_order', 'DESC')->orderBy('created_at', 'DESC')->get()->pluck('category_name', 'id');
  163. $cate_option = $cate_option->toArray();
  164. $cate_option = array('0'=>'顶级分类')+$cate_option;
  165. $form->select('parent_id', '所属分类')->options($cate_option)->rules('required', array('required'=>'请选择所属分类。'))->setWidth(4)->setMustMark();
  166. $form->text('category_name', '分类名称')->rules('required', array('required'=>'分类名称不能为空。'))->attribute('maxlength', '25')->setWidth(4)->setMustMark();
  167. $form->number('list_order', '排序')->min(0)->default(0)->attribute('maxlength', '10')
  168. ->rules('required', array('required'=>'排序不能为空。'))->help('(数字越大越靠前)')->setMustMark();
  169. $form->footer(function ($footer) {
  170. $footer->disableViewCheck();
  171. $footer->disableEditingCheck();
  172. $footer->disableCreatingCheck();
  173. $footer->disableReset();
  174. });
  175. $form->tools(function (Form\Tools $tools) {
  176. $tools->disableDelete();
  177. $tools->disableView();
  178. });
  179. return $form;
  180. }
  181. public function update($id)
  182. {
  183. return $this->editForm()->update($id);
  184. }
  185. public function destroys($id)
  186. {
  187. $id_arr = explode(',', $id);
  188. $children_ids = HelpCategory::whereIn('parent_id', $id_arr)->select('id')->get()->pluck('id')->toArray();
  189. if ($children_ids) {
  190. $id_arr = array_merge($id_arr, $children_ids);
  191. }
  192. $filter_id = implode(',', $id_arr);
  193. if ($this->form()->destroy($filter_id)) {
  194. $data = [
  195. 'status' => true,
  196. 'message' => trans('admin.delete_succeeded'),
  197. ];
  198. } else {
  199. $data = [
  200. 'status' => false,
  201. 'message' => trans('admin.delete_failed'),
  202. ];
  203. }
  204. return response()->json($data);
  205. }
  206. }