 <?php
    $Permissoes = User::getUser(true);
    if(!$Permissoes){
    	User::blockAction("Você não tem permissão para acessar esta tela");
    }

class QuestionariosFluxoController extends GxController {

	var $help = 'Cadastro dos questionarios.';

    public function beforerender($View)
    {
        // if(User::getUser()->FUN_TBL_QUESTIONARIOS != 1) {
        //     throw new CHttpException(403, "Você não tem permissão para acessar esta página");
        // }
        return true;
    }

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

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

        if (isset($_GET['QuestionariosFluxo']))
            $model->setAttributes($_GET['QuestionariosFluxo']);

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

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

		if (isset($_POST['QuestionariosFluxo'])) {

            $transaction = Yii::app()->db->beginTransaction();
			$model->setAttributes($_POST['QuestionariosFluxo']);

            try {
                if ($model->save()) {
                    if (isset($_POST['QuestionarioEtapas']) && is_array($_POST['QuestionarioEtapas'])) {
                        foreach ($_POST['QuestionarioEtapas']['QET_TITULO'] as $valor['QET_TITULO']) {

                            $etapa = new QuestionarioEtapas;
                            $etapa->setAttributes($valor['QET_TITULO']);
                            $etapa->QET_TITULO = $valor['QET_TITULO'];

                            if ($etapa->save()) {

                                $qfe = new QuestionariosFluxoEtapas;
                                $qfe->setAttributes(['QFE_IDQUESTIONARIO' => $model->QUF_IDQUESTIONARIO,
                                                     'QFE_IDETAPA' => $etapa->QET_IDETAPA]);

                                $qfe->QFE_IDQUESTIONARIO = $model->QUF_IDQUESTIONARIO;
                                $qfe->QFE_IDETAPA = $etapa->QET_IDETAPA;

                                $qfe->save();
                            }
                        }
                    }
                    $transaction->commit();

                    if (Yii::app()->getRequest()->getIsAjaxRequest())
                        Yii::app()->end();
                    else
                        $this->redirect(array('view', 'id' => $model->QUF_IDQUESTIONARIO));

                }

            } catch (Exception $e) {
                $transaction->rollback();
            }
		}
		$this->render('create', array( 'model' => $model, 'readonly' => false));
	}

	public function actionUpdate($id)
    {
        $idQuest = $id;
        $modelQuest = $this->loadModel($idQuest, 'QuestionariosFluxo');

        if (isset($_POST['QuestionariosFluxo'])) {

            $transaction = Yii::app()->db->beginTransaction();
            $modelQuest->setAttributes($_POST['QuestionariosFluxo']);

            try {
                if ($modelQuest->save()) {
                    if (isset($_POST['QuestionarioEtapas']['QET_TITULO'])) {
                        QuestionariosFluxoEtapas::model()->deleteAll('QFE_IDQUESTIONARIO = '.$idQuest);

                        foreach($_POST['QuestionarioEtapas']['QET_TITULO'] as $id => $titulo)
                        {
                            $qet = QuestionarioEtapas::model()->findAll("QET_IDETAPA = ".$id);

                            if($qet){
                                 $modelEtapa = $this->loadModel($id, 'QuestionarioEtapas');
                            } else {
                                $modelEtapa = new QuestionarioEtapas;
                            }

                            $modelEtapa->setAttributes($titulo);
                            $modelEtapa->QET_TITULO = $titulo;

                            if ($modelEtapa->save()) {

                                $qfe = new QuestionariosFluxoEtapas;
                                $qfe->setAttributes(['QFE_IDQUESTIONARIO' => $idQuest,
                                                     'QET_IDETAPA' => $modelEtapa->QET_IDETAPA]);

                                $qfe->QFE_IDQUESTIONARIO = $idQuest;
                                $qfe->QFE_IDETAPA = $modelEtapa->QET_IDETAPA;

                                $qfe->save();
                            }
                        }
                    }
                }
                $transaction->commit();

            } catch (Exception $e) {
                $transaction->rollback();
            }
			$this->redirect(array('view', 'id' => $idQuest));
		}
		$this->render('update', array('model' => $modelQuest, 'readonly' => true));
	}

	public function actionDelete($id)
    {

        $transaction = Yii::app()->db->beginTransaction();

        try {
            if (Yii::app()->getRequest()->getRequestType() == 'GET') {

                $questionarios = $this->loadModel($id, 'QuestionariosFluxo');
                $idquest = $questionarios->QUF_IDQUESTIONARIO;

                $qfe = QuestionariosFluxoEtapas::model()->findAll("QFE_IDQUESTIONARIO = ".$idquest);

                foreach ($qfe as $etapasQuest) {

                    QuestionarioEtapas::model()->deleteAll('QET_IDETAPA = '.$etapasQuest->QFE_IDETAPA);
                    QuestionariosFluxoEtapas::model()->deleteAll('QFE_IDQFE = '.$etapasQuest->QFE_IDQFE);
                }

                $questionarios->delete();
                $transaction->commit();

                if (!Yii::app()->getRequest()->getIsAjaxRequest())
                    $this->redirect(array('admin'));

            } else {

    			throw new CHttpException(400, 'Sua solicitação é inválida.');
            }
        } catch (Exception $e) {
            $transaction->rollback();
        }
	}

    public function actionAddEtapa()
    {
        $this->renderPartial('_questionario_etapa');
    }

}