Voteplayer.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use think\db\exception\BindParamException;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\ModelNotFoundException;
  9. use think\exception\DbException;
  10. use think\exception\PDOException;
  11. use think\exception\ValidateException;
  12. /**
  13. * 投票选手管理
  14. *
  15. * @icon fa fa-circle-o
  16. */
  17. class Voteplayer extends Backend
  18. {
  19. /**
  20. * Voteplayer模型对象
  21. * @var \app\admin\model\Voteplayer
  22. */
  23. protected $model = null;
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = new \app\admin\model\Voteplayer;
  28. $this->view->assign("statusList", $this->model->getStatusList());
  29. }
  30. /**
  31. * 添加
  32. *
  33. * @return string
  34. * @throws \think\Exception
  35. */
  36. public function add()
  37. {
  38. if (false === $this->request->isPost()) {
  39. return $this->view->fetch();
  40. }
  41. $params = $this->request->post('row/a');
  42. if (empty($params)) {
  43. $this->error(__('Parameter %s can not be empty', ''));
  44. }
  45. $params = $this->preExcludeFields($params);
  46. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  47. $params[$this->dataLimitField] = $this->auth->id;
  48. }
  49. $result = false;
  50. Db::startTrans();
  51. try {
  52. //是否采用模型验证
  53. if ($this->modelValidate) {
  54. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  55. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  56. $this->model->validateFailException()->validate($validate);
  57. }
  58. $result = $this->model->allowField(true)->save($params);
  59. Db::commit();
  60. } catch (ValidateException|PDOException|Exception $e) {
  61. Db::rollback();
  62. $this->error($e->getMessage());
  63. }
  64. if ($result === false) {
  65. $this->error(__('No rows were inserted'));
  66. }
  67. $this->success();
  68. }
  69. /**
  70. * 编辑
  71. *
  72. * @param $ids
  73. * @return string
  74. * @throws DbException
  75. * @throws \think\Exception
  76. */
  77. public function edit($ids = null)
  78. {
  79. $row = $this->model->get($ids);
  80. if (!$row) {
  81. $this->error(__('No Results were found'));
  82. }
  83. $adminIds = $this->getDataLimitAdminIds();
  84. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  85. $this->error(__('You have no permission'));
  86. }
  87. if (false === $this->request->isPost()) {
  88. $this->view->assign('row', $row);
  89. return $this->view->fetch();
  90. }
  91. $params = $this->request->post('row/a');
  92. if (empty($params)) {
  93. $this->error(__('Parameter %s can not be empty', ''));
  94. }
  95. $params = $this->preExcludeFields($params);
  96. $result = false;
  97. Db::startTrans();
  98. try {
  99. //是否采用模型验证
  100. if ($this->modelValidate) {
  101. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  102. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  103. $row->validateFailException()->validate($validate);
  104. }
  105. $result = $row->allowField(true)->save($params);
  106. Db::commit();
  107. } catch (ValidateException|PDOException|Exception $e) {
  108. Db::rollback();
  109. $this->error($e->getMessage());
  110. }
  111. if (false === $result) {
  112. $this->error(__('No rows were updated'));
  113. }
  114. $this->success();
  115. }
  116. }