<?php

class ConceptosOrdenPagoController extends Controller
{
    public function filters()
    {
        return [
            'accessControl',
            [
                'CrugeAccessControlFilter',
            ],
        ];
    }

    public function accessRules()
    {
        return [
            [
                'allow',
                'actions' => ['index', 'view'],
                'users' => ['*'],
            ],
            [
                'allow',
                'actions' => ['create', 'update', 'admin', 'delete'],
                'users' => ['@'],
            ],
            [
                'deny',
                'users' => ['*'],
            ],
        ];
    }

    public function actionAdmin()
    {
        $model = new ConceptosOrdenPago('search');
        $model->unsetAttributes();

        if (isset($_GET['ConceptosOrdenPago'])) {
            $model->setAttributes($_GET['ConceptosOrdenPago']);
        }

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

    public function actionCreate()
    {
        $model = new ConceptosOrdenPago();
        $titulo_proceso_pago = TituloProcesoPago::getIf([6, 8]);

        if (isset($_POST['ConceptosOrdenPago'])) {
            $model->setAttributes($_POST['ConceptosOrdenPago']);

            if ($model->save()) {
                $this->redirect(['admin']);
            }
        }

        $this->render('create', [
            'model' => $model,
            'titulo_proceso_pago' => $titulo_proceso_pago,
        ]);
    }

    public function actionUpdate($id)
    {
        $model = $this->loadModel($id);
        $titulo_proceso_pago = TituloProcesoPago::getIf([6, 8], $model->id_titulo_proceso_pago);

        if (isset($_POST['ConceptosOrdenPago'])) {
            $model->setAttributes($_POST['ConceptosOrdenPago']);

            if ($model->save()) {
                $this->redirect(['admin']);
            }
        }

        $this->render('update', [
            'model' => $model,
            'titulo_proceso_pago' => $titulo_proceso_pago,
        ]);
    }

    public function actionDelete($id)
    {
        if (Yii::app()->request->isPostRequest) {
            $this->loadModel($id)->delete();

            if (! isset($_GET['ajax'])) {
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : ['admin']);
            }
        } else {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    private function loadModel($id)
    {
        $model = ConceptosOrdenPago::model()->findByPk($id);
        if ($model === null) {
            throw new CHttpException(404, 'The requested page does not exist.');
        }

        return $model;
    }
}
