<?php

class QuestionController extends NodeSiteController
{
    /** @var QuestionRoot */
    public $questionRoot;

    public function init()
    {
        parent::init();

        $this->questionRoot = QuestionRoot::model()->findByPk($this->getNode()->data_id);
        $this->headerGalleryId = $this->questionRoot->header_gallery_id;
    }

    public function actionIndex($category = null)
    {
        $questionCategories = QuestionCategory::model()->with('i18n')->findAll(array(
            'order' => 'rank asc',
            'condition'=>'question_root_id = '. $this->getNode()->data_id,
        ));

        if (isset($category)) {
            /** @var $currentCategory QuestionCategory */
            $currentCategory = QuestionCategory::model()->findByAttributes(array(
                'link' => $category,
            ));
            if (empty($currentCategory)) throw new CHttpException(404);
            $this->sectionGalleryId = $currentCategory->gallery_id;

        } else {
            $currentCategory = $questionCategories[0];
        }
        $questions = Question::model()->with('i18n')->findAll(array(
            'condition' => 'question_category_id=' . $currentCategory->id
        ));

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

        $this->setContacts(explode(',',$currentCategory->contacts_data));
        $this->setContacts(explode(',',$this->questionRoot->contacts_data));

        $this->render('index', array(
            'questionCategories' => $questionCategories,
            'questions' => $questions,
            'currentCategory' => $currentCategory,
        ));
    }
}