Subversion Repositories phpLibraryV2

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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)
31
   protected  $menuItemString = '<li class="menu_item_<level>"><a href="<url>"><caption></a></li>';
32
   // string which is searched/replaced for a menu item that has no URL (<caption> is replaced)
33
   protected  $menuHeaderString = '<li class="menu_header_<level>"><caption></li>';
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)
36
   protected  $menuBlockString = '<ul class="menu"><menublock></ul>';
37
 
38
   // simply pass fields on to DBHierarchicalHash so it can load and parse the table
39
   public function __construct ($tableName = 'menus', $idFieldName = 'id', $parentFieldName = 'parent_id' ) {
40
      parent::__construct($tableName, $idFieldName, $parentFieldName );
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
84
   function DBMenu2String ( $rootDir = '' ) {
85
      return $this->htmlMenu( $this->inputRecords, 0, $rootDir );
86
   }
87
 
88
   // function takes a menu level and creates an HTML Menu items from it. If a node has children, will
89
   // recursively call itself for each child node.
90
   private function htmlMenu ($menu, $level=0, $rootDir = '' ) {
91
      $result = '';
92
      foreach ($menu as $key => $value) { // process each array entry
93
         if ($value[$this->urlName]) { // this is a link, so it is a live menu option
94
            $result .= insertValuesIntoQuery( $this->menuItemString, array( 'url' => $rootDir . $value[$this->urlName], 'caption' => $value[$this->captionName], 'level' => $level) );
95
         } else { // not a link, so just create the text
96
            $result .= insertValuesIntoQuery( $this->menuHeaderString, array( 'caption' => $value[$this->captionName], 'level' => $level) );
97
         }
98
         if ( isset($value['children'])) { // if it has children, process them
99
            $result .=  $this->htmlMenu($value['children'], $level+1, $rootDir);
100
         }
101
      }
102
      // place the block code around the menu, and return the result
103
      return insertValuesIntoQuery( $this->menuBlockString, array( 'menublock' => $result, 'level' => $level ) );
104
   }
105
 
106
} // class DBMenu
107
 
108
/* following block is for testing. comment out for production */
109
//$menu = new DBHierarchicalHash ('menu', 'menu_id');
110
//print_r( $menu );
111
 
112
//$menu = new DBMenu( 'menu', 'menu_id' );
113
//print $menu->DBMenu2String();
114
//print "Menu Structure:\n"; print_r($menu);
115
 
116
/* end of block for testing */
117
 
118
?>