Category.php 5.4 KB

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