-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminBar.module
844 lines (749 loc) · 32.4 KB
/
AdminBar.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
<?php namespace ProcessWire;
/**
* AdminBar module for ProcessWire CMS/CMF
*
* This module provides easy front-end admin bar for editing page content in ProcessWire.
*
* @copyright 2011-2024 @apeisa & Teppo Koivula
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt GNU General Public License, version 2
*/
class AdminBar extends WireData implements Module, ConfigurableModule {
/**
* Flag for disabling the admin bar
*
* @var bool
*/
public static $showBar = true;
/**
* Basic information for this module
*
* See /wire/core/Module.php for more details.
*
* @return array
*/
public static function getModuleInfo(): array {
return [
'title' => 'Admin Bar',
'summary' => 'Fast and easy in-page content editing',
'href' => 'https://processwire.com/modules/admin-bar/',
'version' => '2.9.3',
'permanent' => false,
'autoload' => true,
'singular' => true,
'requires' => [
'ProcessWire>=3.0.112',
'PHP>=7.1.0',
],
];
}
/**
* Default configuration for our module
*
* @return array
*/
static public function getDefaultData(): array {
return [
'theme' => 'uikit',
'custom_theme' => '',
'items_left' => [
'browse' => 'browse',
'edit' => 'edit',
'new' => 'new',
],
'items_right' => [
'admin' => 'admin',
'logout' => 'logout',
],
'user_items' => [
'admin' => 'admin',
'profile' => 'profile',
'logout' => 'logout',
],
'disable_for_debug_mode' => 0,
'disable_duplicate_bars' => 1,
'showModal' => 1,
'stickyModal' => 0,
];
}
/**
* Populate the default config data
*
* ProcessWire will automatically overwrite it with anything the user has specifically configured.
* This is done in construct() rather than init() because ProcessWire populates config data after
* construct(), but before init().
*/
public function __construct() {
foreach (self::getDefaultData() as $key => $value) {
$this->$key = $value;
}
}
/**
* Permission required by our module
*
* This property gets populated in init().
*
* @var Permission
*/
protected $ab_permission = null;
/**
* Initialize the module and setup hooks
*
* @return false|void
*/
public function init() {
// Store a reference to the adminbar permission
$this->ab_permission = $this->permissions->get('adminbar');
// Bail out early if any conditions for disabling Admin Bar apply
if (!$this->isEnabled()) {
return false;
}
// If the ab_modal GET param has been provided, hook before forms are rendered so that we
// can modify the form's "action" attribute.
if ($this->input->get->ab_modal) {
$this->addHookBefore('InputfieldForm::render', $this, 'formRender');
}
}
/**
* Setup rest of the hooks
*
* Some hooks have to be defined here, since we need access to $page var.
*
* @return false|void
*/
public function ready() {
// Bail out early if any conditions for disabling Admin Bar apply
if (!$this->isEnabled()) {
return false;
}
// Bail out early if we're viewing an admin page and current query isn't related to Admin Bar.
if ($this->page && $this->page->template == "admin" && !$this->input->get->ab_modal && !$this->session->AB_pageSaved) {
return false;
}
// Hook after page added in order to keep track of new pages.
$this->pages->addHookAfter('added', $this, 'pageAdded');
// Hook after page save in order to store information into session.
$this->pages->addHookAfter('save', $this, 'pageSave');
// Hook after admin page has been rendered, so that we can perform a redirect or modify page edit form.
$this->addHookAfter('Page(template=admin)::render', $this, "adminPageRender");
// Hook after non-admin page has been rendered, so that we can render the Admin Bar.
$this->addHookAfter('Page(template!=admin)::render', $this, "pageRender");
// Hook before a redirect occurs, so that we can modify the redirect URL.
$this->session->addHookBefore('redirect', $this, 'sessionRedirect');
}
/**
* Is Admin Bar enabled?
*
* @return bool
*/
public function ___isEnabled(): bool {
// Disable if the static flag is set to false
if (!self::$showBar) {
return false;
}
// Disable for guest users
if ($this->user->isGuest()) {
return false;
}
// Optionally disable when the site is in debug mode
if ($this->disable_for_debug_mode && $this->config->debug) {
return false;
}
// Disable if current user doesn't have the adminbar permission
if (!$this->user->hasPermission($this->ab_permission)) {
return false;
}
return true;
}
/**
* Is Admin Bar enabled for given page?
*
* @param Page $page
* @return bool
*/
protected function ___isEnabledFor(Page $page): bool {
// Disable if the static flag is set to false
if (!self::$showBar) {
return false;
}
// Bail out early if current template belongs to Form Builder.
if ($page->template == "form-builder") {
return false;
}
return true;
}
/**
* Hook called when admin page is rendered
*
* @param HookEvent $event
*/
public function adminPageRender(HookEvent $event) {
// If we've saved a page and we're currently viewing an admin page in modal window,
// make a JS redirect and break out of modal (there seems to be no other way than JS).
if ($this->session->AB_pageSaved && $this->input->get->modal && !$this->stickyModal) {
$redirect_url = rtrim($this->config->urls->root, '/') . $this->session->AB_pageSaved;
die('<script>top.location.href = "' . $redirect_url . '";</script>');
}
// Delete our session var.
$this->session->remove('AB_pageSaved');
// Remove "view" and "children" buttons from modal admin view.
if ($this->input->get->modal) {
// First pass: hide with CSS (to prevent flashing content).
$event->return = str_ireplace(
'<form id="ProcessPageEdit"',
'<style>
#_ProcessPageEditView,
#_ProcessPageEditChildren {
display: none;
}
</style>
<form id="ProcessPageEdit"',
$event->return
);
// Second pass: remove from markup with JavaScipt.
$event->return = str_ireplace(
'</body>',
'<script>
["_ProcessPageEditView", "_ProcessPageEditChildren"].forEach(function(remove_id) {
const remove_tab = document.getElementById(remove_id);
if (remove_tab) remove_tab.parentNode.removeChild(remove_tab);
});
const wrap_template = document.getElementById("wrap_template");
if (wrap_template) wrap_template.style.display = "none";
</script>
</body>',
$event->return
);
}
}
/**
* Hook called when a regular non-admin page is rendered
*
* @param HookEvent $event
* @return null|void
*/
public function pageRender(HookEvent $event) {
$page = $event->object;
// Check if Admin Bar is enabled for current Page.
if (!$this->isEnabledFor($page)) {
return;
}
// In case page is deleted through Admin Bar, redirect to its parent.
$this->session->AB_parentPage = $page->parent->path;
// Bail out early if there's no closing </body> tag.
if (strpos($event->return, '</body') === false) {
return;
}
// Bail out early if Admin Bar has already been rendered and added to page markup.
// Note: this is the default behaviour since AdminBar 2.8.4, but if rendering duplicates is actually preferable
// duplicate detection can be disabled by unchecking the disable_duplicate_bars option in module configuration.
if ($this->disable_duplicate_bars && preg_match('/' . str_replace('%s', '.*?', preg_quote($this->getTemplates()['adminbar'], '/')) . '/', $event->return)) {
return;
}
// Placeholder for output.
$out = '';
// Get paths, URLs, and version for styles and scripts.
$module_path = $this->config->paths->get('AdminBar');
$module_url = $this->config->urls->get('AdminBar');
$theme_path = $module_path;
$theme_url = $module_url;
if ($this->theme == 'custom' && !$this->custom_theme) {
$this->theme = 'uikit';
}
$theme_dir = $this->theme == 'custom' ? $this->custom_theme : 'themes/' . basename($this->theme) . '/';
$version = self::getModuleInfo()['version'];
// Process custom theme directory.
if ($this->theme == 'custom') {
$theme_path = $this->config->paths->site;
$theme_url = $this->config->urls->site;
$theme_dir = trim($theme_dir, '/') . '/';
}
// Add CSS files to output
foreach (['AdminBar.css', $theme_dir . 'theme.css'] as $key => $file) {
if (is_file(($key ? $theme_path : $module_path) . $file)) {
$out .= '<link rel="stylesheet" href="' . ($key ? $theme_url : $module_url) . $file . '?v=' . $version . '">';
}
}
// Add JS files to output
foreach (['AdminBar.js', $theme_dir . 'theme.js'] as $key => $file) {
if (is_file(($key ? $theme_path : $module_path) . $file)) {
$out .= '<script type="text/javascript" src="' . ($key ? $theme_url : $module_url) . $file . '?v=' . $version . '"></script>';
}
}
// Load optional theme.php file
if (is_file($theme_path . $theme_dir . 'theme.php')) {
include_once($theme_path . $theme_dir . 'theme.php');
}
// Create markup for Admin Bar.
$out .= $this->render([
'page' => $page,
]);
// Page was saved in modal window, notify the user about what happened.
if ($this->session->AB_pageSaved) {
$out .= vsprintf('<div id="adminbar-page-saved" class="adminbar__page-saved adminbar__page-saved--%s">%s</div>',
$this->session->AB_pageRemove ? ['removed', sprintf($this->_("%s moved to the trash."), $this->session->AB_pageRemove)]
: ['saved', sprintf($this->_("%s saved succesfully."), $page->title)]
);
$this->session->remove('AB_pageSaved');
$this->session->remove('AB_pageRemove');
} else {
$this->session->remove('AB_pageSaved');
$this->session->remove('AB_pageRemove');
}
// Finally add Admin Bar markup to end of the page HTML source.
$event->return = str_ireplace('</body>', $out . '</body>', $event->return);
}
/**
* Render markup for the Admin Bar
*
* This method renders the Admin Bar. You can hook _before_ it if you want to provide custom
* arguments for the method (overriding strings etc.) or you can hook _after_ it if you want
* to manually modify the returned output string.
*
* @param array $args Render arguments.
* @return string Rendered Admin Bar HTML markup.
*/
public function ___render(array $args = []): string {
// Placeholder for output.
$out = '';
// Combine custom arguments with defaults.
$args = array_merge([
'page' => $this->page,
'modal' => $this->showModal,
'template_title' => '',
'classes' => [
'link--modal' => 'adminbar__link--modal',
],
'strings' => [
'browse' => $this->_('Browse'),
'edit' => $this->_('Edit'),
'new' => $this->_('New'),
'logout' => $this->_('Logout'),
'admin' => $this->_('Admin'),
'profile' => $this->_('Profile'),
'locked' => $this->_('This page is locked'),
'norights' => $this->_('No editing rights'),
],
], $args);
// If only one template is allowed, we show it's label or name instead of default "page".
if (empty($args['template_title']) && count($args['page']->template->childTemplates) == 1) {
$child_template_ids = array_values($args['page']->template->childTemplates);
$child_template_id = array_pop($child_template_ids);
$child_template = $this->templates->get((int) $child_template_id);
if ($child_template) {
$label_language = $this->modules->isInstalled('LanguageSupport') && !$this->user->language->isDefault() ? $this->user->language : '';
$args['template_title'] = $child_template->get('label' . ($label_language ? $label_language . '|label' : '') . '|name');
}
}
// Rendering templates.
$templates = $this->getTemplates();
// Add items.
$items = $this->getItems($args);
foreach (['left', 'right'] as $list) {
$list_items = $items[$list] ?? [];
$out .= $this->renderList($list_items, $list, $templates);
}
// Construct the data-adminbar attribute.
$data = json_encode((object) $this->getData([
'classes' => $args['classes'] ?? [],
'urls' => [
'modules' => $this->config->urls->modules,
],
]));
// Return final output.
return sprintf($templates['adminbar'], $data, $out);
}
/**
* Render markup for a single Admin Bar item
*
* @param array $items Items as an array.
* @param string $list List name for fetching the correct template string.
* @param array $templates Template strings.
* @return string Rendered list markup, or empty string if no items were found.
*/
protected function renderList(array $items, string $list, array $templates): string {
// Placeholder for output.
$out = '';
// Iterate the items array and render list items.
foreach ($items as $key => $item) {
if (empty($item)) continue;
$item_class = $key;
if ($key == 'browse') {
$item_class .= ' adminbar__list-item--active';
}
if ($list == 'children') {
$item_class .= ' adminbar__list-item--child';
}
$item_content = $item;
if (is_array($item)) {
$item_type = isset($item['html']) ? 'html' : (isset($item['link']) ? 'link' : 'text');
if (isset($item['class']) && $list == 'children') {
$item['class'] .= ' adminbar__' . $item_type . '--child';
}
$item_content = vsprintf(
$templates[$item_type],
$item
);
if (!empty($item['children'])) {
$item_class .= ' adminbar__list-item--has-children';
$item_content .= $this->renderList($item['children'], 'children', $templates);
}
}
$out .= vsprintf($templates['list_item'], [
$item_class,
$item_content,
]);
}
// Wrap items with the list template.
if (!empty($out)) {
$out = sprintf($templates['list__' . $list], $out);
}
return $out;
}
/**
* Return template strings for rendering Admin Bar
*
* This method returns the "template strings" used by the render method. These are essentially
* the parts that make up the final markup, and if you want to modify the markup of the Admin
* Bar, you can hook _after_ this method and modify the returned template strings array.
*
* @return array Template strings.
*/
protected function ___getTemplates(): array {
return [
'adminbar' => '<div id="adminbar" data-adminbar=\'%s\' class="adminbar adminbar--auto-padding">%s</div>',
'list__left' => '<ul class="adminbar__list adminbar__list--left">%s</ul>',
'list__right' => '<ul class="adminbar__list adminbar__list--right">%s</ul>',
'list__children' => '<ul class="adminbar__list adminbar__list--children">%s</ul>',
'list_item' => '<li class="adminbar__list-item adminbar__list-item--item-%s">%s</li>',
'link' => '<a class="adminbar__link adminbar__link--item-%s" href="%s">%s</a>',
'text' => '<span class="adminbar__text adminbar__text--item-%s">%s</span>',
'html' => '<span class="adminbar__html adminbar__html--item-%s">%s</span>',
];
}
/**
* Return values that should be made available in the front-end via the data-adminbar attribute.
*
* This method returns the value of the data-adminbar attribute added to the Admin Bar. If you
* need custom properties mainly for JavaScript use, you should hook _after_ this method and
* modify the returned array of options.
*
* @param array $data Default data.
* @return array Data attribute values.
*/
protected function ___getData(array $data): array {
return $data;
}
/**
* Return a list of items
*
* This method returns a list of items displayed in the Admin Bar. You can modify certain aspects
* of these items (strings/labels, use of modal links, etc.) by hooking _before_ this method and
* modifying the args array – or if you want to modify the displayed items, you can hook _after_
* this method and modify the returned array of items.
*
* @param array $args Arguments.
* @return array
*/
protected function ___getItems(array $args) {
// Modal variables.
$modal = !empty($args['modal']) ? 'modal=1&ab_modal=1' : '';
$modal_class = $modal ? ' ' . ($args['classes']['link--modal'] ?? '') : '';
// Available items.
$available_items = [
'admin' => [
'class' => 'admin',
'link' => $this->config->urls->admin . 'page/?open=' . $args['page']->id,
'text' => $args['strings']['admin'],
],
'profile' => [
'class' => 'profile' . $modal_class,
'link' => $this->config->urls->admin . 'profile/' . ($modal ? '?' . $modal : ''),
'text' => $args['strings']['profile'],
],
'logout' => [
'class' => 'logout',
'html' => '<form class="adminbar__form" action="' . $this->config->urls->admin . 'login/logout/">'
. '<button class="adminbar__button" type="submit">' . $args['strings']['logout'] . '</button>'
. '</form>',
],
'browse' => [
'class' => 'browse',
'link' => '#',
'text' => $args['strings']['browse'],
],
'edit' => $args['page']->editable() && !$args['page']->is(Page::statusLocked) ? [
'class' => 'edit' . $modal_class,
'link' => $this->config->urls->admin . 'page/edit/?id=' . $args['page']->id . ($modal ? '&' . $modal : ''),
'text' => $args['strings']['edit'],
] : null,
'new' => $args['page']->addable() ? [
'class' => 'new' . $modal_class,
'link' => $this->config->urls->admin . 'page/add/?parent_id=' . $args['page']->id . ($modal ? '&' . $modal : ''),
'text' => $args['strings']['new'] . ' ' . $args['template_title'],
] : null,
'user' => [
'class' => 'user' . $modal_class,
'link' => $this->config->urls->admin . 'profile/' . ($modal ? '?' . $modal : ''),
'text' => ucfirst($this->wire('user')->name),
],
];
// Add child items for the "user" dropdown menu.
$available_items['user']['children'] = array_filter(
array_combine(
$this->user_items,
array_map(function($value) use ($available_items) {
return $available_items[$value] ?? null;
}, $this->user_items)
)
);
// Array of items.
$items = [];
foreach (['left', 'right'] as $column) {
$column_items = $this->get('items_' . $column);
$items[$column] = array_filter(
array_combine(
$column_items,
array_map(function($value) use ($available_items) {
return $available_items[$value] ?? null;
}, $column_items)
)
);
// If page is non-editable, provide additional details.
if (in_array('edit', $column_items) && empty($items[$column]['edit'])) {
if (!$args['page']->editable()) {
$items[$column]['edit'] = [
'class' => 'edit adminbar__text--norights',
'text' => $args['strings']['norights'],
];
} else if ($args['page']->is(Page::statusLocked)) {
$items[$column]['edit'] = [
'class' => 'edit adminbar__text--locked',
'text' => $args['strings']['locked'],
];
}
}
}
return $items;
}
/**
* Hook to take place after page is saved (edit & new page)
*
* @param HookEvent $event
*/
public function pageSave(HookEvent $event) {
$page = $event->arguments[0];
// If creating a new page, then we do not add AB_pageSaved session var. This also prevents
// redirect and keeps the modal window alive.
if (!$page->_ab_added && !($page instanceof RepeaterPage) && $page->template != 'user' && $page->template != 'admin') {
$this->session->AB_pageSaved = $page->path;
}
// If page has been (or is being) trashed, set saved page as parent page and remember the
// title of the page.
if ($page->isTrash()) {
$this->session->AB_pageSaved = $this->session->AB_parentPage;
$this->session->AB_pageRemove = $page->title;
}
}
/**
* Hook to take place after page is added
*
* This method adds _ab_added property to new pages, which allows us to detect newly added pages
* later on in the code. Note that the core adds separate _added property since PW 3.0.127, but
* since it is undocumented, at least for now we'll duplicate the same behaviour here.
*
* @param HookEvent $event
*/
public function pageAdded(HookEvent $event) {
$page = $event->arguments[0];
$page->setQuietly('_ab_added', true);
}
/**
* Hook to take place before forms are rendered
*
* We check if there is a 'modal' get var set, and if so, we add it to the form's action attribute
*
* @param HookEvent $event
* @return null|void
*/
public function formRender(HookEvent $event) {
if (!$this->input->get->modal) return;
$form = $event->object;
$action = $form->attr('action');
$action .= (strpos($action, '?') !== false ? '&' : '?') . 'modal=1&ab_modal=1';
$form->attr('action', $action);
}
/**
* Hook to take place right before a redirect occurs
*
* We intercept the redirect URL and modify it to add 'modal=1&ab_modal=1' to the query string.
*
* @param HookEvent $event
*/
public function sessionRedirect(HookEvent $event) {
if (!$this->page || $this->page->template != 'admin') return;
if (!$this->input->get->modal) return;
$url = $event->arguments(0);
if (preg_match('/[?&]ab_modal=/', $url)) return;
$url .= (strpos($url, '?') !== false ? '&' : '?') . 'modal=1&ab_modal=1';
$event->arguments(0, $url);
}
/**
* Module configuration
*
* @param array $data
* @return InputfieldWrapper
*/
public function getModuleConfigInputfields(array $data): InputfieldWrapper {
$fields = $this->wire(new InputfieldWrapper());
// Merge data array with defaults.
$data = array_merge(self::getDefaultData(), $data);
// Theme selection.
$field = $this->modules->get('InputfieldSelect');
$field->name = 'theme';
$field->label = 'Theme';
$field->icon = 'paint-brush';
$field->required = true;
$field->description = 'Choose a theme to use for your Admin Bar.';
$theme_directory = new \DirectoryIterator(__DIR__ . '/themes/');
foreach ($theme_directory as $theme) {
if ($theme->isDir()) {
$theme_name = $theme->getFilename();
if (strpos($theme_name, '.') !== 0 && $theme_name != 'custom') {
$field->addOption($theme_name);
}
}
}
$field->addOption('custom', 'custom ...');
$field->value = $data['theme'];
$fields->add($field);
// Custom theme.
$field = $this->modules->get('InputfieldText');
$field->name = 'custom_theme';
$field->label = 'Custom theme';
$field->icon = 'graduation-cap';
$field->showIf = 'theme=custom';
$field->required = true;
$field->requireIf = 'theme=custom';
$field->description = 'If you want to use your own custom theme, fill this field (overrides theme selection above).';
$field->notes = 'Type in the path to the directory containing theme.css (and optional theme.js, theme.php, and config.php) file(s) for your theme. Path has to be relative to your site directory, such as /templates/my-adminbar-theme/.';
$field->value = $data['custom_theme'];
$fields->add($field);
// Theme specific custom config.
$config_file = $this->config->paths->get('AdminBar') . 'themes/' . basename($data['theme']) . '/config.php';
if ($data['theme'] == 'custom' && $data['custom_theme']) {
$config_file = $this->config->paths->site . trim($data['custom_theme'], '/') . '/config.php';
}
if (is_file($config_file)) {
$fieldset = $this->modules->get('InputfieldFieldset');
$fieldset->label = 'Settings for the ' . basename($data['theme']) . ' theme';
$fieldset->description = 'These settings are defined in the config.php file included with the theme. Note that if you change the active theme, you need to save the module settings once in order to see possible new theme config options.';
$fieldset->icon = 'sliders';
include_once($config_file);
if ($fieldset->count()) {
$fields->add($fieldset);
}
}
// Displayed item selection.
$fieldset = $this->modules->get('InputfieldFieldset');
$fieldset->label = 'Items visible in the Admin Bar';
$fieldset->description = 'Choose the items to include in the Admin Bar. The items will be displayed in the Admin Bar in the same order they are in the lists below.';
$fieldset->icon = 'list';
$items_available = [
'browse' => 'browse',
'edit' => 'edit',
'new' => 'new',
'logout' => 'logout',
'profile' => 'profile',
'user' => 'user',
'admin' => 'admin',
];
foreach (['left', 'right'] as $column) {
$field = $this->modules->get('InputfieldAsmSelect');
$field->name = 'items_' . $column;
$field->label = ucfirst($column) . ' column';
$field->icon = 'arrow-' . $column;
$field->description = 'Items selected here are displayed on the ' . $column . ' side of the Admin Bar.';
$field->columnWidth = 50;
$field->addOptions($items_available);
$field->value = $data['items_' . $column];
$fieldset->add($field);
}
$fields->add($fieldset);
// Displayed "user" dropdown item selection.
$field = $this->modules->get('InputfieldAsmSelect');
$field->name = 'user_items';
$field->label = 'Items displayed in the Admin Bar "user" dropdown menu.';
$field->icon = 'user-circle';
$field->description = 'Choose the items to include in the Admin Bar "user" dropdown menu.';
$field->showIf = 'items_left|items_right=user';
$user_items_available = [
'admin' => 'admin',
'profile' => 'profile',
'logout' => 'logout',
];
$field->addOptions($user_items_available);
$field->value = $data[$field->name];
$fields->add($field);
// Modal edit feature.
$field = $this->modules->get('InputfieldCheckbox');
$field->name = 'showModal';
$field->label = 'Use slide overlay';
$field->icon = 'window-maximize';
$field->description = 'Whether clicking edit buttons shows slide view (overlay/modal) or goes to traditional admin.';
$field->columnWidth = 50;
$field->value = 1;
$field->attr('checked', empty($data[$field->name]) ? '' : 'checked');
$fields->add($field);
// Keep modal open after page has been saved?
$field = $this->modules->get('InputfieldCheckbox');
$field->name = 'stickyModal';
$field->label = 'Keep slide overlay open after page is saved';
$field->description = 'By default slide overlay is closed after page has been saved. With this option enabled it remains open instead.';
$field->icon = 'thumb-tack';
$field->showIf = 'showModal=1';
$field->columnWidth = 50;
$field->value = 1;
$field->attr('checked', empty($data[$field->name]) ? '' : 'checked');
$fields->add($field);
// Disable settings.
$fieldset = $this->modules->get('InputfieldFieldset');
$fieldset->label = 'Conditions for disabling Admin Bar';
$fieldset->description = 'There may be specific conditions under which you prefer to disable Admin Bar. Those can be configured here.';
$fieldset->icon = 'ban';
$fields->add($fieldset);
// Disable AdminBar when the site is in debug mode?
$field = $this->modules->get('InputfieldCheckbox');
$field->name = 'disable_for_debug_mode';
$field->label = 'Disable Admin Bar when the site is in debug mode';
$field->value = 1;
$field->attr('checked', empty($data[$field->name]) ? '' : 'checked');
$fieldset->add($field);
// Disable AdminBar when duplicate bars are deteted?
$field = $this->modules->get('InputfieldCheckbox');
$field->name = 'disable_duplicate_bars';
$field->label = 'Disable Admin Bar when page markup already contains an Admin Bar (prevent duplicate bars)';
// $field->description = ';
$field->notes = 'As of AdminBar version 2.8.4 the module attempts to detect and prevent duplicate bars by default. If you prefer multiple bars to be rendered instead, uncheck this option.'
. ' '
. 'Please note that AdminBar doesn\'t officially support multiple simultaneous bars; disabling this option is not recommended, as it may result in unexpected issues.';
$field->value = 1;
$field->attr('checked', empty($data[$field->name]) ? '' : 'checked');
$fieldset->add($field);
return $fields;
}
/**
* Called only when this module is installed
*/
public function ___install() {
$ab_permission = $this->permissions->add('adminbar');
$ab_permission->title = 'Use AdminBar';
$ab_permission->save();
}
/**
* Called only when this module is uninstalled
*/
public function ___uninstall() {
$ab_permission = $this->permissions->get('adminbar');
if ($ab_permission->id) $this->permissions->delete($ab_permission);
}
}