TuningController.php 9.54 KB
<?php

class TuningController extends NodeSiteController
{
    /** @var TuningBrand[] */
    private $_brands;
    /** @var TuningRoot */
    public $tuningRoot;
    /** @var FeedbackForm */
    public $feedbackForm;

    /** @return TuningBrand[] */
    public function getBrands()
    {
        if (!isset($this->_brands))
            $this->_brands = TuningBrand::model()->with('i18n')->findAll(array(
                'order' => 'rank asc',
                'condition' => 'not t.hidden and tuning_root_id = ' . $this->getNode()->data_id,
            ));
        return $this->_brands;
    }

    public function init()
    {
        parent::init();
        $this->tuningRoot = TuningRoot::model()->with('i18n')->findByPk($this->getNode()->data_id);
        $this->headerGalleryId = $this->tuningRoot->header_gallery_id;

        $this->feedbackForm = new FeedbackForm();
        if (isset($_POST['FeedbackForm'])) {
            Yii::app()->request->validateCsrfToken(null);
            $this->feedbackForm->setAttributes($_POST['FeedbackForm']);
            if ($this->feedbackForm->validate() && empty($this->feedbackForm->link)) {
                /** @var $mailer Swift_Mailer */
                $mailer = Yii::app()->swiftMailer->getMailer();
                $message = Swift_Message::newInstance();
                $message->setReplyTo($this->feedbackForm->email);
                $message->setFrom('site@auto-life.ua');
                $message->setTo($this->tuningRoot->feedback_email);
                $message->setSubject('Вопрос, c  раздела Тюнинг сайта Авто-лайф');
                $message->setBody(
                    "Имя: {$this->feedbackForm->name}\r\n" .
                        "E-Mail: {$this->feedbackForm->email}\r\n" .
                        "Контактный номер: {$this->feedbackForm->phone}\r\n" .
                        "Текст вопроса: {$this->feedbackForm->body}\r\n");

                $mailer->send($message);
                Yii::app()->user->setFlash('contact', Yii::t('site', 'Спасибо что написали. Мы свяжесся с вами как только сможем'));
                $this->refresh();
            }
        }
    }


    public function actionIndex()
    {
        $brands = TuningBrand::model()->with('tuningModels', 'i18n')->findAll(array(
            'order' => 'rank asc',
            'condition' => 'not t.hidden and tuning_root_id = ' . $this->getNode()->data_id,
        ));
        $this->_brands = $brands;
        //$pageData = $this->loadStaticPage($this->id, $this->action->id);
        $this->pageName = $this->tuningRoot->i18n->page_name;
        $this->setSEOParams($this->tuningRoot->i18n->title, $this->tuningRoot->i18n->keywords, $this->tuningRoot->i18n->description);

//        $this->setContacts($pageData['contacts']);
        $this->setContacts(explode(',', $this->tuningRoot->contacts_data));

        $this->render('index', array(
            'brands' => $brands,
        ));
    }

    public function actionBrand($brand)
    {
        /** @var $currentBrand TuningBrand */
        $currentBrand = TuningBrand::model()->with('i18n')->findByAttributes(array(
            'link' => $brand,
        ));
        if (empty($currentBrand))
            throw new CHttpException(404);
        $this->sectionGalleryId = $currentBrand->gallery_id;
        $this->pageName = $currentBrand->i18n->page_name;
        $this->setSEOParams($currentBrand->i18n->title, $currentBrand->i18n->keywords, $currentBrand->i18n->description);

        $this->setContacts(explode(',', $currentBrand->contacts_data));
        $this->setContacts(explode(',', $this->tuningRoot->contacts_data));

        $models = TuningModel::model()->findAll(array(
            'condition' => 'not t.hidden and t.tuning_brand_id = :brand_id',
            'params' => array(
                ':brand_id' => $currentBrand->id
            ),
        ));
        $this->render('brand', array(
            'currentBrand' => $currentBrand,
            'models' => $models,
        ));
    }

    public function actionModel($brand, $model)
    {
        /** @var $currentBrand TuningBrand */
        $currentBrand = TuningBrand::model()->with('i18n')->findByAttributes(array(
            'link' => $brand,
        ));
        if (empty($currentBrand))
            throw new CHttpException(404);
        $this->sectionGalleryId = $currentBrand->gallery_id;
        /** @var $currentModel TuningModel */
        $currentModel = TuningModel::model()->with('i18n')->findByAttributes(array(
            'link' => $model,
            'tuning_brand_id' => $currentBrand->id,
        ));
        if (empty($currentModel))
            throw new CHttpException(404);

        if ($currentModel->headerGalleryBehavior->getGalleryPhotoCount() > 0) {
            $this->sectionGalleryId = $currentModel->header_gallery_id;
        }
        $this->pageName = $currentModel->i18n->page_name;
        $this->setSEOParams($currentModel->i18n->title, $currentModel->i18n->keywords, $currentModel->i18n->description);

        $this->setContacts(explode(',', $currentModel->contacts_data));
        $this->setContacts(explode(',', $currentBrand->contacts_data));
        $this->setContacts(explode(',', $this->tuningRoot->contacts_data));

        $sets = TuningSet::model()->findAll(array(
            'order'=>'t.rank asc, t.id desc',
            'condition' => 'not t.hidden and t.tuning_model_id = :model_id',
            'params' => array(
                ':model_id' => $currentModel->id
            ),
        ));
        $currentModel->tuningSets;

        $this->render('model', array(
            'currentModel' => $currentModel,
            'currentBrand' => $currentBrand,
            'sets' => $sets,
        ));
    }

    public function actionView($brand, $model, $set)
    {
        /** @var $currentBrand TuningBrand */
        $currentBrand = TuningBrand::model()->with('i18n')->findByAttributes(array(
            'link' => $brand,
        ));
        if (empty($currentBrand))
            throw new CHttpException(404);
        $this->sectionGalleryId = $currentBrand->gallery_id;
        /** @var $currentModel TuningModel */
        $currentModel = TuningModel::model()->with('i18n')->findByAttributes(array(
            'link' => $model,
            'tuning_brand_id' => $currentBrand->id,
        ));
        if (empty($currentModel))
            throw new CHttpException(404);

        /** @var $currentSet TuningSet */
        $currentSet = TuningSet::model()->with('i18n')->findByAttributes(array(
            'link' => $set,
            'tuning_model_id' => $currentModel->id,
        ));
        if (empty($currentSet))
            throw new CHttpException(404);


        $this->pageName = $currentSet->i18n->page_name;
        $this->setSEOParams($currentSet->i18n->title, $currentSet->i18n->keywords, $currentSet->i18n->description);

        $this->setContacts(explode(',', $currentSet->contacts_data));
        $this->setContacts(explode(',', $currentModel->contacts_data));
        $this->setContacts(explode(',', $currentBrand->contacts_data));
        $this->setContacts(explode(',', $this->tuningRoot->contacts_data));


        $criteria = new CDbCriteria(array(
            'alias' => 't',
            'with' => array('i18n', 'tuningModel', 'tuningModel.tuningBrand'),
            'order' => 't.rank desc, t.id asc',
            'condition' => 'not t.hidden and t.id>:id and t.tuning_model_id = :model_id',
            'limit' => '2',
            'params' => array(':id' => $currentSet->id, ':model_id' => $currentModel->id),
        ));
        $next = TuningSet::model()->findAll($criteria);
        if (count($next) < 2) {
            $criteria->limit = 2 - count($next);
            $criteria->condition = 't.id<:id and t.tuning_model_id = :model_id';
            $next += TuningSet::model()->findAll($criteria);
        }

        $criteria->order = 't.rank asc, t.id desc';
        $criteria->condition = 'not t.hidden and t.id<:id and t.tuning_model_id = :model_id';
        $criteria->limit = 2;
        $prev = TuningSet::model()->findAll($criteria);
        if (count($prev) < 2) {
            $criteria->limit = 2 - count($prev);
            $criteria->condition = 'not t.hidden and t.id>:id and t.tuning_model_id = :model_id';
            $prev += TuningSet::model()->findAll($criteria);
        }

        if (count($next) > 0 && count($prev) > 0 && $next[0]->id == $prev[0]->id) $prev = array();

        $this->render('view', array(
            'currentSet' => $currentSet,
            'currentModel' => $currentModel,
            'currentBrand' => $currentBrand,
            'prev' => $prev,
            'next' => $next,
        ));
    }

    public function actionService($link)
    {
        /** @var $service TuningService */
        $service = TuningService::model()->with('i18n')->findByAttributes(array(
            'link' => $link,
        ));
        if (empty($service))
            throw new CHttpException(404);
        $this->sectionGalleryId = $service->gallery_id;

        $this->setContacts(explode(',', $service->contacts_data));
        $this->setContacts(explode(',', $this->tuningRoot->contacts_data));

        $this->pageName = $service->i18n->page_name;
        $this->setSEOParams($service->i18n->title, $service->i18n->keywords, $service->i18n->description);

        $this->render('service', array(
            'service' => $service,
        ));
    }

    /** @return TuningService[] */
    public function getServices()
    {
        return TuningService::model()->with('i18n')->findAll(array(
            'order' => 'rank asc',
            'condition'=>'not t.hidden',
        ));
    }
}