Category.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace app\admin\controller\content\article;
  3. use app\common\controller\Backend;
  4. use app\common\Enum\StatusEnum;
  5. use fast\Tree;
  6. /**
  7. * 文章分类管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Category extends Backend
  12. {
  13. protected $CategoryList = [];
  14. /**
  15. * @var Tree
  16. */
  17. protected $tree = null;
  18. /**
  19. * Category模型对象
  20. * @var \app\admin\model\content\article\Category
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\content\article\Category;
  27. $this->tree = Tree::instance();
  28. $this->tree->init(collection($this->model->order('sort desc,id desc')->select())->toArray(), 'parent_id');
  29. $this->CategoryList = $this->tree->getTreeList($this->tree->getTreeArray(0), 'title');
  30. $this->view->assign("CategoryList", $this->CategoryList);
  31. $this->view->assign("statusList",StatusEnum::getMap());
  32. $this->assignconfig('statusSearchList',json_encode(StatusEnum::getMap()));
  33. }
  34. /**
  35. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  36. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  37. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  38. */
  39. public function getList()
  40. {
  41. $tree = Tree::instance();
  42. $tree->init(collection($this->model->order('sort desc,id desc')->select())->toArray(), 'parent_id');
  43. $list = $tree->getTreeList($tree->getTreeArray(0), 'name');
  44. return json($list);
  45. }
  46. public function edit($ids = null)
  47. {
  48. $category = \app\admin\model\content\article\Category::get($ids);
  49. if (!$category) {
  50. $this->error(__('No Results were found'));
  51. }
  52. $childrenIds = $this->tree->getChildrenIds($category['id'], true);
  53. $this->view->assign('childrenIds', $childrenIds);
  54. return parent::edit($ids);
  55. }
  56. }