FuelPHP Bin
<?php /** * Return the tree suitable for use with jstree * * @param string property name to store the node's children * @param string property name that contains the data value, defaults to the defined title field * @param array if defined, specifies the list if fields to include in the attributes * @return array */ public function dump_jstree($children = 'children', $data_field = 'title_field', $filter = array()) { // Define the tree pointers $left_field = static::tree_config('left_field'); $right_field = static::tree_config('right_field'); // closure to create a jstree node from a nestedsets node $jstreenode = function($node) use($data_field, $filter, $children, $left_field, $right_field) { // create a new node $new = array(); // add the data field $new['data'] = $node->{$data_field}; // and the left- and right pointers $new['left_field'] = $node->{$left_field}; $new['right_field'] = $node->{$right_field}; // add the attributes if (empty($filter)) { // add them all $new['attr'] = (object) $node->to_array(); } else { $new['attr'] = array(); foreach ($filter as $field) { $new['attr'][$field] = $node->{$field}; } } // add storage for the node's children $new['children'] = array(); // return the new node return $new; }; // get the PK $pk = reset(static::$_primary_key); // start building the tree with the current node $tree = array($this->{$pk} => $jstreenode($this)); // parent tracker $tracker = array(); $index = 0; $tracker[$index] =& $tree[$this->{$pk}]; // loop over the descendants foreach ($this->descendants()->get() as $treenode) { // construct ht jstree node object $node = $jstreenode($treenode); #\Debug::dump($tracker[$index]['right_field']); die; // is this node a child of the current parent? if ($treenode->{$left_field} > $tracker[$index]['right_field']) { // no, so pop the last parent and move a level back up $index--; } // add it as a child to the current parent $tracker[$index][$children][$treenode->{$pk}] = $node; // does this node have children? if ($treenode->{$right_field} - $treenode->{$left_field} > 1) { // create a new parent level $tracker[$index+1] =& $tracker[$index][$children][$treenode->{$pk}]; $index++; } } return $tree; }