Code

Layer menu cleanup
[gosa.git] / gosa-core / include / utils / layer-menu / lib / phptreemenu.inc.php
1 <?php
2 // PHP Layers Menu 3.2.0-rc (C) 2001-2004 Marco Pratesi - http://www.marcopratesi.it/
4 /**
5 * This file contains the code of the PHPTreeMenu class.
6 * @package PHPLayersMenu
7 */
9 /**
10 * This is the PHPTreeMenu class of the PHP Layers Menu library.
11 *
12 * This class depends on the LayersMenuCommon class.  It provides "server-side" (PHP-based) tree menus, that to do not require JavaScript to work.
13 *
14 * @version 3.2.0-rc
15 * @package PHPLayersMenu
16 */
17 class PHPTreeMenu extends LayersMenuCommon
18 {
20 /**
21 * The character used for the PHP Tree Menu in the query string to separate items ids
22 * @access private
23 * @var string
24 */
25 var $phpTreeMenuSeparator;
26 /**
27 * The default value of the expansion string for the PHP Tree Menu
28 * @access private
29 * @var string
30 */
31 var $phpTreeMenuDefaultExpansion;
32 /**
33 * Type of images used for the Tree Menu
34 * @access private
35 * @var string
36 */
37 var $phpTreeMenuImagesType;
38 /**
39 * Prefix for filenames of images of a theme
40 * @access private
41 * @var string
42 */
43 var $phpTreeMenuTheme;
44 /**
45 * An array where we store the PHP Tree Menu code for each menu
46 * @access private
47 * @var array
48 */
49 var $_phpTreeMenu;
51 /**
52 * The constructor method; it initializates some variables
53 * @return void
54 */
55 function PHPTreeMenu()
56 {
57         $this->LayersMenuCommon();
59         $this->phpTreeMenuSeparator = '|';
60         $this->phpTreeMenuDefaultExpansion = '';
61         $this->phpTreeMenuImagesType = 'png';
62         $this->phpTreeMenuTheme = '';
63         $this->_phpTreeMenu = array();
64 }
66 /**
67 * The method to set the dirroot directory
68 * @access public
69 * @return boolean
70 */
71 function setDirroot($dirroot)
72 {
73         return $this->setDirrootCommon($dirroot);
74 }
76 /**
77 * The method to set the value of separator for the Tree Menu query string
78 * @access public
79 * @return void
80 */
81 function setPHPTreeMenuSeparator($phpTreeMenuSeparator)
82 {
83         $this->phpTreeMenuSeparator = $phpTreeMenuSeparator;
84 }
86 /**
87 * The method to set the default value of the expansion string for the PHP Tree Menu
88 * @access public
89 * @return void
90 */
91 function setPHPTreeMenuDefaultExpansion($phpTreeMenuDefaultExpansion)
92 {
93         $this->phpTreeMenuDefaultExpansion = $phpTreeMenuDefaultExpansion;
94 }
96 /**
97 * The method to set the type of images used for the Tree Menu
98 * @access public
99 * @return void
100 */
101 function setPHPTreeMenuImagesType($phpTreeMenuImagesType)
103         $this->phpTreeMenuImagesType = $phpTreeMenuImagesType;
106 /**
107 * The method to set the prefix for filenames of images of a theme
108 * @access public
109 * @return void
110 */
111 function setPHPTreeMenuTheme($phpTreeMenuTheme)
113         $this->phpTreeMenuTheme = $phpTreeMenuTheme;
116 /**
117 * Method to prepare a new PHP Tree Menu.
119 * This method processes items of a menu and parameters submitted
120 * through GET (i.e. nodes to be expanded) to prepare and return
121 * the corresponding Tree Menu code.
123 * @access public
124 * @param string $menu_name the name of the menu whose items have to be processed
125 * @return string
126 */
127 function newPHPTreeMenu(
128         $menu_name = '' // non consistent default...
129         )
131         $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
132         $this_host = (isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
133         if (isset($_SERVER['SCRIPT_NAME'])) {
134                 $me = $_SERVER['SCRIPT_NAME'];
135         } elseif (isset($_SERVER['REQUEST_URI'])) {
136                 $me = $_SERVER['REQUEST_URI'];
137         } elseif (isset($_SERVER['PHP_SELF'])) {
138                 $me = $_SERVER['PHP_SELF'];
139         } elseif (isset($_SERVER['PATH_INFO'])) {
140                 $me = $_SERVER['PATH_INFO'];
141         }
142         $url = $protocol . $this_host . $me;
143         $query = '';
144         reset($_GET);
145         while (list($key, $value) = each($_GET)) {
146                 if ($key != 'p' && $value != '') {
147                         $query .= '&amp;' . $key . '=' . $value;
148                 }
149         }
150         if ($query != '') {
151                 $query = '?' . substr($query, 5) . '&amp;p=';
152         } else {
153                 $query = '?p=';
154         }
155         $p = (isset($_GET['p'])) ? $_GET['p'] : $this->phpTreeMenuDefaultExpansion;
157 /* ********************************************************* */
158 /* Based on TreeMenu 1.1 by Bjorge Dijkstra (bjorge@gmx.net) */
159 /* ********************************************************* */
160         $this->_phpTreeMenu[$menu_name] = '';
162         $img_collapse                   = $this->imgwww . $this->phpTreeMenuTheme . 'tree_collapse.' . $this->phpTreeMenuImagesType;
163         $alt_collapse                   = '--';
164         $img_collapse_corner            = $this->imgwww . $this->phpTreeMenuTheme . 'tree_collapse_corner.' . $this->phpTreeMenuImagesType;
165         $alt_collapse_corner            = '--';
166         $img_collapse_corner_first      = $this->imgwww . $this->phpTreeMenuTheme . 'tree_collapse_corner_first.' . $this->phpTreeMenuImagesType;
167         $alt_collapse_corner_first      = '--';
168         $img_collapse_first             = $this->imgwww . $this->phpTreeMenuTheme . 'tree_collapse_first.' . $this->phpTreeMenuImagesType;
169         $alt_collapse_first             = '--';
170         $img_corner                     = $this->imgwww . $this->phpTreeMenuTheme . 'tree_corner.' . $this->phpTreeMenuImagesType;
171         $alt_corner                     = '`-';
172         $img_expand                     = $this->imgwww . $this->phpTreeMenuTheme . 'tree_expand.' . $this->phpTreeMenuImagesType;
173         $alt_expand                     = '+-';
174         $img_expand_corner              = $this->imgwww . $this->phpTreeMenuTheme . 'tree_expand_corner.' . $this->phpTreeMenuImagesType;
175         $alt_expand_corner              = '+-';
176         $img_expand_corner_first        = $this->imgwww . $this->phpTreeMenuTheme . 'tree_expand_corner_first.' . $this->phpTreeMenuImagesType;
177         $alt_expand_corner_first        = '+-';
178         $img_expand_first               = $this->imgwww . $this->phpTreeMenuTheme . 'tree_expand_first.' . $this->phpTreeMenuImagesType;
179         $alt_expand_first               = '+-';
180         $img_folder_closed              = $this->imgwww . $this->phpTreeMenuTheme . 'tree_folder_closed.' . $this->phpTreeMenuImagesType;
181         $alt_folder_closed              = '->';
182         $img_folder_open                = $this->imgwww . $this->phpTreeMenuTheme . 'tree_folder_open.' . $this->phpTreeMenuImagesType;
183         $alt_folder_open                = '->';
184         $img_leaf                       = $this->imgwww . $this->phpTreeMenuTheme . 'tree_leaf.' . $this->phpTreeMenuImagesType;
185         $alt_leaf                       = '->';
186         $img_space                      = $this->imgwww . $this->phpTreeMenuTheme . 'tree_space.' . $this->phpTreeMenuImagesType;
187         $alt_space                      = '  ';
188         $img_split                      = $this->imgwww . $this->phpTreeMenuTheme . 'tree_split.' . $this->phpTreeMenuImagesType;
189         $alt_split                      = '|-';
190         $img_split_first                = $this->imgwww . $this->phpTreeMenuTheme . 'tree_split_first.' . $this->phpTreeMenuImagesType;
191         $alt_split_first                = '|-';
192         $img_vertline                   = $this->imgwww . $this->phpTreeMenuTheme . 'tree_vertline.' . $this->phpTreeMenuImagesType;
193         $alt_vertline                   = '| ';
195         for ($i=$this->_firstItem[$menu_name]; $i<=$this->_lastItem[$menu_name]; $i++) {
196                 $expand[$i] = 0;
197                 $visible[$i] = 0;
198                 $this->tree[$i]['last_item'] = 0;
199         }
200         for ($i=0; $i<=$this->_maxLevel[$menu_name]; $i++) {
201                 $levels[$i] = 0;
202         }
204         // Get numbers of nodes to be expanded
205         if ($p != '') {
206                 $explevels = explode($this->phpTreeMenuSeparator, $p);
207                 $explevels_count = count($explevels);
208                 for ($i=0; $i<$explevels_count; $i++) {
209                         $expand[$explevels[$i]] = 1;
210                 }
211         }
213         // Find last nodes of subtrees
214         $last_level = $this->_maxLevel[$menu_name];
215         for ($i=$this->_lastItem[$menu_name]; $i>=$this->_firstItem[$menu_name]; $i--) {
216                 if ($this->tree[$i]['level'] < $last_level) {
217                         for ($j=$this->tree[$i]['level']+1; $j<=$this->_maxLevel[$menu_name]; $j++) {
218                                 $levels[$j] = 0;
219                         }
220                 }
221                 if ($levels[$this->tree[$i]['level']] == 0) {
222                         $levels[$this->tree[$i]['level']] = 1;
223                         $this->tree[$i]['last_item'] = 1;
224                 } else {
225                         $this->tree[$i]['last_item'] = 0;
226                 }
227                 $last_level = $this->tree[$i]['level'];
228         }
230         // Determine visible nodes
231         // all root nodes are always visible
232         for ($i=$this->_firstItem[$menu_name]; $i<=$this->_lastItem[$menu_name]; $i++) {
233                 if ($this->tree[$i]['level'] == 1) {
234                         $visible[$i] = 1;
235                 }
236         }
237         if (isset($explevels)) {
238                 for ($i=0; $i<$explevels_count; $i++) {
239                         $n = $explevels[$i];
240                         if ($n >= $this->_firstItem[$menu_name] && $n <= $this->_lastItem[$menu_name] && $visible[$n] == 1 && $expand[$n] == 1) {
241                                 $j = $n + 1;
242                                 while ($j<=$this->_lastItem[$menu_name] && $this->tree[$j]['level']>$this->tree[$n]['level']) {
243                                         if ($this->tree[$j]['level'] == $this->tree[$n]['level']+1) {
244                                                 $visible[$j] = 1;
245                                         }
246                                         $j++;
247                                 }
248                         }
249                 }
250         }
252         // Output nicely formatted tree
253         for ($i=0; $i<$this->_maxLevel[$menu_name]; $i++) {
254                 $levels[$i] = 1;
255         }
256         $max_visible_level = 0;
257         for ($cnt=$this->_firstItem[$menu_name]; $cnt<=$this->_lastItem[$menu_name]; $cnt++) {
258                 if ($visible[$cnt]) {
259                         $max_visible_level = max($max_visible_level, $this->tree[$cnt]['level']);
260                 }
261         }
262         for ($cnt=$this->_firstItem[$menu_name]; $cnt<=$this->_lastItem[$menu_name]; $cnt++) {
263                 if ($this->tree[$cnt]['text'] == '---') {
264                         continue;       // separators are significant only for layers-based menus
265                 }
267                 if (isset($this->tree[$cnt]['selected']) && $this->tree[$cnt]['selected']) {
268                         $linkstyle = 'phplmselected';
269                 } else {
270                         $linkstyle = 'phplm';
271                 }
273                 if ($visible[$cnt]) {
274                         $this->_phpTreeMenu[$menu_name] .= '<div class="treemenudiv">' . "\n"; 
276                         // vertical lines from higher levels
277                         for ($i=0; $i<$this->tree[$cnt]['level']-1; $i++) {
278                                 if ($levels[$i] == 1) {
279                                         $img = $img_vertline;
280                                         $alt = $alt_vertline;
281                                 } else {
282                                         $img = $img_space;
283                                         $alt = $alt_space;
284                                 }
285                                 $this->_phpTreeMenu[$menu_name] .= '<img align="top" border="0" class="imgs" src="' . $img . '" alt="' . $alt . '" />';
286                         }
288                         $not_a_leaf = $cnt<$this->_lastItem[$menu_name] && $this->tree[$cnt+1]['level']>$this->tree[$cnt]['level'];
290                         if ($not_a_leaf) {
291                                 // Create expand/collapse parameters
292                                 $params = '';
293                                 for ($i=$this->_firstItem[$menu_name]; $i<=$this->_lastItem[$menu_name]; $i++) {
294                                         if ($expand[$i] == 1 && $cnt!= $i || ($expand[$i] == 0 && $cnt == $i)) {
295                                                 $params .= $this->phpTreeMenuSeparator . $i;
296                                         }
297                                 }
298                                 if ($params != '') {
299                                         $params = substr($params, 1);
300                                 }
301                         }
303                         if ($this->tree[$cnt]['last_item'] == 1) {
304                         // corner at end of subtree or t-split
305                                 if ($not_a_leaf) {
306                                         if ($expand[$cnt] == 0) {
307                                                 if ($cnt == $this->_firstItem[$menu_name]) {
308                                                         $img = $img_expand_corner_first;
309                                                         $alt = $alt_expand_corner_first;
310                                                 } else {
311                                                         $img = $img_expand_corner;
312                                                         $alt = $alt_expand_corner;
313                                                 }
314                                         } else {
315                                                 if ($cnt == $this->_firstItem[$menu_name]) {
316                                                         $img = $img_collapse_corner_first;
317                                                         $alt = $alt_collapse_corner_first;
318                                                 } else {
319                                                         $img = $img_collapse_corner;
320                                                         $alt = $alt_collapse_corner;
321                                                 }
322                                         }
323                                         $this->_phpTreeMenu[$menu_name] .= '<a name="' . $cnt . '" class="' . $linkstyle . '" href="' . $url . $query . $params . '#' . $cnt . '"><img align="top" border="0" class="imgs" src="' . $img . '" alt="' . $alt . '" /></a>';
324                                 } else {
325                                         $this->_phpTreeMenu[$menu_name] .= '<img align="top" border="0" class="imgs" src="' . $img_corner . '" alt="' . $alt_corner . '" />';
326                                 }
327                                 $levels[$this->tree[$cnt]['level']-1] = 0;
328                         } else {
329                                 if ($not_a_leaf) {
330                                         if ($expand[$cnt] == 0) {
331                                                 if ($cnt == $this->_firstItem[$menu_name]) {
332                                                         $img = $img_expand_first;
333                                                         $alt = $alt_expand_first;
334                                                 } else {
335                                                         $img = $img_expand;
336                                                         $alt = $alt_expand;
337                                                 }
338                                         } else {
339                                                 if ($cnt == $this->_firstItem[$menu_name]) {
340                                                         $img = $img_collapse_first;
341                                                         $alt = $alt_collapse_first;
342                                                 } else {
343                                                         $img = $img_collapse;
344                                                         $alt = $alt_collapse;
345                                                 }
346                                         }
347                                         $this->_phpTreeMenu[$menu_name] .= '<a name="' . $cnt . '" class="' . $linkstyle . '" href="' . $url . $query . $params . '#' . $cnt . '"><img align="top" border="0" class="imgs" src="' . $img . '" alt="' . $alt . '" /></a>';
348                                 } else {
349                                         if ($cnt == $this->_firstItem[$menu_name]) {
350                                                 $img = $img_split_first;
351                                                 $alt = $alt_split_first;
352                                         } else {
353                                                 $img = $img_split;
354                                                 $alt = $alt_split;
355                                         }
356                                         $this->_phpTreeMenu[$menu_name] .= '<img align="top" border="0" class="imgs" src="' . $img . '" alt="' . $alt . '" />';
357                                 }
358                                 $levels[$this->tree[$cnt]['level']-1] = 1;
359                         }
361                         if ($this->tree[$cnt]['parsed_href'] == '' || $this->tree[$cnt]['parsed_href'] == '#') {
362                                 $a_href_open_img = '';
363                                 $a_href_close_img = '';
364                                 $a_href_open = '<a class="phplmnormal">';
365                                 $a_href_close = '</a>';
366                         } else {
367                                 $a_href_open_img = '<a href="' . $this->tree[$cnt]['parsed_href'] . '"' . $this->tree[$cnt]['parsed_title'] . $this->tree[$cnt]['parsed_target'] . '>';
368                                 $a_href_close_img = '</a>';
369                                 $a_href_open = '<a href="' . $this->tree[$cnt]['parsed_href'] . '"' . $this->tree[$cnt]['parsed_title'] . $this->tree[$cnt]['parsed_target'] . ' class="' . $linkstyle . '">';
370                                 $a_href_close = '</a>';
371                         }
373                         if ($not_a_leaf) {
374                                 if ($expand[$cnt] == 1) {
375                                         $img = $img_folder_open;
376                                         $alt = $alt_folder_open;
377                                 } else {
378                                         $img = $img_folder_closed;
379                                         $alt = $alt_folder_closed;
380                                 }
381                                 $this->_phpTreeMenu[$menu_name] .= $a_href_open_img . '<img align="top" border="0" class="imgs" src="' . $img . '" alt="' . $alt . '" />' . $a_href_close_img;
382                         } else {
383                                 if ($this->tree[$cnt]['parsed_icon'] != '') {
384                                         $this->_phpTreeMenu[$menu_name] .= $a_href_open_img . '<img align="top" border="0" src="' . $this->tree[$cnt]['parsed_icon'] . '" width="' . $this->tree[$cnt]['iconwidth'] . '" height="' . $this->tree[$cnt]['iconheight'] . '" alt="' . $alt_leaf . '" />' . $a_href_close_img;
385                                 } else {
386                                         $this->_phpTreeMenu[$menu_name] .= $a_href_open_img . '<img align="top" border="0" class="imgs" src="' . $img_leaf . '" alt="' . $alt_leaf . '" />' . $a_href_close_img;
387                                 }
388                         }
390                         // output item text
391                         $foobar = $max_visible_level - $this->tree[$cnt]['level'] + 1;
392                         if ($foobar > 1) {
393                                 $colspan = ' colspan="' . $foobar . '"';
394                         } else {
395                                 $colspan = '';
396                         }
397                         $this->_phpTreeMenu[$menu_name] .= '&nbsp;' . $a_href_open . $this->tree[$cnt]['parsed_text'] . $a_href_close . "\n";
398                         $this->_phpTreeMenu[$menu_name] .= '</div>' . "\n";
399                 }
400         }
401 /* ********************************************************* */
403 /*
404         $this->_phpTreeMenu[$menu_name] =
405         '<div class="phplmnormal">' . "\n" .
406         $this->_phpTreeMenu[$menu_name] .
407         '</div>' . "\n";
408 */
409         // Some (old) browsers do not support the "white-space: nowrap;" CSS property...
410         $this->_phpTreeMenu[$menu_name] =
411         '<table cellspacing="0" cellpadding="0" border="0">' . "\n" .
412         '<tr>' . "\n" .
413         '<td class="phplmnormal" nowrap="nowrap">' . "\n" .
414         $this->_phpTreeMenu[$menu_name] .
415         '</td>' . "\n" .
416         '</tr>' . "\n" .
417         '</table>' . "\n";
419         return $this->_phpTreeMenu[$menu_name];
422 /**
423 * Method that returns the code of the requested PHP Tree Menu
424 * @access public
425 * @param string $menu_name the name of the menu whose PHP Tree Menu code
426 *   has to be returned
427 * @return string
428 */
429 function getPHPTreeMenu($menu_name)
431         return $this->_phpTreeMenu[$menu_name];
434 /**
435 * Method that prints the code of the requested PHP Tree Menu
436 * @access public
437 * @param string $menu_name the name of the menu whose PHP Tree Menu code
438 *   has to be printed
439 * @return void
440 */
441 function printPHPTreeMenu($menu_name)
443         print $this->_phpTreeMenu[$menu_name];
446 } /* END OF CLASS */
448 ?>