środa, 15 września 2010

ExtJS + tree behaviour = categories sorting

http://blogs.bigfish.tv/adam/2008/02/12/drag-and-drop-using-ext-js-with-the-cakephp-tree-behavior/
Note:
All root level categories needs to have parent_id set to NULL


Code:
    function reorder(){
   
    // retrieve the node instructions from javascript
    // delta is the difference in position (1 = next node, -1 = previous node)
    $node = intval($this->params['form']['node']);
    $delta = intval($this->params['form']['delta']);
    if ($delta > 0) {
        $this->Category->movedown($node, abs($delta));
    } elseif ($delta < 0) {
        $this->Category->moveup($node, abs($delta));
    }
    // send success response
    exit('1');
   
}
    /*do sortowania*/
    function reparent(){
   
    $node = intval($this->params['form']['node']);
    $parent = intval($this->params['form']['parent']);
    $position = intval($this->params['form']['position']);
   
    // save the employee node with the new parent id
    // this will move the employee node to the bottom of the parent list
   
    $this->Category->id = $node;
    $this->Category->saveField('parent_id', $parent);
   
    // If position == 0, then we move it straight to the top
    // otherwise we calculate the distance to move ($delta).
    // We have to check if $delta > 0 before moving due to a bug
    // in the tree behavior (https://trac.cakephp.org/ticket/4037)
   
    if ($position == 0){
        $this->Category->moveup($node, true);
    } else {
        $count = $this->Category->childcount($parent, true);
        $delta = $count-$position-1;
        if ($delta > 0){
            $this->Category->moveup($node, $delta);
        }
    }
   
    // send success response
    exit('1');
    }

    function sort(){
        $this->layout = 'extjs';
        $this->set('categories', $this->paginate());
        $stuff = $this->Category->find('all',
        array('fields' => array('id','name', 'lft', 'rght', 'parent_id'), 'order' => 'lft ASC'));
        $this->set('stuff', $stuff);
//        debug($stuff);
    }
    /*do sortowania*/
    function getnodes(){
        $this->layout = 'ajax';
        // retrieve the node id that Ext JS posts via ajax
        $parent = intval($this->params['form']['node']);
        $this->log('getNodes: '.$parent, LOG_DEBUG);

        // find all the nodes underneath the parent node defined above
        // the second parameter (true) means we only want direct children
        $nodes = $this->Category->children($parent, true);
        $this->log('getNodes: NODES: '.var_export($nodes,true), LOG_DEBUG);
//        debug($nodes);

        // send the nodes to our view
        $this->set(compact('nodes'));

    }

    function reorderCategories($id = null){
        if ($id === null && $this->Category->id) {
            $id = $this->Category->id;
        } elseif (!$id) {
            $id = null;
        }
        $this->Category->id = null;
//        $res = $this->Category->reorder(array('id'=>null,  'field'=> 'lft','order'=>'ASC'));
//        $this->Category->reorder(array('field' => 'Category.lft', 'order' =>'ASC'));
        $this->Category->reorder();
    }

needed Files:
categories/view/getnodes.ctp

Brak komentarzy:

Prześlij komentarz