Button event in Yii Framework

0

I'm doing an application with Yii Framework which is relatively simple. First I made a view called total.php , which asks for a data inicial , data final and a button:

<?php 
    $form = $this->beginWidget('CActiveForm', array(
        'id'=>'impressoes-do-usuario-form-teste3-form',
        'enableAjaxValidation'=>false,
    )); 
?>
<div class="wide form">
   <?php echo $form->labelEx($model,'dataInicio'); ?>
   <?php $this->widget('CMaskedTextField', array(
       'mask'=>'99-99-9999', 
       'model'=>$model, 
       'attribute'=>'dataInicio',
       'value' => (empty($model->dataInicio) ? $model->dataInicio : '31/03/1987'),
    ));?>
</div>
<div class="wide form">
    <?php echo $form->labelEx($model,'dataFim'); ?>
    <?php $this->widget('CMaskedTextField', array(
        'mask'=>'99-99-9999', 
        'model'=>$model, 
        'attribute'=>'dataFim'
    ));?>
<div class="row buttons">
    <?php echo CHtml::submitButton('OK');?>
</div>
<?php $this->endWidget(); ?>

My controller is JobsLogController , and action is actionTotal :

<?php

class JobsLogController extends Controller
{
    public $layout='//layouts/column2';
    public $defaultAction = "total";

    public function actionTotal()
    {


        $model = new ImpressoesDoUsuarioForm;
        $impressoras = null;    

        if (isset($_POST['ImpressoesDoUsuarioForm']))
        {
                $model->attributes=$_POST['ImpressoesDoUsuarioForm'];

            // CONVERTENDO AS DATAS
            $dataInicio = Yii::app()->dateFormatter->format('yyyy-MM-dd HH:mm:ss', strtotime($model->dataInicio));
            $dataFim    = Yii::app()->dateFormatter->format('yyyy-MM-dd HH:mm:ss', strtotime($model->dataFim));

            // BUSCA POR TODAS AS IMPRESSORAS NA DATA ESCOLHIDA
            $criteria = new CDbCriteria;
            $criteria->select='printer';
            $criteria->distinct=true;
            $criteria->condition='date>:dataInicio AND date<:dataFim';
            $criteria->params=array(":dataInicio"=>$dataInicio,":dataFim"=>$dataFim);
            $printers = JobsLog::model()->findAll($criteria);
            array_multisort($printers,SORT_ASC);

            $impressoras = array();

            // BUSCA IMPRESSOES DO USUARIO NA IMPRESSORA
            foreach($printers as $printer)
            {
                $criteria = new CDbCriteria;
                $criteria->select='user';
                $criteria->distinct=true;
                $criteria->condition='printer=:printer AND date>:dataInicio AND date<:dataFim';
                $criteria->params=array(":printer"=>$printer->printer,
                            ":dataInicio"=>$dataInicio,
                            ":dataFim"=>$dataFim);

                $users = JobsLog::model()->findAll($criteria);

                $impressoes = array();

                foreach($users as $user)
                {
                    $criteria->select='*';
                    $criteria->condition='user=:user AND printer=:impressora AND date>:dataInicio AND date<:dataFim';
                    $criteria->params=array(":user"=>$user->user,
                                ":impressora"=>$printer->printer,
                                ":dataInicio"=>$dataInicio,
                                "dataFim"=>$dataFim);

                    $impressoesDoUsuario = JobsLog::model()->findAll($criteria);
                    $contagem = 0;
                    foreach($impressoesDoUsuario as $impressaoDoUsuario)
                    {
                        $contagem += $impressaoDoUsuario->copies * $impressaoDoUsuario->pages;  
                    }
                    $impressoes[$user->user] = $contagem;
                }
                $impressoras[$printer->printer] = $impressoes;
            }

        }
        $this->render('total',array("model"=>$model,"impressoras"=>$impressoras));

    }
}
?>

My problem is that I do not know how to call the action actionTotal through the button I created in total.php . Does anyone know how I do this in this framework? I looked in the documentation, but I did not succeed.

Thank you!

    
asked by anonymous 16.10.2015 / 15:13

1 answer

0

Initially, you are using a Framework MVC , so be aware of direct access to the database in your controller.

That said we can continue. To make your button perform a controller action, you can use it in several ways. One of them is submitting the form.

When you use only the CHtml::submitButton('OK'); command, you are saying that the form will be sent to the current action, that is, the action that called this page.

To change this is easy, using the variable 'action' of widget CActiveForm this way ( ref1 ):

<?php 
    $form = $this->beginWidget('CActiveForm', array(
        'id'=>'impressoes-do-usuario-form-teste3-form',
        'action' => array('JobsLog/total'),
        'enableAjaxValidation'=>false,
    )); 
?>

You can also do this by rewriting the button by directing it ( ref2 ) :

<?php echo CHtml::button('OK', array('submit' => array('JobsLog/total'))); ?>

I hope you have helped.

    
09.03.2016 / 22:59