<?php

class NewsController extends Controller {

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

    public function accessRules() {
        return array(
            array('allow',
                'actions' => array('index', 'view'),
                'users' => array('@'),
            ),
            array('deny',
                'users' => array('*'),
            //'deniedCallback' => $this->redirect('?r=site/index');
            )
        );
    }

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

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

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

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

    public function actionView($id) {
        $model = News::findNews($id);
        $author_name = Users::model()->findByPk($model->author, array('select' => 'login'));
        $this->render('view', array(
            'model' => $model,
            'author' => $author_name->login,
        ));
    }
    
    

}
