Social Icons

Pages

Friday, March 29, 2013

Before Save Function in Model

beforeSave() -- model is a nice function in yii framework

When we update record in table, Some fields are changed by default like when record created, when record modified etc. Here I wrote code for this action. In Format 1, I configured separately for created date, modified date. $this->isNewRecord is return true, if it is new record. Now I affected date only If $this->isNewRecord is return false, I will affect modified date only. In Format 2, I configured both created date, modified date in beforeSave() function. In this code, created date will affected only on new record. modified date will affected on when recored created or When record modified This function reduce our workflow. Just make this function once It was very helpful.




class Mail extends CActiveRecord
{
    // Format 1
    public function beforeSave() 
    {
        if($this->isNewRecord)
        {           
            $this->date=new CDbExpression('NOW()');
            $this->status=0; //option by default
        }else{
             $this->modified = new CDbExpression('NOW()');
        }
        return parent::beforeSave();
    }

    // Format 2

    public function beforeSave() 
    {
        if($this->isNewRecord)
        {           
            $this->date=new CDbExpression('NOW()');
            $this->status=0; //option by default
        }
        $this->modified = new CDbExpression('NOW()');
        return parent::beforeSave();
    }
}










Thursday, March 28, 2013

gridview view in modal popup Yii


In view

$this->widget('bootstrap.widgets.TbExtendedGridView', array(
    'type'=>'bordered',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'template'=>"{items}",
    'columns'=>array(
        'id',
        'firstName',
        'lastName',
        'language',
        'hours',
        array(
            'header'=>'Options',
            'class'=>'bootstrap.widgets.TbButtonColumn',
            'buttons'=>array(
                'view'=>
                    array(
                        'url'=>'Yii::app()->createUrl("person/view", array("id"=>$data->id))',
                        'options'=>array(
                            'ajax'=>array(
                                'type'=>'POST',
                                'url'=>"js:$(this).attr('href')",
                                'success'=>'function(data) { $("#viewModal .modal-body p").html(data); $("#viewModal").modal(); }'
                            ),
                        ),
                    ),
            ),
        )
    )));
?>

<!-- View Popup  -->
<?php $this->beginWidget('bootstrap.widgets.TbModal', array('id'=>'viewModal')); ?>
<!-- Popup Header -->
<div class="modal-header">
<h4>View Employee Details</h4>
</div>
<!-- Popup Content -->
<div class="modal-body">
<p>Employee Details</p>
</div>
<!-- Popup Footer -->
<div class="modal-footer">

<!-- close button -->
<?php $this->widget('bootstrap.widgets.TbButton', array(
    'label'=>'Close',
    'url'=>'#',
    'htmlOptions'=>array('data-dismiss'=>'modal'),
)); ?>
<!-- close button ends-->
</div>
<?php $this->endWidget(); ?>
<!-- View Popup ends -->



And in Controller 


public function actionView($id)
{
    if( Yii::app()->request->isAjaxRequest )
        {
        $this->renderPartial('view',array(
            'model'=>$this->loadModel($id),
        ), false, true);
    }
    else
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }
}





Monday, March 25, 2013

CGridView and show detail in CJuiDialog Yii


in view file, add the following to CButtonClass section of the array:


<?php $this->widget('zii.widgets.grid.CGridView', array(
    .....
    'columns' => array(
        ....
        array(
            'class' => 'CButtonColumn',
            'buttons' => array(
                'view' => array(
                    'url' => 'url' => 'Yii::app()->createUrl("/path/to/controller/view")',
                    'options' => array(
                        'ajax' => array(
                            'type' => 'POST',
                            'url' => "js:$(this).attr('href')",
                            'update' => '#detail-section',
                        ),
                    ),

                ),    // view button
            ),
        ),
    )
)) ?>


<?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
        'id' => 'dlg-detail',
        'options' => array(
            'title' => 'Dialog Box Title',
            'closeOnEscape' => true,
            'autoOpen' => false,
            'model' => false,
            'width' => 550,
            'height' => 450,
        ),
)) ?>
<div id="detail-section"></div>
<?php $this->endWidget() ?>




change the view action in controller file:



public function actionView($id) {
    if (Yii::app()->request->isAjaxRequest) {
        $this->renderPartial('_view', array(
            'model' => $this->loadModel($id),
        ), false, true);

        Yii::app()->end();
    }
}












Ajax button with confirm and before send texts


In View

<?php
echo "<div id='resPass'>";
echo CHtml::ajaxButton(
'Reset Password',          // the link body (it will NOT be HTML-encoded.)
array('resetPassword','id'=>$model->user_id), // the URL for the AJAX request. If empty, it is assumed to be the current URL.
array(
'update'=>'#req_res_loading',
'beforeSend' => 'function() {   
     
 $("#resPass").html("please wait......");

     }',

     'complete' => 'function() {
      $("#resPass").hide();
     }', 
),

      array(
   'class'=>'btn btn-info',
   'confirm'=>Yii::t('admin','Are you want to Reset Password ?'),
      )


);
echo "</div>"
?>
<div id="req_res_loading"></div>


In controller 


       public function actionResetPassword($id)
{
        if(Yii::app()->request->isAjaxRequest)
                {
       .................
              Yii::app()->end();
        }
   
}



Sunday, March 24, 2013

Add button append text box with delete


   <div id="content">
      <div>
 
        <input type="text"/>
        <a class="add" href="#">Add</a>
    </div>








$("#content").delegate(".add", "click", function() {
    var content = '<div>' +
                   
                         '<input type="text"/>'+
                         '<a class="del" href="#">Delete</a>&nbsp;' +
                         '<a class="add" href="#">Add</a>'+
                         '</div>';
    $("#content").append(content);
    return false;
});

$("#content").delegate(".del", "click", function() {
    $(this).parent().remove();
    return false;
});







Friday, March 22, 2013

CgridView Two column value concate Yii


$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'scenarios-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(    
   
        array(
            'name'=>'fname',
            'header'=>'Client Name',
            'value'=>'$data->first_name." ".$data->last_name',
        ),
              array(
            'class'=>'CButtonColumn',
                        'template'=>'{view}{delete}'  
        ),
    ),
));

Monday, January 14, 2013

CJuiTabs Content

When I used CJuiTabs I did something. They are 1. Direct Content for tab 2. Append content Using Ajax 3. Tab with ID and Id For every single tab of CJuitab 4. Content Using renderPartial Method 5. Dynamic Tab menu from database I post code below. It was very userful for me.

Format 1:Basic

$this->widget('zii.widgets.jui.CJuiTabs', array(
    'tabs' => array(
        'StaticTab 1' => 'Content for tab 1',
        'StaticTab 2' => array('content' => 'Content for tab 2', 'id' => 'tab2'),
        // panel 3 contains the content rendered by a partial view
        'AjaxTab' => array('ajax' => $this->createUrl('...')),
    ),

    // additional javascript options for the tabs plugin
    'options' => array(
        'collapsible' => true,
    ),
));
Format 2:Content Using Render Method

$this->widget('zii.widgets.jui.CJuiTabs', array(
    'tabs' => array(
        'StaticTab 1' => 'Content for tab 1',
        'StaticTab 2' => array('content' => 'Content for tab 2', 'id' => 'tab2'),
        // panel 3 contains the content rendered by a partial view
        'AjaxTab' => array('ajax' => $this->createUrl('..')),
 // Get Content From Another page. Also Pass Parameter
        'Render View'=>$this->renderPartial('_newpage',
                                array('value'=>$value),
                                              TRUE
       )),
    ),

    // additional javascript options for the tabs plugin
    'options' => array(
        'collapsible' => true,
    ),
    // set id for this widgets
    'id'=>'MyTab',
));
Format 3:Content Using Render Method And Set Id For Each Single Tab

$this->widget('zii.widgets.jui.CJuiTabs', array(
    'tabs' => array(
        'StaticTab 1' => 'Content for tab 1',
        'StaticTab 2' => array('content' => 'Content for tab 2', 'id' => 'tab2'),
        // panel 3 contains the content rendered by a partial view
        'AjaxTab' => array('ajax' => $this->createUrl('..')),
        'Render View'=>array('id'=>'renderid',
                             'content'=>$this->renderPartial(
                                '_newpage',
                                 array('value'=>$value),TRUE)
                              ),
    ),

    // additional javascript options for the tabs plugin
    'options' => array(
        'collapsible' => true,
    ),
    // set id for this widgets
    'id'=>'MyTab',
));
Dynamic CJuiTabs

$tab_list=Componenttabs::gettabs();
$tabarray=array();
$i=1;
foreach ($tab_list as $key=>$value){
 $tabarray["Tab $i"]=array('id'=>$i,
                            'content'=>$this->renderPartial(
                                       '_newpage',                              
                                        array('value'=>$value),
                                        TRUE)
     );
 $i++;
}

$this->widget('zii.widgets.jui.CJuiTabs', array(
    'tabs'=>
  $tabarray,
/*array(
        'StaticTab 1'=>'Content for tab 1',
        'StaticTab 2'=>array('content'=>'Content for tab 2', 'id'=>'tab2'),
        // panel 3 contains the content rendered by a partial view
        //'AjaxTab'=>array('ajax'=>$ajaxUrl),
    ),*/

    // additional javascript options for the tabs plugin
    'options'=>array(
        'collapsible'=>true,
    ),
    'id'=>'categorytabs',
));



for reference please go through this link cjuitabs