Wednesday, 22 April 2015

Wordpress Disable Admin Panel Menus Based on Role

In wordpress admin panel, if you want to hide certain menu's for non-admins , then add the below code in your current theme's functions.php file.


function remove_menus () {
global $menu;

        $restricted = array(__('Dashboard'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'),  __('Plugins'));
        end ($menu);
        while (prev($menu)){
            $value = explode(' ',$menu[key($menu)][0]);
            if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
        }
}
      /* To hide menus for non -admin users*/
if(!is_super_admin()){
add_action('admin_menu', 'remove_menus');   
}


Sunday, 8 March 2015

Yii2 gridview row options

In Yii2 GridView, if you want to highlight or focus on certain row, you can use rowOptions attribute in your gridview.

Below are the example code.

<?=
GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
      'rowOptions' => function ($model, $index, $widget, $grid) {

                            if ($model->is_read == 1) {
                                return ['class' => 'read'];
                            } else {
                                return [];
                            }
                         },
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
         ['class' => 'yii\grid\ActionColumn']
    ],
]);
?>

Tuesday, 10 February 2015

Yii2 Modify GridView Filters


In Yii2  the filter input fields are automatically generated by the widget component, but we can modify to our needs by using the "filterPosition" property in Gridview widget.


To remove entire filters on Gridview,

'filterPosition'=>' ',

To show the fiters  on  top of each column's header cell,

  'filterPosition'=>'header',

To show the fiters  right below of each column's header cell,

 'filterPosition'=>'body',


To show the fiters  below each column's footer cell,

 'filterPosition'=>'footer',






Yii2 Dataprovider Pagination Set Default Value


To set yii2 data provider default pagination, the code is
    
 $dataProvider = new ActiveDataProvider([
         'query' => $query,
         'pagination'=> ['defaultPageSize' => 50]

     ]) 


    ( or )


  $dataProvider->pagination = ['defaultPageSize' => 50];



Yii2 Dataprovider Default Sorting


To set the default sorting for Yii2 Data provider is

    $dataProvider = new ActiveDataProvider([
         'query' => $query,
         'sort'=> ['defaultOrder' => ['id' => 'DESC']]

     ]) 


    ( or )


   $dataProvider->sort = ['defaultOrder' => ['id' => 'DESC']]; 



Thursday, 5 February 2015

yii2 get logged in user details

 
//To get whole logged user data
$user = \Yii::$app->user->identity;

//To get id of f logged user
$userId = \Yii::$app->user->identity->id



Monday, 17 March 2014

Yii CGridView Update onkeyup

By default Yii filters the table in cgridview, when a user entering data on search textfield and press Enter key or clicking oustside the grid.
I had a strange situation, i want to apply filter on keyup in cgridview for all pages,

Below is the code to apply filter on keyup in  yii cgridview.

    $(function() {
        $('body').on('keyup', '.filters > td > input', function() {
           
            var focusedId = $(document.activeElement).attr('id');
            var grid_id = $('#' + focusedId + '.form').closest("div").attr('id');
           
            $('#' + grid_id).yiiGridView('update', {
                data: $(this).serialize(),
                complete: function(jqXHR, status) {
                   
                    if (status == 'success') {
                        var tmpStr = $('#' + focusedId + '.form').val();
                        $('#' + focusedId + '.form').focus();
                        $('#' + focusedId + '.form').val('');
                        $('#' + focusedId + '.form').val(tmpStr);
                    }
                   
                }
            });
            return false;
        });

    });