Allow admin to change user role

1. Add following code to _form.php

Yii::app()->getModule('rights');
if (Yii::app()->user->isSuperuser)
{
    $all_roles = new RAuthItemDataProvider('roles', array(
                'type' => 2,
            ));
    $data = $all_roles->fetchData();
    echo CHtml::activeDropDownList($model, 'user_role', CHtml::listData($data, 'name', 'name'));
}

2. Update actionCreate & actionUpdate of UsersController.php like this

public function actionCreate()
{
    $model = new User;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if (isset($_POST['User']))
    {
        $model->attributes = $_POST['User'];
        if ($model->save())
        {
            Rights::assign($model->user_role, $model->id);
            $this->redirect(array('view', 'id' => $model->id));
        }
    }

    $this->render('create', array(
        'model' => $model,
    ));
}

public function actionUpdate($id)
{
    $model = $this->loadModel($id);

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if (isset($_POST['User']))
    {
        $model->attributes = $_POST['User'];
        if ($model->save())
        {
            Yii::app()->getModule('rights');
            $assignedRoles = Rights::getAssignedRoles($model->id);

            foreach ($assignedRoles as $role => $detail)
            {
                Rights::revoke($role, $model->id);
            }
            Rights::assign($model->user_role, $model->id);
            $this->redirect(array('view', 'id' => $model->id));
        }
    }

    $this->render('update', array(
        'model' => $model,
    ));
}

Switch Yii layout based on user role

You can assign different layouts to different users using following filter class

1. Add LayoutFilter.php to your protected/components/ folder

class LayoutFilter extends CFilter
{
 
    protected function preFilter($filterChain)
    {
        // logic being applied before the action is executed
        $roles = Yii::app()->user->getState('roles');
        if (!is_array($roles))
            $roles = array();
 
        if (in_array(User::ROLE_ADMIN, $roles))
        {
            $filterChain->controller->layout = 'admin/column2';
        } else
        {
            $filterChain->controller->layout = '//layouts/column2';
        }
 
        return parent::preFilter($filterChain);
    }
 
    protected function postFilter($filterChain)
    {
    // logic being applied after the action is executed
        return parent::postFilter($filterChain);
    }
 
}

2. Update protected/components/Controller.php and add following filters

public function filters()
{
    return array(
        'rights',
        array('application.components.LayoutFilter'),
    );
}

3. Update all controllers and add following filters

public function filters()
{
    return parent::filters() + array(
        'postOnly + delete', // we only allow deletion via POST request
    );
}

keyup function with delay

 

This code will execute a function after the user has stopped typing for a time of 1000ms. Hence the function will not be called on each keyup event. Great!!!

jQuery(function(){
    var delay = (function(){
        var timer = 0;
        return function(callback, ms){
            clearTimeout (timer);
            timer = setTimeout(callback, ms);
        };
    })();

    $('input').keyup(function() {
        delay(function(){
            alert('Time elapsed!');
        }, 1000 );
    });

});

Remove index.php from URL in YII

1- Open root/protected/config/main.php and uncomment the UrlManager
yii_urlManager
2- Create .htaccess file and place it on the root of your site. Contents of .htaccess should be

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

Integrate Filemanager to FCKeditor in YII

Before starting the tutorial you may also like to view the fully functional demo, or you may download the example and start working on it. Please note that this tutorial is for the PHP language and connectors, but same steps can also be applied for other supported languages and connectors.

Also I assume that you have sufficient knowledge about integrating CKEditor on your page.

$this->widget('ext.ckeditor.CKEditorWidget', array(
    "model" => $model, # Data-Model
    "attribute" => 'page_content', # Attribute in the Data-Model
    "defaultValue" => $model->page_content, # Optional
    # Additional Parameter (Check http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html)
    "config" => array(
        "height" => "400px",
        "width" => "100%",
        "toolbar" => "Full",
        //"skin"=>'kama',
        /* */
        "filebrowserImageUploadUrl" => Yii::app()->baseUrl . '/ckeditor/filemanager/connectors/php/upload.php',
        "filebrowserBrowseUrl" => Yii::app()->baseUrl . '/ckeditor/filemanager/browser/default/browser.html?Connector=' . Yii::app()->baseUrl . '/ckeditor/filemanager/connectors/php/connector.php',
        "filebrowserImageBrowseUrl" => Yii::app()->baseUrl . '/ckeditor/filemanager/browser/default/browser.html?Type=Image&Connector=' . Yii::app()->baseUrl . '/ckeditor/filemanager/connectors/php/connector.php',
        "filebrowserFlashBrowseUrl" => Yii::app()->baseUrl . '/ckeditor/filemanager/browser/default/browser.html?Type=Flash&Connector=' . Yii::app()->baseUrl . '/ckeditor/filemanager/connectors/php/connector.php',
    ),
    #Optional address settings if you did not copy ckeditor on application root
    "ckEditor" => Yii::app()->basePath . "/../ckeditor/ckeditor.php",
    "ckBasePath" => Yii::app()->baseUrl . "/ckeditor/",
));