Subversion Repositories phpLibraryV2

Rev

Rev 31 | 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
 
31 rodolico 92
 
93
   /**
94
    *  function takes a menu level and creates an HTML Menu items from it.
95
    * 
96
    * If a node has children, will recursively call itself for each child node.
97
    * If $permissions is set, any permission (based on shortname) that is set
98
    * to false is not shown. If an item is not in the list, or is set to true,
99
    * the menu item is displayed.
100
    */
30 rodolico 101
   private function htmlMenu ($menu, $level=0, $rootDir = '', $permissions = array() ) {
1 rodolico 102
      $result = '';
103
      foreach ($menu as $key => $value) { // process each array entry
30 rodolico 104
         // ignore if there is a permission and it is false
105
         if ( isset( $permissions[$value['shortname']] ) && ! $permissions[$value['shortname']] ) {
106
            continue;
107
         }
1 rodolico 108
         if ($value[$this->urlName]) { // this is a link, so it is a live menu option
46 rodolico 109
            $result .= $this->dbConnection->insertValuesIntoQuery( $this->menuItemString, array( 'url' => $rootDir . $value[$this->urlName], 'caption' => $value[$this->captionName], 'level' => $level) );
1 rodolico 110
         } else { // not a link, so just create the text
111
            $result .= insertValuesIntoQuery( $this->menuHeaderString, array( 'caption' => $value[$this->captionName], 'level' => $level) );
112
         }
113
         if ( isset($value['children'])) { // if it has children, process them
30 rodolico 114
            $result .=  $this->htmlMenu($value['children'], $level+1, $rootDir, $permissions);
1 rodolico 115
         }
116
      }
29 rodolico 117
      /*
118
      // if they want anything added, do it here.
119
      foreach ( $toAdd as $key => $value ) {
120
         $result .= insertValuesIntoQuery( $this->menuItemString, array( 'url' => $value, 'caption' =>$key ) );
121
      }
122
      */
1 rodolico 123
      // place the block code around the menu, and return the result
46 rodolico 124
      return $this->dbConnection->insertValuesIntoQuery( $this->menuBlockString, array( 'menublock' => $result, 'level' => $level ) );
1 rodolico 125
   }
126
 
127
} // class DBMenu
128
 
129
/* following block is for testing. comment out for production */
8 rodolico 130
/*
131
$menu = new DBHierarchicalHash ('_menu', '_menu_id');
132
print_r( $menu );
1 rodolico 133
 
8 rodolico 134
$menu = new DBMenu( 'menu', 'menu_id' );
135
print $menu->DBMenu2String();
136
print "Menu Structure:\n"; print_r($menu);
137
*/
1 rodolico 138
/* end of block for testing */
139
 
140
?>