<?php

class ActionController extends Controller
{
    /** @var array General Section Params */
    public $generalData;

    public function init()
    {
        parent::init();
        /** @var $sectionData StaticData */
        $sectionData = StaticData::model()->with('i18n')->findByPk(array('section' => $this->id, 'key' => 'general'));
        $this->generalData = $sectionData->getDataAttributes() + $sectionData->i18n->getDataAttributes();
        $this->headerGalleryId = $this->generalData['gallery_id'];
    }

    public function actionIndex()
    {
        $data = $this->loadStaticPage($this->id, $this->action->id);
        $this->sectionGalleryId = $data['gallery_id'];

        $this->setContacts($data['contacts']);
        $this->setContacts($this->generalData['contacts']);

        /** @var $actions Action[] */
        $actions = Action::model()->with('i18n', 'actionCategory')->findAll(array(
//            'condition' => 'not t.hidden and not actionCategory.hidden and not is_finished',
            'order' => 't.rank asc',
        ));



        $this->render('index', array(
            'actions' => $actions,
            'content' => $data['content'],
        ));
    }

    public function actionCategory($link)
    {
        /** @var $category ActionCategory */
        $category = ActionCategory::model()->with('i18n')->findByAttributes(array(
            'link' => $link,
        ));
        if (empty($category))
            throw new CHttpException(404);

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

        $this->setContacts(explode(',', $category->contacts_data));
        $this->setContacts($this->generalData['contacts']);
        /** @var $actions Action[] */
        $actions = Action::model()->with('i18n', 'actionCategory')->findAll(array(
            'condition' => 'not t.hidden and not is_finished and action_category_id =' . $category->id,
            'order' => 't.rank asc',
        ));
        $this->render('index', array(
            'actions' => $actions,
            'content' => $category->i18n->content,
            'currentCategory' => $category,
        ));
    }

    public function actionFinished()
    {
        $data = $this->loadStaticPage($this->id, $this->action->id);
        $this->sectionGalleryId = $data['gallery_id'];

        $this->setContacts($data['contacts']);
        $this->setContacts($this->generalData['contacts']);

        /** @var $actions Action[] */
        $actions = Action::model()->with('i18n', 'actionCategory')->findAll(array(
            'condition' => 'not t.hidden and is_finished',
            'order' => 't.rank asc',
        ));
        $this->render('index', array(
            'actions' => $actions,
            'content' => $data['content'],
        ));
    }

    public function actionView($category, $link)
    {
        /** @var $category ActionCategory */
        $category = ActionCategory::model()->with('i18n')->findByAttributes(array(
            'link' => $category,
        ));
        if (empty($category))
            throw new CHttpException(404);
        $this->sectionGalleryId = $category->gallery_id;
        /** @var $action Action */
        $action = Action::model()->with('i18n')->findByAttributes(array(
            'link' => $link,
            'action_category_id' => $category->id,
        ));
        if (empty($action))
            throw new CHttpException(404);
        $this->pageName = $action->i18n->page_name;
        $this->setSEOParams($action->i18n->title, $action->i18n->keywords, $action->i18n->description);

        $this->setContacts(explode(',', $action->contacts_data));
        $this->setContacts(explode(',', $category->contacts_data));
        $this->setContacts($this->generalData['contacts']);

        $this->render('view', array(
            'currentCategory' => $category,
            'currentAction' => $action,
            'content' => $action->i18n->content,
        ));

    }

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