CalendarSectionController.php 3.59 KB
<?php

class CalendarSectionController extends NodeSectionController
{
    public function actionCalendar($category = 'all', $country = 'all', $year = null)
    {

        /** @var $sportRoot SportRoot */
        $sportRoot = SportRoot::model()->with('i18n')->findByPk($this->getRootNode()->data_id);
        $this->headerGalleryId = $sportRoot->header_gallery_id;

        /** @var $calendarSection CalendarSection */
        $calendarSection = CalendarSection::model()->with('i18n')->findByPk($this->getNode()->data_id);

        $this->sectionGalleryId = $calendarSection->header_gallery_id;

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

        /** @var $categories SportEventCategory[] */
        $categories = SportEventCategory::model()->with('i18n')->findAll(
            array('order' => 'rank asc',
                'condition' => 'calendar_section_id = ' . $this->getNode()->data_id));
        if (count($categories) == 1 && $category == 'all') $category = $categories[0]->id;
        $categoryIds = array();
        foreach ($categories as $item) $categoryIds[] = $item->id;

        $year = isset($year) ? $year : $calendarSection->year;
        //$years = array(2011, 2012, 2013);

        $cmd = Yii::app()->db->createCommand()
            ->selectDistinct('YEAR(start_date)')
            ->from(SportEvent::model()->tableName());
        if ($category != 'all') {
            $cmd->where('sport_event_category_id = :category', array(':category' => $category));
        } else {
            $cmd->where(array('in', 'sport_event_category_id', $categoryIds));
        }
        $years1 = $cmd->queryColumn();
        $cmd = Yii::app()->db->createCommand()
            ->selectDistinct('YEAR(end_date)')
            ->from(SportEvent::model()->tableName());
        if ($category != 'all') {
            $cmd->where('sport_event_category_id = :category', array(':category' => $category));
        } else {
            $cmd->where(array('in', 'sport_event_category_id', $categoryIds));
        }
        $years2 = $cmd->queryColumn();

        $t = array((string)(date('Y') - 1), date('Y'), (string)(date('Y') + 1));
        $years = array_intersect($t, array_unique(array_merge($years1, $years2)));


        $criteria = new CDbCriteria(array(
            'index' => 'id',
            'condition' => 'sportEventCategory.calendar_section_id = ' . $calendarSection->id,
        ));
        $criteria->with = array('sportEventCategory');
        $criteria->addCondition('start_date <= :year_end');
        $criteria->addCondition('end_date >= :year_begin');
        $criteria->params[':year_begin'] = date($year . '-01-01');
        $criteria->params[':year_end'] = date('Y-m-d', mktime(0, 0, 1, 1, 0, (int)($year) + 1));
        if ($category != 'all') {
            $criteria->addCondition('sport_event_category_id = :category');
            $criteria->params[':category'] = $category;
        }
        if ($country != 'all') {
            $criteria->addCondition('country_id = :country');
            $criteria->params[':country'] = $country;
        }
        $events = SportEvent::model()->with('i18n')->findAll($criteria);

        $this->render('calendar', array(
            'links_block' => $calendarSection->i18n->links_block,
            'content' => $calendarSection->i18n->content,
            'events' => $events,
            'categories' => $categories,
            'category' => $category,
            'country' => $country,
            'year' => $year,
            'years' => $years,
        ));
    }

}