Category.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\Category as CategoryModel;
  5. use fast\Tree;
  6. /**
  7. * 分类管理
  8. *
  9. * @icon fa fa-list
  10. * @remark 用于统一管理网站的所有分类,分类可进行无限级分类,分类类型请在常规管理->系统配置->字典配置中添加
  11. */
  12. class Category extends Backend
  13. {
  14. /**
  15. * @var \app\common\model\Category
  16. */
  17. protected $model = null;
  18. protected $categorylist = [];
  19. protected $noNeedRight = ['selectpage'];
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('app\common\model\Category');
  24. $tree = Tree::instance();
  25. $tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'pid');
  26. $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  27. $categorydata = [0 => ['type' => 'all', 'name' => __('None')]];
  28. foreach ($this->categorylist as $k => $v) {
  29. $categorydata[$v['id']] = $v;
  30. }
  31. $typeList = CategoryModel::getTypeList();
  32. $this->view->assign("flagList", $this->model->getFlagList());
  33. $this->view->assign("typeList", $typeList);
  34. $this->view->assign("parentList", $categorydata);
  35. $this->assignconfig('typeList', $typeList);
  36. }
  37. /**
  38. * 查看
  39. */
  40. public function index()
  41. {
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags']);
  44. if ($this->request->isAjax()) {
  45. $search = $this->request->request("search");
  46. $type = $this->request->request("type");
  47. //构造父类select列表选项数据
  48. $list = [];
  49. foreach ($this->categorylist as $k => $v) {
  50. if ($search) {
  51. if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
  52. if ($type == "all" || $type == null) {
  53. $list = $this->categorylist;
  54. } else {
  55. $list[] = $v;
  56. }
  57. }
  58. } else {
  59. if ($type == "all" || $type == null) {
  60. $list = $this->categorylist;
  61. } elseif ($v['type'] == $type) {
  62. $list[] = $v;
  63. }
  64. }
  65. }
  66. $total = count($list);
  67. $result = array("total" => $total, "rows" => $list);
  68. return json($result);
  69. }
  70. return $this->view->fetch();
  71. }
  72. /**
  73. * 添加
  74. */
  75. public function add()
  76. {
  77. if ($this->request->isPost()) {
  78. $this->token();
  79. }
  80. return parent::add();
  81. }
  82. /**
  83. * 编辑
  84. */
  85. public function edit($ids = null)
  86. {
  87. $row = $this->model->get($ids);
  88. if (!$row) {
  89. $this->error(__('No Results were found'));
  90. }
  91. $adminIds = $this->getDataLimitAdminIds();
  92. if (is_array($adminIds)) {
  93. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  94. $this->error(__('You have no permission'));
  95. }
  96. }
  97. if ($this->request->isPost()) {
  98. $this->token();
  99. $params = $this->request->post("row/a");
  100. if ($params) {
  101. $params = $this->preExcludeFields($params);
  102. if ($params['pid'] != $row['pid']) {
  103. $childrenIds = Tree::instance()->init(collection(\app\common\model\Category::select())->toArray())->getChildrenIds($row['id'], true);
  104. if (in_array($params['pid'], $childrenIds)) {
  105. $this->error(__('Can not change the parent to child or itself'));
  106. }
  107. }
  108. try {
  109. //是否采用模型验证
  110. if ($this->modelValidate) {
  111. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  112. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  113. $row->validate($validate);
  114. }
  115. $result = $row->allowField(true)->save($params);
  116. if ($result !== false) {
  117. $this->success();
  118. } else {
  119. $this->error($row->getError());
  120. }
  121. } catch (\think\exception\PDOException $e) {
  122. $this->error($e->getMessage());
  123. } catch (\think\Exception $e) {
  124. $this->error($e->getMessage());
  125. }
  126. }
  127. $this->error(__('Parameter %s can not be empty', ''));
  128. }
  129. $this->view->assign("row", $row);
  130. return $this->view->fetch();
  131. }
  132. /**
  133. * Selectpage搜索
  134. *
  135. * @internal
  136. */
  137. public function selectpage()
  138. {
  139. return parent::selectpage();
  140. }
  141. }