<?php

class ServiceController 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->setContacts($data['contacts']);
        $this->setContacts($this->generalData['contacts']);

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

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

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

        $this->pageName = $currentCategory->i18n->page_name;
        $this->setSEOParams($currentCategory->i18n->title, $currentCategory->i18n->keywords, $currentCategory->i18n->description);
        $this->render('category', array(
            'currentCategory' => $currentCategory,
        ));
    }

    public function actionView($category, $link)
    {
        /** @var $currentCategory ServiceCategory */
        $currentCategory = ServiceCategory::model()->with('i18n')->findByAttributes(array(
            'link' => $category,
        ));
        if (empty($currentCategory))
            throw new CHttpException(404);
        $this->sectionGalleryId = $currentCategory->gallery_id;
        /** @var $currentService Service */
        $currentService = Service::model()->with('i18n')->findByAttributes(array(
            'link' => $link,
            'service_category_id' => $currentCategory->id,
        ));
        if (empty($currentService))
            throw new CHttpException(404);

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

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

        $this->render('view', array(
            'currentService' => $currentService,
            'currentCategory' => $currentCategory,
        ));
    }

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

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

        $this->pageName = $currentCenter->i18n->page_name;
        $this->setSEOParams($currentCenter->i18n->title, $currentCenter->i18n->keywords, $currentCenter->i18n->description);
        $this->render('center', array(
            'currentCenter' => $currentCenter,
        ));
    }

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

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