<?php

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

	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', 'Download'),
				'users'=>array('*'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete','llamarObs'),
				'users'=>array('*'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	function actionDownload($obs){
	    $filecontent=file_get_contents('http://172.17.123.45/gestor_documental/tesis/'.$obs);
		  header("Content-Type: text/plain");
		  header("Content-disposition: attachment; filename=$obs");
		  header("Pragma: no-cache");
		  echo $filecontent;
		  exit;
	}

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

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

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

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

	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

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

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

	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();
    	if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

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

	public function actionllamarObs($obs_id){
		$this->id_observacion=$obs_id;
		$this->actionAdmin();
	}

	public function actionAdmin()
	{
		$model=new Observacion('search');
		$model->unsetAttributes();  // clear any default values
		$model->doc_codigo=$this->id_observacion; //necesario para trabajar con clave foranea de avance
		if(isset($_GET['Observacion']))
			$model->attributes=$_GET['Observacion'];

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

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