<?php

class JobsSectionController extends NodeSectionController
{
    /** @var AboutRoot */
    public $_aboutRoot;
    /** @var JobsSection */
    public $section;

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


    public function actionJobs()
    {
        $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));


        $jobs = Job::model()->with('i18n')->findAll('not hidden and jobs_section_id = ' . $this->getNode()->data_id);
        $this->render('jobs', array(
            'content1' => $section->i18n->content1,
            'content2' => $section->i18n->content1,
            'jobs' => $jobs,
        ));
    }

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

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

        $this->setContacts(explode(',', $job->contacts_data));
        $this->setContacts(explode(',', $this->aboutRoot->contacts_data));

        $jobs = Job::model()->with('i18n')->findAll('jobs_section_id = ' . $this->getNode()->data_id);
        $this->render('job', array(
            'job' => $job,
            'jobs' => $jobs,
        ));
    }


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