Manage.php 5.1 KB

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