-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProcessMenuBuilder.module
3129 lines (2464 loc) · 111 KB
/
ProcessMenuBuilder.module
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php namespace ProcessWire;
/**
* Process Menu Builder Module for ProcessWire
* This module enables you to create custom menus for your website using drag and drop in the ProcessWire Admin Panel
*
* @author Francis Otieno (Kongondo)
*
* https://github.com/kongondo/ProcessMenuBuilder
* Created 4 August 2013
* Major update in March 2015
*
* ProcessWire 3.x
* Copyright (C) 2016 by Ryan Cramer
*
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
*
*/
class ProcessMenuBuilder extends Process implements Module {
/**
* Return information about this module (required)
*
* @access public
*
*/
public static function getModuleInfo() {
// @User role needs 'menu-builder' permission
// @$permission = 'menu-builder';
// @Installs MarkupMenuBuilder
return array(
'title' => 'Menu Builder: Process',
'summary' => 'Easy, drag and drop menu builder',
'author' => 'Francis Otieno (Kongondo)',
'version' => '0.2.7',
'href' => 'http:// processwire.com/talk/topic/4451-module-menu-builder/',
'singular' => true,
'autoload' => false,
'permission' => 'menu-builder',
'installs' => 'MarkupMenuBuilder'
);
}
const PAGE_NAME = 'menu-builder';
/**
* Property to return this module's admin page (parent of all menus).
*
*/
protected $menusParent;
/**
* Property to store include children setting (boolean).
*
*/
private $includeChildren;
/**
* Property to store disable items setting (boolean).
*
*/
private $disableItems;
/**
* string name of the cookie used to save limit of posts to show per page in posts dashboard.
*
*/
private $cookieName;
/**
* int value of number of menus to show per dashboard page.
*
*/
private $showLimit;
// other single menu properties
private $menuItems;
private $menuPages;
private $menuSettings;
// multilingual
private $multilingual;// bool to check if in multilingual environment
private $menuItemsLanguages;
/**
* Initialise the module. This is an optional initialisation method called before any execute methods.
*
* Initialises various class properties ready for use throughout the class.
*
* @access public
*
*/
public function init() {
$user = $this->wire('user');
if ($this->wire('permissions')->get('menu-builder')->id && !$user->hasPermission('menu-builder'))
throw new WirePermissionException("You have no permission to use this module");
$this->wire('modules')->get('JqueryWireTabs');
$config = $this->wire('config');
$config->scripts->add($this->config->urls->ProcessMenuBuilder . 'scripts/jquery.mjs.nestedSortable.js');
$config->scripts->add($this->config->urls->ProcessMenuBuilder . 'scripts/jquery.asmselect-mb.js');
$this->multilingual = $this->wire('languages') ? true : false;
$this->menusParent = $this->wire('page');
// cookie per user to save state of number of menus to display per pagination screen in execute()
$this->cookieName = $user->id . '-menubuilder';
// default number of menus to show in menu builder landing page if no custom limit set (via post/session cookie).
$this->showLimit = 10;
parent::init();
}
/* ######################### - MARKUP BUILDERS - ######################### */
/**
* Displays a list of the menus.
*
* This function is executed when a menu with Menu Builder Process assigned is accessed.
*
* @access public
* @return string $form Form markup.
*
*/
public function ___execute() {
$modules = $this->wire('modules');
$post = $this->wire('input')->post;
// CREATE A NEW FORM
$form = $modules->get('InputfieldForm');
$form->attr('id', 'menu-builder');
$form->action = './';
$form->method = 'post';
// CREATE A NEW WRAPPER
$w = new InputfieldWrapper;
// quick create menu markup
$w->add($this->buildQuickCreateMenuMarkup());
// menus table/list markup
$w->add($this->buildMenusTableMarkup());
// actions markup
if ($this->menusTotal !=0) $w->add($this->buildMenusActionsMarkup());
// add to form for rendering
$form->add($w);
// send input->post values to the Method save();
if($post->menus_action_btn || $post->menu_new_unpublished_btn || $post->menu_new_published_btn) $this->save($form);
// render the final form
return $form->render();
}
/**
* Renders a single menu for editing.
*
* Called when the URL is Menu Builders page URL + "/edit/"
* note: matches what is appended after ___execute below.
*
* @access public
* @return string $form Form markup.
*
*/
public function ___executeEdit() {
$modules = $this->wire('modules');
$post = $this->input->post;
// get the menu (page) we are editing
$menuID = (int) $this->wire('input')->get->id;
$menu = $this->wire('pages')->get("id=$menuID, parent=$this->menusParent, include=all");// only get menu pages!
$form = $modules->get('InputfieldForm');
// if we found a valid menu page
if($menu->id) {
// menu settings
$this->menuSettings = $menu->menu_settings ? json_decode($menu->menu_settings, true) : array();
// fetch this menu's JSON string with menu pages properties (pages find selector and inputfield to use)
$this->menuPages = $menu->menu_pages ? json_decode($menu->menu_pages, true) : array();
// fetch this menu's JSON string with menu items properties
$this->menuItems = $menu->menu_items ? json_decode($menu->menu_items, true) : array();
// if multilingual, active MB languages
$this->menuItemsLanguages = isset($this->menuPages['menu_items_languages']) ? $this->menuPages['menu_items_languages'] : null;
##############################
$this->nestedSortableConfigs();
$this->menuConfigs();// @note: we check if user has right permission in the method itself
// check if menu is published or not
$menu->is(Page::statusUnpublished) ? $pubStatus = 1 : $pubStatus = '';
// check if menu is locked for editing
$menu->is(Page::statusLocked) ? $editStatus = 1 : $editStatus = '';
$editStatusNote = $editStatus ? $this->_(' (locked)') : '';
// add a breadcrumb that returns to our main page @todo - don't show non-superadmins breadcrumbs?
$this->breadcrumbs->add(new Breadcrumb('../', $this->wire('page')->title));
// headline when editing a menu
// @todo: delete this old style since now we show warning message?
//$this->headline(sprintf(__('Edit menu: %s'), $menu->title) . $editStatusNote);
$this->headline(sprintf(__('Edit menu: %s'), $menu->title));
$form->attr('id', 'MenuBuilderEdit');
$form->action = './';
$form->method = 'post';
############################################ - prep for tabs - ############################################
$menuPages = $this->menuPages;
// set include children + disable items status + for users with right permission
if(!empty($menuPages)) {
// enable include children feature
if(isset($menuPages['children']) && $this->wire('user')->hasPermission('menu-builder-include-children')) {
$this->includeChildren = $menuPages['children'];
}
// enable 'enable/disable' items feature
if(isset($menuPages['disable_items']) && $this->wire('user')->hasPermission('menu-builder-disable-items')) {
$this->disableItems = $menuPages['disable_items'];
}
}
############################################ - First Tab (build menu) - ############################################
// @note: only shown for menu that are not locked
if(!$menu->is(Page::statusLocked)) $form->add($this->editTabBuild());
############################################ - Second Tab (menu items overview) - ####################################
$form->add($this->editTabOverview());
############################################ - Third Tab (menu settings) - ###########################################
// @note: only shown for menu that are not locked
if(!$menu->is(Page::statusLocked)) $form->add($this->editTabMenuSettings($menu, $pubStatus, $editStatus));
############################################ - Fourth Tab (delete) - ############################################
// @note: only shown for menu that are not locked
if(!$menu->is(Page::statusLocked)) $form->add($this->editTabDelete($menu->id));
/***************** Add input buttons to Fourth tab *****************/
$m = $modules->get('InputfieldHidden');
$m->attr('name', 'menu_id');
$m->attr('value', $menuID);
$form->add($m);
// @note: only shown for menu that are not locked
if(!$menu->is(Page::statusLocked)) {
$m = $modules->get('InputfieldSubmit');
$m->class .= ' head_button_clone';
$m->attr('id+name', 'menu_save');
$m->class .= " menu_save";// add a custom class to this submit button
$m->attr('value', $this->_('Save'));
$form->add($m);
$m = $modules->get('InputfieldSubmit');
$m->attr('id+name', 'menu_save_exit');
$m->class .= " ui-priority-secondary";
$m->class .= " menu_save";// add a custom class to this submit button
$m->attr('value', $this->_('Save & Exit'));
$form->add($m);
}
// show an exit button for locked menus
else {
$m = $modules->get('InputfieldSubmit');
$m->attr('id+name', 'menu_locked_exit');
$m->class .= " ui-priority-secondary";
$m->class .= " menu_save";// add a custom class to this submit button
$m->attr('value', $this->_('Exit'));
$form->add($m);
// show menu locked warning
$this->warning($this->_('Menu Builder: This menu is locked for edits.'));
}
return $form->render();
}// end if $menu
############################################ - if input->post - ############################################
// if saving menu
elseif($post->menu_save || $post->menu_save_exit || $post->menu_delete) $this->save($form);
// else invalid menu ID or no ID provided (e.g. /edit/) or exiting 'view' a locked menu
else $this->wire('session')->redirect($this->wire('page')->url);// redirect to landing page
}
/**
* First tab contents for executeEdit()
*
* @access protected
* @return object $tab To render as markup.
*
*/
protected function editTabBuild() {
$modules = $this->wire('modules');
$menuPages = $this->menuPages;
// First Tab - Drag & Drop + add menu items. Only show if a menu exists
$tab = new InputfieldWrapper();
$tab->attr('title', $this->_('Build Menu'));
$id = $this->className() . 'Build';
$tab->attr('id', $id);
$tab->class .= " WireTab";
$m = $modules->get('InputfieldMarkup');
$m->label = $this->_('Add menu items');
$m->description = '<span id="add_menu_items"><a href="#" id="add_page_menu_items">' . $this->_('Pages') . '</a> ';
$m->description .= '<a href="#" id="add_custom_menu_items">' . $this->_('Custom') . '</a>';
if($this->user->hasPermission('menu-builder-selector')) $m->description .= '<a href="#" id="add_selector_menu_items">' . $this->_('Selector') . '</a>';
$m->description .= '</span>';
$m->textFormat = Inputfield::textFormatNone;// make sure ProcessWire renders the HTML
$tab->add($m);
// if user specified (hence limited) the pages selectable for adding to this menu, we use that
// if user has not specified pages to return, we find all pages (except admin pages [including trash]) but limit to 50.
// @note: user can override the 50 pages limit by adding their own 'limit=n' in $menuPages['sel']
$defaultSelector = "template!=admin, has_parent!=2, parent!=7, id!=27, limit=50";
if(isset($menuPages['sel'])) $pagesSelector = $defaultSelector . ', ' . $menuPages['sel'];
else $pagesSelector = $defaultSelector;
$description = $this->_('Select Pages to add to your menu. Optionally enter a CSS ID and single/multiple Classes.');
$extraNotes = $this->includeChildren ?
$this->_("The setting 'Level' is for use in conjunction with 'include children' with respect to your menu, i.e. the selections 'Menu' or 'Both'. The default level (i.e. how 'deep' to fetch descendant children, granchildren, etc. is 1. If that is what you want then you do not have to enter a level.") :
'';
$menuPagesInput = 1;
if (isset($menuPages['input']) && $menuPages['input'] == 2) {
// modify PageAutoComplete output
$this->wire('page')->addHookBefore("InputfieldPageAutocomplete::renderListItem", $this, "customAc");
// if we are using page autocomplete inputfield to find pages for menu items selection
$menuAddPageItems = $modules->get('InputfieldPageAutocomplete');
$menuAddPageItems->set('findPagesSelector', $pagesSelector);
$menuAddPageItems->notes = $this->_('Start typing to search for pages.' . "\n");
$menuAddPageItems->notes .= $extraNotes;
// we'll use this variable when saving PageAutocomplete values
// especially important for non-superusers since the radio input 'menu_pages_select' will not be output for them
$menuPagesInput = 2;
}
elseif (isset($menuPages['input']) && $menuPages['input'] == 3) {
// modify PageListSelectMultiple output
$this->wire('page')->addHookAfter("InputfieldPageListSelectMultiple::render", $this, "customPls");
$menuAddPageItems = $modules->get('InputfieldPageListSelectMultiple');
$menuAddPageItems->label = $this->_('Pages');
//$menuAddPageItems->set('parent_id', 1300);// @todo: configurable?
// see notes above about this variable
$menuPagesInput = 3;
}
// else we default to AsmSelect inputfield
else {
$opts = $this->wire('pages')->find($pagesSelector);
if(empty($opts)) {
$this->error($this->_('Menu Builder: Your selector did not find any selectable pages for your menu! Confirm its validity.'));
$description = $this->_('No pages were found to add to your menu. Rectify the specified error first.');
}
// modify AsmSelect output
$this->wire('page')->addHookAfter("InputfieldAsmSelect::render", $this, "customAsm");
$menuAddPageItems = $modules->get('InputfieldAsmSelect');
$menuAddPageItems->notes = $extraNotes;
foreach($opts as $opt) $menuAddPageItems->addOption($opt->id, $opt->title);
}
// Page Select to add menu items from pages
$menuAddPageItems->label = $this->_('Pages');
$menuAddPageItems->attr('name+id', 'item_addpages');
$menuAddPageItems->description = $description;
$tab->add($menuAddPageItems);// add page asmSelect/autocomplete/page list select multiple to markup
// hidden field to store value if using PageAutocomplete to select menu items from pages
$h = $modules->get('InputfieldHidden');
$h->attr('name', 'menu_pages_input');
$h->attr('value', $menuPagesInput);
$tab->add($h);// add hidden field to markup
// Add Custom menu items
$t = $modules->get('MarkupAdminDataTable');
$t->setEncodeEntities(false);
$t->setSortable(false);
$t->setClass('menu_add_custom_items_table');
$t->headerRow(array(
$this->_('Title'),
$this->_('Link'),
$this->_('CSS ID'),
$this->_('CSS Class'),
$this->_('New Tab'),
));
$n = $modules->get('InputfieldName');
$n->required = true;
$n->attr('name', 'new_item_custom_title[]');
$n->attr('class', 'new_custom');
$u = $modules->get('InputfieldURL');
$u->attr('name', 'new_item_custom_url[]');
$u->attr('class', 'new_custom');
$n2 = $modules->get('InputfieldName');
$n2->attr('name', 'new_css_itemid[]');
$n2->attr('class', 'new_custom');
$n3 = $modules->get('InputfieldName');
$n3->attr('name', 'new_css_itemclass[]');
$n3->attr('class', 'new_custom');
$itemCustomNewTab = "<input type='checkbox' name='new_newtab[]' value='0' class='newtab'>";
$itemCustomNewTabHidden = "<input type='hidden' name='new_newtab_hidden[]' value='0' class='newtabhidden'>";// force send a value for new tabs
$t->row(array(
$n->render(),
$u->render(),
$n2->render(),
$n3->render(),
$itemCustomNewTab . $itemCustomNewTabHidden,
'<a href="#" class="remove_row"><i class="fa fa-trash"></i></a>',
));
$addRow = "<a class='addrow' href='#'>" . $this->_('add row') . "</a>";
$m = $modules->get('InputfieldMarkup');
$m->attr('id', 'item_addcustom');
$m->label = $this->_('Custom links');
$m->description = $this->_('Add custom menu items. Title and Link are required.');
$m->attr('value', $addRow . $t->render());
$tab->add($m);
// Add menu items from pages returned by a selector
// only add for users with the permission 'menu-builder-selector' (it means that permission has to be created if it doesn't exist)
if ($this->wire('user')->hasPermission('menu-builder-selector')) {
$tx = $modules->get('InputfieldText');
$tx->label = $this->_('Pages search');
$tx->attr('name+id', 'item_addselector');
$tx->description = $this->_('Use a ProcessWire selector to find and add menu items.');
$tab->add($tx);
}
// Drag and drop to sort + reorder menu items area
$m = $modules->get('InputfieldMarkup');
$m->label = $this->_('Drag & Drop');
$m->skipLabel = Inputfield::skipLabelHeader;// we don't want a label displayed here
$m->attr('id', 'dragdrop');
$m->notes = $this->_('Add items to start building your menu. You can add both Pages (internal links) and Custom (external) links. Drag and drop each item in the order you wish.') . "\n";
$m->notes .= $this->_('Advanced optional settings can be edited by clicking the "down-arrow" button or the menu item label.');
// if menu populated, create nested list
$markup = '<h4>' . $this->_('No items have been added to this menu yet') . '</h4>';
if(!empty($this->menuItems)) {
$markup = '<div id="menu_sortable_wrapper">' .
'<a href="#" id="remove_menus">' . $this->_('Delete All') . '</a>' .
$this->listMenu(0) .
'</div>';
// add hidden markup for extra labels for all page select methods
$markup.= $this->buildExtraLabels();
// add hidden markup for extra input for page autocomplete
$markup.= $this->buildAsmExtraInputs();
}
$m->attr('value', $markup);
$tab->add($m);
return $tab;
}
/**
* Second tab contents for executeEdit().
*
* @access protected
* @return object $tab To render as markup.
*
*/
protected function editTabOverview() {
$modules = $this->wire('modules');
$menuItems = $this->menuItems;
// Third Tab - Menu item properties overview [read only]. Only show if a menu exists
$tab = new InputfieldWrapper();
$tab->attr('title', $this->_('Items Overview'));
$id = $this->className() . 'Overview';
$tab->attr('id', $id);
$tab->class .= ' WireTab';
// we'll use this to wrap the table below
$m = $modules->get('InputfieldMarkup');
$m->label = $this->_('Menu items');
$t = $modules->get('MarkupAdminDataTable');
$t->setEncodeEntities(false);
$t->setClass('menu_items_table no_disable');
$t->headerRow(array(
// $this->_('ID'),// id of item in menu; not PW page id!!!
$this->_('Title'),// for PW pages, actual title saved. The title can also be edited in the add menu item settings
$this->_('URL'),// path to PW pages + normal url for custom menu items
$this->_('Parent'),// parent in this menu! NOT PW PAGE PARENT!
$this->_('CSS ID'),
$this->_('CSS Class'),
$this->_('New Tab'),
$this->_('Type'),// custom or PW page
));
// fetch menu items and display their properties in the overview table
if(!empty($menuItems)) {
foreach ($menuItems as $menu => $menuItem) {
// if an internal PW page
if (isset($menuItem['pages_id'])) {
$itemURL = $this->wire('pages')->get($menuItem['pages_id'])->url;
$itemType = 'Page';
}
// else it is a custom menu
else {
$itemURL = $menuItem['url'];
$itemType = 'Custom';
}
// check if top tier menu item (has no parent) or below (has a parent)
if (isset($menuItem['parent_id'])) $itemParent = $menuItems[$menuItem['parent_id']]['title'];
else $itemParent = '';
// does this menu item link open in a new window or not (i.e. target='_blank') - for custom menu items only
$itemNewTab = isset($menuItem['newtab']) ? $this->_('Yes') : $this->_('No');
$itemCSSID = isset($menuItem['css_itemid']) ? $menuItem['css_itemid'] :'';
$itemCSSClass = isset($menuItem['css_itemclass']) ? $menuItem['css_itemclass'] : '';
$t->row(array(
$menuItem['title'],
$itemURL,
$itemParent,
$itemCSSID,
$itemCSSClass,
$itemNewTab,
$itemType)
);
}// end foreach
$m->attr('value', $t->render());
}// end if count $menuItems
else {
// give user feedback that no menu items have been added to this menu
$m->description = '<h4>' . $this->_('No items have been added to this menu yet.') . '</h4>';
$m->textFormat = Inputfield::textFormatNone;// make sure ProcessWire renders the HTML
}
$tab->add($m);
return $tab;
}
/**
* Third tab contents for executeEdit().
*
* @access protected
* @param Page $menu The menu being edited.
* @param string $unpublished String to show whether the menu being edited is unpublished.
* @param string $locked String to show whether the menu being edited is locked.
* @return object $tab To render as markup.
*
*/
protected function editTabMenuSettings($menu, $unpublished = null, $locked = null) {
$modules = $this->wire('modules');
$user = $this->wire('user');
$menuPages = $this->menuPages;
// Third Tab - Settings
$tab = new InputfieldWrapper();
$tab->attr('title', $this->_('Settings'));
$id = $this->className() . 'Settings';
$tab->attr('id', $id);
$tab->class .= ' WireTab';
// menu title
// @note: multi-lingual aware title field
$f = $modules->get('InputfieldPageTitle');
$f->attr('name', 'menu_title');
$f->label = $this->_('Menu title');
$f->useLanguages = true;
$f->required = true;
$f->attr('value', $menu->title);
// different description if in multi-lingual setting
if($this->multilingual) {
$description = $this->_('A menu title is required for at least the default language.');
}
else $description = $this->_('A menu title is required.');
$f->description = $description;
$notes = ($unpublished || $locked) ? $this->_('Menu status: ') : '';
if ($unpublished) $notes .= $this->_('Unpublished, ');
if ($locked) $notes .= $this->_('Locked');
$f->notes = rtrim($notes, ', ');
// if in multilingual site, set respective languages description values where available
if($this->multilingual) {
foreach ($this->wire('languages') as $language) {
// skip default language as already set above
if($language->name == 'default') continue;
$langTitle = $menu->getLanguageValue($language, 'title');
// set title in the language
$f->set("value$language->id", $langTitle);
}
}
$tab->add($f);
// display configurable backend menu settings for users with right permissions
// options for nestedSortable + ProcessWire selector for pages selectable in $menuAddPageItems AsmSelect
// if this user has permission to SPECIFY pages selectable as menu items in AsmSelect and PageAutocomplete
if($user->hasPermission('menu-builder-selectable')) {
// if selector to find pages to add to menu specified
$selectorValue = isset($menuPages['sel']) ? $menuPages['sel'] : '';
$tx = $modules->get('InputfieldText');
$tx->attr('name', 'menu_pages');
$tx->label = $this->_('Pages selectable in menu');
$tx->attr('value', $selectorValue);
$tx->description = $this->_('Optionally, you can specify a valid ProcessWire selector to limit the Pages that can be added to this menu (see Build Menu Tab). Otherwise, all valid pages will be available to add to the menu. By default, returned pages are limited to 50. You can override this by setting your own limit here. NOTE: This feature only works with Asm Select and Page Auto Complete.');
$tx->notes = $this->_('Example: parent=/products/, template=product, sort=title');
$tab->add($tx);
}// end if user has permission menu-builder-selectable
// if user has permission to allow changing of page field type used to select pages to add as menu items [AsmSelect vs PageAutocomplete]
if($user->hasPermission('menu-builder-page-field')) {
// only 'PageAutocomplete' and PageListSelectMultiple options are saved in the field menu_pages (JSON): 'input'=> 2 || 3
// else we assume default 'input' => 1 (AsmSelect)
$pageSel = isset($menuPages['input']) && (($menuPages['input'] == 2) || ($menuPages['input'] == 3)) ? $menuPages['input'] : 1;
// radios: page inputfield selection
$r = new InputfieldRadios();
$r->attr('id+name', 'menu_pages_select');
$r->label = $this->_('Choose a method for selecting pages to add to your menu');
$r->notes = $this->_('If you will have a large selection of pages to choose from, you may want to use Page Auto Complete.');
$radioOptions = array (
1 => $this->_('Asm Select'),
2 => $this->_('Page Auto Complete'),
3 => $this->_('Page List Select Multiple'),
);
$r->addOptions($radioOptions);
$r->value = $pageSel;
$tab->add($r);
}// end if user has permision menu-builder-page-field
// if user can change and use allow markup/HTML setting
if($user->hasPermission('menu-builder-markup')) {
// only 'Allow Markup' (Yes) option is saved in the field menu_pages (JSON): 'markup'=> 2. Else we assume default 'markup' => 1 (No)
$allowMarkup = isset($menuPages['markup']) ? 1 : 2;
// radios: allow markup in menu item title/label
$r = new InputfieldRadios();
$r->attr('id+name', 'menu_item_title_markup');
$r->label = $this->_('Allow HTML in menu items title');
$r->notes = $this->_('Example: <span>Home</span>. If you allow this, the HTML will be run through HTML purifier before saving. Take care not to input malformed HTML.');
$radioOptions = array (
1 => $this->_('Yes'),
2 => $this->_('No'),
);
$r->addOptions($radioOptions);
$r->value = $allowMarkup;
$tab->add($r);
}// end if user can change and use allow markup/HTML setting
// if user can edit and use 'include children' feature
if($user->hasPermission('menu-builder-include-children')) {
// only 'Allow Include Children' (Yes) option is saved in the field menu_pages (JSON): 'markup'=> 2. Else we assume default 'markup' => 1 (No)
$includeChildren = isset($menuPages['children']) ? 1 : 2;
// radios: enable include children feature
$r = new InputfieldRadios();
$r->attr('id+name', 'menu_item_include_children');
$r->label = $this->_('Use include children feature');
$r->notes = $this->_('This feature allows you to designate menu items that can have their natural ProcessWire pages descendants included in the menu/breadcrumbs output in the frontend without actually including those pages here in Menu Builder. Be careful when using the feature as you could potentially output a very large amount of menu items than intended.');
$radioOptions = array (
1 => $this->_('Yes'),
2 => $this->_('No'),
);
$r->addOptions($radioOptions);
$r->value = $includeChildren;
$tab->add($r);
}// end if user can change and use allow markup/HTML setting
// if user can edit and use 'disable menu items' feature
if($user->hasPermission('menu-builder-disable-items')) {
// only 'Enable Disable Item' options
$disableItems = isset($menuPages['disable_items']) ? 1 : 2;
$r = new InputfieldRadios();
$r->attr('id+name', 'menu_item_disable_items');
$r->label = $this->_('Use enable/disable menu items feature');
$r->notes = $this->_('Allows you to set some menu items as disabled. If an item is disabled, the item together will all of its descendants will be set as disabled after you save the menu settings. Disabled items will not be output when the menu is viewed in the frontend.');
$radioOptions = array (
1 => $this->_('Yes'),
2 => $this->_('No'),
);
$r->addOptions($radioOptions);
$r->value = $disableItems;
$tab->add($r);
}// end if user can edit and use 'disable menu items' feature
// if user can use 'multi-lingual menu items' feature
if($user->hasPermission('menu-builder-multi-lingual-items') && !is_null($user->language)) {
// active languages select checkboxes
$menuItemsLanguages = isset($menuPages['menu_items_languages']) ? $menuPages['menu_items_languages'] : array();
$languages = $this->getLanguages();// @note: grabs all available languages
// active languages select checkboxes
$c = $modules->get('InputfieldCheckboxes');
$c->label = $this->_('Other active languages for this menu');
$c->attr('id+name', 'menu_items_languages');
$c->attr('value', $menuItemsLanguages);
$c->description = $this->_('Optionally, you can choose other languages other than the default for which you want to save values for your menu items.');
#$c->addOptions($languageOptions);
foreach ($languages as $langName => $langTitle) {
if($langName == 'default') continue;
$c->addOption($langName, $langTitle);
}
$c->notes = $this->_('When building the menu, you will see tabs for other active languages selected here to input titles and URLs. If a title or URL is left blank, in the frontend, the respective values for the default language will be used instead.');
$tab->add($c);
}// user can use 'multi-lingual menu items' feature
// if user has permission to allow editing of nestedSortable settings
if($user->hasPermission('menu-builder-settings')) {
$t = $modules->get('MarkupAdminDataTable');
$t->setEncodeEntities(false);
$t->setSortable(false);
$t->setClass('menu_items_table');
$t->headerRow(array(
$this->_('Name'),
$this->_('Default'),// for PW pages, actual title saved. The title can also be edited in the add menu item settings
$this->_('Setting'),// path to PW pages + normal url for custom menu items
$this->_('Notes'),// parent in this menu! NOT PW PAGE PARENT!
));
// advanced/optional settings for nestedSortable
$mergedMenuSettings = $this->nestedSortableMenuSettings();
foreach ($mergedMenuSettings as $key => $value) {
if($key == 'includeChildren') continue;// setting not for nestedSortable
$t->row(array(
$key,// name
$value['default'],// default value
"<input type='text' name='menu_settings[" . $key . "]' value='" . $value['setting'] . "'>",// setting - saved in menu_settings as JSON
$value['notes'],
));
}// end foreach $menuSettings
$m = $modules->get('InputfieldMarkup');
$m->attr('id', 'menu_settings');
$m->label = $this->_('Menu settings');
$m->textFormat = Inputfield::textFormatNone;// make sure ProcessWire renders the HTML
$m->description = $this->_('These are optional settings for') . ' <a href="https:// github.com/ilikenwf/nestedSortable" target="_blank">nestedSortable</a> ' .
$this->_('(the Drag and Drop menu functionality in Build Menu Tab).');
$m->notes = $this->_('Note: These settings do not affect how your menu is displayed in the frontend.');
$m->collapsed = Inputfield::collapsedYes;
$m->attr('value', $t->render());
$tab->add($m);
}// end if user has permission to edit nestedSortable settings
return $tab;
}
/**
* Fourth tab contents for executeEdit()
*
* @access protected
* @param integer $menuID ID of the menu being edited
* @return object $tab To render as markup.
*
*/
protected function editTabDelete($menuID) {
$modules = $this->wire('modules');
// Fourth Tab - Delete Menu. Only show if a menu exists
$tab = new InputfieldWrapper();
$tab->attr('title', $this->_('Delete'));
$id = $this->className() . 'Delete';
$tab->attr('id', $id);
$tab->class .= " WireTab";
$f = $modules->get('InputfieldCheckbox');
$f->attr('id+name', 'menu_delete_confirm');
$f->attr('value', $menuID);
$f->icon = 'trash-o';
$f->label = $this->_('Move to Trash');
$f->description = $this->_('Check the box to confirm you want to do this.');
$f->label2 = $this->_('Confirm');
$tab->add($f);
$f = $modules->get('InputfieldButton');
$f->attr('id+name', 'menu_delete');
$f->value = $this->_('Move to Trash');
$tab->append($f);
return $tab;
}
/**
* Displays a nested list (menu items) of a single menu.
*
* This is a recursive function to display list of menu items.
* Also displays each menu item's settings.
*
* @access private
* @param integer $parent ID of menu items.
* @param integer $first Helper variable to designate first menu item. Ensures CSS Class 'sortable' is output only once.
* @return string $out Menu items markup.
*
*/
private function listMenu($parent = 0, $first = 0) {
$menuID = (int) $this->wire('input')->get->id;
if($menuID) {
/*
INPUTS
- id: item id of the menu item in relation to the menu (not same as pages_id!)
- title: the menu item title as saved in Build Menu (note: even PW native page->title can be customised)
- parent_id: the parent of this menu item in relation to the menu (note: does not have to reflect PW tree!; top tier items have parent_id = 0)
- url: the url of the menu item (if PW, use native $page->url; if custom use provided url)
- css_itemid: this menu item's CSS ID (optional)
- css_itemclass: this menu items's CSS Class (optional)
- pages_id: for PW pages items = $page->id; for custom menu items = 0 (note: this is different from id!)
- optional include children feature
- opitional disable menu items feature
*/
$out = '';
$has_child = false;
// $id is = id; $item = arrays of title, url, newtab, etc
foreach ($this->menuItems as $id => $item) {
## - MENU ITEM PROPERTIES - ##
// set on the fly properties
$this->itemID = $id;
$this->itemTitle = $item['title'];
$this->itemTitle2 = $this->wire('sanitizer')->entities($this->itemTitle);// for value of title input
$this->itemURL = isset($item['url']) ? $item['url'] : '';
// if multilingual, also set language specific titles and urls
// @note: format is $this->itemTitle_de; $this->itemURL_de; $this->itemTitle2_de, etc...
if(!is_null($this->menuItemsLanguages)) $this->setLanguageTitlesAndURLs($item);
// items without parent ids are top level items
// we give them an ID of 0 for display purposes (we won't save the value [see wireEncodeJSON()])
$this->itemParentID = isset($item['parent_id']) ? $item['parent_id'] : 0;
$this->cssItemID = isset($item['css_itemid']) ? $item['css_itemid'] : '';
$this->cssItemClass = isset($item['css_itemclass']) ? $item['css_itemclass'] : '';
$this->itemPagesID = isset($item['pages_id']) ? $item['pages_id'] : 0;// only PW pages will have a pages_id > 0 (equal to their PW page->id)
$this->newTab = isset($item['newtab']) ? $item['newtab'] : 0;
$this->itemIncludeChildren = isset($item['include_children']) ? $item['include_children'] : '';
$this->itemMenuMaxLevel = isset($item['m_max_level']) ? $item['m_max_level'] : '';
$this->disabledItem = isset($item['disabled_item']) ? $item['disabled_item'] : '';
// custom menu items
if(!$this->itemPagesID) {
$this->itemType = $this->_('Custom');
$this->readOnly = '';
}
// pw page menu items
else {
$this->itemType = $this->_('Page');
$this->readOnly = ' readonly';
$this->itemURL = $this->wire('pages')->get($this->itemPagesID)->path;
}
## - BUILD MENU - ##
######################### item is a parent #########################
// if this menu item is a parent; create the inner-items/child-menu-items
if ($this->itemParentID == $parent) {
// if this is the first child output '<ol>' with the class 'sortable'
if ($has_child === false) {
$has_child = true;// This is a parent
if ($first == 0){
$out .= "<ol id='sortable_main' class='sortable'>\n";
$first = 1;
}
else $out .= "\n<ol>\n";
}
######################### menu item drag n drop handle #########################
$out .= $this->buildMenuItemDragDropHandleMarkup();
######################### item settings #########################
$out .= $this->buildMenuItemSettingsPanel();
######################### generate sub-menu items [recursion] #########################
// call function again to generate nested list for sub-menu items belonging to this menu item.
$out .= $this->listMenu($id, $first);
// close the <li>