cvs: pear /DB_NestedSet/NestedSet Menu.php /DB_NestedSet/docs Menu_example.php

From: Date: Sun, 18 Jan 2004 02:40:21 +0000
Subject: cvs: pear /DB_NestedSet/NestedSet Menu.php /DB_NestedSet/docs Menu_example.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-15748@lists.php.net to get a copy of this message
datenpunk Sat Jan 17 21:40:21 2004 EDT Added files: /pear/DB_NestedSet/NestedSet Menu.php /pear/DB_NestedSet/docs Menu_example.php Log: Added PEAR::HTML_Menu output driver

Index: pear/DB_NestedSet/NestedSet/Menu.php +++ pear/DB_NestedSet/NestedSet/Menu.php <?php // +----------------------------------------------------------------------+ // | PEAR :: DB_NestedSet_TreeMenu | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2003 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Daniel Khan <dk@webcluster.at> | // +----------------------------------------------------------------------+ // $Id: Menu.php,v 1.1 2004/01/18 02:40:20 datenpunk Exp $ // {{{ DB_NestedSet_Menu:: class /** * Simple helper class which takes a node array create by DB_NestedSet and transforms it * into an array useable by HTML_Menu * * @see docs/Menu_example.php * @author Daniel Khan <dk@webcluster.at> * @package DB_NestedSet * @version $Revision: 1.1 $ * @access public */ // }}} /** * DB_NestedSet_Menu * * @package * @author daniel * @copyright Copyright (c) 2004 * @version $Id: Menu.php,v 1.1 2004/01/18 02:40:20 datenpunk Exp $ * @access public */ class DB_NestedSet_Menu extends DB_NestedSet_Output { // {{{ properties var $_structMenu = array(); /** * * @var array Default field mappings * @access private */ var $_paramDefaults = array('textField' => 'text', 'titleField' => 'name', 'urlField' => 'url' ); // }}} // {{{ DB_NestedSet_Menu /** * The constructor * * @param array $params The config parameters used for building the array. * @see _createFromStructure * @access public * @return void */ function & DB_NestedSet_Menu($params) { $this->_structMenu = & $this->_createFromStructure($params); } // }}} // {{{ _createFromStructure /** * DB_NestedSet_Menu::_createFromStructure() * Creates the HTML_Menu Structure * * <pre> * @param array $params The configuration parameters. Available * params are: * o 'structure' => [REQU] The result from $nestedSet->getAllNodes(true) * o 'titleField' => [REQU] The field in the table that has the text for node * o 'urlField' => [REQU] The field in the table that has the link for the node * </pre> * @param $params * @return array The menu array **/ function _createFromStructure($params) { static $menuStructure, $menuParts; // always start at level 1 if (!isset($params['currentLevel'])) { $params['currentLevel'] = 1; } if (!isset($params['currentParent'])) { $params['currentParent'] = false; } if (!isset($menuParts)) { $menuParts = array(); } if (!isset($menuStructure)) { $menuStructure = array(); } // Set the default field mappings if not set in userland if (!isset($params['defaultsSet'])) { $this->_setParamDefaults($params); } // have to use a while loop here because foreach works on a copy of the array and // the child nodes are passed by reference during the recursion so that the parent // will know when they have been hit. reset($params['structure']); while (list($key, $node) = each($params['structure'])) { // see if we've already been here before if (isset($node['hit']) || $node['level'] < $params['currentLevel']) { continue; } // mark that we've hit this node $params['structure'][$key]['hit'] = $node['hit'] = true; // We are at a rootnode - let's add it to the structure if ($key == $node['rootid']) { $menuStructure[$node['id']] = array( 'title' => isset($node[$params['titleField']]) ? $node[$params['titleField']] : false, 'url' => isset($node[$params['urlField']]) ? $node[$params['urlField']] : false ); // Use a reference so we can happily modify $menuParts to change $menuStructure $menuParts[$node['id']] = & $menuStructure[$node['id']]; } // Perform action for non-root nodes $currentParent = & $params['currentParent']['id']; if ($currentParent && isset($menuParts[$currentParent])) { $currentPart = & $menuParts[$currentParent]['sub']; $currentPart[$node['id']] = array( 'title' => isset($node[$params['titleField']]) ? $node[$params['titleField']] : false, 'url' => isset($node[$params['urlField']]) ? $node[$params['urlField']] : false ); $menuParts[$node['id']] = & $currentPart[$node['id']]; } // see if it has children if (($node['r'] - 1) != $node['l']) { $children = array(); // harvest all the children $tempStructure = $params['structure']; foreach ($tempStructure as $childKey => $childNode) { if (!isset($childNode['hit']) && $node['rootid'] == $childNode['rootid'] && $node['l'] < $childNode['l'] && $node['r'] > $childNode['r'] && $childNode['level'] > $params['currentLevel']) { // important that we assign it by reference here, so that when the child // marks itself 'hit' the parent loops will know $children[] = & $params['structure'][$childKey]; } } $recurseParams = $params; $recurseParams['structure'] = $children; $recurseParams['currentLevel']++; $recurseParams['currentParent'] = & $node; $this->_createFromStructure($recurseParams); } } return $menuStructure; } // }}} // {{{ returnStructure /** * DB_NestedSet_Menu::returnStructure() * * Returns an array suitable for HTML_Menu * * @return array An array useable for HTML_Menu */ function returnStructure() { return $this->_structMenu; } // }}} // {{{ _setParamDefaults() /** * DB_NestedSet_Menu::_setParamDefaults() * * @param $params Param array passed from userland * @return bool True on completion * @access private */ function _setParamDefaults(& $params) { $defaults = $this->_paramDefaults; foreach($defaults AS $fieldName => $fieldAlias) { if (!isset($params[$fieldName])) { $params[$fieldName] = $fieldAlias; } } $params['defaultsSet'] = true; return true; } // }}} } ?> Index: pear/DB_NestedSet/docs/Menu_example.php +++ pear/DB_NestedSet/docs/Menu_example.php <?php /** $Id: Menu_example.php,v 1.1 2004/01/18 02:40:20 datenpunk Exp $ */ ?> <html> <title>DB_NestedSet using PEAR::HTML_Menu Output class</title> <body> <div style="font-weight: bold;">DB_NestedSet using PEAR::HTML_Menu Output class</div> <div> <?php /** * Tests the DB_NestedSet class using the Menu renderer * Requires that you have HTML_Menu installed * * @author Daniel Khan <dk@webcluster.at> */ // {{{ mysql dump /** * Dump of the example mysql table and data: # # Table structure for table `nested_set` # CREATE TABLE `nested_set` ( `id` int(10) unsigned NOT NULL default '0', `parent_id` int(10) unsigned NOT NULL default '0', `order_num` tinyint(4) unsigned NOT NULL default '0', `level` int(10) unsigned NOT NULL default '0', `left_id` int(10) unsigned NOT NULL default '0', `right_id` int(10) unsigned NOT NULL default '0', `name` varchar(60) NOT NULL default '', PRIMARY KEY (`id`), KEY `right` (`right_id`), KEY `left` (`left_id`), KEY `order` (`order_num`), KEY `level` (`level`), KEY `parent_id` (`parent_id`), KEY `right_left` (`id`,`parent_id`,`left_id`,`right_id`) ) TYPE=MyISAM; # # Dumping data for table `nested_set` # INSERT INTO `nested_set` VALUES (5, 5, 1, 1, 1, 10, 'Root A'); INSERT INTO `nested_set` VALUES (7, 7, 1, 1, 1, 4, 'Root B'); INSERT INTO `nested_set` VALUES (6, 5, 1, 2, 2, 5, 'Sub1 of A'); INSERT INTO `nested_set` VALUES (1, 5, 2, 2, 6, 9, 'Sub2 of A'); INSERT INTO `nested_set` VALUES (2, 5, 1, 3, 3, 4, 'Child of Sub1'); INSERT INTO `nested_set` VALUES (3, 5, 1, 3, 7, 8, 'Child of Sub2'); INSERT INTO `nested_set` VALUES (4, 7, 1, 2, 2, 3, 'Sub of B'); # -------------------------------------------------------- # # 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('HTML/Menu.php'); require_once(dirname(__FILE__).'/../NestedSet.php'); require_once(dirname(__FILE__).'/../NestedSet/Output.php'); $dsn = 'mysql://user:password@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]['url'] = $_SERVER['PHP_SELF'].'?nodeID=' . $node['id']; } // }}} // {{{ render output $params = array( 'structure' => $data, 'titleField' => 'name', 'urlField' => 'url'); // Create the output driver object $output =& DB_NestedSet_Output::factory($params, 'Menu'); // Fetch the menu array $structure = $output->returnStructure(); // Instantiate the menu object, we presume that $data contains menu structure $currentUrl = $_SERVER['PHP_SELF'].'?nodeID=' . $_GET['nodeID']; echo "Menu type 'sitemap'<br>"; $menu = & new HTML_Menu($structure, 'sitemap'); // Force menu to understand the nodeID passed with the request $menu->forceCurrentUrl($currentUrl); // Output the menu $menu->show(); echo "<hr>"; echo "Menu type 'tree'<br>"; // Set another type $menu->setMenuType('tree'); // Output the menu $menu->show(); echo "<hr>"; echo "Menu type 'rows'<br>"; // Set another type $menu->setMenuType('rows'); // Output the menu $menu->show(); echo "<hr>"; echo "Menu type 'urhere'<br>"; // Set another type $menu->setMenuType('urhere'); // Output the menu $menu->show(); echo "<hr>"; $menu->setMenuType('prevnext'); // Set another type $menu->setMenuType('prevnext'); // Output the menu $menu->show(); echo "<hr>"; // }}} ?> </div> </body> </html>
« previous php.pear.cvs (#15748) next »