Category.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. protected $searchFields = 'id,name';
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = model('app\common\model\Category');
  25. $tree = Tree::instance();
  26. $tree->init(collection($this->model->order('sort asc')->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. //设置过滤方法
  44. $this->request->filter(['strip_tags']);
  45. if ($this->request->isAjax()) {
  46. $search = $this->request->request("search");
  47. $type = $this->request->request("type");
  48. //构造父类select列表选项数据
  49. $list = [];
  50. foreach ($this->categorylist as $k => $v) {
  51. if ($search) {
  52. if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
  53. if ($type == "all" || $type == null) {
  54. $list = $this->categorylist;
  55. } else {
  56. $list[] = $v;
  57. }
  58. }
  59. } else {
  60. if ($type == "all" || $type == null) {
  61. $list = $this->categorylist;
  62. } elseif ($v['type'] == $type) {
  63. $list[] = $v;
  64. }
  65. }
  66. }
  67. // p($list);
  68. $total = count($list);
  69. $result = array("total" => $total, "rows" => $list);
  70. return json($result);
  71. }
  72. return $this->view->fetch();
  73. }
  74. /**
  75. * 添加
  76. */
  77. public function add()
  78. {
  79. if ($this->request->isPost()) {
  80. $this->token();
  81. }
  82. return parent::add();
  83. }
  84. function object_array($array) {
  85. if(is_object($array)) {
  86. $array = (array)$array;
  87. }
  88. if(is_array($array)) {
  89. foreach($array as $key=>$value) {
  90. $array[$key] = $this->object_array($value);
  91. }
  92. }
  93. return $array;
  94. }
  95. /**
  96. * 编辑
  97. */
  98. public function edit($ids = null)
  99. {
  100. $row = $this->model->get($ids);
  101. $adminIds = $this->getDataLimitAdminIds();
  102. if (is_array($adminIds)) {
  103. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  104. $this->error(__('You have no permission'));
  105. }
  106. }
  107. if ($this->request->isPost()) {
  108. $params = $this->request->post("row/a");
  109. if (!empty($params['sort'])) {
  110. // if(!is_int($params['sort'])){
  111. // $this->error(__('请输入数字'));
  112. // }
  113. $ids = input('ids',0);
  114. $c_model = new \app\admin\model\Category();
  115. $res = $c_model->where(array('id'=>$ids))->update(array('sort'=>$params['sort']));
  116. if($res){
  117. $this->success('保存成功');
  118. }else{
  119. $this->error(__('保存失败'));
  120. }
  121. }
  122. $this->token();
  123. //$params = $this->request->post("row/a");
  124. if ($params) {
  125. $params = $this->preExcludeFields($params);
  126. if ($params['pid'] != $row['pid']) {
  127. $childrenIds = Tree::instance()->init(collection(\app\common\model\Category::select())->toArray())->getChildrenIds($row['id'], true);
  128. if (in_array($params['pid'], $childrenIds)) {
  129. $this->error(__('Can not change the parent to child or itself'));
  130. }
  131. }
  132. try {
  133. //是否采用模型验证
  134. if ($this->modelValidate) {
  135. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  136. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  137. $row->validate($validate);
  138. }
  139. $result = $row->allowField(true)->save($params);
  140. if ($result !== false) {
  141. $this->success();
  142. } else {
  143. $this->error($row->getError());
  144. }
  145. } catch (\think\exception\PDOException $e) {
  146. $this->error($e->getMessage());
  147. } catch (\think\Exception $e) {
  148. $this->error($e->getMessage());
  149. }
  150. }
  151. $this->error(__('Parameter %s can not be empty', ''));
  152. }
  153. $this->view->assign("row", $row);
  154. return $this->view->fetch();
  155. }
  156. /**
  157. * Selectpage搜索
  158. *
  159. * @internal
  160. */
  161. public function selectpage()
  162. {
  163. return parent::selectpage();
  164. }
  165. }