<?php

class PartnersSectionController extends NodeSectionController
{
    /** @var AboutRoot */
    public $_aboutRoot;
    /** @var PartnersSection */
    public $section;

    public function init()
    {
        parent::init();
        /** @var $section PartnersSection */
        $section = PartnersSection::model()->with('i18n')->findByPk($this->getNode()->data_id);
        $this->headerGalleryId = $section->header_gallery_id;
        $this->section = $section;
    }

    public function actionPartners()
    {
        $section = $this->section;

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

        $this->setContacts(explode(',', $section->contacts_data));
        $this->setContacts(explode(',', $this->getAboutRoot()->contacts_data));

        $partners = Partner::model()->with('i18n')->findAll(array(
            'order' => 'rank asc',
            'condition' => 'not t.hidden and t.partners_section_id = ' . $this->getNode()->data_id,
        ));
        $this->render('partners', array(
            'content1' => $section->i18n->content1,
            'content2' => $section->i18n->content1,
            'partners' => $partners,
        ));
    }

    public function actionPartner($link)
    {
        /** @var $partner Partner */
        $partner = Partner::model()->with('i18n')->findByAttributes(array(
            'link' => $link,
        ));
        if (!isset($partner))
            throw new CHttpException(404);

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


        $this->setContacts(explode(',', $partner->contacts_data));
        $this->setContacts(explode(',', $this->getAboutRoot()->contacts_data));

        $partners = Partner::model()->with('i18n')->findAll(array(
            'order' => 'rank asc',
            'condition' => 'not t.hidden and t.partners_section_id = ' . $this->getNode()->data_id,
        ));
        $this->render('partner', array(
            'partner' => $partner,
            'partners' => $partners,
        ));
    }

    /**
     * @return AboutRoot|CActiveRecord
     */
    public function getAboutRoot()
    {
        if (!isset($this->_aboutRoot)) $this->_aboutRoot = AboutRoot::model()->findByPk($this->getRootNode()->data_id);
        return $this->_aboutRoot;
    }
}
