<?php

class CargoController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}


		public function accessRules()
	{
		return array(
			array('allow',  
				'actions'=>array('index','view','create','update','delete'),
				'expression' => array('CargoController', 'iniciarseccion'),
			),
			array('deny',
				'users'=>array('*'),
			),
		);
	}

	
       public static function iniciarseccion() {
            if (!Yii::app()->user->isGuest) {
                    return true;
            } else {
                Yii::app()->user->setFlash('danger', Yii::app()->params['sesion']);
                return false;
            }
        }
        
        
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	public function actionCreate()
	{       $this->pageTitle = 'REGISTRAR CARGO';
		$model=new Cargo;
                $model->setScenario('create');
	        $this->performAjaxValidation($model);

		if(isset($_POST['Cargo']))
		{
			$model->attributes=$_POST['Cargo'];
                        
                          if($model->validate()){    
                            try
                            {    
                              $agregar= $model->add();
                              if ($agregar) {
                              $model->unsetAttributes();     
                              Yii::app()->user->setFlash('success', Yii::app()->params['add']);
                              $this->redirect(array('index'));  
                              }
                            }catch(Exception $e)
                            {
                             Yii::app()->user->setFlash('danger', Yii::app()->params['error']); 
                            }  

                            }
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

        
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
                $model->setScenario('update');
         
                $this->performAjaxValidation($model);

		if(isset($_POST['Cargo']))
		{
			$model->attributes=$_POST['Cargo'];
                        if($model->validate()){    
                        try
                        {

                          $editar=$model->edit($id);  

                    
                          Yii::app()->user->setFlash('success', Yii::app()->params['edit']);
                          $this->redirect(array('index'));  
                      

                        }catch(Exception $e)
                        {
                         Yii::app()->user->setFlash('danger', Yii::app()->params['error']); 
                        }  

                        }
                }

		$this->render('update',array(
			'model'=>$model,
		));
	}

	
	public function actionDelete($id)
	{
		$model= new Cargo();
                $model->borrar($id);
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
	}


	public function actionIndex()
	{
            
                $this->pageTitle = 'CARGOS';
            
                if (isset($_GET['pageSize'])) {
                    Yii::app()->user->setState('pageSize', (int) $_GET['pageSize']);
                    unset($_GET['pageSize']);
                }
            
		$model=new Cargo('search');
		$model->unsetAttributes();
		if(isset($_GET['Cargo']))
			$model->attributes=$_GET['Cargo'];

		$this->render('index',array(
			'model'=>$model,
		));
	}
        
	public function loadModel($id)
	{
		$model=Cargo::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}


	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='cargo-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
