Page.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace app\admin\controller\cms;
  3. use app\common\controller\Backend;
  4. use think\Hook;
  5. /**
  6. * 单页表
  7. *
  8. * @icon fa fa-file
  9. */
  10. class Page extends Backend
  11. {
  12. /**
  13. * Page模型对象
  14. */
  15. protected $model = null;
  16. protected $searchFields = 'id,title,seotitle';
  17. protected $noNeedRight = ['select', 'selectpage_type', 'check_element_available'];
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $config = get_addon_config('cms');
  22. if ($config['pagedatalimit'] != 'all') {
  23. $this->dataLimit = $config['pagedatalimit'];
  24. }
  25. $config = get_addon_config('cms');
  26. $this->assignconfig('spiderRecord', intval($config['spiderrecord']?? 0));
  27. $this->model = new \app\admin\model\cms\Page;
  28. $this->view->assign("flagList", $this->model->getFlagList());
  29. $this->view->assign("statusList", $this->model->getStatusList());
  30. $this->assignconfig("flagList", $this->model->getFlagList());
  31. }
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. $typeArr = \app\admin\model\cms\Page::distinct('type')->column('type');
  38. $this->view->assign('typeList', $typeArr);
  39. $this->assignconfig('typeList', $typeArr);
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $list = $this->model
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. \app\admin\model\cms\SpiderLog::render($list, 'page');
  53. $result = array("total" => $list->total(), "rows" => $list->items());
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. /**
  59. * 添加
  60. */
  61. public function add()
  62. {
  63. if ($this->request->isPost()) {
  64. $row = $this->request->post("row/a", []);
  65. if (isset($row['parsetpl']) && $row['parsetpl']) {
  66. $this->token();
  67. }
  68. }
  69. $values = [];
  70. $fields = \addons\cms\library\Service::getCustomFields('page', 0, $values);
  71. $this->view->assign('fields', $fields);
  72. $this->view->assign('values', $values);
  73. return parent::add();
  74. }
  75. /**
  76. * 编辑
  77. */
  78. public function edit($ids = null)
  79. {
  80. if ($this->request->isPost()) {
  81. $row = $this->request->post("row/a", []);
  82. if (isset($row['parsetpl']) && $row['parsetpl']) {
  83. $this->token();
  84. }
  85. }
  86. $values = \app\admin\model\cms\Page::get($ids);
  87. if (!$values) {
  88. $this->error(__('No Results were found'));
  89. }
  90. $values = $values->toArray();
  91. $fields = \addons\cms\library\Service::getCustomFields('page', 0, $values);
  92. $this->view->assign('fields', $fields);
  93. $this->view->assign('values', $values);
  94. return parent::edit($ids);
  95. }
  96. /**
  97. * 选择单页
  98. */
  99. public function select()
  100. {
  101. if (!$this->auth->check('cms/page/index')) {
  102. Hook::listen('admin_nopermission', $this);
  103. $this->error(__('You have no permission'), '');
  104. }
  105. $typeArr = \app\admin\model\cms\Page::distinct('type')->column('type');
  106. $this->view->assign('typeList', $typeArr);
  107. $this->assignconfig('typeList', $typeArr);
  108. //设置过滤方法
  109. $this->request->filter(['strip_tags']);
  110. if ($this->request->isAjax()) {
  111. //如果发送的来源是Selectpage,则转发到Selectpage
  112. if ($this->request->request('keyField')) {
  113. return $this->selectpage();
  114. }
  115. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  116. $total = $this->model
  117. ->where($where)
  118. ->order($sort, $order)
  119. ->count();
  120. $list = $this->model
  121. ->where($where)
  122. ->order($sort, $order)
  123. ->limit($offset, $limit)
  124. ->select();
  125. $list = collection($list)->toArray();
  126. $result = array("total" => $total, "rows" => $list);
  127. return json($result);
  128. }
  129. return $this->view->fetch();
  130. }
  131. /**
  132. * 动态下拉选择类型
  133. * @internal
  134. */
  135. public function selectpage_type()
  136. {
  137. $list = [];
  138. $word = (array)$this->request->request("q_word/a");
  139. $field = $this->request->request('showField');
  140. $keyValue = $this->request->request('keyValue');
  141. if (!$keyValue) {
  142. if (array_filter($word)) {
  143. foreach ($word as $k => $v) {
  144. $list[] = ['id' => $v, $field => $v];
  145. }
  146. }
  147. $typeArr = \app\admin\model\cms\Page::column('type');
  148. $typeArr = array_unique($typeArr);
  149. foreach ($typeArr as $index => $item) {
  150. $list[] = ['id' => $item, $field => $item];
  151. }
  152. } else {
  153. $list[] = ['id' => $keyValue, $field => $keyValue];
  154. }
  155. return json(['total' => count($list), 'list' => $list]);
  156. }
  157. /**
  158. * 检测元素是否可用
  159. * @internal
  160. */
  161. public function check_element_available()
  162. {
  163. $id = $this->request->request('id');
  164. $name = $this->request->request('name');
  165. $value = $this->request->request('value');
  166. $name = substr($name, 4, -1);
  167. if (!$name) {
  168. $this->error(__('Parameter %s can not be empty', 'name'));
  169. }
  170. if ($id) {
  171. $this->model->where('id', '<>', $id);
  172. }
  173. $exist = $this->model->where($name, $value)->find();
  174. if ($exist) {
  175. $this->error(__('The data already exist'));
  176. } else {
  177. $this->success();
  178. }
  179. }
  180. }