NavigationCategoryController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\NavigationCategory;
  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. use Illuminate\Validation\Rule;
  12. class NavigationCategoryController extends Controller
  13. {
  14. use HasResourceActions;
  15. /**
  16. * Index interface.
  17. *
  18. * @param Content $content
  19. * @return Content
  20. */
  21. public function index(Content $content)
  22. {
  23. return $content
  24. ->header('导航分类')
  25. ->description('')
  26. ->body($this->grid());
  27. }
  28. /**
  29. * Show interface.
  30. *
  31. * @param mixed $id
  32. * @param Content $content
  33. * @return Content
  34. */
  35. public function show($id, Content $content)
  36. {
  37. return $content
  38. ->header('导航分类')
  39. ->description('详细')
  40. ->body($this->detail($id));
  41. }
  42. /**
  43. * Edit interface.
  44. *
  45. * @param mixed $id
  46. * @param Content $content
  47. * @return Content
  48. */
  49. public function edit($id, Content $content)
  50. {
  51. return $content
  52. ->header('导航分类')
  53. ->description('编辑')
  54. ->body($this->editForm($id)->edit($id));
  55. }
  56. /**
  57. * Create interface.
  58. *
  59. * @param Content $content
  60. * @return Content
  61. */
  62. public function create(Content $content)
  63. {
  64. return $content
  65. ->header('导航分类')
  66. ->description('创建')
  67. ->body($this->createForm());
  68. }
  69. /**
  70. * Make a grid builder.
  71. *
  72. * @return Grid
  73. */
  74. protected function grid()
  75. {
  76. $grid = new Grid(new NavigationCategory);
  77. $grid->id('ID');
  78. $grid->alias('分类别名')->width(150);
  79. $grid->name('分类名称')->width(150);
  80. $grid->admin_set('类别')->display(function ($admin_set) {
  81. return $admin_set ? '系统内置' : '自定义';
  82. });
  83. $grid->created_at('添加时间');
  84. $grid->updated_at('更新时间');
  85. $grid->actions(function ($actions) use ($grid) {
  86. if (Admin::user()->can('system_nav_type_edit')) {
  87. $actions->disableEdit(false);
  88. }
  89. if (Admin::user()->can('system_nav_type_delete')) {
  90. $actions->disableDelete(false);
  91. }
  92. });
  93. if (Admin::user()->can('system_nav_type_delete')) {
  94. $grid->tools(function ($tools) {
  95. $tools->batch(function ($batch) {
  96. $batch->disableDelete(false);
  97. });
  98. });
  99. $grid->disableRowSelector(false);
  100. }
  101. if (Admin::user()->can('system_nav_type_create')) {
  102. $grid->disableCreateButton(false);
  103. }
  104. return $grid;
  105. }
  106. /**
  107. * Make a show builder.
  108. *
  109. * @param mixed $id
  110. * @return Show
  111. */
  112. protected function detail($id)
  113. {
  114. $show = new Show(NavigationCategory::findOrFail($id));
  115. $show->id('ID');
  116. $show->alias('分类别名');
  117. $show->name('分类名称');
  118. $show->admin_set('类别')->as(function ($admin_set) {
  119. return $admin_set ? '系统内置' : '自定义';
  120. });
  121. $show->created_at('添加时间');
  122. $show->updated_at('更新时间');
  123. return $show;
  124. }
  125. /**
  126. * Make a form builder.
  127. *
  128. * @return Form
  129. */
  130. protected function editForm($id)
  131. {
  132. $form = new Form(new NavigationCategory);
  133. $form->text('alias', '分类别名')->rules([
  134. 'required',
  135. Rule::unique('navigation_categorys')->ignore($id),
  136. ])->setWidth(3)->setMustMark();
  137. $form->text('name', '分类名称')->rules([
  138. 'required',
  139. Rule::unique('navigation_categorys')->ignore($id),
  140. ])->setWidth(3)->setMustMark();
  141. $form->radio('admin_set', '类别')->options(['1'=>'系统内置',0=>'自定义']);
  142. return $form;
  143. }
  144. protected function createForm()
  145. {
  146. $form = new Form(new NavigationCategory);
  147. $form->text('alias', '分类别名')->rules([
  148. 'required',
  149. 'unique:navigation_categorys',
  150. ])->setWidth(3)->setMustMark();
  151. $form->text('name', '分类名称')->rules([
  152. 'required',
  153. 'unique:navigation_categorys',
  154. ])->setWidth(3)->setMustMark();
  155. $form->radio('admin_set', '类别')->options(['1'=>'系统内置',0=>'自定义']);
  156. return $form;
  157. }
  158. /**
  159. * Store a newly created resource in storage.
  160. *
  161. * @return mixed
  162. */
  163. public function store()
  164. {
  165. return $this->createForm()->store();
  166. }
  167. /**
  168. * Update the specified resource in storage.
  169. *
  170. * @param int $id
  171. *
  172. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  173. */
  174. public function update($id)
  175. {
  176. return $this->editForm($id)->update($id);
  177. }
  178. protected function form()
  179. {
  180. $form = new Form(new NavigationCategory);
  181. $form->display('ID');
  182. $form->display('添加时间');
  183. $form->display('更新时间');
  184. return $form;
  185. }
  186. }