Text.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\admin\controller\setting;
  3. use app\common\controller\Backend;
  4. use think\Config;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use think\Route;
  9. /**
  10. * 文本配置管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Text extends Backend
  15. {
  16. /**
  17. * Text模型对象
  18. * @var \app\admin\model\setting\Text
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\setting\Text;
  25. $this->view->assign("typeList", $this->model->getTypeList());
  26. }
  27. public function import()
  28. {
  29. parent::import();
  30. }
  31. /**
  32. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  33. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  34. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  35. */
  36. public function index()
  37. {
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax()) {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $list = $this->model
  47. ->field('id,desc,text,title,update_time')
  48. ->where($where)
  49. ->where('public', 1)
  50. ->order('weigh desc,id asc')
  51. ->paginate($limit)
  52. ->each(function ($item) {
  53. $item->link = $this->request->domain() . '/agreement/' . $item->title . '.html';
  54. $item->hidden(['text']);
  55. });
  56. $result = array("total" => $list->total(), "rows" => $list->items());
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. public function edit($ids = null)
  62. {
  63. $row = $this->model->get($ids);
  64. if (!$row) {
  65. $this->error(__('No Results were found'));
  66. }
  67. $adminIds = $this->getDataLimitAdminIds();
  68. if (is_array($adminIds)) {
  69. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  70. $this->error(__('You have no permission'));
  71. }
  72. }
  73. if ($this->request->isPost()) {
  74. $params = $this->request->post("row/a");
  75. if ($params) {
  76. $params = $this->preExcludeFields($params);
  77. if ('text' == $row->getAttr('type')) {
  78. $params['text'] = strip_tags($params['text']);
  79. }
  80. $result = false;
  81. Db::startTrans();
  82. try {
  83. //是否采用模型验证
  84. if ($this->modelValidate) {
  85. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  86. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  87. $row->validateFailException(true)->validate($validate);
  88. }
  89. $result = $row->allowField(true)->save($params);
  90. Db::commit();
  91. } catch (ValidateException $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. } catch (PDOException $e) {
  95. Db::rollback();
  96. $this->error($e->getMessage());
  97. } catch (\Exception $e) {
  98. Db::rollback();
  99. $this->error($e->getMessage());
  100. }
  101. if ($result !== false) {
  102. $this->success();
  103. } else {
  104. $this->error(__('No rows were updated'));
  105. }
  106. }
  107. $this->error(__('Parameter %s can not be empty', ''));
  108. }
  109. $this->assign('is_rich_text', 'rich_text' == $row->getAttr('type') ? true : false);
  110. $this->view->assign("row", $row);
  111. return $this->view->fetch();
  112. }
  113. }