Social Icons

Pages

Sunday, March 31, 2013

auth module integration steps

1) Download the latest release from Yii extensions Auth.
http://www.yiiframework.com/extension/auth/

and placed under folder protected/modules, if not please create one folder named modules

2)Then protected/config/main.php 

on module array add

        'modules'=>array(
                .........
                'auth', 
                
                ),


OR 

'modules'=>array(
        .........

  'auth' => array(
        'strictMode' => true, // when enabled authorization items cannot be assigned children of the same type.
        'userClass' => 'User', // the name of the user model class.
        'userIdColumn' => 'id', // the name of the user id column.
        'userNameColumn' => 'name', // the name of the user name column.
        'appLayout' => 'application.views.layouts.main', // the layout used by the module.
        'viewDir' => null, // the path to view files to use with this module.
  ),

  ),


in components array

  'components' => array(
    'authManager' => array(
        ......
      'behaviors' => array(
        'auth' => array(
          'class' => 'auth.components.AuthBehavior',
          'admins' => array('admin', 'foo', 'bar'), // users with full access
        ),
      ),
    ),
    'user' => array(
      'class' => 'auth.components.AuthWebUser',
    ),
  ),



Please note that while the module doesn't require you to use a database, if you wish to use CDbAuthManager you need it's schema (it can be found in the framework under web/auth/schema-mysql.sql(if mysql is using), and it import to your db).

also change the authManager array to 

   'authManager' => array(
                'class' => 'CDbAuthManager',
                'connectionID' => 'db',
                'behaviors' => array(
                        'auth.components.AuthBehavior',
                ),
        ),
        


and in main controller  use 

        public function filters()
        {
        return array(
        array('auth.filters.AuthFilter'),
        );
        }




auth module will ready to use index.php?r=auth with normal template 

3) if you want to use bootstrap pleas do as below

download bootstrap from http://www.yiiframew...nsion/bootstrap and placed under protected/extension/bootstrap
then copy the theme from bootstrap to theme folder under you webapplication/themes/



Top of the protected/config/main.php add 

        Yii::setPathOfAlias('bootstrap', dirname(__FILE__).'/../extensions/bootstrap');

in components array 

   'bootstrap'=>array(
                                'class'=>'bootstrap.components.Bootstrap',
                ),



and change appLayout from auth array as below

    modules =array(
                
                auth=>array(
                ......
                'appLayout' => 'webroot.themes.theme.views.layouts.main', // the layout used by the module.
                                )
        ),

Saturday, March 30, 2013

Display a nice exception message on ajax requests


please refer the link


 http://www.yiiframework.com/wiki/228/display-a-nice-exception-message-on-ajax-requests/


or use the below code in your default controller


public function actionError()
{
    if($error=Yii::app()->errorHandler->error)
    {
        if(Yii::app()->request->isAjaxRequest)
            echo $error['message'];
        else
            $this->render('error', $error);
    }
}


Friday, March 29, 2013

tbtabs yii ajax

In view

<?php $this->widget('bootstrap.widgets.TbTabs', array(
        'id' => 'mytabs',
        'type' => 'tabs',
        'tabs' => array(
           
                array('id' => 'tab2', 'label' => 'Tab 2', 'content' => 'loading ....'),
                array('id' => 'tab3', 'label' => 'Tab 3', 'content' => 'loading ....'),


        ),
        'events'=>array('shown'=>'js:loadContent')
    )
);?>



<script type="text/javascript">

function loadContent(e){

    var tabId = e.target.getAttribute("href");

    var ctUrl = '';

    if(tabId == '#tab2') {
        ctUrl = 'controller/action';


//eg: ctUrl = '<?php echo $this->createUrl('emailQueues/test/id/'.$id)?>';




    } else if(tabId == '#tab3') {
        ctUrl = 'url to get tab 3 content';
    }

    if(ctUrl != '') {

        $.ajax({


            url      : ctUrl,
            type     : 'POST',
            dataType : 'html',
            cache    : false,
            success  : function(html)
            {
                jQuery(tabId).html(html);
            },
            error:function(){
                    alert('Request failed');
            }
        });
    }

    preventDefault();
    return false;
}

</script>



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}'  
        ),
    ),
));