<?php

class NewsController extends NodeSiteController
{
    /** @var NewsRoot */
    public $newsRoot;

    public function init()
    {
        parent::init();
        $this->newsRoot = NewsRoot::model()->findByPk($this->getNode()->data_id);
        $this->headerGalleryId = $this->newsRoot->header_gallery_id;

    }

    public function actionIndex()
    {
        $criteria = new CDbCriteria(array(
            'order' => 'date desc',
            'condition' => 'not newsCategory.hidden and not t.hidden and newsCategory.news_root_id = ' . $this->getNode()->data_id
        ));
        $criteria->with = array('newsCategory');
        $pages = new CPagination(News::model()->count());
        $pages->pageSize = 10;
        $pages->applyLimit($criteria);
        $news = News::model()->with(array('i18n', 'newsCategory'))->findAll($criteria);

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

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

        $this->render('index', array(
            'news' => $news,
            'pages' => $pages,
        ));
    }

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

        $criteria = new CDbCriteria(array(
            'order' => 'date desc',
            'condition' => 'not t.hidden and t.news_category_id=' . $category->id
        ));
        $pages = new CPagination(News::model()->count($criteria));
        $pages->pageSize = 10;
        $pages->applyLimit($criteria);
        $news = News::model()->with(array('i18n', 'newsCategory'))->findAll($criteria);


        $this->setContacts(explode(',', $category->contacts_data));
        $this->setContacts(explode(',', $this->newsRoot->contacts_data));

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

    public function actionPublications($link)
    {
        /** @var $category NewsCategory */
        $category = NewsCategory::model()->findByAttributes(array(
            'link' => $link,
            'news_root_id' => $this->getNode()->data_id,
        ));
        if (empty($category)) throw new CHttpException(404);
        $this->sectionGalleryId = $category->gallery_id;

        $criteria = new CDbCriteria(array(
            'order' => 'date desc',
            'condition' => 'not t.hidden and t.news_category_id=' . $category->id
        ));
        $pages = new CPagination(Publication::model()->count($criteria));
        $pages->pageSize = 10;
        $pages->applyLimit($criteria);
        $publications = Publication::model()->with(array('i18n', 'newsCategory'))->findAll($criteria);


        $this->setContacts(explode(',', $category->contacts_data));
        $this->setContacts(explode(',', $this->newsRoot->contacts_data));

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

    public function actionPublication($link)
    {
        /** @var $publication News */
        $publication = Publication::model()->findByAttributes(array(
            'link' => $link,
        ));
        if (empty($publication))
            throw new CHttpException(404);
        $this->sectionGalleryId = $publication->newsCategory->gallery_id;

        $this->setContacts(explode(',', $publication->contacts_data));
        $this->setContacts(explode(',', $publication->newsCategory->contacts_data));
        $this->setContacts(explode(',', $this->newsRoot->contacts_data));

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

        $this->render('publication', array(
            'publication' => $publication,
            'category' => $publication->newsCategory,
        ));
    }

    public function actionView($link)
    {
        /** @var $news News */
        $news = News::model()->findByAttributes(array(
            'link' => $link,
        ));
        if (empty($news))
            throw new CHttpException(404);
        $this->sectionGalleryId = $news->newsCategory->gallery_id;

        $this->setContacts(explode(',', $news->contacts_data));
        $this->setContacts(explode(',', $news->newsCategory->contacts_data));
        $this->setContacts(explode(',', $this->newsRoot->contacts_data));

        $this->pageName = $news->i18n->page_name;
        $this->setSEOParams($news->i18n->title, $news->i18n->keywords, $news->i18n->description);
        $news->views += 1;
        $news->save(false);
        $this->render('view', array(
            'news' => $news,
            'category' => $news->newsCategory,
        ));
    }

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


    /** @return News */
    public function getPhotoFact()
    {
        return News::model()->with('i18n', 'newsCategory')->find(array('order' => 'rand()', 'condition' => 'newsCategory.type=3'));
    }

    /**
     * @param int $limit
     * @return News[]
     */
    public function getPopularNews($limit = 3)
    {
        return News::model()->with('i18n', 'newsCategory')->findAll(array(
            'order' => 'views desc',
            'condition' => 'not t.hidden and not newsCategory.hidden and (newsCategory.type=1 or newsCategory.type=3)', 'limit' => $limit));
    }

    public function actionShow($link)
    {
        /** @var $category NewsCategory */
        $category = NewsCategory::model()->findByAttributes(array(
            'link' => $link,
            'news_root_id' => $this->getNode()->data_id,
        ));
        if (empty($category)) throw new CHttpException(404);
        $this->sectionGalleryId = $category->gallery_id;

        $criteria = new CDbCriteria(array(
            'order' => 'date desc',
            'condition' => 'news_category_id=' . $category->id
        ));
        $pages = new CPagination(Show::model()->count($criteria));
        $pages->pageSize = 8;
        $pages->applyLimit($criteria);
        $videos = Show::model()->with(array('i18n', 'newsCategory'))->findAll($criteria);


        $this->setContacts(explode(',', $category->contacts_data));
        $this->setContacts(explode(',', $this->newsRoot->contacts_data));

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

}