Subversion Repositories phpLibraryV2

Rev

Rev 29 | Rev 31 | Go to most recent revision | Details | Compare with Previous | 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)
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
30 rodolico 84
   function DBMenu2String ( $rootDir = '', $toAdd = null, $permissions = array() ) {
29 rodolico 85
      foreach ( $toAdd as $additional ) {
86
         $this->inputRecords[] = $additional;
87
      }
88
      //print "<pre>\n" . print_r( $this, true) . "</pre>"; die;
30 rodolico 89
      return $this->htmlMenu( $this->inputRecords, 0, $rootDir, $permissions );
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.
30 rodolico 94
   private function htmlMenu ($menu, $level=0, $rootDir = '', $permissions = array() ) {
1 rodolico 95
      $result = '';
96
      foreach ($menu as $key => $value) { // process each array entry
30 rodolico 97
         // ignore if there is a permission and it is false
98
         if ( isset( $permissions[$value['shortname']] ) && ! $permissions[$value['shortname']] ) {
99
            continue;
100
         }
1 rodolico 101
         if ($value[$this->urlName]) { // this is a link, so it is a live menu option
102
            $result .= insertValuesIntoQuery( $this->menuItemString, array( 'url' => $rootDir . $value[$this->urlName], 'caption' => $value[$this->captionName], 'level' => $level) );
103
         } else { // not a link, so just create the text
104
            $result .= insertValuesIntoQuery( $this->menuHeaderString, array( 'caption' => $value[$this->captionName], 'level' => $level) );
105
         }
106
         if ( isset($value['children'])) { // if it has children, process them
30 rodolico 107
            $result .=  $this->htmlMenu($value['children'], $level+1, $rootDir, $permissions);
1 rodolico 108
         }
109
      }
29 rodolico 110
      /*
111
      // if they want anything added, do it here.
112
      foreach ( $toAdd as $key => $value ) {
113
         $result .= insertValuesIntoQuery( $this->menuItemString, array( 'url' => $value, 'caption' =>$key ) );
114
      }
115
      */
1 rodolico 116
      // place the block code around the menu, and return the result
117
      return insertValuesIntoQuery( $this->menuBlockString, array( 'menublock' => $result, 'level' => $level ) );
118
   }
119
 
120
} // class DBMenu
121
 
122
/* following block is for testing. comment out for production */
8 rodolico 123
/*
124
$menu = new DBHierarchicalHash ('_menu', '_menu_id');
125
print_r( $menu );
1 rodolico 126
 
8 rodolico 127
$menu = new DBMenu( 'menu', 'menu_id' );
128
print $menu->DBMenu2String();
129
print "Menu Structure:\n"; print_r($menu);
130
*/
1 rodolico 131
/* end of block for testing */
132
 
133
?>