Page.php 5.0 KB

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