/** * Returns the parent category id of passed category. * * @access protected * @param integer $category * @return integer or boolean $cmd */ protected function getParentCatId($category) { if (($parent = $this->objNestedSet->getParent($category)) && $parent->id != $this->nestedSetRootId) { return $parent->id; } return false; }
function setUp() { $params = array("STRID" => "id", "ROOTID" => "rootid", "l" => "l", "r" => "r", "STREH" => "norder", "LEVEL" => "level", "STRNA" => "name", "parent" => "parent"); $db_driver = 'DB'; $db_dsn = 'mysql://*****:*****@localhost/test'; $this->_NeSe = DB_NestedSet::factory($db_driver, $db_dsn, $params); $this->_NeSe->setAttr(array('node_table' => 'tb_nodes', 'lock_table' => 'tb_locks', 'lockTTL' => 5, 'debug' => 0)); $this->_NeSe2 = DB_NestedSet::factory($db_driver, $db_dsn, $params); $this->_NeSe2->setAttr(array('node_table' => 'tb_nodes2', 'lock_table' => 'tb_locks', 'lockTTL' => 5, 'debug' => 0)); }
function test_convertModel() { $rnc = 3; $depth = 1; $npl = 2; $this->_createSubNode($rnc, $depth, $npl); DB_NestedSet::convertTreeModel($this->_NeSe, $this->_NeSe2); $this->_NeSe->setSortMode(NESE_SORT_LEVEL); $this->_NeSe2->setSortMode(NESE_SORT_LEVEL); $this->assertEquals($this->_NeSe->getAllNodes(true), $this->_NeSe2->getAllNodes(true), 'Converted tree should match original'); }
/** * Perform on admin requests for this module * * @param array $data */ function perform($data) { // User rights class //include(SF_BASE_DIR.'/admin/modules/navigation/class.rights.php'); require_once SF_BASE_DIR . '/admin/modules/common/PEAR/DB/NestedSet.php'; $params = array('node_id' => 'id', 'parent_id' => 'rootid', 'left_id' => 'l', 'right_id' => 'r', 'order_num' => 'norder', 'level' => 'level', 'name' => 'name', 'node_status' => 'status'); $dsn = $this->B->sys['db']['dbtype'] . '://' . $this->B->sys['db']['user'] . ':' . $this->B->sys['db']['passwd'] . '@' . $this->B->sys['db']['host'] . '/' . $this->B->sys['db']['name']; $nestedSet =& DB_NestedSet::factory('DB', $dsn, $params); $nestedSet->node_table = "{$this->B->sys['db']['table_prefix']}navigation_nested_set"; $nestedSet->lock_table = "{$this->B->sys['db']['table_prefix']}navigation_nested_set_locks"; $nestedSet->sequence_table = "{$this->B->sys['db']['table_prefix']}navigation_seq_nested_set"; // set the base template for this module $this->B->module = SF_BASE_DIR . '/admin/modules/navigation/templates/index.tpl.php'; // Switch to module features switch ($_REQUEST['mf']) { case 'edit_node': include SF_BASE_DIR . "/admin/modules/navigation/edit_node.php"; // set the base template for this module feature $this->B->section = SF_BASE_DIR . '/admin/modules/navigation/templates/edit_node.tpl.php'; break; case 'add_node': /* // have rights to add nav_node? if(FALSE == rights::ask_access_to_add_nav_node ()) { @header('Location: '.SF_BASE_LOCATION.'/admin/index.php?m=NAVIGATION'); exit; } */ if (isset($_POST['addnavnode'])) { include SF_BASE_DIR . "/admin/modules/navigation/add_node.php"; } // set the base template for this module feature $this->B->section = SF_BASE_DIR . '/admin/modules/navigation/templates/add_node.tpl.php'; break; default: // set the default template for this module $this->B->section = SF_BASE_DIR . '/admin/modules/navigation/templates/default.tpl.php'; // check if we are at root level if (empty($_REQUEST['node_id'])) { // get root nodes $this->B->nodes = $nestedSet->getRootNodes(); } else { // get child nodes $this->B->nodes = $nestedSet->getChildren((int) $_REQUEST['node_id']); } } }
# Table structure for table `nested_set_locks` # CREATE TABLE `nested_set_locks` ( `lockID` char(32) NOT NULL default '', `lockTable` char(32) NOT NULL default '', `lockStamp` int(11) NOT NULL default '0', PRIMARY KEY (`lockID`,`lockTable`) ) TYPE=MyISAM COMMENT='Table locks for comments'; */ // }}} // {{{ set up variables require_once 'DB/NestedSet.php'; require_once 'DB/NestedSet/Output.php'; $dsn = 'mysql://*****:*****@localhost/test'; $params = array('id' => 'id', 'parent_id' => 'rootid', 'left_id' => 'l', 'right_id' => 'r', 'order_num' => 'norder', 'level' => 'level', 'name' => 'name'); $nestedSet =& DB_NestedSet::factory('DB', $dsn, $params); // we want the nodes to be displayed ordered by name, so we add the secondarySort attribute $nestedSet->setAttr(array('node_table' => 'nested_set', 'lock_table' => 'nested_set_locks', 'secondarySort' => 'name')); // get data (important to fetch it as an array, using the true flag) $data = $nestedSet->getAllNodes(true); // }}} // {{{ manipulate data // add links to each item foreach ($data as $id => $node) { $data[$id]['link'] = 'http://example.com/foo.php?' . $node['id']; } // }}} // {{{ render output $params = array('structure' => $data, 'textField' => 'name', 'linkField' => 'link'); $output =& DB_NestedSet_Output::factory($params, 'TreeMenu'); $output->printListbox();
/** * Convert a <1.3 tree into a 1.3 tree format * * This will convert the tree into a format needed for some new features in * 1.3. Your <1.3 tree will still work without converting but some new features * like preorder sorting won't work as expected. * * <pre> * Usage: * - Create a new node table (tb_nodes2) from the current node table (tb_nodes1) (only copy the structure). * - Create a nested set instance of the 'old' set (NeSe1) and one of the new set (NeSe2) * - Now you have 2 identical objects where only node_table differs * - Call DB_NestedSet::convertTreeModel(&$orig, &$copy); * - After that you have a cleaned up copy of tb_nodes1 inside tb_nodes2 * </pre> * * @param object DB_NestedSet $orig Nested set we want to copy * @param object DB_NestedSet $copy Object where the new tree is copied to * @param integer $_parent ID of the parent node (private) * * @static * @access public * @return bool True uns success */ function convertTreeModel(&$orig, &$copy, $_parent = false) { static $firstSet; $isRoot = false; if (!$_parent) { if (!is_object($orig) || !is_object($copy)) { return false; } if ($orig->node_table == $copy->node_table) { return false; } $copy->_dumbmode = true; $orig->sortMode = NESE_SORT_LEVEL; $copy->sortMode = NESE_SORT_LEVEL; $sibl = $orig->getRootNodes(true); $isRoot = true; } else { $sibl = $orig->getChildren($_parent, true); } if (empty($sibl)) { return false; } foreach ($sibl as $sid => $sibling) { unset($sibling['l']); unset($sibling['r']); unset($sibling['norder']); $values = array(); foreach ($sibling as $key => $val) { if (!isset($copy->_flparams[$key])) { continue; } $values[$copy->_flparams[$key]] = $val; } if (!$firstSet) { $psid = $copy->createRootNode($values, false, true); $firstSet = true; } elseif ($isRoot) { $psid = $copy->createRightNode($psid, $values); } else { $copy->createSubNode($_parent, $values); } DB_NestedSet::convertTreeModel($orig, $copy, $sid); } return true; }
Content of menu_items.php: ------------------------ */ require_once 'DB/NestedSet.php'; require_once 'DB/NestedSet/Output.php'; // Choose a database abstraction layer. 'DB' and 'MDB' are supported. $nese_driver = 'DB'; // Set the DSN - see http://pear.php.net/manual/en/core.db.tut_dsn.php for details $nese_dsn = 'mysql://*****:*****@localhost/test'; // Specify the database columns which will be used to specify a node // Use an associative array. On the left side write down the name of the column. // On the right side write down how the property will be called in a node object // Some params are needed $nese_params = array("STRID" => "id", "ROOTID" => "rootid", "l" => "l", "r" => "r", "STREH" => "norder", "LEVEL" => "level", "STRNA" => "name", "STLNK" => "link"); // Now create an instance of DB_NestedSet $NeSe =& DB_NestedSet::factory($nese_driver, $nese_dsn, $nese_params); if (PEAR::isError($NeSe)) { echo $NeSe->getCode() . ": " . $NeSe->getMessage(); } // Fetch the tree as array $nodes = $NeSe->getAllNodes(true); // Set the basic params $params = array('structure' => $nodes, 'options' => array(), 'textField' => 'name', 'linkField' => 'link', 'currentLevel' => 1); // This array contains the options needed // for printing out the menu. $options = array('rootStyles' => array('onmouseout' => array('color' => '#FF0000', 'background' => '#000000', 'textDecoration' => 'none', 'border' => "1px solid #FFFFFF", 'fontSize' => '11px', 'fontFamily' => 'Verdana, Arial, Helvetica, sans-serif', 'fontWeight' => 'bold', 'textAlign' => 'center', 'padding' => '2px'), 'onmouseover' => array('color' => '#FFFFFF', 'background' => '#000000', 'textDecoration' => 'none'), 'onmousedown' => array('color' => '#FFFFFF', 'background' => '#000000', 'textDecoration' => 'none')), 'childStyles' => array('onmouseout' => array('color' => '#000000', 'background' => '#CCCCCC', 'textDecoration' => 'none', 'border' => "1px solid #FFFFFF", 'fontSize' => '11px', 'fontFamily' => 'Verdana, Arial, Helvetica, sans-serif', 'fontWeight' => 'normal', 'textAlign' => 'left', 'padding' => '2px'), 'onmouseover' => array('color' => '#FFFFFF', 'background' => '#EEEEEE', 'textDecoration' => 'none'), 'onmousedown' => array('color' => '#FFFFFF', 'background' => '#EEEEEE', 'textDecoration' => 'none')), 'rootGeometry' => array('width' => '120', 'height' => '21', 'left' => '119', 'top' => '0', 'block_left' => '169', 'block_top' => '121', 'hide_delay' => '200'), 'childGeometry' => array('width' => '*6', 'height' => '21', 'left' => '0', 'top' => '20', 'block_left' => '0,-5', 'block_top' => '20,10', 'hide_delay' => '2000'), 'menu_id' => 1); // Now create the menu object, set the options and do the output $menu =& DB_NestedSet_Output::factory($params, 'TigraMenu'); $menu->setOptions('printTree', $options); $menu->printTree(); // Have fun!