<?php

class AdminController extends Controller {

    public function filters() {
        return array(
            'accessControl',
        );
    }

    public function accessRules() {
        return array(
            array('allow',
                'actions' => array('index', 'create_news', 'news_list', 'del_news', 'edit_news', 'del_message', 'edituser', 'searchuser'),
                'roles' => array('administrator'),
            ),
            array('deny',
                'users' => array('*'),
            //'deniedCallback' => $this->redirect('?r=main/index'),
            )
        );
    }

    public function actions() {
        return array(
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    public function actionIndex() {
        $this->render('index');
    }

    public function actionCreate_news() {
        $form = new News();

        if (isset($_POST['News'])) {
            $form->attributes = $_POST['News'];
            if ($form->validate('/admin/news_form')) {
                $form->time = time();
                $form->text = htmlspecialchars($_POST['News']['text']);
                $author_id = Users::model()->findByAttributes(array('login' => $_POST['News']['author']));
                $form->author = $author_id->id;
                $form->save();
                Yii::app()->user->setFlash('news_added', "Новость успешно добавлена");
                //$this->refresh();
            }
        }

        $this->render('news_form', array(
            'form' => $form,
        ));
    }

    public function actionNews_list() {
        $criteria = new CDbCriteria();
        $count = News::model()->count($criteria);
        $pages = new CPagination($count);
        // элементов на страницу
        $pages->pageSize = 100;
        $pages->applyLimit($criteria);
        $criteria->order = 'id DESC';

        $models = News::model()->findAll($criteria);

        $this->render('news_list', array(
            'models' => $models,
        ));
    }

    public function actionDel_news($id) {
        $model = News::findNews($id);
        $model->deleteByPk($id);

        Yii::app()->user->setFlash('news_del', "Новость " . $model->title . " была успешно удалена!");

        $this->redirect(array('admin/news_list'));
    }

    public function actionEdit_news($id) {
        $form = new News();
        $model = News::findNews($id);
        if (isset($_POST['News'])) {
            $form->attributes = $_POST['News'];
            if ($form->validate('/admin/news_form')) {
                $form->time = time();
                $author_id = Users::model()->findByAttributes(array('login' => $_POST['News']['author']));
                $form->author = $author_id->id;
                if (!empty($id)) {
                    News::model()->updateByPk($id, array('title' => $form->title, 'text' => $form->text, 'author' => $form->author));
                    Yii::app()->user->setFlash('news_added', "Новость успешно изменена");
                } else {
                    $form->author = $author_id->id;
                    $form->save();
                    Yii::app()->user->setFlash('news_added', "Новость успешно добавлена");
                    //$this->refresh();
                }
            }
        }

        $this->render('news_form', array(
            'form' => $form,
            'model' => $model,
        ));
    }
    
    public function actionEdituser($id = NULL) {
        $model = Users::findUser();
        if (isset($_POST['Users'])) {
        $model->attributes = $_POST['Users'];
        $model->save(false);
        Yii::app()->user->setFlash('success', 'Данные успешно изменены');
        $this->redirect(array('admin/edituser', 'id' => $id));
        }
        $this->render('edituser', array(
           'model' => $model,
        ));
    }
    
    public function actionSearchuser() {
        $model = new Users;
         if (isset($_POST['Users']['login'])) {
         $user = Users::model()->findByAttributes(array('login' => $_POST['Users']['login']));
         if ($user) {
             
         } else {
             Yii::app()->user->setFlash('error', 'Пользователь не найден');
             $this->redirect(array('admin/searchuser'));
         }
         }
        $this->render('searchuser', array(
                'model' => $model,
            'user' => $user,
                ));
    }

}
