Category.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\admin\controller\service;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. /**
  6. * 分类管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Category extends Backend
  11. {
  12. /**
  13. * Category模型对象
  14. * @var \app\admin\model\service\Category
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\service\Category;
  21. $tree = Tree::instance();
  22. $tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'pid');
  23. $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  24. $categorydata = [0 => ['name' => __('None')]];
  25. foreach ($this->categorylist as $k => $v) {
  26. if($v['pid'] == 0)
  27. {
  28. $categorydata[$v['id']] = $v;
  29. }
  30. }
  31. $this->view->assign("flagList", $this->model->getFlagList());
  32. $this->view->assign("statusList", $this->model->getStatusList());
  33. $this->view->assign("isshowList", $this->model->getIsShowList());
  34. $this->view->assign("parentList", $categorydata);
  35. }
  36. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. $this->request->filter(['strip_tags']);
  42. if ($this->request->isAjax()) {
  43. $search = $this->request->request("search");
  44. //构造父类select列表选项数据
  45. $list = [];
  46. foreach ($this->categorylist as $k => $v) {
  47. if ($search) {
  48. if (stripos($v['name'], $search) !== false) {
  49. $list = $this->categorylist;
  50. }
  51. } else {
  52. $list = $this->categorylist;
  53. }
  54. }
  55. $total = count($list);
  56. $result = array("total" => $total, "rows" => $list);
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 添加
  63. */
  64. public function add()
  65. {
  66. if ($this->request->isPost()) {
  67. $this->token();
  68. }
  69. return parent::add();
  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. $this->token();
  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. }