Index.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. use app\admin\model\content\article\Category;
  7. use think\Db;
  8. /**
  9. * 文章管理管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Index extends Backend
  14. {
  15. protected $CategoryList = [];
  16. /**
  17. * @var Tree
  18. */
  19. protected $tree = null;
  20. /**
  21. * 允许的文章分类IDs
  22. */
  23. protected $channelIds = [];
  24. /**
  25. * Index模型对象
  26. * @var \app\admin\model\content\article\Index
  27. */
  28. protected $model = null;
  29. public function _initialize()
  30. {
  31. parent::_initialize();
  32. $this->model = new \app\admin\model\content\article\Index;
  33. // 移除权限检查
  34. // $this->isSuperAdmin = $this->auth->isSuperAdmin();
  35. $channelList = [];
  36. $all = collection(Category::order("sort desc,id desc")->select())->toArray();
  37. //允许的栏目 - 不区分权限,所有用户都可以选择所有栏目
  38. $this->channelIds = Category::column('id');
  39. $parentChannelIds = Category::column('parent_id');
  40. $parentChannelIds = array_unique($parentChannelIds);
  41. $tree = Tree::instance()->init($all, 'parent_id');
  42. foreach ($all as $k => &$v) {
  43. $state = ['opened' => true];
  44. // 移除外部链接类型检查,因为表结构中没有type字段
  45. // 不再检查权限,所有用户都可以看到所有分类
  46. $channelList[] = [
  47. 'id' => $v['id'],
  48. 'parent' => $v['parent_id'] ?: '0',
  49. 'text' => htmlentities(__($v['title'])),
  50. 'expand' => true,
  51. 'state' => $state
  52. ];
  53. $v['title'] = htmlentities($v['title']);
  54. }
  55. unset($v);
  56. $tree = Tree::instance()->init($all, 'parent_id');
  57. $categoryOptions = $tree->getTree(0, "<option model='@model_id' value=@id @selected>@spacer@title</option>", '');
  58. $this->view->assign('categoryOptions', $categoryOptions);
  59. $this->view->assign("statusList",StatusEnum::getMap());
  60. $this->assignconfig('statusSearchList',json_encode(StatusEnum::getMap()));
  61. }
  62. /**
  63. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  64. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  65. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  66. */
  67. /**
  68. * 编辑
  69. *
  70. * @param mixed $ids
  71. */
  72. public function edit($ids = null)
  73. {
  74. $row = $this->model->get($ids);
  75. if (!$row) {
  76. $this->error(__('No Results were found'));
  77. }
  78. // 获取当前文章分类ID(确保字段名称正确)
  79. $categoryId = isset($row['category_id']) ? $row['category_id'] : 0;
  80. // 获取所有分类,不限制权限
  81. $all = collection(Category::order("sort DESC,id DESC")->select())->toArray();
  82. foreach ($all as $k => &$v) {
  83. $v['title'] = htmlentities($v['title']);
  84. }
  85. // 初始化树结构并生成分类选项
  86. $tree = Tree::instance()->init($all, 'parent_id');
  87. $categoryOptions = $tree->getTree(0, "<option value=@id @selected>@spacer@title</option>", $categoryId);
  88. $this->view->assign('categoryOptions', $categoryOptions);
  89. $this->view->assign("row", $row);
  90. return parent::edit($ids);
  91. }
  92. }