Notice.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\admin\controller\exam;
  3. use addons\exam\library\FrontService;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 系统公告
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Notice extends Backend
  14. {
  15. /**
  16. * NoticeModel模型对象
  17. * @var \app\admin\model\exam\NoticeModel
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\exam\NoticeModel;
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 添加
  33. */
  34. public function add()
  35. {
  36. if ($this->request->isPost()) {
  37. $params = $this->request->post("row/a");
  38. if ($params) {
  39. $params = $this->preExcludeFields($params);
  40. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  41. $params[$this->dataLimitField] = $this->auth->id;
  42. }
  43. if (!empty($params['contents'])) {
  44. $params['contents'] = FrontService::replaceImgUrl($params['contents']);
  45. }
  46. $result = false;
  47. Db::startTrans();
  48. try {
  49. //是否采用模型验证
  50. if ($this->modelValidate) {
  51. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  52. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  53. $this->model->validateFailException(true)->validate($validate);
  54. }
  55. $result = $this->model->allowField(true)->save($params);
  56. Db::commit();
  57. } catch (ValidateException $e) {
  58. Db::rollback();
  59. $this->error($e->getMessage());
  60. } catch (PDOException $e) {
  61. Db::rollback();
  62. $this->error($e->getMessage());
  63. } catch (Exception $e) {
  64. Db::rollback();
  65. $this->error($e->getMessage());
  66. }
  67. if ($result !== false) {
  68. $this->success();
  69. } else {
  70. $this->error(__('No rows were inserted'));
  71. }
  72. }
  73. $this->error(__('Parameter %s can not be empty', ''));
  74. }
  75. return $this->view->fetch();
  76. }
  77. /**
  78. * 编辑
  79. */
  80. public function edit($ids = null)
  81. {
  82. $row = $this->model->get($ids);
  83. if (!$row) {
  84. $this->error(__('No Results were found'));
  85. }
  86. $adminIds = $this->getDataLimitAdminIds();
  87. if (is_array($adminIds)) {
  88. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  89. $this->error(__('You have no permission'));
  90. }
  91. }
  92. if ($this->request->isPost()) {
  93. $params = $this->request->post("row/a");
  94. if ($params) {
  95. $params = $this->preExcludeFields($params);
  96. if (!empty($params['contents'])) {
  97. $params['contents'] = FrontService::replaceImgUrl($params['contents']);
  98. }
  99. $result = false;
  100. Db::startTrans();
  101. try {
  102. //是否采用模型验证
  103. if ($this->modelValidate) {
  104. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  105. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  106. $row->validateFailException(true)->validate($validate);
  107. }
  108. $result = $row->allowField(true)->save($params);
  109. Db::commit();
  110. } catch (ValidateException $e) {
  111. Db::rollback();
  112. $this->error($e->getMessage());
  113. } catch (PDOException $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. } catch (Exception $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. }
  120. if ($result !== false) {
  121. $this->success();
  122. } else {
  123. $this->error(__('No rows were updated'));
  124. }
  125. }
  126. $this->error(__('Parameter %s can not be empty', ''));
  127. }
  128. $this->view->assign("row", $row);
  129. return $this->view->fetch();
  130. }
  131. }