Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        module
5
 * @package         show_menu2
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2011, Website Baker Org. e.V.
9
 * @link			http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.7.0 | 2.8.x
12
 * @requirements    PHP 5.2.2 and higher
13
 * @version         $Id: include.php 1408 2011-01-23 00:43:45Z DarkViper $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/modules/show_menu2/include.php $
15
 * @lastmodified    $Date: 2011-01-23 01:43:45 +0100 (Sun, 23 Jan 2011) $
16
 *
17
 */
18

    
19
define('SM2_ROOT',          -1000);
20
define('SM2_CURR',          -2000);
21
define('SM2_ALLMENU',          -1);
22
define('SM2_START',          1000);
23
define('SM2_MAX',            2000);
24
define('SM2_ALL',          0x0001); // bit 0 (group 1) (Note: also used for max level!)
25
define('SM2_TRIM',         0x0002); // bit 1 (group 1)
26
define('SM2_CRUMB',        0x0004); // bit 2 (group 1)
27
define('SM2_SIBLING',      0x0008); // bit 3 (group 1)
28
define('SM2_NUMCLASS',     0x0010); // bit 4
29
define('SM2_ALLINFO',      0x0020); // bit 5
30
define('SM2_NOCACHE',      0x0040); // bit 6
31
define('SM2_PRETTY',       0x0080); // bit 7
32
define('SM2_ESCAPE',       0x0100); // bit 8
33
define('SM2_NOESCAPE',          0); // NOOP, unnecessary with WB 2.6.7+
34
define('SM2_BUFFER',       0x0200); // bit 9
35
define('SM2_CURRTREE',     0x0400); // bit 10
36
define('SM2_SHOWHIDDEN',   0x0800); // bit 11
37
define('SM2_XHTML_STRICT', 0x1000); // bit 12
38
define('SM2_NO_TITLE',     0x1001); // bit 13
39

    
40
define('_SM2_GROUP_1',  0x000F); // exactly one flag from group 1 is required
41

    
42

    
43
// Implement support for page_menu and show_menu using show_menu2. If you remove
44
// the comments characters from the beginning of the following include, all menu
45
// functions in Website Baker will be implemented using show_menu2. While it is
46
// commented out, the original WB functions will be used.
47
//include('legacy.php');
48

    
49
// This class is the default menu formatter for sm2. If desired, you can 
50
// create your own formatter class and pass the object into show_menu2 
51
// as $aItemFormat.
52
define('SM2_CONDITIONAL','if\s*\(([^\)]+)\)\s*{([^}]*)}\s*(?:else\s*{([^}]*)}\s*)?');
53
define('SM2_COND_TERM','\s*(\w+)\s*(<|<=|==|=|=>|>|!=)\s*([\w\-]+)\s*');
54
class SM2_Formatter
55
{
56
    var $output;
57
    var $flags;
58
    var $itemOpen;
59
    var $itemClose;
60
    var $menuOpen;
61
    var $menuClose;
62
    var $topItemOpen;
63
    var $topMenuOpen;
64
    
65
    var $isFirst;
66
    var $page;
67
    var $url;
68
    var $currSib;
69
    var $sibCount;
70
    var $currClass;
71
    var $prettyLevel;
72

    
73
    // output the data
74
    function output($aString) {
75
        if ($this->flags & SM2_BUFFER) {
76
            $this->output .= $aString;
77
        }
78
        else {
79
            echo $aString;
80
        }
81
    }
82
    
83
    // set the default values for all of our formatting items
84
    function set($aFlags, $aItemOpen, $aItemClose, $aMenuOpen, $aMenuClose, $aTopItemOpen, $aTopMenuOpen) {
85
        $this->flags        = $aFlags;
86
        $this->itemOpen     = is_string($aItemOpen)    ? $aItemOpen    : '[li][a][menu_title]</a>';
87
        $this->itemClose    = is_string($aItemClose)   ? $aItemClose   : '</li>';
88
        $this->menuOpen     = is_string($aMenuOpen)    ? $aMenuOpen    : '[ul]';
89
        $this->menuClose    = is_string($aMenuClose)   ? $aMenuClose   : '</ul>';
90
        $this->topItemOpen  = is_string($aTopItemOpen) ? $aTopItemOpen : $this->itemOpen;
91
        $this->topMenuOpen  = is_string($aTopMenuOpen) ? $aTopMenuOpen : $this->menuOpen;
92
    }
93

    
94
    // initialize the state of the formatter before anything is output
95
    function initialize() {
96
        $this->output = '';
97
        $this->prettyLevel = 0;
98
        if ($this->flags & SM2_PRETTY) {
99
            $this->output("\n<!-- show_menu2 -->");
100
        }
101
    }
102

    
103
    // start a menu     
104
    function startList(&$aPage, &$aUrl) {
105
        $currClass = '';
106
        $currItem = $this->menuOpen;
107
        
108
        // use the top level menu open if this is the first menu
109
        if ($this->topMenuOpen) {
110
            $currItem = $this->topMenuOpen;
111
            $currClass .= ' menu-top';
112
            $this->topMenuOpen = false;
113
        }
114
        
115
        // add the numbered menu class only if requested
116
        if (($this->flags & SM2_NUMCLASS) == SM2_NUMCLASS) {
117
            $currClass .= ' menu-'.$aPage['level'];
118
        }
119
        
120
        $this->prettyLevel += 1;
121
        
122
        // replace all keywords in the output
123
        if ($this->flags & SM2_PRETTY) {
124
            $this->output("\n".str_repeat(' ',$this->prettyLevel).
125
                $this->format($aPage, $aUrl, $currItem, $currClass));
126
        }
127
        else {
128
            $this->output($this->format($aPage, $aUrl, $currItem, $currClass));
129
        }
130
        
131
        $this->prettyLevel += 3;
132
    }
133
    
134
    // start an item within the menu
135
    function startItem(&$aPage, &$aUrl, $aCurrSib, $aSibCount) {
136
        // generate our class list
137
        $currClass = '';
138
        if (($this->flags & SM2_NUMCLASS) == SM2_NUMCLASS) {
139
            $currClass .= ' menu-'.$aPage['level'];
140
        }
141
        if (array_key_exists('sm2_has_child', $aPage)) {
142
            // not set if false, so existence = true
143
            $currClass .= ' menu-expand';
144
        }
145
        if (array_key_exists('sm2_is_curr', $aPage)) { 
146
            $currClass .= ' menu-current';
147
        }
148
        elseif (array_key_exists('sm2_is_parent', $aPage)) { 
149
            // not set if false, so existence = true
150
            $currClass .= ' menu-parent';
151
        }
152
        elseif (array_key_exists('sm2_is_sibling', $aPage)) {
153
            // not set if false, so existence = true
154
            $currClass .= ' menu-sibling';
155
        }
156
        elseif (array_key_exists('sm2_child_level',$aPage)) {
157
            // not set if not a child
158
            $currClass .= ' menu-child';
159
            if (($this->flags & SM2_NUMCLASS) == SM2_NUMCLASS) {
160
                $currClass .= ' menu-child-'.($aPage['sm2_child_level']-1);
161
            }
162
        }
163
        if ($aCurrSib == 1) {
164
            $currClass .= ' menu-first';
165
        }
166
        if ($aCurrSib == $aSibCount) {
167
            $currClass .= ' menu-last';
168
        }
169

    
170
        // use the top level item if this is the first item
171
        $currItem = $this->itemOpen;
172
        if ($this->topItemOpen) {
173
            $currItem = $this->topItemOpen;
174
            $this->topItemOpen = false;
175
        }
176

    
177
        // replace all keywords in the output
178
        if ($this->flags & SM2_PRETTY) {
179
            $this->output("\n".str_repeat(' ',$this->prettyLevel));
180
        }
181
        $this->output($this->format($aPage, $aUrl, $currItem, $currClass, $aCurrSib, $aSibCount));
182
    }
183
    
184
    // find and replace all keywords, setting the state variables first
185
    function format(&$aPage, &$aUrl, &$aCurrItem, &$aCurrClass, 
186
        $aCurrSib = 0, $aSibCount = 0) 
187
    {
188
        $this->page      = &$aPage;
189
        $this->url       = &$aUrl;
190
        $this->currClass = trim($aCurrClass);
191
        $this->currSib   = $aCurrSib;
192
        $this->sibCount  = $aSibCount;
193
        
194
        $item = $this->format2($aCurrItem);
195
        
196
        unset($this->page);
197
        unset($this->url);
198
        unset($this->currClass);
199
        
200
        return $item;
201
    }
202
    
203
    // find and replace all keywords
204
    function format2(&$aCurrItem) {
205
        if (!is_string($aCurrItem)) return '';
206
        return preg_replace(
207
            '@\[('.
208
                'a|ac|/a|li|/li|ul|/ul|menu_title|menu_icon_0|menu_icon_1|'.
209
				'page_title|page_icon|url|target|page_id|tooltip|'.
210
                'parent|level|sib|sibCount|class|description|keywords|'.
211
                SM2_CONDITIONAL.
212
            ')\]@e', 
213
            '$this->replace("\1")', $aCurrItem);
214
    }
215
    
216
    // replace the keywords
217
    function replace($aMatch) {
218
        $retval = '['.$aMatch.'=UNKNOWN]';
219
        switch ($aMatch) {
220
        case 'a':
221
            $retval = '<a href="'.$this->url.'"';
222
			// break; // ignore 'break' to add the rest of <a>-tag
223
		case 'ac':
224
			if( substr($retval, 0, 2) != '<a'){
225
				$retval = '<a href="'.$this->url.'" class="'.$this->currClass.'"';
226
			}
227
			if(($this->flags & SM2_NO_TITLE)) {
228
				$retval .= ' title="'.$this->page['tooltip'].'"';
229
			}
230
			if(!($this->flags & SM2_XHTML_STRICT)) {
231
				$retval .= ' target="'.$this->page['target'].'"';
232
			}
233
			$retval .= '>';
234
			break;
235
        case '/a':
236
            $retval = '</a>'; break;
237
        case 'li':
238
            $retval = '<li class="'.$this->currClass.'">'; break;
239
        case '/li':
240
            $retval = '</li>'; break;
241
        case 'ul':
242
            $retval = '<ul class="'.$this->currClass.'">'; break;
243
        case '/ul':
244
            $retval = '</ul>'; break;
245
        case 'url':
246
            $retval = $this->url; break;
247
        case 'sib':
248
            $retval = $this->currSib; break;
249
        case 'sibCount':
250
            $retval = $this->sibCount; break;
251
        case 'class':
252
            $retval = $this->currClass; break;
253
        default:
254
            if (array_key_exists($aMatch, $this->page)) {
255
                if ($this->flags & SM2_ESCAPE) {
256
                    $retval = htmlspecialchars($this->page[$aMatch], ENT_QUOTES);
257
                }
258
                else {
259
                    $retval = $this->page[$aMatch];
260
                }
261
            }
262
            if (preg_match('/'.SM2_CONDITIONAL.'/', $aMatch, $rgMatches)) {
263
                $retval = $this->replaceIf($rgMatches[1], $rgMatches[2], $rgMatches[3]);
264
            }
265
        }
266
        return $retval;
267
    }
268
    
269
    // conditional replacement
270
    function replaceIf(&$aExpression, &$aIfValue, &$aElseValue) {
271
        // evaluate all of the tests in the conditional (we don't do short-circuit
272
        // evaluation) and replace the string test with the boolean result
273
        $rgTests = preg_split('/(\|\||\&\&)/', $aExpression, -1, PREG_SPLIT_DELIM_CAPTURE);
274
        for ($n = 0; $n < count($rgTests); $n += 2) {
275
            if (preg_match('/'.SM2_COND_TERM.'/', $rgTests[$n], $rgMatches)) {
276
                $rgTests[$n] = $this->ifTest($rgMatches[1], $rgMatches[2], $rgMatches[3]);
277
            }
278
            else {
279
                @error_logs("show_menu2 error: conditional expression is invalid!");
280
                $rgTests[$n] = false;
281
            }
282
        }
283

    
284
        // combine all test results for a final result
285
        $ok = $rgTests[0];
286
        for ($n = 1; $n+1 < count($rgTests); $n += 2) {
287
            if ($rgTests[$n] == '||') {
288
                $ok = $ok || $rgTests[$n+1];
289
            }
290
            else {
291
                $ok = $ok && $rgTests[$n+1];
292
            }
293
        }
294
        
295
        // return the formatted expression if the test succeeded
296
        return $ok ? $this->format2($aIfValue) : $this->format2($aElseValue);
297
    }
298

    
299
    // conditional test
300
    function ifTest(&$aKey, &$aOperator, &$aValue) {
301
        global $wb;
302
        
303
        // find the correct operand
304
        $operand = false;
305
        switch($aKey) {
306
        case 'class':
307
            // we need to wrap the class names in spaces so we can test for a unique
308
            // class name that will not match prefixes or suffixes. Same must be done
309
            // for the value we are testing.
310
            $operand = " $this->currClass "; 
311
            break;
312
		case 'target':
313
			$operand = $this->page['target'];
314
			break;
315
        case 'sib':
316
            $operand = $this->currSib;
317
            if ($aValue == 'sibCount') {
318
                $aValue = $this->sibCount;
319
            }
320
            break;
321
        case 'sibCount':
322
            $operand = $this->sibCount;
323
            break;
324
        case 'level':
325
            $operand = $this->page['level'];
326
            switch ($aValue) {
327
            case 'root':    $aValue = 0; break;
328
            case 'granny':  $aValue = $wb->page['level']-2; break;
329
            case 'parent':  $aValue = $wb->page['level']-1; break;
330
            case 'current': $aValue = $wb->page['level'];   break;
331
            case 'child':   $aValue = $wb->page['level']+1; break;
332
            }
333
            if ($aValue < 0) $aValue = 0;
334
            break;
335
        case 'id':
336
            $operand = $this->page['page_id'];
337
            switch ($aValue) {
338
            case 'parent':  $aValue = $wb->page['parent'];  break;
339
            case 'current': $aValue = $wb->page['page_id']; break;
340
            }
341
            break;
342
        default:
343
            return '';
344
        }
345

    
346
        // do the test        
347
        $ok = false;
348
        switch ($aOperator) { 
349
        case '<':
350
            $ok = ($operand < $aValue); 
351
            break;
352
        case '<=':
353
            $ok = ($operand <= $aValue); 
354
            break;
355
        case '=':
356
        case '==':
357
        case '!=':
358
            if ($aKey == 'class') {
359
                $ok = strstr($operand, " $aValue ") !== FALSE;
360
            }
361
            else {
362
                $ok = ($operand == $aValue); 
363
            }
364
            if ($aOperator == '!=') {
365
                $ok = !$ok;
366
            }
367
            break;
368
        case '>=':
369
            $ok = ($operand >= $aValue); 
370
        case '>':
371
            $ok = ($operand > $aValue); 
372
        }
373
        
374
        return $ok;
375
    }
376
    
377
    // finish the current menu item
378
    function finishItem() {
379
        if ($this->flags & SM2_PRETTY) {
380
            $this->output(str_repeat(' ',$this->prettyLevel).$this->itemClose);
381
        }
382
        else {
383
            $this->output($this->itemClose);
384
        }
385
    }
386

    
387
    // finish the current menu
388
    function finishList() {
389
        $this->prettyLevel -= 3;
390
        
391
        if ($this->flags & SM2_PRETTY) {
392
            $this->output("\n".str_repeat(' ',$this->prettyLevel).$this->menuClose."\n");
393
        }
394
        else {
395
            $this->output($this->menuClose);
396
        }
397
        
398
        $this->prettyLevel -= 1;
399
    }
400
    
401
    // cleanup the state of the formatter after everything has been output
402
    function finalize() {
403
        if ($this->flags & SM2_PRETTY) {
404
            $this->output("\n");
405
        }
406
    }
407

    
408
    // return the output
409
    function getOutput() {
410
        return $this->output;
411
    }
412
};
413

    
414
function error_logs($error_str)
415
{
416
                $log_error = true;
417
                if ( ! function_exists('error_log') )
418
                        $log_error = false;
419

    
420
                $log_file = @ini_get('error_log');
421
                if ( !empty($log_file) && ('syslog' != $log_file) && !@is_writable($log_file) )
422
                        $log_error = false;
423

    
424
                if ( $log_error )
425
                        @error_log($error_str, 0);
426
}
427

    
428
function show_menu2(
429
    $aMenu          = 0,
430
    $aStart         = SM2_ROOT,
431
    $aMaxLevel      = -1999, // SM2_CURR+1
432
    $aOptions       = SM2_TRIM,
433
    $aItemOpen      = false,
434
    $aItemClose     = false,
435
    $aMenuOpen      = false,
436
    $aMenuClose     = false,
437
    $aTopItemOpen   = false,
438
    $aTopMenuOpen   = false
439
    )
440
{
441
    global $wb;
442

    
443
    // extract the flags and set $aOptions to an array
444
    $flags = 0;
445
    if (is_int($aOptions)) {
446
        $flags = $aOptions;
447
        $aOptions = array();
448
    }
449
    else if (isset($aOptions['flags'])) {
450
        $flags = $aOptions['flags'];
451
    }
452
    else {
453
        $flags = SM2_TRIM;
454
        $aOptions = array();
455
        @error_logs('show_menu2 error: $aOptions is invalid. No flags supplied!');
456
    }
457
    
458
    // ensure we have our group 1 flag, we don't check for the "exactly 1" part, but
459
    // we do ensure that they provide at least one.
460
    if (0 == ($flags & _SM2_GROUP_1)) {
461
        @error_logs('show_menu2 error: $aOptions is invalid. No flags from group 1 supplied!');
462
        $flags |= SM2_TRIM; // default to TRIM
463
    }
464
    
465
    // search page results don't have any of the page data loaded by WB, so we load it 
466
    // ourselves using the referrer ID as the current page
467
    $CURR_PAGE_ID = defined('REFERRER_ID') ? REFERRER_ID : PAGE_ID;
468
    if (count($wb->page) == 0 && defined('REFERRER_ID') && REFERRER_ID > 0) {
469
        global $database;
470
        $sql = 'SELECT * FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.REFERRER_ID.'';
471
        $result = $database->query($sql);
472
        if ($result->numRows() == 1) {
473
            $wb->page = $result->fetchRow();
474
        }
475
        unset($result);
476
    }
477
    
478
    // fix up the menu number to default to the menu number
479
    // of the current page if no menu has been supplied
480
    if ($aMenu == 0) {
481
        $aMenu = $wb->page['menu'] == '' ? 1 : $wb->page['menu'];
482
    } 
483

    
484
    // Set some of the $wb->page[] settings to defaults if not set
485
    $pageLevel  = $wb->page['level']  == '' ? 0 : $wb->page['level'];
486
    $pageParent = $wb->page['parent'] == '' ? 0 : $wb->page['parent'];
487
    
488
    // adjust the start level and start page ID as necessary to
489
    // handle the special values that can be passed in as $aStart
490
    $aStartLevel = 0;
491
    if ($aStart < SM2_ROOT) {   // SM2_CURR+N
492
        if ($aStart == SM2_CURR) {
493
            $aStartLevel = $pageLevel;
494
            $aStart = $pageParent;
495
        }
496
        else {
497
            $aStartLevel = $pageLevel + $aStart - SM2_CURR;
498
            $aStart = $CURR_PAGE_ID; 
499
        }
500
    }
501
    elseif ($aStart < 0) {   // SM2_ROOT+N
502
        $aStartLevel = $aStart - SM2_ROOT;
503
        $aStart = 0;
504
    }
505

    
506
    // we get the menu data once and store it in a global variable. This allows 
507
    // multiple calls to show_menu2 in a single page with only a single call to 
508
    // the database. If this variable exists, then we have already retrieved all
509
    // of the information and processed it, so we don't need to do it again.
510
    if (($flags & SM2_NOCACHE) != 0
511
        || !array_key_exists('show_menu2_data', $GLOBALS)
512
        || !array_key_exists($aMenu, $GLOBALS['show_menu2_data'])) 
513
    {
514
        global $database;
515

    
516
        // create an array of all parents of the current page. As the page_trail
517
        // doesn't include the theoretical root element 0, we add it ourselves.
518
        $rgCurrParents = explode(",", '0,'.$wb->page['page_trail']);
519
        array_pop($rgCurrParents); // remove the current page
520
        $rgParent = array();
521

    
522
        // if the caller wants all menus gathered together (e.g. for a sitemap)
523
        // then we don't limit our SQL query
524
        $menuLimitSql = ' AND `menu`='.$aMenu;
525
        if ($aMenu == SM2_ALLMENU) {
526
            $menuLimitSql = '';
527
        }
528

    
529
        // we only load the description and keywords if we have been told to,
530
        // this cuts the memory load for pages that don't use them. Note that if
531
        // we haven't been told to load these fields the *FIRST TIME* show_menu2
532
        // is called (i.e. where the database is loaded) then the info won't
533
        // exist anyhow.
534
        $fields  = '`parent`,`page_id`,`menu_title`,`page_title`,`link`,`target`,';
535
		$fields .= '`level`,`visibility`,`viewing_groups`';
536
        if (version_compare(WB_VERSION, '2.7', '>=')) { // WB 2.7+
537
            $fields .= ',`viewing_users`';
538
        }
539
		if(version_compare(WB_VERSION, '2.9.0', '>=')) {
540
            $fields .= ',`menu_icon_0`,`menu_icon_1`,`page_icon`,`tooltip`';
541
		}
542
        if ($flags & SM2_ALLINFO) {
543
            $fields = '*';
544
        }
545

    
546
        // we request all matching rows from the database for the menu that we
547
        // are about to create it is cheaper for us to get everything we need
548
        // from the database once and create the menu from memory then make 
549
        // multiple calls to the database. 
550
        $sql  = 'SELECT '.$fields.' FROM `'.TABLE_PREFIX.'pages` ';
551
		$sql .= 'WHERE '.$wb->extra_where_sql.' '.$menuLimitSql.' ';
552
		$sql .= 'ORDER BY `level` ASC, `position` ASC';
553
        $sql = str_replace('hidden', 'IGNOREME', $sql); // we want the hidden pages
554
        $oRowset = $database->query($sql);
555
        if (is_object($oRowset) && $oRowset->numRows() > 0) {
556
            // create an in memory array of the database data based on the item's parent. 
557
            // The array stores all elements in the correct display order.
558
            while ($page = $oRowset->fetchRow()) {
559
                // ignore all pages that the current user is not permitted to view
560
                if(version_compare(WB_VERSION, '2.7', '>=')) { // WB >= 2.7
561
                    // 1. hidden pages aren't shown unless they are on the current page
562
                    if ($page['visibility'] == 'hidden') {
563
                        $page['sm2_hide'] = true;
564
                    }
565
                    
566
                    // 2. all pages with no active sections (unless it is the top page) are ignored
567
                    else if (!$wb->page_is_active($page) && $page['link'] != $wb->default_link && !INTRO_PAGE) {
568
                        continue;
569
                    }
570

    
571
                    // 3. all pages not visible to this user (unless always visible to registered users) are ignored
572
                    else if (!$wb->page_is_visible($page) && $page['visibility'] != 'registered') {
573
                        continue;
574
                    }
575
                }
576
				if(!isset($page['tooltip'])) { $page['tooltip'] = $page['page_title']; }
577
                // ensure that we have an array entry in the table to add this to
578
                $idx = $page['parent'];
579
                if (!array_key_exists($idx, $rgParent)) {
580
                    $rgParent[$idx] = array();
581
                }
582

    
583
                // mark our current page as being on the current path
584
                if ($page['page_id'] == $CURR_PAGE_ID) {
585
                    $page['sm2_is_curr'] = true;
586
                    $page['sm2_on_curr_path'] = true;
587
                    if ($flags & SM2_SHOWHIDDEN) 
588
					{ 
589
                        // show hidden pages if active and SHOWHIDDEN flag supplied
590
                        unset($page['sm2_hide']); 
591
                    }
592
                }
593

    
594
                // mark parents of the current page as such
595
                if (in_array($page['page_id'], $rgCurrParents)) {
596
                    $page['sm2_is_parent'] = true;
597
                    $page['sm2_on_curr_path'] = true;
598
                    if ($flags & SM2_SHOWHIDDEN) 
599
					{
600
                        // show hidden pages if active and SHOWHIDDEN flag supplied
601
						unset($page['sm2_hide']); // don't hide a parent page                
602
                    }
603
                }
604
                
605
                // add the entry to the array                
606
                $rgParent[$idx][] = $page;
607
            }
608
        }    
609
        unset($oRowset);
610

    
611
        // mark all elements that are siblings of any element on the current path
612
        foreach ($rgCurrParents as $x) {
613
            if (array_key_exists($x, $rgParent)) {
614
                foreach (array_keys($rgParent[$x]) as $y) {
615
                    $mark =& $rgParent[$x][$y];
616
                    $mark['sm2_path_sibling'] = true;
617
                    unset($mark);
618
                }
619
            }
620
        }
621

    
622
        // mark all elements that have children and are siblings of the current page
623
        $parentId = $pageParent;
624
        foreach (array_keys($rgParent) as $x) {
625
            $childSet =& $rgParent[$x];
626
            foreach (array_keys($childSet) as $y) {
627
                $mark =& $childSet[$y];
628
                if (array_key_exists($mark['page_id'], $rgParent)) {
629
                    $mark['sm2_has_child'] = true;
630
                }
631
                if ($mark['parent'] == $parentId && $mark['page_id'] != $CURR_PAGE_ID) {
632
                    $mark['sm2_is_sibling'] = true;
633
                }
634
                unset($mark);
635
            }
636
            unset($childSet);
637
        }
638
        
639
        // mark all children of the current page. We don't do this when 
640
        // $CURR_PAGE_ID is 0, as 0 is the parent of everything. 
641
        // $CURR_PAGE_ID == 0 occurs on special pages like search results
642
        // when no referrer is available.s
643
        if ($CURR_PAGE_ID != 0) {
644
            sm2_mark_children($rgParent, $CURR_PAGE_ID, 1);
645
        }
646
        
647
        // store the complete processed menu data as a global. We don't 
648
        // need to read this from the database anymore regardless of how 
649
        // many menus are displayed on the same page.
650
        if (!array_key_exists('show_menu2_data', $GLOBALS)) {
651
            $GLOBALS['show_menu2_data'] = array();
652
        }
653
        $GLOBALS['show_menu2_data'][$aMenu] =& $rgParent;
654
        unset($rgParent);
655
    }
656

    
657
    // adjust $aMaxLevel to the level number of the final level that 
658
    // will be displayed. That is, we display all levels <= aMaxLevel.
659
    if ($aMaxLevel == SM2_ALL) {
660
        $aMaxLevel = 1000;
661
    }
662
    elseif ($aMaxLevel < 0) {   // SM2_CURR+N
663
        $aMaxLevel += $pageLevel - SM2_CURR;
664
    }
665
    elseif ($aMaxLevel >= SM2_MAX) { // SM2_MAX+N
666
        $aMaxLevel += $aStartLevel - SM2_MAX;
667
        if ($aMaxLevel > $pageLevel) {
668
            $aMaxLevel = $pageLevel;
669
        }
670
    }
671
    else {  // SM2_START+N
672
        $aMaxLevel += $aStartLevel - SM2_START;
673
    }
674

    
675
    // generate the menu
676
    $retval = false;
677
    if (array_key_exists($aStart, $GLOBALS['show_menu2_data'][$aMenu])) {
678
        $formatter = $aItemOpen;
679
        if (!is_object($aItemOpen)) {
680
            static $sm2formatter;
681
            if (!isset($sm2formatter)) {
682
                $sm2formatter = new SM2_Formatter;
683
            }
684
            $formatter = $sm2formatter;
685
            $formatter->set($flags, $aItemOpen, $aItemClose, 
686
                $aMenuOpen, $aMenuClose, $aTopItemOpen, $aTopMenuOpen);
687
        }
688
        
689
        // adjust the level until we show everything and ignore the SM2_TRIM flag.
690
        // Usually this will be less than the start level to disable it.
691
        $showAllLevel = $aStartLevel - 1;
692
        if (isset($aOptions['notrim'])) {
693
            $showAllLevel = $aStartLevel + $aOptions['notrim'];
694
        }
695
        
696
        // display the menu
697
        $formatter->initialize();
698
        sm2_recurse(
699
            $GLOBALS['show_menu2_data'][$aMenu],
700
            $aStart,    // parent id to start displaying sub-menus
701
            $aStartLevel, $showAllLevel, $aMaxLevel, $flags, 
702
            $formatter);
703
        $formatter->finalize();
704
        
705
        // if we are returning something, get the data
706
        if (($flags & SM2_BUFFER) != 0) {
707
            $retval = $formatter->getOutput();
708
        }
709
    }
710

    
711
    // clear the data if we aren't caching it
712
    if (($flags & SM2_NOCACHE) != 0) {
713
        unset($GLOBALS['show_menu2_data'][$aMenu]);
714
    }
715
    
716
    return $retval;
717
}
718

    
719
function sm2_mark_children(&$rgParent, $aStart, $aChildLevel)
720
{
721
    if (array_key_exists($aStart, $rgParent)) {
722
        foreach (array_keys($rgParent[$aStart]) as $y) {
723
            $mark =& $rgParent[$aStart][$y];
724
            $mark['sm2_child_level'] = $aChildLevel;
725
            $mark['sm2_on_curr_path'] = true;
726
            sm2_mark_children($rgParent, $mark['page_id'], $aChildLevel+1);
727
        }
728
    }
729
}
730

    
731
function sm2_recurse(
732
    &$rgParent, $aStart, 
733
    $aStartLevel, $aShowAllLevel, $aMaxLevel, $aFlags, 
734
    &$aFormatter
735
    )
736
{
737
    global $wb;
738

    
739
    // on entry to this function we know that there are entries for this 
740
    // parent and all entries for that parent are being displayed. We also 
741
    // need to check if any of the children need to be displayed too.
742
    $isListOpen = false;
743
    $currentLevel = $wb->page['level'] == '' ? 0 : $wb->page['level'];
744

    
745
    // get the number of siblings skipping the hidden pages so we can pass 
746
    // this in and check if the item is first or last
747
    $sibCount = 0;
748
    foreach ($rgParent[$aStart] as $page) {
749
        if (!array_key_exists('sm2_hide', $page)) $sibCount++;
750
    }
751
    
752
    $currSib = 0;
753
    foreach ($rgParent[$aStart] as $page) {
754
        // skip all hidden pages 
755
        if (array_key_exists('sm2_hide', $page)) { // not set if false, so existence = true
756
            continue;
757
        }
758
        
759
        $currSib++;
760

    
761
        // skip any elements that are lower than the maximum level
762
        $pageLevel = $page['level'];
763
        if ($pageLevel > $aMaxLevel) {
764
            continue;
765
        }
766
        
767
        // this affects ONLY the top level
768
        if ($aStart == 0 && ($aFlags & SM2_CURRTREE)) {
769
            if (!array_key_exists('sm2_on_curr_path', $page)) { // not set if false, so existence = true
770
                continue;
771
            }
772
            $sibCount = 1;
773
        }
774
        
775
        // trim the tree as appropriate
776
        if ($aFlags & SM2_SIBLING) {
777
            // parents, and siblings and children of current only
778
            if (!array_key_exists('sm2_on_curr_path', $page)      // not set if false, so existence = true
779
                && !array_key_exists('sm2_is_sibling', $page)     // not set if false, so existence = true
780
                && !array_key_exists('sm2_child_level', $page)) { // not set if false, so existence = true
781
                continue;
782
            }
783
        }
784
        else if ($aFlags & SM2_TRIM) {
785
            // parents and siblings of parents
786
            if ($pageLevel > $aShowAllLevel  // permit all levels to be shown
787
                && !array_key_exists('sm2_on_curr_path', $page)    // not set if false, so existence = true
788
                && !array_key_exists('sm2_path_sibling', $page)) {  // not set if false, so existence = true
789
                continue;
790
            }
791
        }
792
        elseif ($aFlags & SM2_CRUMB) {
793
            // parents only
794
            if (!array_key_exists('sm2_on_curr_path', $page)    // not set if false, so existence = true
795
                || array_key_exists('sm2_child_level', $page)) {  // not set if false, so existence = true
796
                continue;
797
            }
798
        }
799

    
800
        // depth first traverse
801
        $nextParent = $page['page_id'];
802

    
803
        // display the current element if we have reached the start level
804
        if ($pageLevel >= $aStartLevel) {
805
            // massage the link into the correct form
806
            if(!INTRO_PAGE && $page['link'] == $wb->default_link) {
807
                $url = WB_URL;
808
            }
809
            else {
810
                $url = $wb->page_link($page['link']);
811
            }
812
                    
813
            // we open the list only when we absolutely need to
814
            if (!$isListOpen) {
815
                $aFormatter->startList($page, $url);
816
                $isListOpen = true;
817
            }
818

    
819
            $aFormatter->startItem($page, $url, $currSib, $sibCount);
820
        }
821
        
822
        // display children as appropriate
823
        if ($pageLevel + 1 <= $aMaxLevel 
824
            && array_key_exists('sm2_has_child', $page)) {  // not set if false, so existence = true
825
            sm2_recurse(
826
                $rgParent, $nextParent, // parent id to start displaying sub-menus
827
                $aStartLevel, $aShowAllLevel, $aMaxLevel, $aFlags, 
828
                $aFormatter);
829
        }
830
        
831
        // close the current element if appropriate
832
        if ($pageLevel >= $aStartLevel) {
833
            $aFormatter->finishItem($pageLevel, $page);
834
        }
835
    }
836

    
837
    // close the list if we opened one
838
    if ($isListOpen) {
839
        $aFormatter->finishList();
840
    }
841
}
842

    
843
?>
(4-4/10)