1
|
show_menu2, version 4.6
|
2
|
=======================
|
3
|
A code snippet for the Website Baker CMS software. It provides a complete
|
4
|
replacement for the builtin menu functions. All menu data is retrieved using
|
5
|
a single database query, all types of menu styles (lists, breadcrums, sitemaps)
|
6
|
can be generated with extensive customisation of the resulting HTML.
|
7
|
|
8
|
|
9
|
INSTALLATION
|
10
|
============
|
11
|
1. Download the latest version from http://code.jellycan.com/show_menu2/
|
12
|
2. Log into your WebsiteBaker installation
|
13
|
3. Go to Addons -> Modules
|
14
|
4. If a previous version of show_menu2 is already installed, select it from
|
15
|
the "Uninstall Module" list and choose the "Uninstall" button.
|
16
|
5. In the "Install Module" section, enter the path to the show_menu2 zip file
|
17
|
that you downloaded in step 1, and choose the "Install" button.
|
18
|
|
19
|
|
20
|
USING SHOW_MENU2
|
21
|
================
|
22
|
You need to modify the PHP files of your template to call show_menu2 where you
|
23
|
wish to have the menu displayed. Remember when you replace calls to the old
|
24
|
menu functions to use the new parameters that show_menu2 requires.
|
25
|
|
26
|
Often times the default menu generated by show_menu2 is all that you need.
|
27
|
This menu shows the current page and children of the current page. It is
|
28
|
generated by just calling show_menu2 with no parameters. For example:
|
29
|
|
30
|
show_menu2();
|
31
|
|
32
|
Note that the call to show_menu2 is PHP, so you usually need to wrap it in the
|
33
|
PHP code brackets so that it will execute. Like this:
|
34
|
|
35
|
<?php show_menu2(); ?>
|
36
|
|
37
|
This default menu generates a complete list based menu with many classes that
|
38
|
allow easy CSS styling. For example, the current menu item will have the
|
39
|
"menu-current" class added to the <li> tag. Additionally, every menu item with
|
40
|
a sub-menu will have the "menu-expand" class added to the <li> tag. This allows
|
41
|
you to create CSS rules to style those menu items differently. For example:
|
42
|
|
43
|
li.menu-expand { font-weight: bold; }
|
44
|
li.menu-current { background: red; }
|
45
|
|
46
|
See the "Output" section for details of exactly what classes are added to each
|
47
|
element. More elaborate and different menu structures are able to be created by
|
48
|
supplying parameters to the show_menu2 function call. For example, to show only
|
49
|
menu items from the top level of the menu you use:
|
50
|
|
51
|
show_menu2(0, SM2_ROOT, SM2_START);
|
52
|
|
53
|
Alternatively, to show up to two levels of the child menus of the current page:
|
54
|
|
55
|
show_menu2(0, SM2_CURR+1, SM2_CURR+2);
|
56
|
|
57
|
There are many more possible menus that can be generated by show_menu2. See the
|
58
|
demonstration website at http://code.jellycan.com/sm2test/ for more examples.
|
59
|
|
60
|
|
61
|
FUNCTION
|
62
|
========
|
63
|
|
64
|
The complete call signature and default parameter value for show_menu2 is:
|
65
|
|
66
|
show_menu2(
|
67
|
$aMenu = 0,
|
68
|
$aStart = SM2_ROOT,
|
69
|
$aMaxLevel = SM2_CURR+1,
|
70
|
$aFlags = SM2_TRIM,
|
71
|
$aItemOpen = '[li][a][menu_title]</a>',
|
72
|
$aItemClose = '</li>',
|
73
|
$aMenuOpen = '[ul]',
|
74
|
$aMenuClose = '</ul>',
|
75
|
$aTopItemOpen = false,
|
76
|
$aTopMenuOpen = false
|
77
|
)
|
78
|
|
79
|
See the "Parameters" section for detailed descriptions of each parameter.
|
80
|
Note that every parameter from $aItemOpen onwards can be supplied as false
|
81
|
in order to get the default value, this allows (for example) a numbered list
|
82
|
to be used while still using the the default menu parameters for the items.
|
83
|
Note that all parameters up to $aFlags need to be explicitly supplied.
|
84
|
For example:
|
85
|
|
86
|
show_menu2(0, SM2_ROOT, SM2_ALL, SM2_ALL, false, false, '<ol>', '</ol>');
|
87
|
|
88
|
|
89
|
OUTPUT
|
90
|
======
|
91
|
The menu is output differently depending on what parameters have been
|
92
|
supplied to the function, however in general the following classes are used
|
93
|
for each menu. Note that items will have multiple classes when relevant.
|
94
|
|
95
|
CLASS ATTACHED TO
|
96
|
------------ -------------------------------------------------------
|
97
|
menu-top First menu tag only
|
98
|
menu-parent Every parent menu item of the current page.
|
99
|
menu-current Only the menu item for the current page.
|
100
|
menu-sibling Every sibling of the current page.
|
101
|
menu-child Every sub-menu of the current page.
|
102
|
menu-expand Every menu item with children.
|
103
|
menu-first First item in any menu or sub-menu.
|
104
|
menu-last Last item in any menu or sub-menu.
|
105
|
|
106
|
The following classes are added only if SM2_NUMCLASS flag has been used.
|
107
|
|
108
|
menu-N Every menu item. The N is replaced with the ABSOLUTE
|
109
|
menu depth of the item starting with 0. The root level
|
110
|
menu is always menu-0, the next level is menu-1, etc.
|
111
|
menu-child-N Every sub-menu of the current page, the N is replaced
|
112
|
with the relative depth of the submenu starting at 0.
|
113
|
|
114
|
|
115
|
<ul class="menu-top menu-0">
|
116
|
<li class="menu-0 menu-first"> ... </li>
|
117
|
<li class="menu-0 menu-expand menu-parent"> ...
|
118
|
<ul class="menu-1">
|
119
|
<li class="menu-1 menu-expand menu-first"> ...
|
120
|
<ul class="menu-2">
|
121
|
<li class="menu-2 menu-first"> ...
|
122
|
<li class="menu-2 menu-last"> ...
|
123
|
</ul>
|
124
|
</li>
|
125
|
<li class="menu-1 menu-expand menu-parent"> ...
|
126
|
<ul class="menu-2">
|
127
|
<li class="menu-2 menu-expand menu-current menu-first"> ... ** CURRENT PAGE **
|
128
|
<ul class="menu-3">
|
129
|
<li class="menu-3 menu-child menu-child-0 menu-first"> ...
|
130
|
<ul class="menu-4">
|
131
|
<li class="menu-4 menu-child menu-child-1 menu-first"> ... </li>
|
132
|
<li class="menu-4 menu-child menu-child-1 menu-last"> ... </li>
|
133
|
</ul>
|
134
|
</li>
|
135
|
<li class="menu-3 menu-child menu-child-0 menu-last"> ... </li>
|
136
|
</ul>
|
137
|
</li>
|
138
|
<li class="menu-2 menu-sibling menu-last"> ... </li>
|
139
|
</ul>
|
140
|
</li>
|
141
|
<li class="menu-1"> ... </li>
|
142
|
<li class="menu-1 menu-expand menu-last"> ...
|
143
|
<ul class="menu-2">
|
144
|
<li class="menu-2 menu-first menu-last"> ... </li>
|
145
|
</ul>
|
146
|
</li>
|
147
|
</ul>
|
148
|
</li>
|
149
|
<li class="menu-0 menu-last"> ... </li>
|
150
|
</ul>
|
151
|
|
152
|
|
153
|
|
154
|
PARAMETERS
|
155
|
==========
|
156
|
$aMenu
|
157
|
Menu number to use. This is useful when you are using multiple menus.
|
158
|
Supplying a menu number of 0 will use the default menu for the current
|
159
|
page. Supplying SM2_ALLMENU will return all menus in the system.
|
160
|
|
161
|
$aStart
|
162
|
Specify where the menu generation should start from. This is most
|
163
|
times the parent item of the menu to display. It must be one of the
|
164
|
following values:
|
165
|
SM2_ROOT+N Start N levels down from the root. e.g.
|
166
|
SM2_ROOT Starting at the root menu
|
167
|
SM2_ROOT+1 Start 1 level below the root
|
168
|
SM2_ROOT+2 Start 2 levels below the root
|
169
|
SM2_CURR+N Start N levels down from the current page level. e.g.
|
170
|
SM2_CURR Starts at the current page level. All
|
171
|
sibling menus to the current page.
|
172
|
SM2_CURR+1 Starts 1 level down from the current
|
173
|
page with the children menus.
|
174
|
page_id Display using the specific page as the parent. All
|
175
|
child menus of that page will be displayed. The
|
176
|
page_id can be found by editing the page in WB admin
|
177
|
interface. The page_id is included in the URL like:
|
178
|
http://SITE/admin/pages/modify.php?page_id=35
|
179
|
|
180
|
$aMaxLevel
|
181
|
Maximum menu level to display. Menus are displayed from the start
|
182
|
level down to this level.
|
183
|
SM2_ALL No limit, all levels are displayed
|
184
|
SM2_CURR+N Always show to the current page + N levels.
|
185
|
SM2_CURR Current (no children)
|
186
|
SM2_CURR+3 All parents + current + 3 children
|
187
|
SM2_START+N Always show from the starting level + N levels. The
|
188
|
levels of menu will always be displayed regardless of
|
189
|
what level the current page is.
|
190
|
SM2_START Single level of menus from starting level
|
191
|
SM2_START+1 Starting level and 1 level down
|
192
|
SM2_MAX+N Show at most N levels from the starting level. Levels
|
193
|
won't be shown if they are below the current level.
|
194
|
SM2_MAX Starting level only (same as SM2_START)
|
195
|
SM2_MAX+1 Maximum of starting level and 1 level.
|
196
|
|
197
|
$aFlags
|
198
|
Specify flags for different generation options for the menu. The flags
|
199
|
may be combined together using bitwise OR (|). For example, to specify
|
200
|
both TRIM and PRETTY you should use, SM2_TRIM|SM2_PRETTY.
|
201
|
|
202
|
GROUP 1
|
203
|
-------
|
204
|
Exactly one flag from this group must always be supplied. These flags
|
205
|
affect how the siblings in the tree are removed from the output.
|
206
|
|
207
|
SM2_ALL Show all branches of the menu tree
|
208
|
A-1 -> B-1
|
209
|
-> B-2 -> C-1
|
210
|
-> C-2 (CURRENT)
|
211
|
-> D-1
|
212
|
-> D-2
|
213
|
-> C-3
|
214
|
A-2 -> B-3
|
215
|
-> B-4
|
216
|
SM2_TRIM Show all sibling menus of pages on the current path.
|
217
|
All sub-menus of elements that are not on the path
|
218
|
are removed.
|
219
|
A-1 -> B-1
|
220
|
-> B-2 -> C-1
|
221
|
-> C-2 (CURRENT)
|
222
|
-> D-1
|
223
|
-> D-2
|
224
|
-> C-3
|
225
|
A-2
|
226
|
SM2_CRUMB Show only the breadcrumb trail, i.e. the current
|
227
|
menu and all of it's ancestor menus.
|
228
|
A-1 -> B-2 -> C-2 (CURRENT)
|
229
|
SM2_SIBLING The same as SM2_TRIM however only sibling menus of
|
230
|
the current page are displayed. All other menus are
|
231
|
trimmed to show only the path.
|
232
|
A-1 -> B-2 -> C-1
|
233
|
-> C-2 (CURRENT)
|
234
|
-> D-1
|
235
|
-> D-2
|
236
|
-> C-3
|
237
|
|
238
|
GROUP 2
|
239
|
-------
|
240
|
All of these flags are optional. Any number of them may be combined.
|
241
|
|
242
|
SM2_NUMCLASS Add the numbered menu classes to the menu. If this
|
243
|
flag is supplied, the "menu-N" and "menu-child-N"
|
244
|
classes will be added.
|
245
|
SM2_ALLINFO Load all fields from the page table of the database.
|
246
|
This will result in quite a lot of memory being used
|
247
|
and is not recommended, however it will make keywords,
|
248
|
descriptions, and other fields available. This data
|
249
|
is not loaded by default.
|
250
|
NOTE: This flag must be used on the *FIRST* call to
|
251
|
show_menu2 *for this menu ID*, or in combination with
|
252
|
SM2_NOCACHE otherwise it will have no effect.
|
253
|
SM2_NOCACHE Do not reuse or store the data read from the database
|
254
|
between calls to show_menu2.
|
255
|
SM2_PRETTY Pretty print the menu HTML with spacing and newlines
|
256
|
for debugging purposes.
|
257
|
SM2_BUFFER Do not output the menu HTML but instead buffer it
|
258
|
internally and return it as a string from show_menu2.
|
259
|
SM2_CURRTREE Exclude all other top level menus from being considered.
|
260
|
Only items in the current menu tree will be output.
|
261
|
This can be combined with any of the Group 1 flags as
|
262
|
necessary.
|
263
|
SM2_ESCAPE Call htmlspecialchars on the menu strings. This may be
|
264
|
required with older installations of WB. By escaping the
|
265
|
raw database strings, it permits menus to have HTML
|
266
|
formatting in them that would cause otherwise cause
|
267
|
pages to fail validation.
|
268
|
SM2_NOESCAPE Default behaviour. Exists only for backwards compatibility.
|
269
|
|
270
|
|
271
|
$aItemOpen
|
272
|
Format string to use for creating each individual menu item entry.
|
273
|
A different format string may be used for the very first entry by
|
274
|
supplying a different format string for $aTopItemOpen. When set to
|
275
|
false, it uses the default of '[li][a][menu_title]</a>' to maintain
|
276
|
compatibility with show_menu(). Note however that CSS formatting is
|
277
|
often easier if the classes are added to the <a> tag. Use the format
|
278
|
string of '<li>[ac][menu_title]</a>' for this style of tag.
|
279
|
|
280
|
This parameter may also be specified as an instance of a formatting
|
281
|
class for the menu. See the section "Formatter" below for details of
|
282
|
the API this class must expose. When a formatter is supplied, all
|
283
|
arguments after $aItemOpen are ignored.
|
284
|
|
285
|
$aItemClose
|
286
|
String used to close each item. Note that this is not a format
|
287
|
string and no keywords will be replaced. When set to false, it uses
|
288
|
the default of '</li>'.
|
289
|
|
290
|
$aMenuOpen
|
291
|
Format string to use for opening a list of menu item entries. A
|
292
|
different format string may be used for the very first menu by
|
293
|
supplying a different format string for $aTopMenuOpen. When set to
|
294
|
false, it uses the default of '[ul]'.
|
295
|
|
296
|
$aMenuClose
|
297
|
String used to close each menu. Note that this is not a format
|
298
|
string and no keywords will be replaced. When set to false, it uses
|
299
|
the default of '</ul>'.
|
300
|
|
301
|
$aTopItemOpen
|
302
|
Format string for the first item. When set to false, it uses the same
|
303
|
format as $aItemOpen.
|
304
|
|
305
|
$aTopMenuOpen
|
306
|
Format string for the first menu. When set to false, it uses the same
|
307
|
format as $aMenuOpen.
|
308
|
|
309
|
|
310
|
|
311
|
FORMAT STRINGS
|
312
|
==============
|
313
|
The following tags may be included in the format strings for $aItemOpen and
|
314
|
$aMenuOpen and will be replaced with the appropriate text.
|
315
|
|
316
|
[a] <a> tag (no class): '<a href="[url]" target="[target]">'
|
317
|
[ac] <a> tag including class: '<a href="[url]" target="[target]" class="[class]">'
|
318
|
[li] <li> tag including class: '<li class="[class]">'
|
319
|
[ul] <ul> tag including class: '<ul class="[class]">'
|
320
|
[class] List of classes for that page
|
321
|
[menu_title] Menu title text (HTML entity escaped unless SM2_NOESCAPE flag is used)
|
322
|
[page_title] Page title text (HTML entity escaped unless SM2_NOESCAPE flag is used)
|
323
|
[url] Page URL for the <a> tag
|
324
|
[target] Page target for the <a> tag
|
325
|
[page_id] Page ID of the current menu item
|
326
|
[parent] Page ID of the parent menu item
|
327
|
[level] Page level, the same number as is used for the "menu-N" CSS tag.
|
328
|
[sib] Current menu sibling number
|
329
|
[sibCount] Total number of siblings in this menu
|
330
|
[if] Conditional test (see section CONDITIONAL FORMATTING)
|
331
|
|
332
|
The following tags are only available when the SM2_ALLINFO flag is used.
|
333
|
|
334
|
[description] Page description
|
335
|
[keywords] Page keywords
|
336
|
|
337
|
|
338
|
CONDITIONAL FORMATTING
|
339
|
======================
|
340
|
The conditional formatting directive takes one of the following forms:
|
341
|
|
342
|
[if(A){B}]
|
343
|
[if(A){B}else{C}]
|
344
|
|
345
|
A Conditional test. See below for more details.
|
346
|
|
347
|
B Expression emitted when the if-test is true. This may be any string
|
348
|
that does NOT include the '}' character. It may include any of the
|
349
|
format strings described in the section FORMAT STRINGS with the
|
350
|
exception of the conditional test (because '}' is not permitted).
|
351
|
|
352
|
C Expression emitted when the if-test is false. This may be any string
|
353
|
that does NOT include the '}' character. It may include any of the
|
354
|
format strings described in the section FORMAT STRINGS with the
|
355
|
exception of the conditional test (because '}' is not permitted).
|
356
|
|
357
|
The conditional test is a combination of one or more boolean tests.
|
358
|
If more than one test is supplied, it must be combined with other tests
|
359
|
using either || (boolean OR) or && (boolean AND).
|
360
|
|
361
|
A single test is made up of the left operand, operator and right operand.
|
362
|
e.g. X == Y where X is the left operand, == is the operator and Y is the
|
363
|
right operand.
|
364
|
|
365
|
Left operand. It must be one of the following keywords:
|
366
|
class Test for existence of one of the classes. Only the
|
367
|
"==" and "!=" operators are permitted. In this case
|
368
|
these operators have the meaning of "includes"
|
369
|
instead of "equals".
|
370
|
level Test against the page level.
|
371
|
sib Test against the current page sibling number.
|
372
|
sibCount Test against the number of siblings in the menu.
|
373
|
id Test against the page id.
|
374
|
|
375
|
Operator. It must be one of the following:
|
376
|
< Less Than
|
377
|
<= Less Than Equals
|
378
|
== Equals
|
379
|
!= Not Equal
|
380
|
>= Greater Than Equals
|
381
|
> Greater Than
|
382
|
|
383
|
Right operand. The type of this operand depends on the keyword used
|
384
|
for the left operand:
|
385
|
class One of the "menu-*" class names as listed in the
|
386
|
section "OUTPUT".
|
387
|
level Test the page level against the following values:
|
388
|
<number> absolute page level
|
389
|
root the root page level
|
390
|
granny the grand-parent page level
|
391
|
parent the parent page level
|
392
|
current the current page level
|
393
|
child the child page level
|
394
|
id Test the page id against the following values:
|
395
|
<number> absolute page id
|
396
|
parent the parent page id
|
397
|
current the current page id
|
398
|
sib A positive integer, or "sibCount" to test against
|
399
|
the count of siblings in this menu.
|
400
|
sibCount A positive integer.
|
401
|
|
402
|
For example, valid tests are expression "exp" is emitted only when the menu item:
|
403
|
|
404
|
[if(class==menu-expand){exp}] has a sub-menu
|
405
|
[if(class==menu-first){exp}] is first item in a menu
|
406
|
[if(class!=menu-first){exp}] is NOT first item in a menu
|
407
|
[if(class==menu-last){exp}] is last item in a menu
|
408
|
[if(level==0){exp}] is at the root
|
409
|
[if(level>0){exp}] is not at the root
|
410
|
[if(sib==2){exp}] is the second item in a menu
|
411
|
[if(sibCount>1){exp}] is in a menu with more than 1 entry
|
412
|
[if(sibCount!=2){exp}] is in a menu which doesn't have exactly
|
413
|
[if(level>parent){exp}] is in a sibling menu or child of a sibling
|
414
|
[if(id==parent){exp}] is the parent of the current page
|
415
|
|
416
|
If an else-clause was added, then the expression for the else would be
|
417
|
emitted in all other cases. For example the expression "foo" is emitted
|
418
|
whenever the if-test is false, so therefore:
|
419
|
|
420
|
[if(sib==2){exp}else{foo}] is NOT the second item in a menu
|
421
|
[if(sibCount>2){exp}else{foo}] is NOT in a menu with more than 2 entries
|
422
|
|
423
|
For multiple tests, the expression "exp" is emitted only when the menu item:
|
424
|
|
425
|
[if(sib == 1 || sib > 3){exp}]
|
426
|
[is the first item] OR [is the 4th or larger item] in the menu
|
427
|
|
428
|
[if(id == current && class == menu-expand){exp}
|
429
|
[is the current item] AND [it has children]
|
430
|
|
431
|
Note that all tests are evaluated in the order listed because:
|
432
|
* there is no short-circuit evaluation (all individual tests are always evaluated)
|
433
|
* there is no grouping of tests (i.e. no support for parenthesis)
|
434
|
* both || and && are considered the same level
|
435
|
|
436
|
|
437
|
|
438
|
FORMATTER
|
439
|
=========
|
440
|
Note: This is an advanced and rarely needed feature!
|
441
|
|
442
|
If you are capable of extensive PHP programming, it is possible to replace the
|
443
|
predefined menu formatter that show_menu2 is uses with a custom module. See the
|
444
|
include.php file of show_menu2 for an example of how the menu formatter must be
|
445
|
written. The API it must use is:
|
446
|
|
447
|
class SM2_Formatter
|
448
|
{
|
449
|
// called once before any menu is processed to allow object initialization
|
450
|
function initialize() { }
|
451
|
|
452
|
// called to open the menu list
|
453
|
function startList($aPage, $aUrl) { }
|
454
|
|
455
|
// called to open the menu item
|
456
|
function startItem($aPage, $aUrl, $aCurrSib, $aSibCount) { }
|
457
|
|
458
|
// called to close the menu item
|
459
|
function finishItem() { }
|
460
|
|
461
|
// called to close the menu list
|
462
|
function finishList() { }
|
463
|
|
464
|
// called once after all menu has been processed to allow object finalization
|
465
|
function finalize() { }
|
466
|
|
467
|
// called once after finalize() if the SM2_NOOUTPUT flag is used
|
468
|
function getOutput() { }
|
469
|
};
|
470
|
|
471
|
|
472
|
|