HomeBlock.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\admin\controller\ps;
  3. use app\common\controller\Backend;
  4. use app\common\library\ps\HomePage;
  5. use think\Db;
  6. use think\Exception;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 工厂端首页模块
  11. *
  12. * @icon fa fa-home
  13. */
  14. class HomeBlock extends Backend
  15. {
  16. /**
  17. * @var \app\admin\model\ps\Home_block
  18. */
  19. protected $model = null;
  20. protected $searchFields = 'name,title,kicker,subtitle';
  21. protected $multiFields = 'status';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\ps\Home_block;
  26. $this->view->assign('typeList', $this->model->getTypeList());
  27. $this->view->assign('statusList', $this->model->getStatusList());
  28. }
  29. public function add()
  30. {
  31. if ($this->request->isPost()) {
  32. $this->token();
  33. $params = $this->request->post('row/a');
  34. if (!$params) {
  35. $this->error(__('Parameter %s can not be empty', ''));
  36. }
  37. $params = $this->preExcludeFields($params);
  38. $params = HomePage::normalize($params);
  39. $result = false;
  40. Db::startTrans();
  41. try {
  42. $result = $this->model->allowField(true)->save($params);
  43. Db::commit();
  44. } catch (ValidateException|PDOException|Exception $e) {
  45. Db::rollback();
  46. $this->error($e->getMessage());
  47. }
  48. if ($result === false) {
  49. $this->error(__('No rows were inserted'));
  50. }
  51. $this->success();
  52. }
  53. $this->assignForm([
  54. 'type' => 'features',
  55. 'name' => '',
  56. 'kicker' => '',
  57. 'title' => '',
  58. 'subtitle' => '',
  59. 'weigh' => 50,
  60. 'status' => 'normal',
  61. 'content' => [],
  62. ]);
  63. return $this->view->fetch();
  64. }
  65. public function edit($ids = null)
  66. {
  67. $row = $this->model->get($ids);
  68. if (!$row) {
  69. $this->error(__('No Results were found'));
  70. }
  71. if ($this->request->isPost()) {
  72. $this->token();
  73. $params = $this->request->post('row/a');
  74. if (!$params) {
  75. $this->error(__('Parameter %s can not be empty', ''));
  76. }
  77. $params = $this->preExcludeFields($params);
  78. $params = HomePage::normalize($params);
  79. $result = false;
  80. Db::startTrans();
  81. try {
  82. $result = $row->allowField(true)->save($params);
  83. Db::commit();
  84. } catch (ValidateException|PDOException|Exception $e) {
  85. Db::rollback();
  86. $this->error($e->getMessage());
  87. }
  88. if ($result === false) {
  89. $this->error(__('No rows were updated'));
  90. }
  91. $this->success();
  92. }
  93. $data = $row->toArray();
  94. $data['content'] = [];
  95. if (!empty($row['content'])) {
  96. $decoded = json_decode($row['content'], true);
  97. if (is_array($decoded)) {
  98. $data['content'] = $decoded;
  99. }
  100. }
  101. $this->assignForm($data);
  102. return $this->view->fetch('add');
  103. }
  104. /**
  105. * @param array $data
  106. */
  107. protected function assignForm(array $data)
  108. {
  109. $content = isset($data['content']) && is_array($data['content']) ? $data['content'] : [];
  110. $data['content'] = $content;
  111. $this->view->assign('row', $data);
  112. $this->view->assign('notesText', implode("\n", (array)($content['notes'] ?? [])));
  113. $this->view->assign('contentJson', json_encode(
  114. $content,
  115. JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
  116. ));
  117. }
  118. }