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 1119 Ruebenwurz
    ** Version 4.9: see README for documentation **
26 827 doc
    ***********************************************
27
*/
28
29 1119 Ruebenwurz
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
define('SM2_SHOWHIDDEN', 0x0800); // bit 11
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 1119 Ruebenwurz
                    if ($flags & SM2_SHOWHIDDEN) {
579
                        // show hidden pages if active and SHOWHIDDEN flag supplied
580
                        unset($page['sm2_hide']);
581 1109 Ruebenwurz
                    }
582 827 doc
                }
583
584
                // mark parents of the current page as such
585
                if (in_array($page['page_id'], $rgCurrParents)) {
586
                    $page['sm2_is_parent'] = true;
587
                    $page['sm2_on_curr_path'] = true;
588 1119 Ruebenwurz
                    if ($flags & SM2_SHOWHIDDEN) {
589
                        // show hidden pages if active and SHOWHIDDEN flag supplied
590
                        unset($page['sm2_hide']);
591
                    }
592 827 doc
                }
593
594
                // add the entry to the array
595
                $rgParent[$idx][] = $page;
596
            }
597
        }
598
        unset($oRowset);
599
600
        // mark all elements that are siblings of any element on the current path
601
        foreach ($rgCurrParents as $x) {
602
            if (array_key_exists($x, $rgParent)) {
603
                foreach (array_keys($rgParent[$x]) as $y) {
604
                    $mark =& $rgParent[$x][$y];
605
                    $mark['sm2_path_sibling'] = true;
606
                    unset($mark);
607
                }
608
            }
609
        }
610
611
        // mark all elements that have children and are siblings of the current page
612
        $parentId = $pageParent;
613
        foreach (array_keys($rgParent) as $x) {
614
            $childSet =& $rgParent[$x];
615
            foreach (array_keys($childSet) as $y) {
616
                $mark =& $childSet[$y];
617
                if (array_key_exists($mark['page_id'], $rgParent)) {
618
                    $mark['sm2_has_child'] = true;
619
                }
620 845 doc
                if ($mark['parent'] == $parentId && $mark['page_id'] != $CURR_PAGE_ID) {
621 827 doc
                    $mark['sm2_is_sibling'] = true;
622
                }
623
                unset($mark);
624
            }
625
            unset($childSet);
626
        }
627
628
        // mark all children of the current page. We don't do this when
629 845 doc
        // $CURR_PAGE_ID is 0, as 0 is the parent of everything.
630
        // $CURR_PAGE_ID == 0 occurs on special pages like search results
631
        // when no referrer is available.s
632
        if ($CURR_PAGE_ID != 0) {
633
            sm2_mark_children($rgParent, $CURR_PAGE_ID, 1);
634 827 doc
        }
635
636
        // store the complete processed menu data as a global. We don't
637
        // need to read this from the database anymore regardless of how
638
        // many menus are displayed on the same page.
639
        if (!array_key_exists('show_menu2_data', $GLOBALS)) {
640
            $GLOBALS['show_menu2_data'] = array();
641
        }
642
        $GLOBALS['show_menu2_data'][$aMenu] =& $rgParent;
643
        unset($rgParent);
644
    }
645
646
    // adjust $aMaxLevel to the level number of the final level that
647
    // will be displayed. That is, we display all levels <= aMaxLevel.
648
    if ($aMaxLevel == SM2_ALL) {
649
        $aMaxLevel = 1000;
650
    }
651
    elseif ($aMaxLevel < 0) {   // SM2_CURR+N
652
        $aMaxLevel += $pageLevel - SM2_CURR;
653
    }
654
    elseif ($aMaxLevel >= SM2_MAX) { // SM2_MAX+N
655
        $aMaxLevel += $aStartLevel - SM2_MAX;
656
        if ($aMaxLevel > $pageLevel) {
657
            $aMaxLevel = $pageLevel;
658
        }
659
    }
660
    else {  // SM2_START+N
661
        $aMaxLevel += $aStartLevel - SM2_START;
662
    }
663
664
    // generate the menu
665
    $retval = false;
666
    if (array_key_exists($aStart, $GLOBALS['show_menu2_data'][$aMenu])) {
667
        $formatter = $aItemOpen;
668
        if (!is_object($aItemOpen)) {
669
            static $sm2formatter;
670
            if (!isset($sm2formatter)) {
671
                $sm2formatter = new SM2_Formatter;
672
            }
673
            $formatter = $sm2formatter;
674 859 Ruebenwurz
            $formatter->set($flags, $aItemOpen, $aItemClose,
675 827 doc
                $aMenuOpen, $aMenuClose, $aTopItemOpen, $aTopMenuOpen);
676
        }
677
678 859 Ruebenwurz
        // adjust the level until we show everything and ignore the SM2_TRIM flag.
679
        // Usually this will be less than the start level to disable it.
680
        $showAllLevel = $aStartLevel - 1;
681
        if (isset($aOptions['notrim'])) {
682
            $showAllLevel = $aStartLevel + $aOptions['notrim'];
683
        }
684
685 827 doc
        // display the menu
686
        $formatter->initialize();
687
        sm2_recurse(
688
            $GLOBALS['show_menu2_data'][$aMenu],
689
            $aStart,    // parent id to start displaying sub-menus
690 859 Ruebenwurz
            $aStartLevel, $showAllLevel, $aMaxLevel, $flags,
691 827 doc
            $formatter);
692
        $formatter->finalize();
693
694
        // if we are returning something, get the data
695 859 Ruebenwurz
        if (($flags & SM2_BUFFER) != 0) {
696 827 doc
            $retval = $formatter->getOutput();
697
        }
698
    }
699
700
    // clear the data if we aren't caching it
701 859 Ruebenwurz
    if (($flags & SM2_NOCACHE) != 0) {
702 827 doc
        unset($GLOBALS['show_menu2_data'][$aMenu]);
703
    }
704
705
    return $retval;
706
}
707
708
function sm2_mark_children(&$rgParent, $aStart, $aChildLevel)
709
{
710
    if (array_key_exists($aStart, $rgParent)) {
711
        foreach (array_keys($rgParent[$aStart]) as $y) {
712
            $mark =& $rgParent[$aStart][$y];
713
            $mark['sm2_child_level'] = $aChildLevel;
714
            $mark['sm2_on_curr_path'] = true;
715
            sm2_mark_children($rgParent, $mark['page_id'], $aChildLevel+1);
716
        }
717
    }
718
}
719
720
function sm2_recurse(
721
    &$rgParent, $aStart,
722 859 Ruebenwurz
    $aStartLevel, $aShowAllLevel, $aMaxLevel, $aFlags,
723 827 doc
    &$aFormatter
724
    )
725
{
726
    global $wb;
727
728
    // on entry to this function we know that there are entries for this
729
    // parent and all entries for that parent are being displayed. We also
730
    // need to check if any of the children need to be displayed too.
731
    $isListOpen = false;
732
    $currentLevel = $wb->page['level'] == '' ? 0 : $wb->page['level'];
733 972 Ruebenwurz
734
    // get the number of siblings skipping the hidden pages so we can pass
735
    // this in and check if the item is first or last
736
    $sibCount = 0;
737
    foreach ($rgParent[$aStart] as $page) {
738
        if (!array_key_exists('sm2_hide', $page)) $sibCount++;
739
    }
740 827 doc
741
    $currSib = 0;
742
    foreach ($rgParent[$aStart] as $page) {
743 972 Ruebenwurz
        // skip all hidden pages
744
        if (array_key_exists('sm2_hide', $page)) { // not set if false, so existence = true
745
            continue;
746
        }
747
748 827 doc
        $currSib++;
749
750
        // skip any elements that are lower than the maximum level
751
        $pageLevel = $page['level'];
752
        if ($pageLevel > $aMaxLevel) {
753
            continue;
754
        }
755
756
        // this affects ONLY the top level
757
        if ($aStart == 0 && ($aFlags & SM2_CURRTREE)) {
758
            if (!array_key_exists('sm2_on_curr_path', $page)) { // not set if false, so existence = true
759
                continue;
760
            }
761
            $sibCount = 1;
762
        }
763
764
        // trim the tree as appropriate
765
        if ($aFlags & SM2_SIBLING) {
766
            // parents, and siblings and children of current only
767
            if (!array_key_exists('sm2_on_curr_path', $page)      // not set if false, so existence = true
768
                && !array_key_exists('sm2_is_sibling', $page)     // not set if false, so existence = true
769
                && !array_key_exists('sm2_child_level', $page)) { // not set if false, so existence = true
770
                continue;
771
            }
772
        }
773
        else if ($aFlags & SM2_TRIM) {
774
            // parents and siblings of parents
775 859 Ruebenwurz
            if ($pageLevel > $aShowAllLevel  // permit all levels to be shown
776
                && !array_key_exists('sm2_on_curr_path', $page)    // not set if false, so existence = true
777 827 doc
                && !array_key_exists('sm2_path_sibling', $page)) {  // not set if false, so existence = true
778
                continue;
779
            }
780
        }
781
        elseif ($aFlags & SM2_CRUMB) {
782
            // parents only
783
            if (!array_key_exists('sm2_on_curr_path', $page)    // not set if false, so existence = true
784
                || array_key_exists('sm2_child_level', $page)) {  // not set if false, so existence = true
785
                continue;
786
            }
787
        }
788
789
        // depth first traverse
790
        $nextParent = $page['page_id'];
791
792
        // display the current element if we have reached the start level
793
        if ($pageLevel >= $aStartLevel) {
794
            // massage the link into the correct form
795
            if(!INTRO_PAGE && $page['link'] == $wb->default_link) {
796
                $url = WB_URL;
797
            }
798
            else {
799
                $url = $wb->page_link($page['link']);
800
            }
801
802
            // we open the list only when we absolutely need to
803
            if (!$isListOpen) {
804
                $aFormatter->startList($page, $url);
805
                $isListOpen = true;
806
            }
807
808
            $aFormatter->startItem($page, $url, $currSib, $sibCount);
809
        }
810
811
        // display children as appropriate
812
        if ($pageLevel + 1 <= $aMaxLevel
813
            && array_key_exists('sm2_has_child', $page)) {  // not set if false, so existence = true
814
            sm2_recurse(
815
                $rgParent, $nextParent, // parent id to start displaying sub-menus
816 859 Ruebenwurz
                $aStartLevel, $aShowAllLevel, $aMaxLevel, $aFlags,
817 827 doc
                $aFormatter);
818
        }
819
820
        // close the current element if appropriate
821
        if ($pageLevel >= $aStartLevel) {
822
            $aFormatter->finishItem($pageLevel, $page);
823
        }
824
    }
825
826
    // close the list if we opened one
827
    if ($isListOpen) {
828
        $aFormatter->finishList();
829
    }
830
}
831
832
?>