-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMarkupMenu.module.php
507 lines (426 loc) · 18.4 KB
/
MarkupMenu.module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?php
namespace ProcessWire;
/**
* MarkupMenu ProcessWire module
*
* MarkupMenu is a module for generating menu markup. See README.md for more details.
* Some ideas and code in this module are based on the Markup Simple Navigation module.
*
* @version 1.2.0
* @author Teppo Koivula <[email protected]>
* @license Mozilla Public License v2.0 http://mozilla.org/MPL/2.0/
*/
class MarkupMenu extends WireData implements Module {
/**
* Default options
*
* @var array
*/
public static $defaultOptions = [
'root_page' => null,
'menu_items' => null,
'current_page' => null,
'templates' => [
'nav' => '<nav class="{classes}">%s</nav>',
'list' => '<ul class="{classes} {class}--level-{level}">%s</ul>',
'list_item' => '<li class="{classes} {class}--level-{level}">%s</li>',
'item' => '<a href="{item.url}" class="{classes} {class}--level-{level}">{item.title}</a>',
'item_current' => '<span class="{classes} {class}--level-{level}">{item.title}</span>',
],
'include' => [
'selector' => null,
'root_page' => false,
],
'exclude' => [
'selector' => null,
'listable' => false,
'level_greater_than' => null,
],
'collapsed' => true,
'flat_root' => true,
'placeholder_options' => [],
'placeholders' => [],
'classes' => [
// 'page_id' => '&--page-id-',
'nav' => 'menu',
'list' => 'menu__list',
'list_item' => 'menu__list-item',
'item' => 'menu__item',
'item_current' => 'menu__item',
'current' => '&--current',
'parent' => '&--parent',
'has_children' => '&--has-children',
],
'array_item_keys' => [
'id' => 'id',
'is_current' => 'is_current',
'is_parent' => 'is_parent',
'children' => 'children',
],
];
/**
* Render menu markup
*
* @param array $options Custom options
* @return string Rendered menu markup
*/
public function render(array $options = []): string {
// merge options with default options and config options
$options = array_replace_recursive(
static::$defaultOptions,
is_array($this->wire('config')->MarkupMenu) ? $this->wire('config')->MarkupMenu : [],
$options
);
// get the root page
$options['root_page'] = $this->getPage($options['root_page'], empty($options['menu_items']) ? '/' : null);
// get current page
$options['current_page'] = $this->getPage($options['current_page']);
// load MarkupMenuData
require_once __DIR__ . '/MarkupMenuData.php';
// generate menu markup
$menu = '';
if (!empty($options['root_page'] || !empty($options['menu_items']))) {
if (is_array($options['menu_items'])) {
$menu = $this->renderArray($options, null, $options['menu_items']);
} else {
$menu = $this->renderTree($options, $options['root_page'], $options['menu_items']);
}
}
return $menu;
}
/**
* Render menu from fixed array of items
*
* @param array $options Options for rendering
* @param array|null $root Root item for the menu
* @param array $items Menu items
* @param int $level Current tree level (depth)
* @return string Rendered menu markup
*/
protected function renderArray(array $options = [], ?array $root, array $items, int $level = 1): string {
$out = '';
// iterate items and render markup for each separately
foreach ($items as $item) {
$out .= $this->renderArrayItem($options, $item, $root, $level);
}
if (!empty($out) && (!empty($options['templates']['list']) || !empty($options['templates']['nav']))) {
// set up a placeholders
$placeholders = [
'level' => $level,
'root_page' => $options['root_page'],
'root' => $root,
];
// generate list markup
$out = $this->applyTemplate('list', $placeholders, $options, null, $out);
// generate nav markup
if ($level === 1) {
$out = $this->applyTemplate('nav', $placeholders, $options, null, $out);
}
}
return $out;
}
/**
* Render tree of items using recursion
*
* @param array $options Options for rendering
* @param Page|null $root Root page for the menu
* @param PageArray|null $items Menu items
* @param int $level Current tree level (depth)
* @return string Rendered menu markup
*/
protected function renderTree(array $options = [], Page $root = null, PageArray $items = null, int $level = 1): string {
$out = '';
// get items and make sure that root page is only prepended once
if (empty($items)) {
$items = $this->getItems($options, $root, $level);
$options['include']['root_page'] = false;
}
// iterate items and render markup for each separately
foreach ($items as $item) {
$out .= $this->renderTreeItem($options, $item, $root, $level);
}
if (!empty($out) && (!empty($options['templates']['list']) || !empty($options['templates']['nav']))) {
// set up a placeholders
$placeholders = [
'level' => $level,
'root_page' => $options['root_page'],
'root' => $root,
];
// generate list markup
$out = $this->applyTemplate('list', $placeholders, $options, null, $out);
// generate nav markup
if ($level === 1) {
$out = $this->applyTemplate('nav', $placeholders, $options, null, $out);
}
}
return $out;
}
/**
* Get menu items for rendering
*
* @param array $options Options array
* @param Page|null $root Root page for the menu
* @param int $level Current tree level (depth)
* @return PageArray Menu items
*/
protected function ___getItems(array $options, Page $root = null, int $level): PageArray {
// fetch items (children of the root page), optionally filtered by a selector string
$items = new PageArray();
if (!empty($root) && (!$options['include']['root_page'] || $options['flat_root'])) {
$items->add($root->children($this->getSelector($root, 'include', $options)));
}
// optionally prepend the root page itself
if ($options['include']['root_page'] && !empty($root)) {
$items->prepend($root);
}
// exclude rules based on selector string
$exclude_selector = $this->getSelector($root, 'exclude', $options);
if (!empty($exclude_selector)) {
$items->not($exclude_selector);
}
return $items;
}
/**
* Render markup for a single array item
*
* @param array $options Options for rendering
* @param array $item Menu item being rendered
* @param array|null $root Root item for the menu
* @param int $level Current tree level (depth)
* @return string Rendered menu item markup
*/
protected function ___renderArrayItem(array $options = [], array $item = null, ?array $root = null, int $level = 1): string {
$out = '';
// bail out early if item is empty
if (empty($item)) {
return $out;
}
// instantiate new MarkupMenuData object and populate with item properties
$data_item = new MarkupMenuData($item);
// array item keys
$keys = $options['array_item_keys'];
// default classes
$classes = [];
if (!empty($options['classes']['page_id']) && !empty($item[$keys['id']])) {
$classes['page_id'] = $options['classes']['page_id'] . $item[$keys['id']];
}
// is this current page?
$item_is_current = $item[$keys['is_current']] ?? (!empty($item[$keys['id']]) && $options['current_page'] && $options['current_page']->id === $item[$keys['id']]);
if ($item_is_current) $classes['current'] = $options['classes']['current'];
// is this a parent page?
$item_is_parent = $item[$keys['is_parent']] ?? (
!$item_is_current && $this->arrayItemIsParent($options, $item, $root)
);
if ($item_is_parent) $classes['parent'] = $options['classes']['parent'];
// have we reached the level limit?
$level_limit_reached = $options['exclude']['level_greater_than'] && $level >= $options['exclude']['level_greater_than'];
// does this page have children?
$has_children = (!empty($root) && !empty($item[$keys['id']]) && $item[$keys['id']] !== $root[$options['array_item_keys']['id']] || !$options['flat_root']) && !$level_limit_reached && !empty($item[$keys['children']]);
if ($has_children) $classes['has_children'] = $options['classes']['has_children'];
// should we render the children for this item?
$with_children = $has_children && (!$options['collapsed'] || $item_is_current || $item_is_parent);
// placeholders for string replacements
$placeholders = array_merge(
[
'level' => $level,
'item' => $data_item,
'classes' => $classes,
],
$options['placeholders']
);
// generate markup for menu item
$item_template_name = 'item' . ($item_is_current ? '_current' : '');
$item_markup = $this->applyTemplate($item_template_name, $placeholders, $options, $data_item);
// generate markup for menu item children
if ($with_children) {
$item_markup .= $this->renderArray($options, $item, $item[$keys['children']], $level + 1);
}
// generate markup for current list item
$out .= $this->applyTemplate('list_item', $placeholders, $options, $data_item, $item_markup);
return $out;
}
/**
* Render markup for a single menu item
*
* @param array $options Options for rendering
* @param Page $item Menu item being rendered
* @param Page|null $root Root page for the menu
* @param int $level Current tree level (depth)
* @return string Rendered menu item markup
*/
protected function ___renderTreeItem(array $options = [], Page $item, Page $root = null, int $level = 1): string {
$out = '';
// exclude rules based on listability
if (isset($options['exclude']['listable']) && $item->listable() == $options['exclude']['listable']) {
return $out;
}
// default classes
$classes = [];
if (!empty($options['classes']['page_id'])) {
$classes['page_id'] = $options['classes']['page_id'] . $item->id;
}
// is this current page?
$item_is_current = $options['current_page'] && $options['current_page']->id === $item->id;
if ($item_is_current) $classes['current'] = $options['classes']['current'];
// is this a parent page?
$item_is_parent = !$item_is_current && $this->treeItemIsParent($options, $item, $root);
if ($item_is_parent) $classes['parent'] = $options['classes']['parent'];
// have we reached the level limit?
$level_limit_reached = $options['exclude']['level_greater_than'] && $level >= $options['exclude']['level_greater_than'];
// does this page have children?
$has_children_selector = $this->getSelector($item, 'include', $options) ?: true;
$has_children = (!empty($root) && $item->id !== $root->id || !$options['flat_root']) && !$level_limit_reached && $item->hasChildren($has_children_selector);
if ($has_children) $classes['has_children'] = $options['classes']['has_children'];
// should we render the children for this item?
$with_children = $has_children && (!$options['collapsed'] || $item_is_current || $item_is_parent);
// placeholders for string replacements
$placeholders = array_merge(
[
'level' => $level,
'item' => $item,
'classes' => $classes,
],
$options['placeholders']
);
// generate markup for menu item
$item_template_name = 'item' . ($item_is_current ? '_current' : '');
$item_markup = $this->applyTemplate($item_template_name, $placeholders, $options, $item);
// generate markup for menu item children
if ($with_children) {
$item_markup .= $this->renderTree($options, $item, null, $level + 1);
}
// generate markup for current list item
$out .= $this->applyTemplate('list_item', $placeholders, $options, $item, $item_markup);
return $out;
}
/**
* Check if a tree item is a parent of current page
*
* @param array $options Options for rendering
* @param Page $item Menu item being rendered
* @param Page|null $root Root page for the menu
* @return bool
*/
protected function ___treeItemIsParent(array $options = [], Page $item, ?Page $root = null): bool {
return (!empty($root) && $item->id !== $root->id || !$options['flat_root'])
&& $options['current_page']
&& $options['current_page']->parents->has($item);
}
/**
* Check if an array item is a parent of current page
*
* @param array $options Options for rendering
* @param array $item Menu item being rendered
* @param array|null $root Root item for the menu
* @return bool
*/
protected function ___arrayItemIsParent(array $options = [], array $item, ?array $root = null): bool {
return !empty($item[$options['array_item_keys']['id']])
&& (!empty($root) && $item[$options['array_item_keys']['id']] !== $root[$options['array_item_keys']['id']] || !$options['flat_root'])
&& $options['current_page']
&& $options['current_page']->parents->has("id=" . $item[$options['array_item_keys']['id']]);
}
/**
* Get selector for specific menu item
*
* @param Page|null $item
* @param string $context 'include' or 'exclude'
* @param array $options
* @return string|null
*/
protected function ___getSelector(?Page $item = null, string $context, array $options = []): ?string {
return $options[$context]['selector'] ?? null;
}
/**
* Get a Page based on mixed source param
*
* @param mixed $source Page source argument
* @param mixed $default Optional default page in case source is empty
* @return null|Page Page object or null
*/
protected function getPage($source = null, $default = null): ?Page {
$page = null;
if ($source instanceof Page) {
$page = $source;
} else if (is_int($source)) {
$page = $this->wire('pages')->findOne('id=' . $source);
} else if (is_string($source)) {
$page = $this->wire('pages')->findOne($source);
} else if (!empty($default)) {
$page = $this->getPage($default);
}
return $page;
}
/**
* Get template for rendering an element
*
* @param string $template_name Template name
* @param Page|MarkupMenuData|null $item Item being rendered
* @param array $options An array of options
* @param array $placeholders Array of placeholders for string replacements
* @return string Template
*/
protected function ___getTemplate($template_name, ?WireData $item = null, array $options = [], array $placeholders = []): string {
$template = $options['templates'][$template_name];
return is_callable($template) ? $template($item, $options, $placeholders) : $template;
}
/**
* Apply a template to content string
*
* @param string $template_name Name of the template
* @param array $placeholders Array of placeholders for string replacements
* @param array $options An array of options
* @param Page|MarkupMenuData|null $item Item being rendered
* @param string|null $content Content to be wrapped in template
* @return string Content either wrapped in template, or as-is if no template was defined
*/
protected function applyTemplate(string $template_name, array $placeholders, array $options, ?WireData $item = null, ?string $content = null): string {
$out = '';
$template = $this->getTemplate($template_name, $item, $options, $placeholders);
if (!empty($template)) {
$placeholders['class'] = $placeholders['classes'][$template_name] ?? $options['classes'][$template_name] ?? null;
$placeholders['classes'] = $this->parseClassesString($placeholders, $options, $template_name);
$out = wirePopulateStringTags(
$template,
new MarkupMenuData(array_merge(
$placeholders,
$options['placeholders']
)),
$options['placeholder_options']
);
if ($content !== null) {
$out = sprintf($out, $content);
}
}
return $out;
}
/**
* Parse classes array to a string, adding template class and processing self-references
*
* @param array $placeholders
* @param array $options
* @param string $template_name
* @return string Parsed classes string
*/
protected function parseClassesString(array $placeholders, array $options, string $template_name): string {
$out = '';
// get classes array
$classes = [];
if (!empty($placeholders['classes']) && is_array($placeholders['classes'])) {
$classes = $placeholders['classes'];
}
// add template name class (if available)
$template_name_class = $classes[$template_name] ?? $options['classes'][$template_name] ?? null;
if (!empty($template_name_class)) {
array_walk($classes, function(&$class) use ($template_name_class) {
$class = str_replace('&', $template_name_class, $class);
});
$classes[$template_name] = $template_name_class;
}
// convert classes array to string
if (!empty($classes)) {
$out = implode(' ', array_filter($classes));
}
return $out;
}
}