| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace App\Module\Article\AdminControllers;
- use App\Module\Article\AdminControllers\Helper\FilterHelper;
- use App\Module\Article\AdminControllers\Helper\FormHelper;
- use App\Module\Article\AdminControllers\Helper\GridHelper;
- use App\Module\Article\AdminControllers\Helper\ShowHelper;
- use App\Module\Article\Enums\STATUS;
- use App\Module\Article\Models\ArticleCate;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Layout\Row;
- use Dcat\Admin\Show;
- use Dcat\Admin\Tree;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- /**
- * 文章分类控制器
- */
- #[Resource('article-cates', names: 'dcat.admin.article-cates')]
- class ArticleCateController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '文章分类管理';
- /**
- * 首页
- *
- * @param Content $content
- * @return Content
- */
- public function index(Content $content)
- {
- return $content
- ->title($this->title)
- ->description('管理文章分类')
- ->body(function (Row $row) {
- $row->column(6, $this->treeView());
- $row->column(6, $this->grid());
- });
- }
- /**
- * 树形视图
- *
- * @return Tree
- */
- protected function treeView()
- {
- return new Tree(new ArticleCate(), function (Tree $tree) {
- $tree->branch(function ($branch) {
- $status = $branch['status'] == STATUS::SHOW->value ? '显示' : '隐藏';
- $icon = $branch['img'] ? "<img src='{$branch['img']}' width='30' height='30'>" : '';
- return "{$icon} {$branch['title']} ({$branch['unid']}) <span class='text-muted'>[{$status}]</span>";
- });
- });
- }
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new ArticleCate(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- $helper->columnId();
- $grid->column('title', '分类名称');
- $grid->column('unid', '标识');
- $grid->column('img', '图标')->image('', 50, 50);
- $helper->columnStatus();
- $grid->column('desc1', '描述')->limit(30);
- $grid->column('pid', '父级分类')->display(function ($pid) {
- if (!$pid) return '顶级';
- $parent = ArticleCate::find($pid);
- return $parent ? $parent->title : '';
- });
- $helper->columnCreatedAt();
- $helper->columnUpdatedAt();
- // 筛选
- $grid->filter(function (Grid\Filter $filter) {
- $helper = new FilterHelper($filter, $this);
- $helper->equalId();
- $filter->like('title', '分类名称');
- $filter->like('unid', '标识');
- $helper->equalStatus();
- $filter->equal('pid', '父级分类')->select(function () {
- $options = [0 => '顶级'];
- $categories = ArticleCate::all();
- foreach ($categories as $category) {
- $options[$category->id] = $category->title;
- }
- return $options;
- });
- });
- // 禁用批量删除
- $grid->disableBatchDelete();
- // 行操作
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- // 如果不可删除,禁用删除按钮
- if (!$actions->row->can_delete) {
- $actions->disableDelete();
- }
- });
- });
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new ArticleCate(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $helper->fieldId();
- $show->field('title', '分类名称');
- $show->field('unid', '标识');
- $show->field('img', '图标')->image();
- $show->field('status', '状态')->using(STATUS::getValueDescription());
- $show->field('desc1', '描述');
- $show->field('pid', '父级分类')->as(function ($pid) {
- if (!$pid) return '顶级';
- $parent = ArticleCate::find($pid);
- return $parent ? $parent->title : '';
- });
- $show->field('can_delete', '可删除')->as(function ($value) {
- return $value ? '是' : '否';
- });
- $helper->fieldCreatedAt();
- $helper->fieldUpdatedAt();
- });
- }
- /**
- * 表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new ArticleCate(), function (Form $form) {
- $helper = new FormHelper($form, $this);
- $form->display('id', 'ID');
- $form->text('title', '分类名称')->required()->rules('required|max:200');
- $form->text('unid', '标识')->required()->rules('required|max:100');
- $form->image('img', '图标')
- ->uniqueName()
- ->autoUpload();
- $helper->selectStatus();
- $form->textarea('desc1', '描述')->rows(3)->rules('max:200');
- // 父级分类选择,排除自己及其子分类
- $form->select('pid', '父级分类')
- ->options(function () use ($form) {
- $categories = ArticleCate::all();
- $options = [0 => '顶级'];
- // 当前分类ID
- $currentId = $form->model()->id;
- foreach ($categories as $category) {
- // 排除自己及其子分类
- if ($currentId && ($category->id == $currentId || $category->pid == $currentId)) {
- continue;
- }
- $options[$category->id] = $category->title;
- }
- return $options;
- })
- ->default(0);
- // 系统分类不可删除
- if ($form->isEditing()) {
- $form->switch('can_delete', '可删除')
- ->default(1)
- ->help('系统分类不可删除');
- } else {
- $form->hidden('can_delete')->value(1);
- }
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- });
- }
- }
|