[ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['post'], ], ], ]; } public function actions() { return [ 'position' => [ 'class' => 'backend\\actions\\Position', 'returnUrl' => Url::current() ] ]; } /** * Lists all Policy models. * * @return mixed */ public function actionIndex() { Url::remember(); $searchModel = new PolicySearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); // $dataProvider = new ActiveDataProvider([ // 'query' => Policy::find(), // ]); $cengciList = Config::find()->select(['extra'])->where(['group' => 'policy', 'name' => 'cengci'])->one(); $cengciList = explode("\r\n",$cengciList['extra']); $cengciList = array_combine($cengciList, $cengciList); return $this->render('index', [ 'dataProvider' => $dataProvider, 'searchModel' => $searchModel, 'cengciList' => $cengciList, ]); } /** * Displays a single Policy model. * * @param int $id * * @return mixed */ public function actionView($id) { return $this->render('view', [ 'model' => $this->findModel($id), ]); } /** * Creates a new Survey model. * If creation is successful, the browser will be redirected to the 'view' Survey. * * @return mixed */ public function actionCreate() { $model = new Policy(); $editor = request('editor') ? : config('page_editor_type'); $model->markdown = $editor == 'markdown' ? 1 : 0; $configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => 'policy'])->all(); if (Yii::$app->request->isPost) { $transaction = Yii::$app->db->beginTransaction(); try { $data = Yii::$app->request->post(); $arr = []; foreach ($data as $key=>$value) { if($key == '_csrfBackend' || $key == 'Policy' || $key == 'cengci'){ continue; } $arr[$key] = $value; } $data['Policy']['filter'] = json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); if (empty($data['cengci'])) { throw new Exception('层次为空'); } if (empty($data['Policy']['type'])) { throw new Exception('类型为空'); } $data['Policy']['cengci'] = join(',',$data['cengci']); $data['Policy']['type'] = join(',',$data['Policy']['type']); $model->load($data); $model->save(); if($model->hasErrors()) { throw new Exception(current($model->getErrors())[0]); } $transaction->commit(); Yii::$app->session->setFlash('success', '操作成功'); } catch (\Exception $e) { $transaction->rollBack(); Yii::$app->session->setFlash('error', $e->getMessage()); } return $this->goBack(); } $model->estate = PolicyEnum::POLICY_INDUSTRY;// 默认现代产业 return $this->render('create', [ 'model' => $model, 'configModels' => $configModels, ]); } /** * Updates an existing Survey model. * If update is successful, the browser will be redirected to the 'view' Survey. * * @param int $id * * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => 'policy'])->all(); if (Yii::$app->request->isPost) { $transaction = Yii::$app->db->beginTransaction(); try { $data = Yii::$app->request->post(); $arr = []; foreach ($data as $key=>$value) { if($key == '_csrfBackend' || $key == 'Policy' || $key == 'cengci'){ continue; } $arr[$key] = $value; } $data['Policy']['filter'] = json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); if (empty($data['cengci'])) { throw new Exception('层次为空'); } if (empty($data['Policy']['type'])) { throw new Exception('类型为空'); } $data['Policy']['cengci'] = join(',',$data['cengci']); $data['Policy']['type'] = join(',',$data['Policy']['type']); $model->load($data); $model->save(); if($model->hasErrors()) { throw new Exception(current($model->getErrors())[0]); } $transaction->commit(); Yii::$app->session->setFlash('success', '操作成功'); } catch (\Exception $e) { $transaction->rollBack(); Yii::$app->session->setFlash('error', $e->getMessage()); } return $this->goBack(); } // $filterArr = []; // $filter = explode(',',$model->filter); // if(!empty($filter)){ // foreach ($filter as $key=>$value) { // if (empty($value)) { // continue; // } // $arr = explode('_',$value); // if (!empty($arr) && isset($arr[0]) && isset($arr[1])) { // $filterArr[$arr[0]] = $arr[1]; // } // } // } $model->cengci = explode(',',$model->cengci); $model->type = explode(',',$model->type); // $model->filter = $filterArr; $model->filter = json_decode($model->filter,true); if (empty($model->estate)) { $model->estate = PolicyEnum::POLICY_INDUSTRY;//默认现代产业 } return $this->render('update', [ 'model' => $model, 'configModels' => $configModels, ]); } /** * Deletes an existing Survey model. * If deletion is successful, the browser will be redirected to the 'index' Survey. * * @param int $id * * @return mixed */ public function actionDelete($id) { $this->findModel($id)->delete(); return $this->redirect(['index']); } /** * Finds the Survey model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param int $id * * @return Policy the loaded model * * @throws NotFoundHttpException if the model cannot be found */ public function findModel($id) { if (($model = Policy::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } }