Cate.php 9.4 KB

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