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 )); } }