Answer.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\admin\controller\questionnaire;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 问卷选项
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Answer extends Backend
  14. {
  15. /**
  16. * Answer模型对象
  17. * @var \app\admin\model\questionnaire\Answer
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\questionnaire\Answer;
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags']);
  37. if ($this->request->isAjax()) {
  38. //如果发送的来源是Selectpage,则转发到Selectpage
  39. if ($this->request->request('keyField')) {
  40. return $this->selectpage();
  41. }
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $total = $this->model
  44. ->where($where)
  45. ->order($sort, $order)
  46. ->count();
  47. $list = $this->model
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->limit($offset, $limit)
  51. ->select();
  52. $list = collection($list)->toArray();
  53. foreach ($list as $k=>$v)
  54. {
  55. $list[$k]['question'] = Db::table('questionnaire_question')->where('id',$v['question_id'])->value('question');
  56. }
  57. $result = array("total" => $total, "rows" => $list);
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 添加
  64. */
  65. public function add()
  66. {
  67. if ($this->request->isPost()) {
  68. $params = $this->request->post("row/a");
  69. if ($params) {
  70. $params = $this->preExcludeFields($params);
  71. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  72. $params[$this->dataLimitField] = $this->auth->id;
  73. }
  74. $result = false;
  75. Db::startTrans();
  76. try {
  77. //是否采用模型验证
  78. if ($this->modelValidate) {
  79. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  80. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  81. $this->model->validateFailException(true)->validate($validate);
  82. }
  83. $params['questionnaire_id'] = Db::table('questionnaire_question')->where('id',$params['question_id'])->value('questionnaire_id');
  84. $result = $this->model->allowField(true)->save($params);
  85. Db::commit();
  86. } catch (ValidateException $e) {
  87. Db::rollback();
  88. $this->error($e->getMessage());
  89. } catch (PDOException $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. } catch (Exception $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. }
  96. if ($result !== false) {
  97. $this->success();
  98. } else {
  99. $this->error(__('No rows were inserted'));
  100. }
  101. }
  102. $this->error(__('Parameter %s can not be empty', ''));
  103. }
  104. return $this->view->fetch();
  105. }
  106. /**
  107. * 编辑
  108. */
  109. public function edit($ids = null)
  110. {
  111. $row = $this->model->get($ids);
  112. if (!$row) {
  113. $this->error(__('No Results were found'));
  114. }
  115. $adminIds = $this->getDataLimitAdminIds();
  116. if (is_array($adminIds)) {
  117. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  118. $this->error(__('You have no permission'));
  119. }
  120. }
  121. if ($this->request->isPost()) {
  122. $params = $this->request->post("row/a");
  123. if ($params) {
  124. $params = $this->preExcludeFields($params);
  125. $result = false;
  126. Db::startTrans();
  127. try {
  128. //是否采用模型验证
  129. if ($this->modelValidate) {
  130. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  131. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  132. $row->validateFailException(true)->validate($validate);
  133. }
  134. $params['questionnaire_id'] = Db::table('questionnaire_question')->where('id',$params['question_id'])->value('questionnaire_id');
  135. $result = $row->allowField(true)->save($params);
  136. Db::commit();
  137. } catch (ValidateException $e) {
  138. Db::rollback();
  139. $this->error($e->getMessage());
  140. } catch (PDOException $e) {
  141. Db::rollback();
  142. $this->error($e->getMessage());
  143. } catch (Exception $e) {
  144. Db::rollback();
  145. $this->error($e->getMessage());
  146. }
  147. if ($result !== false) {
  148. $this->success();
  149. } else {
  150. $this->error(__('No rows were updated'));
  151. }
  152. }
  153. $this->error(__('Parameter %s can not be empty', ''));
  154. }
  155. $this->view->assign("row", $row);
  156. return $this->view->fetch();
  157. }
  158. }