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)); }
/** * 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']); } } }
/** * Initializes the NestedSet object * which is needed to manage the news categories. * * @access public */ public function __construct() { global $objDatabase; //nestedSet setup $arrTableStructure = array('catid' => 'id', 'parent_id' => 'rootid', 'left_id' => 'l', 'right_id' => 'r', 'sorting' => 'norder', 'level' => 'level'); $objNs = new \DB_NestedSet($arrTableStructure); $this->objNestedSet = $objNs->factory('ADODB', $objDatabase, $arrTableStructure); $this->objNestedSet->setAttr(array('node_table' => DBPREFIX . 'module_news_categories', 'lock_table' => DBPREFIX . 'module_news_categories_locks')); if (count($rootNodes = $this->objNestedSet->getRootNodes()) > 0) { foreach ($rootNodes as $rootNode) { $this->nestedSetRootId = $rootNode->id; break; } } else { // create first entry of sequence table for NestedSet $objResult = $objDatabase->SelectLimit("SELECT `id` FROM `" . DBPREFIX . "module_news_categories_catid`", 1); if ($objResult->RecordCount() == 0) { $objDatabase->Execute("INSERT INTO `" . DBPREFIX . "module_news_categories_catid` VALUES (0)"); } $this->nestedSetRootId = $this->objNestedSet->createRootNode(array(), false, false); } }
# 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();
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!