Rev 8 | Rev 30 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
<?php
include_once( 'DBHierarchicalHash.class.php');
/*
simple extension of DBHierarchicalHash allowing a menu structure to be stored in a database.
the structure for the database is simple:
create table menu (
menu_id int unsigned not null auto_increment,
parent_id int unsigned not null default 0,
caption varchar(20) not null,
url varchar(64),
primary key (menu_id)
)
where caption is the string displayed on the menu, and url is the (possibly null) url
to be called when the link is clicked by the user. The resulting line is something like:
<a href="url">caption</a>
though this can be modified by changing $menuItemString and $menuHeaderString (see below)
menu_id is a unique identifier for a single row, and parent_id points to the menu that it
is a submenu of (0 indicates a root menu item)
Note: I tried to avoid any constants in the class, so column names, menu_id data type, root
menu indicator are all modifiable
*/
class DBMenu extends DBHierarchicalHash {
protected $captionName = 'caption'; // column name for display string in database
protected $urlName = 'url'; // column name for URL field in database
// string which is searched/replaced for a menu item that has a URL (<url> and <caption> are replaced)
protected $menuItemString = '<li class="menu_item_<level>"><a href="<url>"><caption></a></li>';
// string which is searched/replaced for a menu item that has no URL (<caption> is replaced)
protected $menuHeaderString = '<li class="menu_header_<level>"><caption></li>';
// string which is placed around <menublock>, ie this goes around a menu/submenu. <level> can be used to determine
// which level (zero based) we are in the menu (level = 0 is top menu)
protected $menuBlockString = '<ul class="menu"><menublock></ul>';
// simply pass fields on to DBHierarchicalHash so it can load and parse the table
public function __construct ($tableName = 'menus', $idFieldName = 'id', $parentFieldName = 'parent_id' ) {
parent::__construct($tableName, $idFieldName, $parentFieldName );
}
// simple setter/getter for the caption column name in table
public function captionColumnName ( $newValue = '' ) {
if ($newValue) {
$this->captionName = $newValue;
}
return $this->captionName;
}
// simple setter/getter for url column name in table
public function urlColumnName ( $newValue = '' ) {
if ($newValue) {
$this->urlName = $newValue;
}
return $this->urlName;
}
// simple setter/getter for menuItemString for output
public function menuItemString ( $newValue = '' ) {
if ($newValue) {
$this->menuItemString = $newValue;
}
return $this->menuItemString;
}
// simple setter/getter for menuHeaderString for output
public function menuHeaderString ( $newValue = '' ) {
if ($newValue) {
$this->menuHeaderString = $newValue;
}
return $this->menuHeaderString;
}
// simple setter/getter for menu block string for output
public function menuBlockString ( $newValue = '' ) {
if ($newValue) {
$this->menuBlockString = $newValue;
}
return $this->menuBlockString;
}
// just an entry point to displayMenu, with root of inputRecords and level 0
function DBMenu2String ( $rootDir = '' ) {
return $this->htmlMenu( $this->inputRecords, 0, $rootDir );
}
// function takes a menu level and creates an HTML Menu items from it. If a node has children, will
// recursively call itself for each child node.
private function htmlMenu ($menu, $level=0, $rootDir = '' ) {
$result = '';
foreach ($menu as $key => $value) { // process each array entry
if ($value[$this->urlName]) { // this is a link, so it is a live menu option
$result .= insertValuesIntoQuery( $this->menuItemString, array( 'url' => $rootDir . $value[$this->urlName], 'caption' => $value[$this->captionName], 'level' => $level) );
} else { // not a link, so just create the text
$result .= insertValuesIntoQuery( $this->menuHeaderString, array( 'caption' => $value[$this->captionName], 'level' => $level) );
}
if ( isset($value['children'])) { // if it has children, process them
$result .= $this->htmlMenu($value['children'], $level+1, $rootDir);
}
}
// place the block code around the menu, and return the result
return insertValuesIntoQuery( $this->menuBlockString, array( 'menublock' => $result, 'level' => $level ) );
}
} // class DBMenu
/* following block is for testing. comment out for production */
/*
$menu = new DBHierarchicalHash ('_menu', '_menu_id');
print_r( $menu );
$menu = new DBMenu( 'menu', 'menu_id' );
print $menu->DBMenu2String();
print "Menu Structure:\n"; print_r($menu);
*/
/* end of block for testing */
?>