Yii 1.1: ejnestedtreeactions

EJNestedTreeActions
11 followers

EJNestedTreeActions

Version. 0.3beta Author: tydeas_dr Copyright © 2010 Evresis

What EJNestedTreeActions IS and What IS NOT

EJNestedTreeActions IS NOT:

  • a nested set behavior for your model
  • a jstree extension

EJNestedTreeActions IS only a set of actions and a behavior and you as well need:

Confused ? MVC

Model

To make your model act like a nested set you will need the ENestedSetBehavior, as we said before. This extension make your model to be able to append,prepend,return the children etc.

View

At view we have the pretty one jstree. To use the jstree you will need the CJstree extension ( found as jstree extension ). This will give you the ability to drag & drop, copy & paste, create new nodes etc. at your view.

Controller

This is where EJNestedTreeActions comes. The jstree has the ability to use async ajax to make request and get results. This async request will call a controller's action to do so. This is what the EJNestedTreeActions does. Take the request from the jstree make the appropriate append,prepend, etc for your model and return data to the jstree.

How all this work and the callbacks

You first create you table and use the ENestedBehavior like it is described in it's documentation. Use the EJNestedTreeBehavior as described in documentation. Use CJstree as described in it's documentation. Attention: You will need to add some proper callbacks when you will use the jstree for each action you want to use from the EJNestedBehavior. This proper callbacks can be found in each actions documentation but will give you an example too.

git repository

To clone project from the github repository:

git clone git://github.com/dmtrs/EJNestedTreeActions.git

Thanks

I would like to thank the CJstree author shocky, the ENestedBehavior author creocoder, samdark, the Evresis team, the people from #yii at freenode in general and specially Javache for the help on this.

Resources

Documentation

EJNestedTreeActions

Version. 0.3beta Author: tydeas_dr

Requirements

  • Yii 1.0 or above
  • ENestedSetBehavior 0.85 (see in README/Overview for link)
  • CJstree (see in overview for link)
  • jsTree version 0.9.9.a

Dirs structure and installation

Extract the release file under protected/extensions

  • EJNestedTreeBehavior/views/example.php
  • EJNestedTreeBehavior/actions/Copynode.php
  • EJNestedTreeBehavior/actions/Createnode.php
  • EJNestedTreeBehavior/actions/Creatroot.php
  • EJNestedTreeBehavior/actions/Deletenode.php
  • EJNestedTreeBehavior/actions/Movenode.php
  • EJNestedTreeBehavior/actions/Renamenode.php
  • EJNestedTreeBehavior/actions/Render.php
  • EJNestedTreeBehavior/EBehavior.php

Usage

After you have installed ENestedSetBehavior and CJstree and you are sure they work properly.

Copy the views/example.php in the views/yourcontroller/

After you have setup properly ENestedSetBehavior and Jstree. Add followin in your controller:

public function behaviors() {
    return array(
        'EJNestedTreeActions'=>array(
            'class'=>'ext.EJNestedTreeActions.EBehavior',
            'classname'=>'Rutree',
            'identity'=>'id',
            'text'=>'text',
        ),
    );
}
public function actions() {
    return array (
        'render'=>'ext.EJNestedTreeActions.actions.Render',
        'createnode'=>'ext.EJNestedTreeActions.actions.Createnode',
        'renamenode'=>'ext.EJNestedTreeActions.actions.Renamenode',
        'deletenode'=>'ext.EJNestedTreeActions.actions.Deletenode',
        'movenode'=>'ext.EJNestedTreeActions.actions.Movenode',
        'copynode'=>'ext.EJNestedTreeActions.actions.Copynode',
    );
 
}
public function actionIndex() {
    $this->render('example');
}

Details and explanation

Behavior

In the above example we have set some attributes to the behavior. This certain attributes are the required and must exist or else extension won't work

Required attributes

classname => string. The name of the model this controller handles. identity => string. The name of model's id attribute. text => string. The name of model's attribute that will be used as the name of the nodes. For the last 2 you can refer to the ENestedSetBehavior.

Inherit feature

inherit => array. The attribute names from which the node child will inherit values. For example if your with countries/locations tree is about has root

Europe 
|
|--UK
|  |-London
|  |-Manchester
|
|--GR
   |-Athens
   |-Thessaloniki
...
...

This model may have a Currency attribute that you may want to inherit every time you creat a new node. In this occassion databse default value won't work because UK has Pounds and GR has Euro. But for every town under the country there is the same currency. So what you have to do in behavior is:

....
'inherit' => array('Currency'),
....

Or maybe you want to inherit languages as well, no problem.

....
'inherit' => array('Currency','language'),
....

All the above will only happen for 3 actions create/move and copy when the corresponding boolean attribute for each action is true. mvinherit => boolean. If inherit will happen when moving node-s from parent to parent. cpinherit => boolean. If inherit will happen when coping node-s from parent to parent. crtinherit => boolean. If inherit will happen when creating node under parent.

For example if you want the inherit to appear when you create a new node:

....
    'inherit' => array('Currency','language'),
'crtinherit'=>true
    ....

And play ;)

Actions

Non of the above actions are required. It's up to what you want user to do with your tree. The available actions are under folder actions and i think their name explain it all.

Some attention here

To use an action you must have the proper callback to the jstree when you initiliaze it. For each action in the .php file at the top there is the proper callback that at least worked. The view/example.php file has a proper all of them there.

Createroot won't create a root if the NestedSet you have is single root (hasmanyroots=>false) and this is a ENestedSetBehavior issue i think (will have to check). To create a root node for single root trees you have to do it manual with an insert to the database with this certain values.

id=1 , lft=1 , rgt=2 , level=1

Issues

If you are facing issues, please refer to the forum or the issues in the github site.

Change Log

EJNestedTreeActions

Version. 0.3beta Author: tydeas_dr

Version 0.3beta

  • bugs occured fixed
  • Createroot action added, works only for hasmanyroot trees
  • Inherit feature added, check documentation
  • Multiroot trees supported Some actions are not ready to be supported like move/copy node from root to another root and this is a ENestedSetehavior issue
  • Createorcopy action deleted ( if ever existed ).
  • Fb calls has been removed ( sorry for all that trouble ).

Version 0.2:

  • bug fixed in CDbCriteria
  • Copy node, childs will be copied too.

Version 0.1:

  • Support trees with only one root . Multiple tries will be available at v 0.3.
  • Render nodes.
  • Create new node inside selected node.
  • Delete node ( delete childs as well ).
  • Rename node.
  • Move node.
  • Copy node. Node's childs will not be copied, this will be avalaible at v 0.2.

Total 1 comment

#4218 report it
yiqing95 at 2011/06/16 01:28pm
the jstree extension seem to be stopping update.

jstree have had some new versions a long timejstree on google, but the jstree extension author did not update his extension, your extension is closely worked with the jstree. so we have to use the older version of the jstree(0.9.9)?

some one can you kindly update the jstree extension in the yii's ext repository jstreeExtionsion.

and may be there exists another way that we can use the ENestedSetBehavior and this extension ?

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: tydeas_dr
  • Category: Others
  • Votes: +5 / -3
  • Downloaded: 2,135 times
  • Created on: May 23, 2010
  • Last updated: May 31, 2010