Category.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 edit($ids = null)
  76. {
  77. $row = $this->model->get($ids);
  78. if (!$row) {
  79. $this->error(__('No Results were found'));
  80. }
  81. $adminIds = $this->getDataLimitAdminIds();
  82. if (is_array($adminIds)) {
  83. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  84. $this->error(__('You have no permission'));
  85. }
  86. }
  87. if ($this->request->isPost()) {
  88. $params = $this->request->post("row/a");
  89. if ($params) {
  90. $params = $this->preExcludeFields($params);
  91. if ($params['pid'] != $row['pid']) {
  92. $childrenIds = Tree::instance()->init(collection(\app\common\model\Category::select())->toArray())->getChildrenIds($row['id'], true);
  93. if (in_array($params['pid'], $childrenIds)) {
  94. $this->error(__('Can not change the parent to child or itself'));
  95. }
  96. }
  97. try {
  98. //是否采用模型验证
  99. if ($this->modelValidate) {
  100. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  101. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  102. $row->validate($validate);
  103. }
  104. $result = $row->allowField(true)->save($params);
  105. if ($result !== false) {
  106. $this->success();
  107. } else {
  108. $this->error($row->getError());
  109. }
  110. } catch (\think\exception\PDOException $e) {
  111. $this->error($e->getMessage());
  112. } catch (\think\Exception $e) {
  113. $this->error($e->getMessage());
  114. }
  115. }
  116. $this->error(__('Parameter %s can not be empty', ''));
  117. }
  118. $this->view->assign("row", $row);
  119. return $this->view->fetch();
  120. }
  121. /**
  122. * Selectpage搜索
  123. *
  124. * @internal
  125. */
  126. public function selectpage()
  127. {
  128. return parent::selectpage();
  129. }
  130. }