Category.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. if ($this->request->isAjax()) {
  48. $search = $this->request->request("search");
  49. $type = $this->request->request("type");
  50. //构造父类select列表选项数据
  51. $list = [];
  52. foreach ($this->categorylist as $k => $v) {
  53. if ($search) {
  54. if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
  55. if ($type == "all" || $type == null) {
  56. $list = $this->categorylist;
  57. } else {
  58. $list[] = $v;
  59. }
  60. }
  61. } else {
  62. if ($type == "all" || $type == null) {
  63. $list = $this->categorylist;
  64. } elseif ($v['type'] == $type) {
  65. $list[] = $v;
  66. }
  67. }
  68. }
  69. $total = count($list);
  70. $result = array("total" => $total, "rows" => $list);
  71. return json($result);
  72. }
  73. return $this->view->fetch();
  74. }
  75. /**
  76. * 添加
  77. */
  78. public function add()
  79. {
  80. if ($this->request->isPost()) {
  81. $this->token();
  82. }
  83. return parent::add();
  84. }
  85. /**
  86. * 编辑
  87. */
  88. public function edit($ids = null)
  89. {
  90. $row = $this->model->get($ids);
  91. if (!$row) {
  92. $this->error(__('No Results were found'));
  93. }
  94. $adminIds = $this->getDataLimitAdminIds();
  95. if (is_array($adminIds)) {
  96. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  97. $this->error(__('You have no permission'));
  98. }
  99. }
  100. if ($this->request->isPost()) {
  101. $this->token();
  102. $params = $this->request->post("row/a");
  103. if ($params) {
  104. $params = $this->preExcludeFields($params);
  105. if ($params['pid'] != $row['pid']) {
  106. $childrenIds = Tree::instance()->init(collection(\app\common\model\Category::select())->toArray())->getChildrenIds($row['id'], true);
  107. if (in_array($params['pid'], $childrenIds)) {
  108. $this->error(__('Can not change the parent to child or itself'));
  109. }
  110. }
  111. try {
  112. //是否采用模型验证
  113. if ($this->modelValidate) {
  114. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  115. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  116. $row->validate($validate);
  117. }
  118. $result = $row->allowField(true)->save($params);
  119. if ($result !== false) {
  120. $this->success();
  121. } else {
  122. $this->error($row->getError());
  123. }
  124. } catch (\think\exception\PDOException $e) {
  125. $this->error($e->getMessage());
  126. } catch (\think\Exception $e) {
  127. $this->error($e->getMessage());
  128. }
  129. }
  130. $this->error(__('Parameter %s can not be empty', ''));
  131. }
  132. $this->view->assign("row", $row);
  133. return $this->view->fetch();
  134. }
  135. /**
  136. * Selectpage搜索
  137. *
  138. * @internal
  139. */
  140. public function selectpage()
  141. {
  142. return parent::selectpage();
  143. }
  144. }