SiteController.php 7.99 KB
<?php

namespace frontend\controllers;

use artbox\core\models\Alias;
use artbox\core\models\Language;
use artbox\core\models\Page;
use common\models\Feedback;
use common\models\Settings;
use common\models\Slider;
use Yii;
use yii\filters\VerbFilter;
use yii\swiftmailer\Mailer;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use common\models\Objectkb;

/**
 * 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()
    {
        
        $slider = Slider::find()
                        ->with("slides.lang.image")
                        ->where(['on_home_page' => true])
                        ->one();
        
        $objects = Objectkb::find()
                           ->with('lang.alias')
                           ->where(
                               [
                                   'id' => [
                                       7,
                                       37,
                                       38,
                                       43,
                                   ],
                               ]
                           )
                           ->orderBy('id')
                           ->all();
        
        $alias = Alias::find()
                      ->where(
                          [
                              'language_id' => Language::getCurrent()->id,
                              'route'       => '{"0":"site/index"}',
                          ]
                      )
                      ->one();
        
        return $this->render(
            'index',
            [
                'slider'  => $slider,
                'objects' => $objects,
                'alias'   => $alias,
            ]
        );
    }
    
    /**
     * 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()
                    ->with('lang')
                    ->where(['id' => 3])
                    ->one();
        
        if ($page) {
            return $this->render(
                'about',
                [
                    'page' => $page,
                ]
            );
            
        } else {
            throw new NotFoundHttpException();
        }
        
    }
    
    public function actionIndividual()
    {
        $idsArray = [
            25,
            37,
            12,
            42,
        ];
        
        $objects = Objectkb::find()
                           ->with('lang.alias')
                           ->where(
                               [
                                   'id' => $idsArray,
                               ]
                           )
                           ->indexBy('id')
                           ->all();
        
        return $this->render(
            'individual',
            [
                'objects'  => $objects,
                'idsArray' => $idsArray,
            ]
        );
    } // частное лицо
    
    public function actionLegal()
    
    {
        $legal = new Feedback();
        
        return $this->render(
            'legal',
            [
                'legal' => $legal,
            ]
        );
    } // юридическое
    
    public function actionMediaAbout()
    {
        return $this->render('media-about');
    } // СМИ о нас
    
    /**
     * 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 {
            
            $post = Yii::$app->request->post('Feedback');
            switch ($post[ 'topic' ]) {
                case Feedback::SCENARIO_FEEDBACK :
                    $model = new Feedback(['scenario' => Feedback::SCENARIO_FEEDBACK]);
                    $view = 'feedback';
                    $isLoaded = $model->load(Yii::$app->request->post());
                    break;
                
                case Feedback::SCENARIO_CALLBACK :
                    $model = new Feedback(['scenario' => Feedback::SCENARIO_CALLBACK]);
                    $view = 'feedback';
                    $isLoaded = $model->load(Yii::$app->request->post());
                    break;
                
                case Feedback::SCENARIO_CALCULATOR:
                    $model = new Feedback(['scenario' => Feedback::SCENARIO_CALCULATOR]);
                    $view = 'calculator';
                    $isLoaded = $model->load(Yii::$app->request->post());
                    $model->setCalcJsonInfo();
                    break;
                
                case Feedback::SCENARIO_LEGAL_FEEDBACK:
                    $model = new Feedback(['scenario' => Feedback::SCENARIO_LEGAL_FEEDBACK]);
                    $view = 'legalfeedback';
                    $isLoaded = $model->load(Yii::$app->request->post());
                    $email = 'haichenko_ky@kbenergy.com.ua';
                    $model->setCalcJsonInfo();
                    break;
                
                default:
                    $model = new Feedback();
                    $view = 'feedback';
                    $isLoaded = $model->load(Yii::$app->request->post());
            }
            
            if ($isLoaded && $model->save()) {
                
                $mailer->compose(
                    $view,
                    [
                        'model' => $model,
                    ]
                )
                       ->setFrom(['artbox@domain.com'])
                       ->setTo(
                           [
                               !empty($email) ? $email : $settings->email,
                           ]
                       )
                       ->setSubject(\Yii::t('app', 'Feedback'))
                       ->send();
                
                return [
                    'success' => true,
                    'message' => 'Success message',
                    'view'    => $view,
                    'model'   => $model->attributeValues,
                    'alert'   => $this->renderPartial('success_alert'),
                ];
            } else {
                Yii::$app->response->setStatusCode(500);
                
                return [
                    'success' => false,
                    'error'   => $model->errors,
                ];
            }
        }
    }
}