<?php

class DefaultController extends AdminController
{
    public function filters()
    {
        return array(
            'accessControl',
        );
    }

    public function accessRules()
    {
        return array(
            array('allow',
                'actions' => array('error', 'login'),
                'users' => array('*'),
            ),
            array('allow',
                'actions' => array('index', 'logout', 'changePassword', 'doIt'),
                'users' => array('@'),
            ),
            array('deny',
                'users' => array('*'),
            ),
        );
    }

    public $defaultAction = 'index';

    public function actionIndex()
    {
        // $this->layout = 'base';
        // $this->render('index');
//        $this->redirect(array('/admin/sitePages/index'));
        $this->redirect(array('/admin/node/index'));
    }

    public function actionChangePassword()
    {

        $model = new ChangePasswordForm();
        if (isset($_POST['ChangePasswordForm'])) {
            $model->setAttributes($_POST['ChangePasswordForm']);
            if ($model->validate() && $model->changePassword()) {
                $this->redirect(array('default/index'));
            }
        }
        $this->layout = 'base';
        $this->render('changePassword', array('model' => $model));
    }

    public function actionError()
    {
        $this->layout = 'error';
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest) {
                echo $error['message'];
            } else {
                $this->render('error', $error);
            }
        }
    }


    public function actionLogin()
    {
        /**
         * @var LoginForm $model
         */
        $model = new LoginForm();

        // collect user input data
        if (isset($_POST['LoginForm'])) {
            $model->setAttributes($_POST['LoginForm']);
            if ($model->validate() && $model->login())
                $this->redirect(Yii::app()->user->returnUrl);
        }
        $this->layout = 'main';
        $this->render('login', array('model' => $model));
    }

    public function actionLogout()
    {
        Yii::app()->user->logout();
        $this->redirect($this->createUrl('default/index'));
    }

    /** @param $data StaticData|StaticDataI18n|StaticPage|StaticPageI18n */
    private function fixData(&$data)
    {
        $t = $data->getDataAttributes();
        foreach ($t as $k => $v) {
            if (is_string($v)) {
                $t[$k] = strtr($v, array(
                    'tuning@auto-life.ua' => 'bosch@auto-life.com.ua',
                    'feedback@auto-life.ua' => 'bosch@auto-life.com.ua',
                    'work@auto-life.ua' => 'bosch@auto-life.com.ua',
                    //'work@auto-life.ua'=>'bosch@auto-life.com.ua',
                    //'http://auto-life.claret-art.com.ua/'=>'/',
                ));
            }
        }
        $data->setDataAttributes($t);
    }

    public function fixEmails()
    {
        /** @var $datas StaticData[] */
        $datas = StaticData::model()->with('i18n')->findAll();
        foreach ($datas as $data) {
            $this->fixData($data);
            foreach ($data->i18ns as $q) {
                $this->fixData($q);
                $q->save();
            }

            $data->save();
        }
        /** @var $pages StaticPage */
        $pages = StaticPage::model()->with('i18n')->findAll();
        foreach ($pages as $data) {
            $this->fixData($data);
            foreach ($data->i18ns as $q) {
                $this->fixData($q);
                $q->save();
            }

            $data->save();
        }
    }

    public function DoIt1()
    {
        $clss = array('Action', 'ActionCategory', 'Job', 'News', 'NewsCategory', 'Partner', 'Publication', 'QuestionCategory', 'Service', 'ServiceCenter', 'TuningBrand', 'TuningModel', 'TuningService', 'TuningSet', 'Vehicle', 'Work', 'WorkBrand', 'WorkModel');
        foreach ($clss as $cls) {
            /** @var $items News[] */
            $items = CActiveRecord::model($cls)->findAll();
            foreach ($items as $item) {
                $link = $item->link;
                $cnt = 1;
                foreach ($items as $q) {
                    if ($item->id != $q->id && $q->link == $link) {
                        $q->link = $link . '-' . $cnt;
                        echo "{$cls}, {$q->id}, {$link}, {$link}-{$cnt}\n";
                        $q->save();
                        $cnt++;
                    }
                }
            }
        }
    }


    public function actionDoIt()
    {
        // foreach (array('Work', 'TuningSet', 'News', 'Partner', 'Vehicle',) as $cls) {
        //     /** @var $work Work */
        //     foreach (CActiveRecord::model($cls)->findAll() as $work) {
        //         $work->galleryBehavior->changeConfig(false, true);
        //
        //     }
        // }
//        set_time_limit(12345678);
//        $links = array(
//            '20',
//            '19',
//            '18',
//            '17',
//            '16',
//            '15',
//        );
//        $criteria = new CDbCriteria();
//        $criteria->addInCondition('link', $links);
//        /** @var $albums Album[] */
//        $albums = Album::model()->findAll($criteria);
//        foreach ($albums as $album) {
//            $album->save();
//            $album->photoGalleryBehavior->changeConfig(true);
//
//        }

//        /** @var $n News */
//        $n = News::model()->find('link = :link',array(':link'=>'VtoroyEtapFoto'));
//        $n->save();
//        $n->galleryBehavior->changeConfig(true);

    }

}