ArticleCateController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Module\Article\AdminControllers;
  3. use App\Module\Article\AdminControllers\Helper\FilterHelper;
  4. use App\Module\Article\AdminControllers\Helper\FormHelper;
  5. use App\Module\Article\AdminControllers\Helper\GridHelper;
  6. use App\Module\Article\AdminControllers\Helper\ShowHelper;
  7. use App\Module\Article\Enums\STATUS;
  8. use App\Module\Article\Models\ArticleCate;
  9. use Dcat\Admin\Form;
  10. use Dcat\Admin\Grid;
  11. use Dcat\Admin\Layout\Content;
  12. use Dcat\Admin\Layout\Row;
  13. use Dcat\Admin\Show;
  14. use Dcat\Admin\Tree;
  15. use Spatie\RouteAttributes\Attributes\Resource;
  16. use UCore\DcatAdmin\AdminController;
  17. /**
  18. * 文章分类控制器
  19. */
  20. #[Resource('article-cates', names: 'dcat.admin.article-cates')]
  21. class ArticleCateController extends AdminController
  22. {
  23. /**
  24. * 页面标题
  25. *
  26. * @var string
  27. */
  28. protected $title = '文章分类管理';
  29. /**
  30. * 首页
  31. *
  32. * @param Content $content
  33. * @return Content
  34. */
  35. public function index(Content $content)
  36. {
  37. return $content
  38. ->title($this->title)
  39. ->description('管理文章分类')
  40. ->body(function (Row $row) {
  41. $row->column(6, $this->treeView());
  42. $row->column(6, $this->grid());
  43. });
  44. }
  45. /**
  46. * 树形视图
  47. *
  48. * @return Tree
  49. */
  50. protected function treeView()
  51. {
  52. return new Tree(new ArticleCate(), function (Tree $tree) {
  53. $tree->branch(function ($branch) {
  54. $status = $branch['status'] == STATUS::SHOW->value ? '显示' : '隐藏';
  55. $icon = $branch['img'] ? "<img src='{$branch['img']}' width='30' height='30'>" : '';
  56. return "{$icon} {$branch['title']} ({$branch['unid']}) <span class='text-muted'>[{$status}]</span>";
  57. });
  58. });
  59. }
  60. /**
  61. * 列表页
  62. *
  63. * @return Grid
  64. */
  65. protected function grid()
  66. {
  67. return Grid::make(new ArticleCate(), function (Grid $grid) {
  68. $helper = new GridHelper($grid, $this);
  69. $helper->columnId();
  70. $grid->column('title', '分类名称');
  71. $grid->column('unid', '标识');
  72. $grid->column('img', '图标')->image('', 50, 50);
  73. $helper->columnStatus();
  74. $grid->column('desc1', '描述')->limit(30);
  75. $grid->column('pid', '父级分类')->display(function ($pid) {
  76. if (!$pid) return '顶级';
  77. $parent = ArticleCate::find($pid);
  78. return $parent ? $parent->title : '';
  79. });
  80. $helper->columnCreatedAt();
  81. $helper->columnUpdatedAt();
  82. // 筛选
  83. $grid->filter(function (Grid\Filter $filter) {
  84. $helper = new FilterHelper($filter, $this);
  85. $helper->equalId();
  86. $filter->like('title', '分类名称');
  87. $filter->like('unid', '标识');
  88. $helper->equalStatus();
  89. $filter->equal('pid', '父级分类')->select(function () {
  90. $options = [0 => '顶级'];
  91. $categories = ArticleCate::all();
  92. foreach ($categories as $category) {
  93. $options[$category->id] = $category->title;
  94. }
  95. return $options;
  96. });
  97. });
  98. // 禁用批量删除
  99. $grid->disableBatchDelete();
  100. // 行操作
  101. $grid->actions(function (Grid\Displayers\Actions $actions) {
  102. // 如果不可删除,禁用删除按钮
  103. if (!$actions->row->can_delete) {
  104. $actions->disableDelete();
  105. }
  106. });
  107. });
  108. }
  109. /**
  110. * 详情页
  111. *
  112. * @param mixed $id
  113. * @return Show
  114. */
  115. protected function detail($id)
  116. {
  117. return Show::make($id, new ArticleCate(), function (Show $show) {
  118. $helper = new ShowHelper($show, $this);
  119. $helper->fieldId();
  120. $show->field('title', '分类名称');
  121. $show->field('unid', '标识');
  122. $show->field('img', '图标')->image();
  123. $show->field('status', '状态')->using(STATUS::getValueDescription());
  124. $show->field('desc1', '描述');
  125. $show->field('pid', '父级分类')->as(function ($pid) {
  126. if (!$pid) return '顶级';
  127. $parent = ArticleCate::find($pid);
  128. return $parent ? $parent->title : '';
  129. });
  130. $show->field('can_delete', '可删除')->as(function ($value) {
  131. return $value ? '是' : '否';
  132. });
  133. $helper->fieldCreatedAt();
  134. $helper->fieldUpdatedAt();
  135. });
  136. }
  137. /**
  138. * 表单
  139. *
  140. * @return Form
  141. */
  142. protected function form()
  143. {
  144. return Form::make(new ArticleCate(), function (Form $form) {
  145. $helper = new FormHelper($form, $this);
  146. $form->display('id', 'ID');
  147. $form->text('title', '分类名称')->required()->rules('required|max:200');
  148. $form->text('unid', '标识')->required()->rules('required|max:100');
  149. $form->image('img', '图标')
  150. ->uniqueName()
  151. ->autoUpload();
  152. $helper->selectStatus();
  153. $form->textarea('desc1', '描述')->rows(3)->rules('max:200');
  154. // 父级分类选择,排除自己及其子分类
  155. $form->select('pid', '父级分类')
  156. ->options(function () use ($form) {
  157. $categories = ArticleCate::all();
  158. $options = [0 => '顶级'];
  159. // 当前分类ID
  160. $currentId = $form->model()->id;
  161. foreach ($categories as $category) {
  162. // 排除自己及其子分类
  163. if ($currentId && ($category->id == $currentId || $category->pid == $currentId)) {
  164. continue;
  165. }
  166. $options[$category->id] = $category->title;
  167. }
  168. return $options;
  169. })
  170. ->default(0);
  171. // 系统分类不可删除
  172. if ($form->isEditing()) {
  173. $form->switch('can_delete', '可删除')
  174. ->default(1)
  175. ->help('系统分类不可删除');
  176. } else {
  177. $form->hidden('can_delete')->value(1);
  178. }
  179. $form->display('created_at', '创建时间');
  180. $form->display('updated_at', '更新时间');
  181. });
  182. }
  183. }