SiteController.php 6.31 KB
<?php
    namespace frontend\controllers;
    
    use artbox\core\models\Feedback;
    use common\models\blog\Article;
    use common\models\Book;
    use common\models\LoginForm;
    use common\models\page\Page;
    use common\models\Settings;
    use common\models\slider\Slide;
    use frontend\models\SignupForm;
    use Yii;
    use yii\filters\VerbFilter;
    use yii\swiftmailer\Mailer;
    use yii\web\BadRequestHttpException;
    use yii\web\Controller;
    use yii\web\Response;
    
    /**
     * Site controller
     */
    class SiteController extends Controller
    {
        /**
         * @inheritdoc
         */
        public function actions()
        {
            return [
                'error' => [
                    'class' => 'yii\web\ErrorAction',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'verbs' => [
                    'class'   => VerbFilter::className(),
                    'actions' => [
                        'feedback' => [ 'post' ],
                    ],
                ],
            ];
        }
        
        /**
         * Displays homepage.
         *
         * @return mixed
         */
        public function actionIndex()
        {
            $books = Book::find()->where(['status' => Book::STATUS_ACTIVE])->andWhere(['on_main' => true])->limit(2)->all();
            $articles = Article::find()
                               ->with('language')
                               ->where([ 'status' => true ])
                               ->orderBy('sort DESC')
                               ->limit(4)
                               ->all();
            return $this->render('index', [
                'books' => $books,
                'articles' => $articles
            ]);
        }
        
        /**
         * Displays contact page.
         *
         * @return mixed
         */
        public function actionContact()
        {
            $contact = new Feedback();
            return $this->render(
                'contact',
                [
                    'contact' => $contact,
                ]
            );
        }
        
        /**
         * Displays about page.
         *
         * @return mixed
         */
        public function actionAbout()
        {
            $page = Page::find()->joinWith('language.alias')->where(['alias.value' => 'about'])->one();
            return $this->render('about', [
                'page' => $page
            ]);
        }
        
        /**
         * Action to view robots.txt file dinamycli
         *
         * @return string
         */
        public function actionRobots()
        {
            $response = \Yii::$app->response;
            /**
             * @var Settings $settings
             */
            $settings = Settings::find()
                                ->one();
            $temp = tmpfile();
            fwrite($temp, $settings->robots);
            $meta = stream_get_meta_data($temp);
            $response->format = $response::FORMAT_RAW;
            $response->headers->set('Content-Type', 'text/plain');
            return $this->renderFile($meta[ 'uri' ]);
        }
        
        public function actionFeedback()
        {
            Yii::$app->response->format = Response::FORMAT_JSON;
            
            /**
             * @var Mailer $mailer
             */
            $mailer = \Yii::$app->get('smtpmailer');
            $settings = Settings::getInstance();
            
            if (empty(Yii::$app->request->post())) {
                throw new BadRequestHttpException();
            } else {
                $model = new Feedback();
                if ($model->load(Yii::$app->request->post()) && $model->save()) {
                    
                    $mailer->compose(
                        'feedback',
                        [
                            'model' => $model,
                        ]
                    )
                           ->setFrom('artbox@domain.com')
                           ->setTo($settings->email)
                           ->setSubject(\Yii::t('app', 'Feedback'))
                           ->send();
                    
                    return [
                        'success' => true,
                        'message' => 'Success message',
                        'alert'   => '<div class="alert alert-success">
            <h3>Success</h3>
            <p>
              Success text
            </p>
          </div>',
                    ];
                } else {
                    return [
                        'success' => false,
                        'error'   => $model->errors,
                    ];
                }
            }
        }
        
        public function actionRegister(){
            if (!\Yii::$app->user->isGuest){
                return $this->redirect(['site/index']);
            }
            $model = new SignupForm(['create' => 1]);
            if ($model->load(\Yii::$app->request->post())){
                /**
                 * @var \yii\web\IdentityInterface $user;
                 */
                if (($user = $model->signup()) !== null){
                    Yii::$app->user->login($user);
                    return $this->redirect(['book/add']);
                }
            }
            return $this->render('register', [
                'model' => $model
            ]);
        }
    
    
        public function actionLogin()
        {
            if (!\Yii::$app->user->isGuest) {
                return $this->redirect(['site/index']);
            }
        
            $loginForm = new LoginForm();
            if ($loginForm->load(\Yii::$app->request->post()) and $loginForm->login())
            {
                return $this->redirect(['site/index']);
            }
            return $this->render('login', [
               'model' => $loginForm,
            ]);
        }

        public function actionAccount(){
            return $this->render('account');
        }

        public function actionView(){
            return $this->render('view');
        }
    
        public function actionLogout()
        {
            \Yii::$app->user->setReturnUrl('index');
            \Yii::$app->user->logout();
        
            return $this->redirect([ 'index' ]);
        }
        
    }