Privatebookpageresource.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Created by: PhpStorm
  4. * User: lytian
  5. * Date: 2020/3/25
  6. * Time: 18:11
  7. */
  8. namespace app\admin\controller;
  9. use app\common\controller\Backend;
  10. class Privatebookpageresource extends Backend
  11. {
  12. protected $model = null;
  13. protected $pageModel = null;
  14. public function _initialize()
  15. {
  16. parent::_initialize(); // TODO: Change the autogenerated stub
  17. $this->model = model("PrivateBookPageResource");
  18. $this->pageModel = model("PrivateBookPage");
  19. //$this->view->assign("statusList", $this->model->getStatusList());
  20. }
  21. /**
  22. * 查看
  23. */
  24. public function index()
  25. {
  26. $page_id = $this->request->param('page_id');
  27. if (empty($page_id)) {
  28. $this->error(__('缺少参数 page_id', ''));
  29. }
  30. //设置过滤方法
  31. $this->request->filter(['strip_tags']);
  32. if ($this->request->isAjax()) {
  33. //如果发送的来源是Selectpage,则转发到Selectpage
  34. if ($this->request->request('pkey_name')) {
  35. return $this->selectpage();
  36. }
  37. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  38. //if (in_array($this->group, [3, 4, 7, 8])) {
  39. $maps = [
  40. 'page_id' => $page_id,
  41. ];
  42. //}
  43. $total = $this->model
  44. ->where($where)
  45. ->where($maps)
  46. ->order($sort, $order)
  47. ->count();
  48. $list = $this->model
  49. ->where($where)
  50. ->where($maps)
  51. ->order($sort, $order)
  52. ->limit($offset, $limit)
  53. ->select();
  54. $result = array("total" => $total, "rows" => $list);
  55. return json($result);
  56. }
  57. $this->assignconfig('page_id', $page_id);
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 添加
  62. */
  63. public function add()
  64. {
  65. $page_id = $this->request->param('page_id');
  66. if ($this->request->isPost()) {
  67. $params = $this->request->post("row/a");
  68. if ($params) {
  69. /*
  70. * 已经弃用,如果为了兼容老版可取消注释
  71. foreach ($params as $k => &$v)
  72. {
  73. $v = is_array($v) ? implode(',', $v) : $v;
  74. }
  75. */
  76. if ($this->dataLimit) {
  77. $params[$this->dataLimitField] = $this->auth->id;
  78. }
  79. try {
  80. //是否采用模型验证
  81. if ($this->modelValidate) {
  82. $name = basename(str_replace('\\', '/', get_class($this->model)));
  83. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  84. $this->model->validate($validate);
  85. }
  86. $result = $this->model->allowField(true)->save($params);
  87. if ($result !== false) {
  88. model("PrivateBookPage")->getResourceList($page_id, true);
  89. $this->success();
  90. } else {
  91. $this->error($this->model->getError());
  92. }
  93. } catch (\think\exception\PDOException $e) {
  94. $this->error($e->getMessage());
  95. }
  96. }
  97. $this->error(__('Parameter %s can not be empty', ''));
  98. }
  99. $this->assignconfig('page_id', $page_id);
  100. $this->assign('page_id', $page_id);
  101. return $this->view->fetch();
  102. }
  103. /**
  104. * 编辑
  105. */
  106. public function edit($ids = null)
  107. {
  108. $page_id = $this->request->param('page_id');
  109. $row = $this->model->get($ids);
  110. if (!$row) {
  111. $this->error(__('No Results were found'));
  112. }
  113. $adminIds = $this->getDataLimitAdminIds();
  114. if (is_array($adminIds)) {
  115. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  116. $this->error(__('You have no permission'));
  117. }
  118. }
  119. if ($this->request->isPost()) {
  120. $params = $this->request->post("row/a");
  121. if ($params) {
  122. /*
  123. * 已经弃用,如果为了兼容老版可取消注释
  124. foreach ($params as $k => &$v)
  125. {
  126. $v = is_array($v) ? implode(',', $v) : $v;
  127. }
  128. */
  129. try {
  130. //是否采用模型验证
  131. if ($this->modelValidate) {
  132. $name = basename(str_replace('\\', '/', get_class($this->model)));
  133. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  134. $row->validate($validate);
  135. }
  136. $result = $row->allowField(true)->save($params);
  137. if ($result !== false) {
  138. model("PrivateBookPage")->getResourceList($page_id, true);
  139. $this->success();
  140. } else {
  141. $this->error($row->getError());
  142. }
  143. } catch (\think\exception\PDOException $e) {
  144. $this->error($e->getMessage());
  145. }
  146. }
  147. $this->error(__('Parameter %s can not be empty', ''));
  148. }
  149. $this->view->assign("row", $row);
  150. $this->assignconfig('page_id', $page_id);
  151. $this->assign('page_id', $page_id);
  152. return $this->view->fetch();
  153. }
  154. /**
  155. * 删除
  156. */
  157. public function del($ids = "")
  158. {
  159. if ($ids) {
  160. $pk = $this->model->getPk();
  161. $adminIds = $this->getDataLimitAdminIds();
  162. if (is_array($adminIds)) {
  163. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  164. }
  165. $list = $this->model->where($pk, 'in', $ids)->select();
  166. $count = 0;
  167. foreach ($list as $k => $v) {
  168. model("PrivateBookPage")->getResourceList($v->page_id, true);
  169. $count += $v->delete();
  170. }
  171. if ($count) {
  172. $this->success();
  173. } else {
  174. $this->error(__('No rows were deleted'));
  175. }
  176. }
  177. $this->error(__('Parameter %s can not be empty', 'ids'));
  178. }
  179. }