Project

General

Profile

1 827 doc
<?php
2 860 Ruebenwurz
3
// $Id$
4
5 827 doc
/*
6
    show_menu2: show_menu replacement for Website Baker
7 915 Ruebenwurz
    Copyright (C) 2006-2009, Brodie Thiesfield
8 827 doc
9
    This program is free software; you can redistribute it and/or
10
    modify it under the terms of the GNU General Public License
11
    as published by the Free Software Foundation; either version 2
12
    of the License, or (at your option) any later version.
13
14
    This program is distributed in the hope that it will be useful,
15
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
    GNU General Public License for more details.
18
19
    You should have received a copy of the GNU General Public License
20
    along with this program; if not, write to the Free Software
21
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
    02110-1301, USA.
23
24
    ***********************************************
25 971 Ruebenwurz
    ** Version 4.8: see README for documentation **
26 827 doc
    ***********************************************
27
*/
28
29
define('SM2_ROOT',       -1000);
30
define('SM2_CURR',       -2000);
31
define('SM2_ALLMENU',       -1);
32
define('SM2_START',       1000);
33
define('SM2_MAX',         2000);
34
define('SM2_ALL',       0x0001); // bit 0 (group 1) (Note: also used for max level!)
35
define('SM2_TRIM',      0x0002); // bit 1 (group 1)
36
define('SM2_CRUMB',     0x0004); // bit 2 (group 1)
37
define('SM2_SIBLING',   0x0008); // bit 3 (group 1)
38
define('SM2_NUMCLASS',  0x0010); // bit 4
39
define('SM2_ALLINFO',   0x0020); // bit 5
40
define('SM2_NOCACHE',   0x0040); // bit 6
41
define('SM2_PRETTY',    0x0080); // bit 7
42
define('SM2_ESCAPE',    0x0100); // bit 8
43
define('SM2_NOESCAPE',       0); // NOOP, unnecessary with WB 2.6.7+
44
define('SM2_BUFFER',    0x0200); // bit 9
45
define('SM2_CURRTREE',  0x0400); // bit 10
46 1109 Ruebenwurz
define('SM2_HIDEPAGES', 0x0800); // bit 11 (reactivate old behavior and doesn't show hidden pages)
47 827 doc
48
define('_SM2_GROUP_1',  0x000F); // exactly one flag from group 1 is required
49
50
51
// Implement support for page_menu and show_menu using show_menu2. If you remove
52
// the comments characters from the beginning of the following include, all menu
53
// functions in Website Baker will be implemented using show_menu2. While it is
54
// commented out, the original WB functions will be used.
55
//include('legacy.php');
56
57
// This class is the default menu formatter for sm2. If desired, you can
58
// create your own formatter class and pass the object into show_menu2
59
// as $aItemFormat.
60
define('SM2_CONDITIONAL','if\s*\(([^\)]+)\)\s*{([^}]*)}\s*(?:else\s*{([^}]*)}\s*)?');
61
define('SM2_COND_TERM','\s*(\w+)\s*(<|<=|==|=|=>|>|!=)\s*([\w\-]+)\s*');
62
class SM2_Formatter
63
{
64
    var $output;
65
    var $flags;
66
    var $itemOpen;
67
    var $itemClose;
68
    var $menuOpen;
69
    var $menuClose;
70
    var $topItemOpen;
71
    var $topMenuOpen;
72
73
    var $isFirst;
74
    var $page;
75
    var $url;
76
    var $currSib;
77
    var $sibCount;
78
    var $currClass;
79
    var $prettyLevel;
80
81
    // output the data
82
    function output($aString) {
83
        if ($this->flags & SM2_BUFFER) {
84
            $this->output .= $aString;
85
        }
86
        else {
87
            echo $aString;
88
        }
89
    }
90
91
    // set the default values for all of our formatting items
92
    function set($aFlags, $aItemOpen, $aItemClose, $aMenuOpen, $aMenuClose, $aTopItemOpen, $aTopMenuOpen) {
93
        $this->flags        = $aFlags;
94
        $this->itemOpen     = is_string($aItemOpen)    ? $aItemOpen    : '[li][a][menu_title]</a>';
95
        $this->itemClose    = is_string($aItemClose)   ? $aItemClose   : '</li>';
96
        $this->menuOpen     = is_string($aMenuOpen)    ? $aMenuOpen    : '[ul]';
97
        $this->menuClose    = is_string($aMenuClose)   ? $aMenuClose   : '</ul>';
98
        $this->topItemOpen  = is_string($aTopItemOpen) ? $aTopItemOpen : $this->itemOpen;
99
        $this->topMenuOpen  = is_string($aTopMenuOpen) ? $aTopMenuOpen : $this->menuOpen;
100
    }
101
102
    // initialize the state of the formatter before anything is output
103
    function initialize() {
104
        $this->output = '';
105
        $this->prettyLevel = 0;
106
        if ($this->flags & SM2_PRETTY) {
107
            $this->output("\n<!-- show_menu2 -->");
108
        }
109
    }
110
111
    // start a menu
112
    function startList(&$aPage, &$aUrl) {
113
        $currClass = '';
114
        $currItem = $this->menuOpen;
115
116
        // use the top level menu open if this is the first menu
117
        if ($this->topMenuOpen) {
118
            $currItem = $this->topMenuOpen;
119
            $currClass .= ' menu-top';
120
            $this->topMenuOpen = false;
121
        }
122
123
        // add the numbered menu class only if requested
124
        if (($this->flags & SM2_NUMCLASS) == SM2_NUMCLASS) {
125
            $currClass .= ' menu-'.$aPage['level'];
126
        }
127
128
        $this->prettyLevel += 1;
129
130
        // replace all keywords in the output
131
        if ($this->flags & SM2_PRETTY) {
132
            $this->output("\n".str_repeat(' ',$this->prettyLevel).
133
                $this->format($aPage, $aUrl, $currItem, $currClass));
134
        }
135
        else {
136
            $this->output($this->format($aPage, $aUrl, $currItem, $currClass));
137
        }
138
139
        $this->prettyLevel += 3;
140
    }
141
142
    // start an item within the menu
143
    function startItem(&$aPage, &$aUrl, $aCurrSib, $aSibCount) {
144
        // generate our class list
145
        $currClass = '';
146
        if (($this->flags & SM2_NUMCLASS) == SM2_NUMCLASS) {
147
            $currClass .= ' menu-'.$aPage['level'];
148
        }
149
        if (array_key_exists('sm2_has_child', $aPage)) {
150
            // not set if false, so existence = true
151
            $currClass .= ' menu-expand';
152
        }
153 845 doc
        if (array_key_exists('sm2_is_curr', $aPage)) {
154 827 doc
            $currClass .= ' menu-current';
155
        }
156
        elseif (array_key_exists('sm2_is_parent', $aPage)) {
157
            // not set if false, so existence = true
158
            $currClass .= ' menu-parent';
159
        }
160
        elseif (array_key_exists('sm2_is_sibling', $aPage)) {
161
            // not set if false, so existence = true
162
            $currClass .= ' menu-sibling';
163
        }
164
        elseif (array_key_exists('sm2_child_level',$aPage)) {
165
            // not set if not a child
166
            $currClass .= ' menu-child';
167
            if (($this->flags & SM2_NUMCLASS) == SM2_NUMCLASS) {
168
                $currClass .= ' menu-child-'.($aPage['sm2_child_level']-1);
169
            }
170
        }
171
        if ($aCurrSib == 1) {
172
            $currClass .= ' menu-first';
173
        }
174
        if ($aCurrSib == $aSibCount) {
175
            $currClass .= ' menu-last';
176
        }
177
178
        // use the top level item if this is the first item
179
        $currItem = $this->itemOpen;
180
        if ($this->topItemOpen) {
181
            $currItem = $this->topItemOpen;
182
            $this->topItemOpen = false;
183
        }
184
185
        // replace all keywords in the output
186
        if ($this->flags & SM2_PRETTY) {
187
            $this->output("\n".str_repeat(' ',$this->prettyLevel));
188
        }
189
        $this->output($this->format($aPage, $aUrl, $currItem, $currClass, $aCurrSib, $aSibCount));
190
    }
191
192
    // find and replace all keywords, setting the state variables first
193
    function format(&$aPage, &$aUrl, &$aCurrItem, &$aCurrClass,
194
        $aCurrSib = 0, $aSibCount = 0)
195
    {
196
        $this->page      = &$aPage;
197
        $this->url       = &$aUrl;
198
        $this->currClass = trim($aCurrClass);
199
        $this->currSib   = $aCurrSib;
200
        $this->sibCount  = $aSibCount;
201
202
        $item = $this->format2($aCurrItem);
203
204
        unset($this->page);
205
        unset($this->url);
206
        unset($this->currClass);
207
208
        return $item;
209
    }
210
211
    // find and replace all keywords
212
    function format2(&$aCurrItem) {
213
        if (!is_string($aCurrItem)) return '';
214
        return preg_replace(
215
            '@\[('.
216
                'a|ac|/a|li|/li|ul|/ul|menu_title|page_title|url|target|page_id|'.
217
                'parent|level|sib|sibCount|class|description|keywords|'.
218
                SM2_CONDITIONAL.
219
            ')\]@e',
220
            '$this->replace("\1")', $aCurrItem);
221
    }
222
223
    // replace the keywords
224
    function replace($aMatch) {
225
        switch ($aMatch) {
226
        case 'a':
227
            return '<a href="'.$this->url.'" target="'.$this->page['target'].'">';
228
        case 'ac':
229
            return '<a href="'.$this->url.'" target="'.$this->page['target'].'" class="'.$this->currClass.'">';
230
        case '/a':
231
            return '</a>';
232
        case 'li':
233
            return '<li class="'.$this->currClass.'">';
234
        case '/li':
235
            return '</li>';
236
        case 'ul':
237
            return '<ul class="'.$this->currClass.'">';
238
        case '/ul':
239
            return '</ul>';
240
        case 'url':
241
            return $this->url;
242
        case 'sib':
243
            return $this->currSib;
244
        case 'sibCount':
245
            return $this->sibCount;
246
        case 'class':
247
            return $this->currClass;
248
        default:
249
            if (array_key_exists($aMatch, $this->page)) {
250
                if ($this->flags & SM2_ESCAPE) {
251
                    return htmlspecialchars($this->page[$aMatch], ENT_QUOTES);
252
                }
253
                else {
254
                    return $this->page[$aMatch];
255
                }
256
            }
257
            if (preg_match('/'.SM2_CONDITIONAL.'/', $aMatch, $rgMatches)) {
258
                return $this->replaceIf($rgMatches[1], $rgMatches[2], $rgMatches[3]);
259
            }
260
        }
261
        return "[$aMatch=UNKNOWN]";
262
    }
263
264
    // conditional replacement
265
    function replaceIf(&$aExpression, &$aIfValue, &$aElseValue) {
266
        // evaluate all of the tests in the conditional (we don't do short-circuit
267
        // evaluation) and replace the string test with the boolean result
268
        $rgTests = preg_split('/(\|\||\&\&)/', $aExpression, -1, PREG_SPLIT_DELIM_CAPTURE);
269
        for ($n = 0; $n < count($rgTests); $n += 2) {
270
            if (preg_match('/'.SM2_COND_TERM.'/', $rgTests[$n], $rgMatches)) {
271
                $rgTests[$n] = $this->ifTest($rgMatches[1], $rgMatches[2], $rgMatches[3]);
272
            }
273
            else {
274
                error_log("show_menu2 error: conditional expression is invalid!");
275
                $rgTests[$n] = false;
276
            }
277
        }
278
279
        // combine all test results for a final result
280
        $ok = $rgTests[0];
281
        for ($n = 1; $n+1 < count($rgTests); $n += 2) {
282
            if ($rgTests[$n] == '||') {
283
                $ok = $ok || $rgTests[$n+1];
284
            }
285
            else {
286
                $ok = $ok && $rgTests[$n+1];
287
            }
288
        }
289
290
        // return the formatted expression if the test succeeded
291
        return $ok ? $this->format2($aIfValue) : $this->format2($aElseValue);
292
    }
293
294
    // conditional test
295
    function ifTest(&$aKey, &$aOperator, &$aValue) {
296
        global $wb;
297
298
        // find the correct operand
299
        $operand = false;
300
        switch($aKey) {
301
        case 'class':
302
            // we need to wrap the class names in spaces so we can test for a unique
303
            // class name that will not match prefixes or suffixes. Same must be done
304
            // for the value we are testing.
305
            $operand = " $this->currClass ";
306
            break;
307
        case 'sib':
308
            $operand = $this->currSib;
309
            if ($aValue == 'sibCount') {
310
                $aValue = $this->sibCount;
311
            }
312
            break;
313
        case 'sibCount':
314
            $operand = $this->sibCount;
315
            break;
316
        case 'level':
317
            $operand = $this->page['level'];
318
            switch ($aValue) {
319
            case 'root':    $aValue = 0; break;
320
            case 'granny':  $aValue = $wb->page['level']-2; break;
321
            case 'parent':  $aValue = $wb->page['level']-1; break;
322
            case 'current': $aValue = $wb->page['level'];   break;
323
            case 'child':   $aValue = $wb->page['level']+1; break;
324
            }
325
            if ($aValue < 0) $aValue = 0;
326
            break;
327
        case 'id':
328
            $operand = $this->page['page_id'];
329
            switch ($aValue) {
330
            case 'parent':  $aValue = $wb->page['parent'];  break;
331
            case 'current': $aValue = $wb->page['page_id']; break;
332
            }
333
            break;
334
        default:
335
            return '';
336
        }
337
338
        // do the test
339
        $ok = false;
340
        switch ($aOperator) {
341
        case '<':
342
            $ok = ($operand < $aValue);
343
            break;
344
        case '<=':
345
            $ok = ($operand <= $aValue);
346
            break;
347
        case '=':
348
        case '==':
349
        case '!=':
350
            if ($aKey == 'class') {
351
                $ok = strstr($operand, " $aValue ") !== FALSE;
352
            }
353
            else {
354
                $ok = ($operand == $aValue);
355
            }
356
            if ($aOperator == '!=') {
357
                $ok = !$ok;
358
            }
359
            break;
360
        case '>=':
361
            $ok = ($operand >= $aValue);
362
        case '>':
363
            $ok = ($operand > $aValue);
364
        }
365
366
        return $ok;
367
    }
368
369
    // finish the current menu item
370
    function finishItem() {
371
        if ($this->flags & SM2_PRETTY) {
372
            $this->output(str_repeat(' ',$this->prettyLevel).$this->itemClose);
373
        }
374
        else {
375
            $this->output($this->itemClose);
376
        }
377
    }
378
379
    // finish the current menu
380
    function finishList() {
381
        $this->prettyLevel -= 3;
382
383
        if ($this->flags & SM2_PRETTY) {
384
            $this->output("\n".str_repeat(' ',$this->prettyLevel).$this->menuClose."\n");
385
        }
386
        else {
387
            $this->output($this->menuClose);
388
        }
389
390
        $this->prettyLevel -= 1;
391
    }
392
393
    // cleanup the state of the formatter after everything has been output
394
    function finalize() {
395
        if ($this->flags & SM2_PRETTY) {
396
            $this->output("\n");
397
        }
398
    }
399
400
    // return the output
401
    function getOutput() {
402
        return $this->output;
403
    }
404
};
405
406
function show_menu2(
407
    $aMenu          = 0,
408
    $aStart         = SM2_ROOT,
409
    $aMaxLevel      = -1999, // SM2_CURR+1
410 859 Ruebenwurz
    $aOptions       = SM2_TRIM,
411 827 doc
    $aItemOpen      = false,
412
    $aItemClose     = false,
413
    $aMenuOpen      = false,
414
    $aMenuClose     = false,
415
    $aTopItemOpen   = false,
416
    $aTopMenuOpen   = false
417
    )
418
{
419
    global $wb;
420
421 859 Ruebenwurz
    // extract the flags and set $aOptions to an array
422
    $flags = 0;
423
    if (is_int($aOptions)) {
424
        $flags = $aOptions;
425
        $aOptions = array();
426
    }
427
    else if (isset($aOptions['flags'])) {
428
        $flags = $aOptions['flags'];
429
    }
430
    else {
431
        $flags = SM2_TRIM;
432
        $aOptions = array();
433
        error_log('show_menu2 error: $aOptions is invalid. No flags supplied!');
434
    }
435
436 827 doc
    // ensure we have our group 1 flag, we don't check for the "exactly 1" part, but
437
    // we do ensure that they provide at least one.
438 859 Ruebenwurz
    if (0 == ($flags & _SM2_GROUP_1)) {
439
        error_log('show_menu2 error: $aOptions is invalid. No flags from group 1 supplied!');
440
        $flags |= SM2_TRIM; // default to TRIM
441 827 doc
    }
442
443 845 doc
    // search page results don't have any of the page data loaded by WB, so we load it
444
    // ourselves using the referrer ID as the current page
445
    $CURR_PAGE_ID = defined('REFERRER_ID') ? REFERRER_ID : PAGE_ID;
446
    if (count($wb->page) == 0 && defined('REFERRER_ID') && REFERRER_ID > 0) {
447
        global $database;
448
        $sql = "SELECT * FROM ".TABLE_PREFIX."pages WHERE page_id = '".REFERRER_ID."'";
449
        $result = $database->query($sql);
450
        if ($result->numRows() == 1) {
451
            $wb->page = $result->fetchRow();
452
        }
453
        unset($result);
454
    }
455
456 827 doc
    // fix up the menu number to default to the menu number
457
    // of the current page if no menu has been supplied
458
    if ($aMenu == 0) {
459
        $aMenu = $wb->page['menu'] == '' ? 1 : $wb->page['menu'];
460
    }
461
462
    // Set some of the $wb->page[] settings to defaults if not set
463 845 doc
    $pageLevel  = $wb->page['level']  == '' ? 0 : $wb->page['level'];
464 827 doc
    $pageParent = $wb->page['parent'] == '' ? 0 : $wb->page['parent'];
465
466
    // adjust the start level and start page ID as necessary to
467
    // handle the special values that can be passed in as $aStart
468
    $aStartLevel = 0;
469
    if ($aStart < SM2_ROOT) {   // SM2_CURR+N
470
        if ($aStart == SM2_CURR) {
471
            $aStartLevel = $pageLevel;
472 845 doc
            $aStart = $pageParent;
473 827 doc
        }
474
        else {
475
            $aStartLevel = $pageLevel + $aStart - SM2_CURR;
476 845 doc
            $aStart = $CURR_PAGE_ID;
477 827 doc
        }
478
    }
479
    elseif ($aStart < 0) {   // SM2_ROOT+N
480
        $aStartLevel = $aStart - SM2_ROOT;
481
        $aStart = 0;
482
    }
483
484
    // we get the menu data once and store it in a global variable. This allows
485
    // multiple calls to show_menu2 in a single page with only a single call to
486
    // the database. If this variable exists, then we have already retrieved all
487
    // of the information and processed it, so we don't need to do it again.
488 859 Ruebenwurz
    if (($flags & SM2_NOCACHE) != 0
489 827 doc
        || !array_key_exists('show_menu2_data', $GLOBALS)
490
        || !array_key_exists($aMenu, $GLOBALS['show_menu2_data']))
491
    {
492
        global $database;
493
494
        // create an array of all parents of the current page. As the page_trail
495
        // doesn't include the theoretical root element 0, we add it ourselves.
496
        $rgCurrParents = split(',', '0,'.$wb->page['page_trail']);
497
        array_pop($rgCurrParents); // remove the current page
498
        $rgParent = array();
499
500
        // if the caller wants all menus gathered together (e.g. for a sitemap)
501
        // then we don't limit our SQL query
502
        $menuLimitSql = ' AND menu = ' .$aMenu;
503
        if ($aMenu == SM2_ALLMENU) {
504
            $menuLimitSql = '';
505
        }
506
507
        // we only load the description and keywords if we have been told to,
508
        // this cuts the memory load for pages that don't use them. Note that if
509
        // we haven't been told to load these fields the *FIRST TIME* show_menu2
510
        // is called (i.e. where the database is loaded) then the info won't
511
        // exist anyhow.
512
        $fields = 'parent,page_id,menu_title,page_title,link,target,level,visibility,viewing_groups';
513 845 doc
        if (version_compare(WB_VERSION, '2.7', '>=')) { // WB 2.7+
514 827 doc
            $fields .= ',viewing_users';
515
        }
516 859 Ruebenwurz
        if ($flags & SM2_ALLINFO) {
517 827 doc
            $fields = '*';
518
        }
519
520
        // get this once for performance. We really should be calling only need to
521
        // call $wb->get_group_id() but that outputs a warning notice if the
522
        // groupid isn't set in the session, so we check it first here.
523
        $currGroup = array_key_exists('GROUP_ID', $_SESSION) ?
524
            ','.$wb->get_group_id().',' : 'NOGROUP';
525
526
        // we request all matching rows from the database for the menu that we
527
        // are about to create it is cheaper for us to get everything we need
528
        // from the database once and create the menu from memory then make
529
        // multiple calls to the database.
530
        $sql = "SELECT $fields FROM ".TABLE_PREFIX.
531
               "pages WHERE $wb->extra_where_sql $menuLimitSql ".
532
               'ORDER BY level ASC, position ASC';
533 971 Ruebenwurz
        $sql = str_replace('hidden', 'IGNOREME', $sql); // we want the hidden pages
534 827 doc
        $oRowset = $database->query($sql);
535
        if (is_object($oRowset) && $oRowset->numRows() > 0) {
536
            // create an in memory array of the database data based on the item's parent.
537
            // The array stores all elements in the correct display order.
538
            while ($page = $oRowset->fetchRow()) {
539 845 doc
                // ignore all pages that the current user is not permitted to view
540 827 doc
                if(version_compare(WB_VERSION, '2.7', '>=')) { // WB >= 2.7
541 971 Ruebenwurz
                    // 1. hidden pages aren't shown unless they are on the current page
542
                    if ($page['visibility'] == 'hidden') {
543
                        $page['sm2_hide'] = true;
544 827 doc
                    }
545 971 Ruebenwurz
546
                    // 2. all pages with no active sections (unless it is the top page) are ignored
547
                    else if (!$wb->page_is_active($page) && $page['link'] != $wb->default_link && !INTRO_PAGE) {
548 827 doc
                        continue;
549
                    }
550 971 Ruebenwurz
551
                    // 3. all pages not visible to this user (unless always visible to registered users) are ignored
552
                    else if (!$wb->page_is_visible($page) && $page['visibility'] != 'registered') {
553
                        continue;
554
                    }
555 827 doc
                }
556
                else {  // WB < 2.7
557
                    // We can't do this in SQL as the viewing_groups column contains multiple
558
                    // values which are hard to process correctly in SQL. Essentially we add the
559
                    // following limit to the SQL query above:
560
                    //  (visibility <> "private" OR $wb->get_group_id() IN viewing_groups)
561
                    if ($page['visibility'] == 'private'
562
                        && false === strstr(",{$page['viewing_groups']},", $currGroup))
563
                    {
564
                        continue;
565
                    }
566
                }
567
568
                // ensure that we have an array entry in the table to add this to
569
                $idx = $page['parent'];
570
                if (!array_key_exists($idx, $rgParent)) {
571
                    $rgParent[$idx] = array();
572
                }
573
574
                // mark our current page as being on the current path
575 845 doc
                if ($page['page_id'] == $CURR_PAGE_ID) {
576
                    $page['sm2_is_curr'] = true;
577 827 doc
                    $page['sm2_on_curr_path'] = true;
578 1109 Ruebenwurz
                    if (($flags & SM2_HIDEPAGES) == 0){
579
                        unset($page['sm2_hide']); // don't hide the current page
580
                    }
581 827 doc
                }
582
583
                // mark parents of the current page as such
584
                if (in_array($page['page_id'], $rgCurrParents)) {
585
                    $page['sm2_is_parent'] = true;
586
                    $page['sm2_on_curr_path'] = true;
587 971 Ruebenwurz
                    unset($page['sm2_hide']); // don't hide a parent page
588 827 doc
                }
589
590
                // add the entry to the array
591
                $rgParent[$idx][] = $page;
592
            }
593
        }
594
        unset($oRowset);
595
596
        // mark all elements that are siblings of any element on the current path
597
        foreach ($rgCurrParents as $x) {
598
            if (array_key_exists($x, $rgParent)) {
599
                foreach (array_keys($rgParent[$x]) as $y) {
600
                    $mark =& $rgParent[$x][$y];
601
                    $mark['sm2_path_sibling'] = true;
602
                    unset($mark);
603
                }
604
            }
605
        }
606
607
        // mark all elements that have children and are siblings of the current page
608
        $parentId = $pageParent;
609
        foreach (array_keys($rgParent) as $x) {
610
            $childSet =& $rgParent[$x];
611
            foreach (array_keys($childSet) as $y) {
612
                $mark =& $childSet[$y];
613
                if (array_key_exists($mark['page_id'], $rgParent)) {
614
                    $mark['sm2_has_child'] = true;
615
                }
616 845 doc
                if ($mark['parent'] == $parentId && $mark['page_id'] != $CURR_PAGE_ID) {
617 827 doc
                    $mark['sm2_is_sibling'] = true;
618
                }
619
                unset($mark);
620
            }
621
            unset($childSet);
622
        }
623
624
        // mark all children of the current page. We don't do this when
625 845 doc
        // $CURR_PAGE_ID is 0, as 0 is the parent of everything.
626
        // $CURR_PAGE_ID == 0 occurs on special pages like search results
627
        // when no referrer is available.s
628
        if ($CURR_PAGE_ID != 0) {
629
            sm2_mark_children($rgParent, $CURR_PAGE_ID, 1);
630 827 doc
        }
631
632
        // store the complete processed menu data as a global. We don't
633
        // need to read this from the database anymore regardless of how
634
        // many menus are displayed on the same page.
635
        if (!array_key_exists('show_menu2_data', $GLOBALS)) {
636
            $GLOBALS['show_menu2_data'] = array();
637
        }
638
        $GLOBALS['show_menu2_data'][$aMenu] =& $rgParent;
639
        unset($rgParent);
640
    }
641
642
    // adjust $aMaxLevel to the level number of the final level that
643
    // will be displayed. That is, we display all levels <= aMaxLevel.
644
    if ($aMaxLevel == SM2_ALL) {
645
        $aMaxLevel = 1000;
646
    }
647
    elseif ($aMaxLevel < 0) {   // SM2_CURR+N
648
        $aMaxLevel += $pageLevel - SM2_CURR;
649
    }
650
    elseif ($aMaxLevel >= SM2_MAX) { // SM2_MAX+N
651
        $aMaxLevel += $aStartLevel - SM2_MAX;
652
        if ($aMaxLevel > $pageLevel) {
653
            $aMaxLevel = $pageLevel;
654
        }
655
    }
656
    else {  // SM2_START+N
657
        $aMaxLevel += $aStartLevel - SM2_START;
658
    }
659
660
    // generate the menu
661
    $retval = false;
662
    if (array_key_exists($aStart, $GLOBALS['show_menu2_data'][$aMenu])) {
663
        $formatter = $aItemOpen;
664
        if (!is_object($aItemOpen)) {
665
            static $sm2formatter;
666
            if (!isset($sm2formatter)) {
667
                $sm2formatter = new SM2_Formatter;
668
            }
669
            $formatter = $sm2formatter;
670 859 Ruebenwurz
            $formatter->set($flags, $aItemOpen, $aItemClose,
671 827 doc
                $aMenuOpen, $aMenuClose, $aTopItemOpen, $aTopMenuOpen);
672
        }
673
674 859 Ruebenwurz
        // adjust the level until we show everything and ignore the SM2_TRIM flag.
675
        // Usually this will be less than the start level to disable it.
676
        $showAllLevel = $aStartLevel - 1;
677
        if (isset($aOptions['notrim'])) {
678
            $showAllLevel = $aStartLevel + $aOptions['notrim'];
679
        }
680
681 827 doc
        // display the menu
682
        $formatter->initialize();
683
        sm2_recurse(
684
            $GLOBALS['show_menu2_data'][$aMenu],
685
            $aStart,    // parent id to start displaying sub-menus
686 859 Ruebenwurz
            $aStartLevel, $showAllLevel, $aMaxLevel, $flags,
687 827 doc
            $formatter);
688
        $formatter->finalize();
689
690
        // if we are returning something, get the data
691 859 Ruebenwurz
        if (($flags & SM2_BUFFER) != 0) {
692 827 doc
            $retval = $formatter->getOutput();
693
        }
694
    }
695
696
    // clear the data if we aren't caching it
697 859 Ruebenwurz
    if (($flags & SM2_NOCACHE) != 0) {
698 827 doc
        unset($GLOBALS['show_menu2_data'][$aMenu]);
699
    }
700
701
    return $retval;
702
}
703
704
function sm2_mark_children(&$rgParent, $aStart, $aChildLevel)
705
{
706
    if (array_key_exists($aStart, $rgParent)) {
707
        foreach (array_keys($rgParent[$aStart]) as $y) {
708
            $mark =& $rgParent[$aStart][$y];
709
            $mark['sm2_child_level'] = $aChildLevel;
710
            $mark['sm2_on_curr_path'] = true;
711
            sm2_mark_children($rgParent, $mark['page_id'], $aChildLevel+1);
712
        }
713
    }
714
}
715
716
function sm2_recurse(
717
    &$rgParent, $aStart,
718 859 Ruebenwurz
    $aStartLevel, $aShowAllLevel, $aMaxLevel, $aFlags,
719 827 doc
    &$aFormatter
720
    )
721
{
722
    global $wb;
723
724
    // on entry to this function we know that there are entries for this
725
    // parent and all entries for that parent are being displayed. We also
726
    // need to check if any of the children need to be displayed too.
727
    $isListOpen = false;
728
    $currentLevel = $wb->page['level'] == '' ? 0 : $wb->page['level'];
729 972 Ruebenwurz
730
    // get the number of siblings skipping the hidden pages so we can pass
731
    // this in and check if the item is first or last
732
    $sibCount = 0;
733
    foreach ($rgParent[$aStart] as $page) {
734
        if (!array_key_exists('sm2_hide', $page)) $sibCount++;
735
    }
736 827 doc
737
    $currSib = 0;
738
    foreach ($rgParent[$aStart] as $page) {
739 972 Ruebenwurz
        // skip all hidden pages
740
        if (array_key_exists('sm2_hide', $page)) { // not set if false, so existence = true
741
            continue;
742
        }
743
744 827 doc
        $currSib++;
745
746
        // skip any elements that are lower than the maximum level
747
        $pageLevel = $page['level'];
748
        if ($pageLevel > $aMaxLevel) {
749
            continue;
750
        }
751
752
        // this affects ONLY the top level
753
        if ($aStart == 0 && ($aFlags & SM2_CURRTREE)) {
754
            if (!array_key_exists('sm2_on_curr_path', $page)) { // not set if false, so existence = true
755
                continue;
756
            }
757
            $sibCount = 1;
758
        }
759
760
        // trim the tree as appropriate
761
        if ($aFlags & SM2_SIBLING) {
762
            // parents, and siblings and children of current only
763
            if (!array_key_exists('sm2_on_curr_path', $page)      // not set if false, so existence = true
764
                && !array_key_exists('sm2_is_sibling', $page)     // not set if false, so existence = true
765
                && !array_key_exists('sm2_child_level', $page)) { // not set if false, so existence = true
766
                continue;
767
            }
768
        }
769
        else if ($aFlags & SM2_TRIM) {
770
            // parents and siblings of parents
771 859 Ruebenwurz
            if ($pageLevel > $aShowAllLevel  // permit all levels to be shown
772
                && !array_key_exists('sm2_on_curr_path', $page)    // not set if false, so existence = true
773 827 doc
                && !array_key_exists('sm2_path_sibling', $page)) {  // not set if false, so existence = true
774
                continue;
775
            }
776
        }
777
        elseif ($aFlags & SM2_CRUMB) {
778
            // parents only
779
            if (!array_key_exists('sm2_on_curr_path', $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
785
        // depth first traverse
786
        $nextParent = $page['page_id'];
787
788
        // display the current element if we have reached the start level
789
        if ($pageLevel >= $aStartLevel) {
790
            // massage the link into the correct form
791
            if(!INTRO_PAGE && $page['link'] == $wb->default_link) {
792
                $url = WB_URL;
793
            }
794
            else {
795
                $url = $wb->page_link($page['link']);
796
            }
797
798
            // we open the list only when we absolutely need to
799
            if (!$isListOpen) {
800
                $aFormatter->startList($page, $url);
801
                $isListOpen = true;
802
            }
803
804
            $aFormatter->startItem($page, $url, $currSib, $sibCount);
805
        }
806
807
        // display children as appropriate
808
        if ($pageLevel + 1 <= $aMaxLevel
809
            && array_key_exists('sm2_has_child', $page)) {  // not set if false, so existence = true
810
            sm2_recurse(
811
                $rgParent, $nextParent, // parent id to start displaying sub-menus
812 859 Ruebenwurz
                $aStartLevel, $aShowAllLevel, $aMaxLevel, $aFlags,
813 827 doc
                $aFormatter);
814
        }
815
816
        // close the current element if appropriate
817
        if ($pageLevel >= $aStartLevel) {
818
            $aFormatter->finishItem($pageLevel, $page);
819
        }
820
    }
821
822
    // close the list if we opened one
823
    if ($isListOpen) {
824
        $aFormatter->finishList();
825
    }
826
}
827
828
?>