Cate.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace app\admin\controller\exam;
  3. use app\admin\model\exam\CateModel;
  4. use app\common\controller\Backend;
  5. use fast\Tree;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 试题分类
  11. * @icon fa fa-circle-o
  12. */
  13. class Cate extends Backend
  14. {
  15. protected $noNeedRight = ['selectpage', 'getQuestionCate'];
  16. /**
  17. * CateModel模型对象
  18. * @var \app\admin\model\exam\CateModel
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\exam\CateModel;
  25. $query = $this->model->order('sort desc,id desc');
  26. $kind = input('kind/s', 'all');
  27. if ($kind != 'all') {
  28. $query->where('kind', $kind);
  29. }
  30. $tree = Tree::instance();
  31. $tree->init(collection($query->select())->toArray(), 'parent_id');
  32. $this->parentlist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  33. $this->view->assign("kindList", $this->model->getKindList());
  34. $this->view->assign("parentList", $this->parentlist);
  35. }
  36. public function import()
  37. {
  38. parent::import();
  39. }
  40. /**
  41. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  42. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  43. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  44. */
  45. /**
  46. * 查看
  47. */
  48. public function index()
  49. {
  50. //设置过滤方法
  51. $this->request->filter(['strip_tags']);
  52. if ($this->request->isAjax()) {
  53. //如果发送的来源是Selectpage,则转发到Selectpage
  54. // if ($this->request->request('keyField')) {
  55. // return $this->selectpage();
  56. // }
  57. /*list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  58. $addWhere = [];
  59. if ($this->request->request('searchKey')) {
  60. $addWhere[] = [
  61. $this->request->request('searchKey'),
  62. [
  63. 'in',
  64. $this->request->request('searchKey')
  65. ]
  66. ];
  67. }
  68. $total = $this->model
  69. ->where($where)
  70. ->where($addWhere)
  71. ->order($sort, $order)
  72. ->count();
  73. $list = $this->model
  74. ->where($where)
  75. ->where($addWhere)
  76. ->order($sort, $order)
  77. ->limit($offset, $limit)
  78. ->select();
  79. $list = collection($list)->toArray();
  80. $result = array("total" => $total, "rows" => $list);*/
  81. $search = $this->request->request("search");
  82. $kind = $this->request->request("kind");
  83. //构造父类select列表选项数据
  84. $list = [];
  85. foreach ($this->parentlist as $k => $v) {
  86. if ($search) {
  87. if ($v['kind'] == $kind) {
  88. $list[] = $v;
  89. }
  90. } else {
  91. $list[] = $v;
  92. }
  93. }
  94. $total = count($list);
  95. $result = array("total" => $total, "rows" => $list);
  96. return json($result);
  97. }
  98. return $this->view->fetch();
  99. }
  100. /**
  101. * Selectpage搜索
  102. * @internal
  103. */
  104. public function selectpage()
  105. {
  106. return parent::selectpage();
  107. }
  108. /**
  109. * 获取有题目的分类
  110. * @internal
  111. */
  112. public function getQuestionCate()
  113. {
  114. if ($cate_ids = $this->request->request("keyValue", "")) {
  115. $list = CateModel::whereIn('id', $cate_ids)->select();
  116. } else {
  117. $ids = Db::name('exam_question')->group('cate_id')->field('cate_id')->select();
  118. $list = $ids ? CateModel::whereIn('id', array_column($ids, 'cate_id'))->select() : [];
  119. // $list = $ids ? CateModel::whereIn('id', array_column($ids, 'cate_id'))->select() : [];
  120. }
  121. return json(['list' => $list, 'total' => count($list)]);
  122. }
  123. /**
  124. * 添加
  125. */
  126. public function add()
  127. {
  128. if ($this->request->isPost()) {
  129. $params = $this->request->post("row/a");
  130. if ($params) {
  131. $params = $this->preExcludeFields($params);
  132. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  133. $params[$this->dataLimitField] = $this->auth->id;
  134. }
  135. $result = false;
  136. Db::startTrans();
  137. try {
  138. //是否采用模型验证
  139. if ($this->modelValidate) {
  140. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  141. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  142. $this->model->validateFailException(true)->validate($validate);
  143. }
  144. $this->setLevel($params);
  145. $result = $this->model->allowField(true)->save($params);
  146. Db::commit();
  147. } catch (ValidateException $e) {
  148. Db::rollback();
  149. $this->error($e->getMessage());
  150. } catch (PDOException $e) {
  151. Db::rollback();
  152. $this->error($e->getMessage());
  153. } catch (Exception $e) {
  154. Db::rollback();
  155. $this->error($e->getMessage());
  156. }
  157. if ($result !== false) {
  158. $this->success();
  159. } else {
  160. $this->error(__('No rows were inserted'));
  161. }
  162. }
  163. $this->error(__('Parameter %s can not be empty', ''));
  164. }
  165. return $this->view->fetch();
  166. }
  167. /**
  168. * 编辑
  169. */
  170. public function edit($ids = null)
  171. {
  172. $row = $this->model->get($ids);
  173. if (!$row) {
  174. $this->error(__('No Results were found'));
  175. }
  176. $adminIds = $this->getDataLimitAdminIds();
  177. if (is_array($adminIds)) {
  178. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  179. $this->error(__('You have no permission'));
  180. }
  181. }
  182. if ($this->request->isPost()) {
  183. $params = $this->request->post("row/a");
  184. if ($params) {
  185. $params = $this->preExcludeFields($params);
  186. $result = false;
  187. Db::startTrans();
  188. try {
  189. //是否采用模型验证
  190. if ($this->modelValidate) {
  191. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  192. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  193. $row->validateFailException(true)->validate($validate);
  194. }
  195. $this->setLevel($params, $row);
  196. $result = $row->allowField(true)->save($params);
  197. Db::commit();
  198. } catch (ValidateException $e) {
  199. Db::rollback();
  200. $this->error($e->getMessage());
  201. } catch (PDOException $e) {
  202. Db::rollback();
  203. $this->error($e->getMessage());
  204. } catch (\Exception $e) {
  205. Db::rollback();
  206. $this->error($e->getMessage());
  207. }
  208. if ($result !== false) {
  209. $this->success();
  210. } else {
  211. $this->error(__('No rows were updated'));
  212. }
  213. }
  214. $this->error(__('Parameter %s can not be empty', ''));
  215. }
  216. $this->view->assign("row", $row);
  217. return $this->view->fetch();
  218. }
  219. /**
  220. * 设置层级
  221. * @param $params
  222. */
  223. protected function setLevel(&$params, $row = null)
  224. {
  225. $params['parent_id'] = $params['parent_id'] ?? 0;
  226. if (!$params['parent_id']) {
  227. $params['level'] = 1;
  228. } else {
  229. $parent = CateModel::get($params['parent_id']);
  230. // 编辑时
  231. if ($row) {
  232. if ($params['parent_id'] == $row['id']) {
  233. throw new \Exception('不能将当前分类设置为父级分类');
  234. }
  235. if ($parent['kind'] != $params['kind']) {
  236. throw new \Exception('不能将当前分类设置为其他种类的下级');
  237. }
  238. $child_ids = CateModel::where('parent_id', $row['id'])->column('id');
  239. if (in_array($params['parent_id'], $child_ids)) {
  240. throw new \Exception('不能将当前分类的子级设置为当前分类的父级');
  241. }
  242. }
  243. if ($parent['level'] == 1) {
  244. $params['level'] = 2;
  245. } else if ($parent['level'] == 2) {
  246. $params['level'] = 3;
  247. } else {
  248. throw new \Exception('错误:最多添加3级分类,请重新选择父级类别');
  249. }
  250. }
  251. }
  252. }