Category.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\admin\controller\unishop;
  3. use app\common\controller\Backend;
  4. use app\admin\model\unishop\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->request->filter(['strip_tags']);
  24. $this->model = new CategoryModel();
  25. $tree = Tree::instance();
  26. $tree->init(collection($this->model->order('weigh asc,id desc')->select())->toArray(), 'pid');
  27. $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  28. $categorydata = [0 => ['type' => 'all', 'name' => __('None')]];
  29. foreach ($this->categorylist as $k => $v) {
  30. $categorydata[$v['id']] = $v;
  31. }
  32. $typeList = CategoryModel::getTypeList();
  33. $this->view->assign("flagList", $this->model->getFlagList());
  34. $this->view->assign("typeList", $typeList);
  35. $this->view->assign("parentList", $categorydata);
  36. $this->assignconfig('typeList', $typeList);
  37. }
  38. /**
  39. * 查看
  40. */
  41. public function index()
  42. {
  43. if ($this->request->isAjax()) {
  44. $search = $this->request->request("search");
  45. $type = $this->request->request("type");
  46. //构造父类select列表选项数据
  47. $list = [];
  48. foreach ($this->categorylist as $k => $v) {
  49. if ($search) {
  50. if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
  51. if ($type == "all" || $type == null) {
  52. $list = $this->categorylist;
  53. } else {
  54. $list[] = $v;
  55. }
  56. }
  57. } else {
  58. if ($type == "all" || $type == null) {
  59. $list = $this->categorylist;
  60. } elseif ($v['type'] == $type) {
  61. $list[] = $v;
  62. }
  63. }
  64. }
  65. $total = count($list);
  66. $result = array("total" => $total, "rows" => $list);
  67. return json($result);
  68. }
  69. return $this->view->fetch();
  70. }
  71. /**
  72. * 编辑
  73. */
  74. public function edit($ids = null)
  75. {
  76. $row = $this->model->get($ids);
  77. if (!$row) {
  78. $this->error(__('No Results were found'));
  79. }
  80. $adminIds = $this->getDataLimitAdminIds();
  81. if (is_array($adminIds)) {
  82. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  83. $this->error(__('You have no permission'));
  84. }
  85. }
  86. if ($this->request->isPost()) {
  87. $params = $this->request->post("row/a");
  88. if ($params) {
  89. $params = $this->preExcludeFields($params);
  90. /*if ($params['pid'] != $row['pid']) {
  91. $childrenIds = Tree::instance()->init(collection(\app\admin\model\unishop\Category::select())->toArray())->getChildrenIds($row['id']);
  92. if (in_array($params['pid'], $childrenIds)) {
  93. $this->error(__('Can not change the parent to child'));
  94. }
  95. }*/
  96. try {
  97. //是否采用模型验证
  98. if ($this->modelValidate) {
  99. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  100. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  101. $row->validate($validate);
  102. }
  103. $result = $row->allowField(true)->save($params);
  104. if ($result !== false) {
  105. $this->success();
  106. } else {
  107. $this->error($row->getError());
  108. }
  109. } catch (\think\exception\PDOException $e) {
  110. $this->error($e->getMessage());
  111. } catch (\think\Exception $e) {
  112. $this->error($e->getMessage());
  113. }
  114. }
  115. $this->error(__('Parameter %s can not be empty', ''));
  116. }
  117. $this->view->assign("row", $row);
  118. return $this->view->fetch();
  119. }
  120. /**
  121. * Selectpage搜索
  122. *
  123. * @internal
  124. */
  125. public function selectpage()
  126. {
  127. return parent::selectpage();
  128. }
  129. }