| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace app\admin\controller\ps;
- use app\common\controller\Backend;
- use app\common\library\ps\HomePage;
- use think\Db;
- use think\Exception;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- /**
- * 工厂端首页模块
- *
- * @icon fa fa-home
- */
- class HomeBlock extends Backend
- {
- /**
- * @var \app\admin\model\ps\Home_block
- */
- protected $model = null;
- protected $searchFields = 'name,title,kicker,subtitle';
- protected $multiFields = 'status';
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\ps\Home_block;
- $this->view->assign('typeList', $this->model->getTypeList());
- $this->view->assign('statusList', $this->model->getStatusList());
- }
- public function add()
- {
- if ($this->request->isPost()) {
- $this->token();
- $params = $this->request->post('row/a');
- if (!$params) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $params = $this->preExcludeFields($params);
- $params = HomePage::normalize($params);
- $result = false;
- Db::startTrans();
- try {
- $result = $this->model->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException|PDOException|Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result === false) {
- $this->error(__('No rows were inserted'));
- }
- $this->success();
- }
- $this->assignForm([
- 'type' => 'features',
- 'name' => '',
- 'kicker' => '',
- 'title' => '',
- 'subtitle' => '',
- 'weigh' => 50,
- 'status' => 'normal',
- 'content' => [],
- ]);
- return $this->view->fetch();
- }
- public function edit($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- if ($this->request->isPost()) {
- $this->token();
- $params = $this->request->post('row/a');
- if (!$params) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $params = $this->preExcludeFields($params);
- $params = HomePage::normalize($params);
- $result = false;
- Db::startTrans();
- try {
- $result = $row->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException|PDOException|Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result === false) {
- $this->error(__('No rows were updated'));
- }
- $this->success();
- }
- $data = $row->toArray();
- $data['content'] = [];
- if (!empty($row['content'])) {
- $decoded = json_decode($row['content'], true);
- if (is_array($decoded)) {
- $data['content'] = $decoded;
- }
- }
- $this->assignForm($data);
- return $this->view->fetch('add');
- }
- /**
- * @param array $data
- */
- protected function assignForm(array $data)
- {
- $content = isset($data['content']) && is_array($data['content']) ? $data['content'] : [];
- $data['content'] = $content;
- $this->view->assign('row', $data);
- $this->view->assign('notesText', implode("\n", (array)($content['notes'] ?? [])));
- $this->view->assign('contentJson', json_encode(
- $content,
- JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
- ));
- }
- }
|