<?php

class RevisarDocController extends Controller
{
	public $layout='//layouts/column2';

	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',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	public function actionCreate()
	{
		$model=new RevisarDoc;

		if(isset($_POST['RevisarDoc']))
		{
			$model->attributes=$_POST['RevisarDoc'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->revisar_codigo));
		}

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

	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
		$model_doc = Documento::model()->findByAttributes(array('revisar_codigo'=>$id));
		$model_obs=new Observacion;

		if(isset($_POST['Documento']))
		{
			$model_doc->attributes=$_POST['Documento'];
			$model_obs->attributes=$_POST['Observacion'];

			$model->revisar_fecha= new CDbExpression('NOW()');
			$model->revisar_estado='Aprobado';
			$model_obs->obs_fecha= new CDbExpression('NOW()');
			$model_obs->obs_ruta=yii::app()->session['file_ruta'];	
			
			if($model_doc->save()){
				if(isset($_POST['Observacion'])){
				$model_obs->doc_codigo=$model_doc->doc_codigo;
				$model_obs->revisar_codigo=$model->revisar_codigo;
				$model_obs->save();
				}
				if($model->save()) 
				$this->redirect(array('documento/admin'));
			}			
		}

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

	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('RevisarDoc');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	public function actionAdmin()
	{
		$model=new RevisarDoc('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['RevisarDoc']))
			$model->attributes=$_GET['RevisarDoc'];

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

	public function loadModel($id)
	{
		$model=RevisarDoc::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']==='revisar-doc-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
