<?php

class SiteController extends Controller
{
    /**
     * Declares class-based actions.
     */
    public function actions()
    {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex()
    {
        $data = $this->loadStaticPage($this->id, $this->action->id);
        $slides = GalleryPhoto::model()->with('i18n')->findAll(array('order' => 'rank asc', 'condition' => 'gallery_id=' . $data['gallery_id']));
//        CVarDumper::dump($data,10,true); exit(0);
        $this->render('index', array('about' => $data['about'], 'slides' => $slides));
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError()
    {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    /**
     * Displays the contact page
     */
    public function actionContact()
    {
        $data = $this->loadStaticPage($this->id, $this->action->id);
        $this->headerGalleryId = $this->siteData['contact_gallery_id'];
        $this->render('contact', array(
            'map' => $data['map'],
            'content' => $data['content'],
        ));
    }


    /**
     * @param int $limit
     * @return News[]
     */
    public function getLatestNews($limit = 3)
    {
        return News::model()->with('i18n')->findAll(array(
            'order' => 'date desc',
            'condition'=>'hidden=0',
            'limit' => $limit,
        ));
    }

    public function actionSiteMap()
    {
        //todo: pagination
        $langs = array('ru','en'); //Yii::app()->params['languages'];
        $writer = new SitemapWriter();
        $staticPages = array(
            'site/index',
            'site/contact',
            'site/warranty',
            'site/reviews',


            'action/index',
            //'action/category' link,
            'action/finished',
            //'action/view' category link,

            'news/index',
            //'news/category', link
            //'news/publications', link
            //'news/publication', link
            //'news/view', link
            //'news/show', link

            'question/index',

            'sale/index',
            //'sale/view' link,

            'service/index',
            //'service/category' category,
            //'service/view' category view,
            //'service/center' center,

            'sport/index',
            'sport/awards',
            'sport/sportCar',
            'sport/team',
            'sport/qualification',
            'sport/study',
            'sport/calendar',
            'sport/albums',
            //'sport/album' link,

            'tuning/index',
            //'tuning/brand' brand,
            //'tuning/model' brand model,
            //'tuning/view' brand model set,
            //'tuning/service' link,

            'work/index',
            //'work/brand' brand,
            //'work/model' brand model,
            //'work/view' brand model set,


        );

        // add all static pages
        foreach ($staticPages as $r) {
            foreach ($langs as $lang) {
                $writer->addPage(Yii::app()->createAbsoluteUrl($r, array('lang' => $lang)));
            }
        }

        /** @var $rootNode Node */
        foreach (Node::model()->findAll('node_id is null') as $rootNode) {
            switch ($rootNode->data_type) {
                case 'aboutRoot':
                    foreach ($langs as $lang) {
                        $writer->addPage(Yii::app()->createAbsoluteUrl('about/index', array('lang' => $lang, 'node_id' => $rootNode->id)));
                    }
                    foreach ($rootNode->nodes as $sectionNode) {
                        switch ($sectionNode->data_type) {
                            case 'jobsSection':
                                foreach ($langs as $lang) {
                                    $writer->addPage(Yii::app()->createAbsoluteUrl('jobsSection/jobs', array('lang' => $lang, 'node_id' => $sectionNode->id)));
                                }
                                foreach (Job::model()->findAll('jobs_section_id =' . $sectionNode->id) as $i) {
                                    foreach ($langs as $lang) {
                                        $writer->addPage(Yii::app()->createAbsoluteUrl('jobsSection/job', array('lang' => $lang, 'link' => $i->link, 'node_id' => $sectionNode->id)));
                                    }
                                }
                                break;
                            case 'partnersSection':
                                foreach ($langs as $lang) {
                                    $writer->addPage(Yii::app()->createAbsoluteUrl('partnersSection/partners', array('lang' => $lang, 'node_id' => $sectionNode->id)));
                                }
                                foreach (Partner::model()->findAll('partners_section_id =' . $sectionNode->id) as $i) {
                                    foreach ($langs as $lang) {
                                        $writer->addPage(Yii::app()->createAbsoluteUrl('partnersSection/partner', array('lang' => $lang, 'link' => $i->link, 'node_id' => $sectionNode->id)));
                                    }
                                }
                                break;
                            case 'aboutSection':
                                foreach ($langs as $lang) {
                                    $writer->addPage(Yii::app()->createAbsoluteUrl('aboutSection/index', array('lang' => $lang, 'node_id' => $sectionNode->id)));
                                }
                                foreach ($sectionNode->nodes as $pageNode) {
                                    foreach ($langs as $lang) {
                                        $writer->addPage(Yii::app()->createAbsoluteUrl('aboutPage/index', array('lang' => $lang, 'node_id' => $pageNode->id)));
                                    }
                                }
                                break;
                        }
                    }
                    break;
                case 'textRoot':
                    foreach ($langs as $lang) {
                        $writer->addPage(Yii::app()->createAbsoluteUrl('textRoot/index', array('lang' => $lang, 'node_id' => $rootNode->id)));
                    }
                    foreach ($rootNode->nodes as $sectionNode) {
                        foreach ($langs as $lang) {
                            $writer->addPage(Yii::app()->createAbsoluteUrl('textSection/index', array('lang' => $lang, 'node_id' => $sectionNode->id)));
                        }
                        foreach ($sectionNode->nodes as $pageNode) {
                            foreach ($langs as $lang) {
                                $writer->addPage(Yii::app()->createAbsoluteUrl('textSection/index', array('lang' => $lang, 'node_id' => $pageNode->id)));
                            }
                        }

                    }
                    break;
                case 'newsRoot':
                    foreach ($langs as $lang) {
                        $writer->addPage(Yii::app()->createAbsoluteUrl('news/index', array('lang' => $lang, 'node_id' => $rootNode->id)));
                    }
                    /** @var $newsCategory NewsCategory */
                    foreach (NewsCategory::model()->findAll() as $newsCategory) {
                        if ($newsCategory->type == 1 || $newsCategory->type == 3) {
                            // 1 = 'Новости',
                            //'news/category', link
                            foreach ($langs as $lang) {
                                $writer->addPage(Yii::app()->createAbsoluteUrl('news/category', array('lang' => $lang, 'link' => $newsCategory->link, 'node_id' => $rootNode->id)));
                            }
                            //'news/view', link
                            foreach ($newsCategory->news as $news) {
                                foreach ($langs as $lang) {
                                    $writer->addPage(Yii::app()->createAbsoluteUrl('news/view', array('lang' => $lang, 'link' => $news->link, 'node_id' => $rootNode->id)));
                                }
                            }
                        }
                        if ($newsCategory->type == 2) {
                            // 'Публикации',
                            //'news/publications', link
                            foreach ($langs as $lang) {
                                $writer->addPage(Yii::app()->createAbsoluteUrl('news/publications', array('lang' => $lang, 'link' => $newsCategory->link, 'node_id' => $rootNode->id)));
                            }
                            //'news/publication', link
                            foreach ($newsCategory->news as $news) {
                                foreach ($langs as $lang) {
                                    $writer->addPage(Yii::app()->createAbsoluteUrl('news/publication', array('lang' => $lang, 'link' => $news->link, 'node_id' => $rootNode->id)));
                                }
                            }
                        }
                        if ($newsCategory->type == 4) {
                            // 'Нас показывают',
                            //'news/show', link
                            foreach ($langs as $lang) {
                                $writer->addPage(Yii::app()->createAbsoluteUrl('news/show', array('lang' => $lang, 'link' => $newsCategory->link, 'node_id' => $rootNode->id)));
                            }
                        }
                    }
                    break;
                case 'sportRoot':
                    foreach ($langs as $lang) {
                        $writer->addPage(Yii::app()->createAbsoluteUrl('sport/index', array('lang' => $lang, 'node_id' => $rootNode->id)));
                    }
                    foreach ($rootNode->nodes as $sectionNode) {
                        switch ($sectionNode->data_type) {
                            case 'albumsSection':
                                foreach ($langs as $lang) {
                                    $writer->addPage(Yii::app()->createAbsoluteUrl('albumsSection/albums', array('lang' => $lang, 'node_id' => $sectionNode->id)));
                                }
                                foreach (Album::model()->findAll('albums_section_id =' . $sectionNode->id) as $i) {
                                    foreach ($langs as $lang) {
                                        $writer->addPage(Yii::app()->createAbsoluteUrl('albumsSection/album', array('lang' => $lang, 'link' => $i->link, 'node_id' => $sectionNode->id)));
                                    }
                                }
                                break;
                            case 'calendarSection':
                                foreach ($langs as $lang) {
                                    $writer->addPage(Yii::app()->createAbsoluteUrl('calendarSection/calendar', array('lang' => $lang, 'node_id' => $sectionNode->id)));
                                }
                                break;
                            case 'sportSection':
                                foreach ($langs as $lang) {
                                    $writer->addPage(Yii::app()->createAbsoluteUrl('sportSection/index', array('lang' => $lang, 'node_id' => $sectionNode->id)));
                                }
                                foreach ($sectionNode->nodes as $pageNode) {
                                    foreach ($langs as $lang) {
                                        $writer->addPage(Yii::app()->createAbsoluteUrl('sportPage/index', array('lang' => $lang, 'node_id' => $pageNode->id)));
                                    }
                                }
                                break;
                        }
                    }
                    break;
                case 'saleRoot':
                    foreach ($langs as $lang) {
                        $writer->addPage(Yii::app()->createAbsoluteUrl('sale/index', array('lang' => $lang, 'node_id' => $rootNode->id)));
                    }
                    foreach (Vehicle::model()->findAll('sale_root_id =' . $rootNode->id) as $i) {
                        foreach ($langs as $lang) {
                            $writer->addPage(Yii::app()->createAbsoluteUrl('sale/view', array('lang' => $lang, 'link' => $i->link, 'node_id' => $rootNode->id)));
                        }
                    }
                    break;
                case 'tuningRoot':
                    foreach ($langs as $lang) {
                        $writer->addPage(Yii::app()->createAbsoluteUrl('tuning/index', array('lang' => $lang, 'node_id' => $rootNode->id)));
                    }
                    //'tuning/service' link,
                    foreach (TuningService::model()->findAll('tuning_root_id = ' . $rootNode->id) as $i) {
                        foreach ($langs as $lang) {
                            $writer->addPage(Yii::app()->createAbsoluteUrl('tuning/service', array('lang' => $lang, 'link' => $i->link, 'node_id' => $rootNode->id)));
                        }
                    }


                    //'tuning/brand' brand,
                    /** @var $tuningBrand TuningBrand */
                    foreach (TuningBrand::model()->findAll('tuning_root_id = ' . $rootNode->id) as $tuningBrand) {
                        foreach ($langs as $lang) {
                            $writer->addPage(Yii::app()->createAbsoluteUrl('tuning/brand', array('lang' => $lang, 'brand' => $tuningBrand->link, 'node_id' => $rootNode->id)));
                        }
                        //'tuning/model' brand model,
                        foreach ($tuningBrand->tuningModels as $tuningModel) {
                            foreach ($langs as $lang) {
                                $writer->addPage(Yii::app()->createAbsoluteUrl('tuning/model', array('lang' => $lang, 'brand' => $tuningBrand->link, 'model' => $tuningModel->link, 'node_id' => $rootNode->id)));
                            }

                            //'tuning/view' brand model set,
                            foreach ($tuningModel->tuningSets as $tuningSet) {
                                foreach ($langs as $lang) {
                                    $writer->addPage(Yii::app()->createAbsoluteUrl('tuning/view', array('lang' => $lang, 'brand' => $tuningBrand->link, 'model' => $tuningModel->link, 'set' => $tuningSet->link, 'node_id' => $rootNode->id)));
                                }
                            }
                        }
                    }
                    break;
                case 'workRoot':
                    foreach ($langs as $lang) {
                        $writer->addPage(Yii::app()->createAbsoluteUrl('work/index', array('lang' => $lang, 'node_id' => $rootNode->id)));
                    }
                    //'work/brand' brand,
                    /** @var $workBrand WorkBrand */
                    foreach (WorkBrand::model()->findAll('work_root_id = ' . $rootNode->id) as $workBrand) {
                        foreach ($langs as $lang) {
                            $writer->addPage(Yii::app()->createAbsoluteUrl('work/brand', array('lang' => $lang, 'brand' => $workBrand->link, 'node_id' => $rootNode->id)));
                        }

                        //'work/model' brand model,
                        foreach ($workBrand->workModels as $workModel) {
                            foreach ($langs as $lang) {
                                $writer->addPage(Yii::app()->createAbsoluteUrl('work/model', array('lang' => $lang, 'brand' => $workBrand->link, 'model' => $workModel->link, 'node_id' => $rootNode->id)));
                            }

                            //'work/view' brand model set,
                            foreach ($workModel->works as $work) {
                                foreach ($langs as $lang) {
                                    $writer->addPage(Yii::app()->createAbsoluteUrl('work/view', array('lang' => $lang, 'brand' => $workBrand->link, 'model' => $workModel->link, 'set' => $work->link, 'node_id' => $rootNode->id)));
                                }
                            }
                        }
                    }
                    break;
            }


        }

        //'action/category' link,

        foreach (ActionCategory::model()->findAll() as $i) {
            foreach ($langs as $lang) {
                $writer->addPage(Yii::app()->createAbsoluteUrl('action/category', array('lang' => $lang, 'link' => $i->link)));
            }
            //'action/view' category link,
            foreach ($i->actions as $j) {
                foreach ($langs as $lang) {
                    $writer->addPage(Yii::app()->createAbsoluteUrl('action/view', array('lang' => $lang, 'category' => $i->link, 'link' => $j->link)));
                }
            }
        }


        //'service/category' category,
        /** @var $serviceCategory ServiceCategory */
        foreach (ServiceCategory::model()->findAll() as $serviceCategory) {
            foreach ($langs as $lang) {
                $writer->addPage(Yii::app()->createAbsoluteUrl('service/category', array('lang' => $lang, 'category' => $serviceCategory->link)));
            }
            //'service/view' category link,
            foreach ($serviceCategory->services as $service) {
                foreach ($langs as $lang) {
                    $writer->addPage(Yii::app()->createAbsoluteUrl('service/view', array('lang' => $lang, 'category' => $serviceCategory->link, 'link' => $service->link)));
                }
            }
        }


        //'service/center' center,
        foreach (ServiceCenter::model()->findAll() as $i) {
            foreach ($langs as $lang) {
                $writer->addPage(Yii::app()->createAbsoluteUrl('service/center', array('lang' => $lang, 'link' => $i->link)));
            }
        }

        header('Content-Type:application/xml');
        $writer->generateXml();
    }

    public function actionFeed()
    {
        Yii::import('ext.feed.*');
        $feed = new EFeed();

        $feed->setTitle('Auto-Life RSS');

        $feed->addChannelTag('language', Yii::app()->language);
        $feed->addChannelTag('pubDate', date(DATE_RSS, time()));
        $feed->addChannelTag('link', $this->createAbsoluteUrl('site/index'));

        $criteria = new CDbCriteria();
        $criteria->order = '`date` DESC';
        /** @var $news News[] */
        $news = News::model()->with('i18n', 'newsCategory')->findAll($criteria);
        foreach ($news as $i) {
            $item = $feed->createNewItem();
            $item->setTitle($i->i18n->name);
            $url = Yii::app()->createAbsoluteUrl('news/view', array('link' => $i->link, 'node_id' => $i->newsCategory->news_root_id));
            $item->setLink($url);
            $item->setDate(CDateTimeParser::parse($i->getDateRu(), 'dd.MM.yyyy'));
            $item->setDescription($i->i18n->short);
            $item->addTag('guid', $url, array('isPermaLink' => 'true'));
            $feed->addItem($item);
        }
        $feed->generateFeed();
        Yii::app()->end();
    }


    public function actionNearestNews($country = 'all')
    {
        Yii::import('application.controllers.SportController');
        foreach (SportController::getNearestEvents($country) as $event) {
            echo
            '<div class="calander-small-item"><p class="date">',
            Yii::app()->getDateFormatter()->format('d MMMM yyyy', CDateTimeParser::parse($event->start_date, 'yyyy-MM-dd')),
            ' - ',
            Yii::app()->getDateFormatter()->format('d MMMM yyyy', CDateTimeParser::parse($event->end_date, 'yyyy-MM-dd')),
            '</p>',
            '<p>', $event->i18n->description, '</p></div>';
        }
    }
}