Thursday, 18 December 2014

Generate Row Number for CgridView in YII

Yii::CGridView – Generate row number
<code>$this->widget(  'zii.widgets.grid.CGridView',  array(    'columns'=>array(      array(        'header'=>'No.',        'value'=>'$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',      ),
      'emp_no',
      'emp_name',
    ),  ));</code>

Ajax Submit Button, Prevent multiple posts in YII

Yii::ajaxsubmitbutton – Prevent multiple post
To prevent the ajaxsubmitbutton from doing multiple post, add the ‘beforeSend’ option to your code:
<?php echo CHtml::ajaxSubmitButton(‘Save’,
                                   array(‘formWorkHoursDays’),
                                   array(
                                                      ‘success’=>’function(html) { alert(html); }’,
                                                      ‘beforeSend’=>’function(){
                                                                        $(\’body\’).undelegate(\’#submitMe\’, \’click\’);
                                                                    }’,    

                                                        ),
                                    array(‘id’=>’submitMe’, ‘name’=>’submitMe’, ‘class’=>’submitButton’)
                              );
?>

Change Themes Dynamically in YII application

So, plan to change themes dynamically on your page with Yii?? Here is what i do.
1. Add this code to protected/components/Controller.php
public $breadcrumbs=array();
public function beforeAction($action)
        {
            $dynamicTheme = ‘classic';
        
            if(isset($_POST[‘mytheme’]))
            {
                $dynamicTheme = $_POST[‘mytheme’];
                //Yii::app()->request->cookies[‘dynamicTheme’] = new CHttpCookie(‘dynamicTheme’, $dynamicTheme);
                $cookie = new CHttpCookie(‘dynamicTheme’, $dynamicTheme);
                $cookie->expire = time()+60*60*24*180; 
                Yii::app()->request->cookies[‘dynamicTheme’] = $cookie;
            }
            
            if(isset(Yii::app()->request->cookies[‘dynamicTheme’]->value))
            {
                $dynamicTheme = Yii::app()->request->cookies[‘dynamicTheme’]->value;
            }
            else
            {
                //Yii::app()->request->cookies[‘dynamicTheme’] = new CHttpCookie(‘dynamicTheme’, $dynamicTheme);
                $cookie = new CHttpCookie(‘dynamicTheme’, $dynamicTheme);
                $cookie->expire = time()+60*60*24*180; 
                Yii::app()->request->cookies[‘dynamicTheme’] = $cookie;
            }
            
            //$dynamicTheme = (isset(Yii::app()->request->cookies[‘dynamicTheme’]->value)) ?    Yii::app()->request->cookies[‘dynamicTheme’]->value : ‘classic';
            Yii::app()->theme=$dynamicTheme;
            
            return parent::beforeAction($action);
        }
2. Add this code anywhere you want. This code for select list of theme:
<form name=”form1″ id=”form1″ method=’post’ action=”<?php echo CHtml::normalizeUrl(array(‘index’))?>” style=”margin:0;padding:0;”>
                        &nbsp;&nbsp;
                        Theme: <SELECT name=’mytheme’ onchange=”Javascript:document.form1.submit()”>
                                    <?php
                                        $arrTheme = Yii::app()->params[‘arrTheme’];
                                        foreach($arrTheme as $value=>$display)
                                        {
                                            $dynamicTheme = (isset(Yii::app()->request->cookies[‘dynamicTheme’]->value)) ? Yii::app()->request->cookies[‘dynamicTheme’]->value : ”;
                                            $s = ”;
                                            if($value == $dynamicTheme) $s = ‘selected';
                                            echo “<option value=’$value’ $s>$display</option>”;
                                        }
                                    ?>
                                </SELECT>
</form>
3. Add theme list parameter on your protected/config/main.php
‘params’=>array(
// this is used in contact page
‘adminEmail’=>’webmaster@example.com’,
 ‘arrTheme’=>array(
                        ‘classic’=>’Default’,
                        ‘white’=>’White’,
                    ),
),

OK THATS ALL.. Hope your code success..

Expression in Access Rules in YII

Here is one of the way on how we do control user access to some function in our controller.
In your controller file, on public function accessRules(), add this array:
array('allow',
                                'actions'=>array('leaveApproval', 'pendingLeave'),
                                'users'=>array(Yii::app()->user->username),
                                'expression' => '(Yii::app()->user->leave_approval == "Y")',
                        ),

Reset Filter on CGridView in YII

Yii:: Reset Filter on GridView
1. Add this reset button:
<?php echo CHtml::resetButton('Clear!', array('id'=>'form-reset-button')); ?>
2. Add this jquery:
$('#form-reset-button').click(function()
{
   var id='document-grid';
   var inputSelector='#'+id+' .filters input, '+'#'+id+' .filters select';
   $(inputSelector).each( function(i,o) {
        $(o).val('');
   });
   var data=$.param($(inputSelector));
   $.fn.yiiGridView.update(id, {data: data});
   return false;
});