Yii 1.1: yiisycrudadmin

Allow use crud like pages for manage table records with some improvements.
6 followers

YiisyCrudAdmin extension provide functionality to create CRUD like controllers without code generation. Also it provides flexable inteface to change displayed fields in admin views, form view, details view.

I'm using it in my projects, so i will update it when new functionality will be required in my projects.

Also was version adapted for twig template engine, but i reject twig in yii development. So many time spent to implement twig support and remove this support :)

Requirements

cleditor extension for use RDbText field.

Usage

Download from here or go to yii application extension directory. Run :

hg clone -q https://yii-rextensions.googlecode.com/hg/ YiisyCrudAdmin -r YiisyCrudAdmin

in protected/config/main.php append section import with

'ext.YiisyCrudAdmin.*',

Example import section:

// autoloading model and component classes
        'import'=>array(
                'application.models.*',
                'application.components.*',
 
                'ext.YiisyCrudAdmin.*',
        ),

Create new model (or use exists) and modify like :

class Export extends RActiveRecord //replace CActiveRecord with RActiveRecord
{
}

Now you can create controller :

class ExampleController extends RController 
{
        protected $_modelName = 'Example';
}

That's all

Customize:

To customize admin pages you should create new class ModelNameAdmin? (Admin should be postfix). And override required methods from RModelAdmin. Admin class should be in same file as related model.

For example admin for Export model:

Class ExportAdmin extends RModelAdmin
{
 
//This method provide information about field types, can return emtpy array if you don't need smth special
// Possible field types you can find in ext.YiisyCrudAdmin.fields
        public function getFieldsDescription()
        {
                return array(
                        'modelField' => 'RDbBoolean',// wher RDbBoolean type mean 0 false, 1 true , and in edit/update it will be checkbox
                        'long_description' => 'RDbText',// where RDbText mean that text can be big, and should be edit in textarea
                );
        }
//...

Resources

I don't want put demo of project on hosting.

Advanced usage

Here goes RModelAdmin methods which can be overriden to change model behavior in RController children.

For example you have model Example so you can override in ExampleAdmin follow methods

Control displayed fields in admin view

class ExampleAdmin extends RModelAdmin
{
     /**
     * Define fields visible in admin view.
     *
     * It's usefull when you have model with many fields, and need to show little set of them, not all
     * 
     * @return array 
     */    
    public function getAdminFields(){}
 
    /**
     * Define fields to exclude from admin view, can be used with RModelAdmin::getAdminFields()
     * 
     * @return array list of fields to be excluded from admin view 
     */
    public function getAdminExcludedFields(){}
 
 
}

Control displayed fields in update/create view

class ExampleAdmin extends RModelAdmin
{
    /**
     * Define list of fields visible in update/create view, other fields will be not shown in update/create view  
     * 
     * @return array list of fields to be shown in update/create view 
     */
    public function getFormFields(){}
 
    /**
     * Returns list of fields to be excluded from update/create views
     * 
     * Can work with RModelAdmin::getFormFields()
     * 
     * @return array
     */
    public function getFormExcludedFields(){}
 
}

Control displayed fields in detail(view) view

class ExampleAdmin extends RModelAdmin
{
    /**
     * Returns list of fields will  be excluded in view action
     *
     * @return array list of fields will be excluded in view atction
     */
    public function getViewExcludedFields(){}
 
    /**
     * Returns list of fields will be shown in view action 
     *
     * @return array
     */
    public function getViewFields(){}
}

Change behavior of fields in model.

By default each field showed in textInputs like in default CRUD controller, but you can change behavior of field to be more specific.

To set behavior of field you need to describe it in getFieldsDescription method

class ExampleAdmin extends RModelAdmin
{
  public function getFieldsDescription() 
  {
    return array(
      'is_admin' => 'RDbBoolean',// true false value in mysql tinyint(1) 0 - false, 1 -true, in admin view select box filter will be applied, in update/edit view checkbox will be rendered
      'content' => 'RDbText', // html editor will be shown in updat/create action, in admin, details view will be cutted to be not big 
      'author_id' => array('RDbRelation', 'autor'),//support for belongs to relation, author_id is field name, 'author' is relation name, you must specify relatin name to work with this field type, it will show filter with list of related model in admin view, and selectbox with related model's list in update/create view, best way to have declare $_repr property in related model admin class 
      'created' => 'RDbDate', // show jui date picker in update/create view 
      'enumfield' => array('RDbSelect', array('key' => value,/* ..*/)),// show select box in admin filter and update/create view, can be data from CHtml::listData()
    );
  } 
}

Naming in controller and menus

class ExampleAdmin extends RModelAdmin
{
    /**
     * @var string name of element in RController menu
     */
    protected $_adminName = 'element';
 
    /**
     * @var string name of elements set in RController menu
     */
     protected $_pluralName = 'elements';
 
    /**
     * Property name used to show model object as string ( for user it can be 'login', it can be complex, for example
     * <code>
     *
     * class User extends RActiveRecord
     * {
     *      public function getFullName()
     *      {
     *          return $this->first_name . ' ' . $this->last_name;
     *      }
     * }
     *
     * class UserAdmin extends RModelAdmin
     * {
     *      protected $_repr = 'fullName';
     * }
     * </code> 
     *
     *
     * @var string
     */
    protected $_repr = '';
 
    /**
     * Returns text for header in actionCreate view 
     *
     * @return string
     */
    public function getCreateHeader(){}
 
    /**
     * Return text for header in actionUpdate
     * @return string
     */
    public function getUpdateHeader(){}
 
 
    /**
     * Returns text for header in actionView
     *
     * @return string
     */
    public function getViewHeader(){}
 
    /**
     * Returns text for header in actionAdmin
     *
     * @return string
     */
    public function getAdminHeader(){}
}

Custom templates

To use custom templates you can copy base template from ext.YiisyCrudMenu.views and need declare it in controller class

Here list predefined templates, you can redeclare it in own Controller

class RController extends Controller
{
        public $actionAdminTemplate = 'ext.YiisyCrudAdmin.views.admin';
        public $actionViewTemplate = 'ext.YiisyCrudAdmin.views.view';
        public $actionUpdateTemplate = 'ext.YiisyCrudAdmin.views.update';
        public $formTemplate = 'ext.YiisyCrudAdmin.views._form';
        public $actionCreateTemplate = 'ext.YiisyCrudAdmin.views.create';
        public $searchTemplate = 'ext.YiisyCrudAdmin.views._search';
}

Overriding admin, update, delete, view actions

It's worst part for now i don't see any way to copy-past implementation from ext.YiisyCrudAdmin.RController, may be later i find way to it better with tricks around beforeAction or smth like it. Need way to smth beforeAdminAction, afterAdminAction

Be the first person to leave a comment

Please to leave your comment.

Create extension