| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 | <?phpnamespace backend\controllers;use common\models\Survey;use Yii;use yii\data\ActiveDataProvider;use yii\filters\VerbFilter;use yii\web\Controller;use yii\web\NotFoundHttpException;use common\modules\config\models\Config;/** * SurveyController implements the CRUD actions for Survey model. */class SurveyController extends Controller{    public function behaviors()    {        return [            'verbs' => [                'class' => VerbFilter::className(),                'actions' => [                    'delete' => ['post'],                ],            ],        ];    }    /**     * Lists all Survey models.     *     * @return mixed     */    public function actionIndex()    {        $dataProvider = new ActiveDataProvider([            'query' => Survey::find(),        ]);        return $this->render('index', [            'dataProvider' => $dataProvider,        ]);    }    /**     * Displays a single Survey 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 Survey();        $editor = request('editor') ? : config('page_editor_type');        $model->markdown = $editor == 'markdown' ? 1 : 0;        $type = Yii::$app->request->get('type','industry');        $type = empty($type) ? 'industry' : $type;        $model->type = $type;                $typeDesc = '';        switch ($type) {            case 'industry':                $typeDesc = '现代产业';                break;            case 'education':                $typeDesc = '高等教育';                break;            case 'medical':                $typeDesc = '医疗卫生';                break;            case 'circuit':                $typeDesc = '集成电路';                break;        }      		$configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => $type])->all();				if (Yii::$app->request->isPost) {			$data = Yii::$app->request->post();						$arr = [];			foreach ($data as $key=>$value) {				if($key == '_csrfBackend' || $key == 'Survey'){					continue;				}				$arr[$key] = $value;			}			$data['Survey']['filter'] = json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);            $data['Survey']['type'] = $type;            			$model->load($data);			$model->save();			return $this->redirect(['index']);		}    		return $this->render('create', [			'model' => $model,			'type'=>$typeDesc,			'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);        $typeDesc = '';        switch ($model['type']) {            case 'industry':                $typeDesc = '现代产业';                break;            case 'education':                $typeDesc = '高等教育';                break;            case 'medical':                $typeDesc = '医疗卫生';                break;            case 'circuit':                $typeDesc = '集成电路';                break;        }        		$configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => $model['type']])->all();				if (Yii::$app->request->isPost) {			$data = Yii::$app->request->post();						$arr = [];			foreach ($data as $key=>$value) {				if($key == '_csrfBackend' || $key == 'Survey'){					continue;				}				$arr[$key] = $value;			}			$data['Survey']['filter'] = json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);          			$model->load($data);			$model->save();			return $this->redirect(['index']);		}				$model->filter = json_decode($model->filter,true);		return $this->render('update', [			'model' => $model,			'type'=>$typeDesc,			'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 Survey the loaded model     *     * @throws NotFoundHttpException if the model cannot be found     */    protected function findModel($id)    {        if (($model = Survey::findOne($id)) !== null) {                        return $model;        } else {            throw new NotFoundHttpException('The requested page does not exist.');        }    }}
 |