Button event in Yii Framework

Asked

Viewed 602 times

0

I am making an application with Yii Framework that is relatively simple. First I made a view called total.php, who 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 the JobsLogController, and the action is the 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 I don’t 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 searched the documentation but was unsuccessful.

Thank you!

  • You managed to solve your problem?

1 answer

0

Initially, you are using a Framework MVC, then take care of direct access to the database on your controller.

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

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

To change this is easy, using the variable 'action' widget CActiveForm in 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 it helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.