CategoryGroupsController.php 5.7 KB

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