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'); } public function actionList($q = null){ \Yii::$app->response->format = Response::FORMAT_JSON; $out = [ 'results' => [ [ 'id' => '', 'text' => '', ], ], ]; if (!is_null($q)) { $books = Book::find() ->select( [ 'id' => 'book.id', 'text' => 'book.title', ] ) ->andFilterWhere( [ 'ilike', 'title', $q, ] ) ->limit(20) ->asArray() ->all(); if (!empty($books)) { $out[ 'results' ] = $books; } } return $out; } public function actionDelete($id){ \Yii::$app->response->format = Response::FORMAT_JSON; $book = Book::findOne($id); if ($book->status == Book::STATUS_ACTIVE){ $book->status = Book::STATUS_DELETED; return $book->save(); }elseif($book->status == Book::STATUS_MODERATION){ return $book->delete(); } } }