SpecialIssue.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace app\admin\controller\cms;
  3. use app\admin\model\cms\Participate;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\DbException;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 特刊管理
  11. *
  12. * @icon fa fa-cny
  13. */
  14. class SpecialIssue extends Backend
  15. {
  16. /**
  17. * Issue模型对象
  18. * @var \app\admin\model\cms\Issue
  19. */
  20. protected $model = null;
  21. protected $searchFields = 'id';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\cms\Issue();
  26. }
  27. /**
  28. * 查看
  29. */
  30. public function index()
  31. {
  32. //设置过滤方法
  33. $this->request->filter(['strip_tags']);
  34. if ($this->request->isAjax()) {
  35. $this->relationSearch = true;
  36. //如果发送的来源是Selectpage,则转发到Selectpage
  37. if ($this->request->request('keyField')) {
  38. return $this->selectpage();
  39. }
  40. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  41. $total = $this->model
  42. ->where($where)
  43. ->order($sort, $order)
  44. ->count();
  45. $list = $this->model->with(['user', 'journal'])
  46. ->where($where)
  47. ->order($sort, $order)
  48. ->limit($offset, $limit)
  49. ->select();
  50. foreach ($list as $value) {
  51. $editor_email_arr = [];
  52. $editor_arr = json_decode($value['editor'],true);
  53. foreach ($editor_arr as $item) {
  54. $editor_email_arr[] = $item['email'];
  55. }
  56. $value['editor'] = $editor_email_arr;
  57. }
  58. $result = array("total" => $total, "rows" => $list);
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 编辑
  65. *
  66. * @param $ids
  67. * @return string
  68. * @throws DbException
  69. * @throws \think\Exception
  70. */
  71. public function edit($ids = null)
  72. {
  73. $row = $this->model->get($ids);
  74. if (!$row) {
  75. $this->error(__('No Results were found'));
  76. }
  77. $adminIds = $this->getDataLimitAdminIds();
  78. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  79. $this->error(__('You have no permission'));
  80. }
  81. if (false === $this->request->isPost()) {
  82. $row['editor'] = json_decode($row['editor'], true);
  83. $this->view->assign('row', $row);
  84. return $this->view->fetch();
  85. }
  86. $params = $this->request->post('row/a');
  87. if (empty($params)) {
  88. $this->error(__('Parameter %s can not be empty', ''));
  89. }
  90. $params = $this->preExcludeFields($params);
  91. $result = false;
  92. Db::startTrans();
  93. try {
  94. //是否采用模型验证
  95. if ($this->modelValidate) {
  96. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  97. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  98. $row->validateFailException()->validate($validate);
  99. }
  100. $result = $row->allowField(true)->save($params);
  101. Db::commit();
  102. } catch (ValidateException|PDOException|Exception $e) {
  103. Db::rollback();
  104. $this->error($e->getMessage());
  105. }
  106. if (false === $result) {
  107. $this->error(__('No rows were updated'));
  108. }
  109. $this->success();
  110. }
  111. }