BookController.php 3.92 KB
<?php
    /**
     * Created by PhpStorm.
     * User: stes
     * Date: 08.06.18
     * Time: 12:39
     */
    
    namespace frontend\controllers;
    
    use common\models\Book;
    use common\models\Support;
    use yii\data\ActiveDataProvider;
    use yii\web\Controller;
    use yii\web\UploadedFile;
    
    class BookController extends Controller
    {
        /**
         * @inheritdoc
         */
        public function beforeAction($action)
        {
            if ($action->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,
                ]
            );
        }
    }