1 |
rodolico |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
include_once( 'DBHierarchicalHash.class.php');
|
|
|
4 |
|
|
|
5 |
/*
|
|
|
6 |
simple extension of DBHierarchicalHash allowing a menu structure to be stored in a database.
|
|
|
7 |
the structure for the database is simple:
|
|
|
8 |
create table menu (
|
|
|
9 |
menu_id int unsigned not null auto_increment,
|
|
|
10 |
parent_id int unsigned not null default 0,
|
|
|
11 |
caption varchar(20) not null,
|
|
|
12 |
url varchar(64),
|
|
|
13 |
primary key (menu_id)
|
|
|
14 |
)
|
|
|
15 |
where caption is the string displayed on the menu, and url is the (possibly null) url
|
|
|
16 |
to be called when the link is clicked by the user. The resulting line is something like:
|
|
|
17 |
<a href="url">caption</a>
|
|
|
18 |
though this can be modified by changing $menuItemString and $menuHeaderString (see below)
|
|
|
19 |
|
|
|
20 |
menu_id is a unique identifier for a single row, and parent_id points to the menu that it
|
|
|
21 |
is a submenu of (0 indicates a root menu item)
|
|
|
22 |
|
|
|
23 |
Note: I tried to avoid any constants in the class, so column names, menu_id data type, root
|
|
|
24 |
menu indicator are all modifiable
|
|
|
25 |
*/
|
|
|
26 |
class DBMenu extends DBHierarchicalHash {
|
|
|
27 |
|
|
|
28 |
protected $captionName = 'caption'; // column name for display string in database
|
|
|
29 |
protected $urlName = 'url'; // column name for URL field in database
|
|
|
30 |
// string which is searched/replaced for a menu item that has a URL (<url> and <caption> are replaced)
|
29 |
rodolico |
31 |
protected $menuItemString = "<li class='menu_item_<level>'><a href='<url>'><caption></a></li>\n";
|
1 |
rodolico |
32 |
// string which is searched/replaced for a menu item that has no URL (<caption> is replaced)
|
29 |
rodolico |
33 |
protected $menuHeaderString = "<li class='menu_header_<level>'><caption></li>\n";
|
1 |
rodolico |
34 |
// string which is placed around <menublock>, ie this goes around a menu/submenu. <level> can be used to determine
|
|
|
35 |
// which level (zero based) we are in the menu (level = 0 is top menu)
|
29 |
rodolico |
36 |
protected $menuBlockString = "<ul class='menu'><menublock></ul>\n";
|
1 |
rodolico |
37 |
|
|
|
38 |
// simply pass fields on to DBHierarchicalHash so it can load and parse the table
|
29 |
rodolico |
39 |
public function __construct ( $dbConnect, $tableName = 'menus', $idFieldName = 'id', $parentFieldName = 'parent_id' ) {
|
|
|
40 |
parent::__construct($dbConnect, $tableName, $idFieldName, $parentFieldName );
|
1 |
rodolico |
41 |
}
|
|
|
42 |
|
|
|
43 |
// simple setter/getter for the caption column name in table
|
|
|
44 |
public function captionColumnName ( $newValue = '' ) {
|
|
|
45 |
if ($newValue) {
|
|
|
46 |
$this->captionName = $newValue;
|
|
|
47 |
}
|
|
|
48 |
return $this->captionName;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
// simple setter/getter for url column name in table
|
|
|
52 |
public function urlColumnName ( $newValue = '' ) {
|
|
|
53 |
if ($newValue) {
|
|
|
54 |
$this->urlName = $newValue;
|
|
|
55 |
}
|
|
|
56 |
return $this->urlName;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
// simple setter/getter for menuItemString for output
|
|
|
60 |
public function menuItemString ( $newValue = '' ) {
|
|
|
61 |
if ($newValue) {
|
|
|
62 |
$this->menuItemString = $newValue;
|
|
|
63 |
}
|
|
|
64 |
return $this->menuItemString;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
// simple setter/getter for menuHeaderString for output
|
|
|
68 |
public function menuHeaderString ( $newValue = '' ) {
|
|
|
69 |
if ($newValue) {
|
|
|
70 |
$this->menuHeaderString = $newValue;
|
|
|
71 |
}
|
|
|
72 |
return $this->menuHeaderString;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
// simple setter/getter for menu block string for output
|
|
|
76 |
public function menuBlockString ( $newValue = '' ) {
|
|
|
77 |
if ($newValue) {
|
|
|
78 |
$this->menuBlockString = $newValue;
|
|
|
79 |
}
|
|
|
80 |
return $this->menuBlockString;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
// just an entry point to displayMenu, with root of inputRecords and level 0
|
29 |
rodolico |
84 |
function DBMenu2String ( $rootDir = '', $toAdd = null ) {
|
|
|
85 |
foreach ( $toAdd as $additional ) {
|
|
|
86 |
$this->inputRecords[] = $additional;
|
|
|
87 |
}
|
|
|
88 |
//print "<pre>\n" . print_r( $this, true) . "</pre>"; die;
|
|
|
89 |
return $this->htmlMenu( $this->inputRecords, 0, $rootDir, $toAdd );
|
1 |
rodolico |
90 |
}
|
|
|
91 |
|
|
|
92 |
// function takes a menu level and creates an HTML Menu items from it. If a node has children, will
|
|
|
93 |
// recursively call itself for each child node.
|
|
|
94 |
private function htmlMenu ($menu, $level=0, $rootDir = '' ) {
|
|
|
95 |
$result = '';
|
|
|
96 |
foreach ($menu as $key => $value) { // process each array entry
|
|
|
97 |
if ($value[$this->urlName]) { // this is a link, so it is a live menu option
|
|
|
98 |
$result .= insertValuesIntoQuery( $this->menuItemString, array( 'url' => $rootDir . $value[$this->urlName], 'caption' => $value[$this->captionName], 'level' => $level) );
|
|
|
99 |
} else { // not a link, so just create the text
|
|
|
100 |
$result .= insertValuesIntoQuery( $this->menuHeaderString, array( 'caption' => $value[$this->captionName], 'level' => $level) );
|
|
|
101 |
}
|
|
|
102 |
if ( isset($value['children'])) { // if it has children, process them
|
|
|
103 |
$result .= $this->htmlMenu($value['children'], $level+1, $rootDir);
|
|
|
104 |
}
|
|
|
105 |
}
|
29 |
rodolico |
106 |
/*
|
|
|
107 |
// if they want anything added, do it here.
|
|
|
108 |
foreach ( $toAdd as $key => $value ) {
|
|
|
109 |
$result .= insertValuesIntoQuery( $this->menuItemString, array( 'url' => $value, 'caption' =>$key ) );
|
|
|
110 |
}
|
|
|
111 |
*/
|
1 |
rodolico |
112 |
// place the block code around the menu, and return the result
|
|
|
113 |
return insertValuesIntoQuery( $this->menuBlockString, array( 'menublock' => $result, 'level' => $level ) );
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
} // class DBMenu
|
|
|
117 |
|
|
|
118 |
/* following block is for testing. comment out for production */
|
8 |
rodolico |
119 |
/*
|
|
|
120 |
$menu = new DBHierarchicalHash ('_menu', '_menu_id');
|
|
|
121 |
print_r( $menu );
|
1 |
rodolico |
122 |
|
8 |
rodolico |
123 |
$menu = new DBMenu( 'menu', 'menu_id' );
|
|
|
124 |
print $menu->DBMenu2String();
|
|
|
125 |
print "Menu Structure:\n"; print_r($menu);
|
|
|
126 |
*/
|
1 |
rodolico |
127 |
/* end of block for testing */
|
|
|
128 |
|
|
|
129 |
?>
|