id == 'add' or $action->id == 'edit') { $this->enableCsrfValidation = false; } return parent::beforeAction($action); } public function actionAdd() { /** * @var \common\models\Author $user ; */ $user = \Yii::$app->user->identity; if (\Yii::$app->user->isGuest) { $this->redirect([ 'site/index' ]); } $model = new Book(); if (\Yii::$app->request->isPost) { if ($model->load(\Yii::$app->request->post(), '') and $model->validate()) { $model->author_id = $user->id; $model->status = $model::STATUS_MODERATION; return ( $model->save() && $model->saveImage(UploadedFile::getInstanceByName('file')) ); } else { return false; } } return $this->render( 'add', [ 'book' => [], ] ); } public function actionEdit($id) { $model = Book::findOne($id); if ($model->author_id !== \Yii::$app->user->getId()) { return $this->redirect([ 'site/index' ]); } if (\Yii::$app->request->isPost) { if ($model->load(\Yii::$app->request->post(), '') and $model->validate()) { return ( $model->save() && $model->saveImage(UploadedFile::getInstanceByName('file')) ); } else { return false; } } return $this->render( 'add', [ 'book' => $model->attributes, ] ); } public function actionIndex() { $dataProvider = new ActiveDataProvider( [ 'query' => Book::find() ->with( [ 'author', 'alias', ] ) ->where([ 'status' => Book::STATUS_ACTIVE ]), 'pagination' => [ 'pageSize' => 10, ], ] ); return $this->render( 'index', [ 'dataProvider' => $dataProvider, ] ); } public function actionView($id) { $model = Book::find() ->with( [ 'author', 'activeComments', ] ) ->where([ 'id' => $id ]) ->one(); $support = Support::find() ->where([ 'book_id' => $model->id ]) ->all(); return $this->render( 'view', [ 'model' => $model, 'support' => $support, ] ); } public function actionSuccess(){ return $this->render('success'); } }