<?php

class ConfAporteVoluntarioController 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',
            array('CrugeAccessControlFilter')
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions' => array('index', 'view', 'create', 'update'),
				'users' => array('@'),
			),
			array('deny',  // deny all users
				'users' => array('*'),
			),
		);
	}

    public function actionIndex()
    {
        $model = new ConfAporteVoluntario('search');

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

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

        $model->porcentaje_retiro_adm = number_format($model->porcentaje_retiro_adm, 2,',','.');
        $model->porcentaje_retiro_asc = number_format($model->porcentaje_retiro_asc, 2,',','.');

        $modelEstatus = EstatusAporte::model()->findAll('blnborrado= false');

        // $this->performAjaxValidation($model);

        if (isset($_POST['ConfAporteVoluntario'])) {
            $model->attributes = $_POST['ConfAporteVoluntario'];
            if ($model->save()) {
                $this->redirect(['index']);
            }
        }

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

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