/
class.administrationpage.php
1440 lines (1267 loc) · 54.6 KB
/
class.administrationpage.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
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
/**
* @package toolkit
*/
/**
* The AdministrationPage class represents a Symphony backend page.
* It extends the HTMLPage class and unlike the Frontend, is generated
* using a number XMLElement objects. Instances of this class override
* the view, switchboard and action functions to construct the page. These
* functions act as pseudo MVC, with the switchboard being controller,
* and the view/action being the view.
*/
class AdministrationPage extends HTMLPage
{
/**
* An array of `Alert` objects used to display page level
* messages to Symphony backend users one by one. Prior to Symphony 2.3
* this variable only held a single `Alert` object.
* @var array
*/
public $Alert = array();
/**
* As the name suggests, a `<div>` that holds the following `$Header`,
* `$Contents` and `$Footer`.
* @var XMLElement
*/
public $Wrapper = null;
/**
* A `<div>` that contains the header of a Symphony backend page, which
* typically contains the Site title and the navigation.
* @var XMLElement
*/
public $Header = null;
/**
* A `<div>` that contains the breadcrumbs, the page title and some contextual
* actions (e.g. "Create new").
* @since Symphony 2.3
* @var XMLElement
*/
public $Context = null;
/**
* An object that stores the markup for the breadcrumbs and is only used
* internally.
* @since Symphony 2.3
* @var XMLElement
*/
public $Breadcrumbs = null;
/**
* An array of Drawer widgets for the current page
* @since Symphony 2.3
* @var array
*/
public $Drawer = array();
/**
* A `<div>` that contains the content of a Symphony backend page.
* @var XMLElement
*/
public $Contents = null;
/**
* An associative array of the navigation where the key is the group
* index, and the value is an associative array of 'name', 'index' and
* 'children'. Name is the name of the this group, index is the same as
* the key and children is an associative array of navigation items containing
* the keys 'link', 'name' and 'visible'. In Symphony, all navigation items
* are contained within a group, and the group has no 'default' link, therefore
* it is up to the children to provide the link to pages. This link should be
* relative to the Symphony path, although it is possible to provide an
* absolute link by providing a key, 'relative' with the value false.
* @var array
*/
public $_navigation = array();
/**
* An associative array describing this pages context. This
* can include the section handle, the current entry_id, the page
* name and any flags such as 'saved' or 'created'. This variable
* often provided in delegates so extensions can manipulate based
* off the current context or add new keys.
* @var array
*/
public $_context = null;
/**
* The class attribute of the `<body>` element for this page. Defaults
* to an empty string
* @var string
*/
private $_body_class = '';
/**
* Constructor calls the parent constructor to set up
* the basic HTML, Head and Body `XMLElement`'s. This function
* also sets the `XMLElement` element style to be HTML, instead of XML
*/
public function __construct()
{
parent::__construct();
$this->Html->setElementStyle('html');
}
/**
* Specifies the type of page that being created. This is used to
* trigger various styling hooks. If your page is mainly a form,
* pass 'form' as the parameter, if it's displaying a single entry,
* pass 'single'. If any other parameter is passed, the 'index'
* styling will be applied.
*
* @param string $type
* Accepts 'form' or 'single', any other `$type` will trigger 'index'
* styling.
*/
public function setPageType($type = 'form')
{
$this->setBodyClass($type == 'form' || $type == 'single' ? 'single' : 'index');
}
/**
* Setter function to set the class attribute on the `<body>` element.
* This function will respect any previous classes that have been added
* to this `<body>`
*
* @param string $class
* The string of the classname, multiple classes can be specified by
* uses a space separator
*/
public function setBodyClass($class)
{
// Prevents duplicate "index" classes
if (!isset($this->_context['page']) || $this->_context['page'] !== 'index' || $class !== 'index') {
$this->_body_class .= $class;
}
}
/**
* Accessor for `$this->_context` which includes contextual information
* about the current page such as the class, file location or page root.
* This information varies depending on if the page is provided by an
* extension, is for the publish area, is the login page or any other page
*
* @since Symphony 2.3
* @return array
*/
public function getContext()
{
return $this->_context;
}
/**
* Given a `$message` and an optional `$type`, this function will
* add an Alert instance into this page's `$this->Alert` property.
* Since Symphony 2.3, there may be more than one `Alert` per page.
* Unless the Alert is an Error, it is required the `$message` be
* passed to this function.
*
* @param string $message
* The message to display to users
* @param string $type
* An Alert constant, being `Alert::NOTICE`, `Alert::ERROR` or
* `Alert::SUCCESS`. The differing types will show the error
* in a different style in the backend. If omitted, this defaults
* to `Alert::NOTICE`.
* @throws Exception
*/
public function pageAlert($message = null, $type = Alert::NOTICE)
{
if (is_null($message) && $type == Alert::ERROR) {
$message = __('There was a problem rendering this page. Please check the activity log for more details.');
} else {
$message = __($message);
}
if (strlen(trim($message)) == 0) {
throw new Exception(__('A message must be supplied unless the alert is of type Alert::ERROR'));
}
$this->Alert[] = new Alert($message, $type);
}
/**
* Appends the heading of this Symphony page to the Context element.
* Action buttons can be provided (e.g. "Create new") as second parameter.
*
* @since Symphony 2.3
* @param string $value
* The heading text
* @param array|XMLElement|string $actions
* Some contextual actions to append to the heading, they can be provided as
* an array of XMLElements or strings. Traditionally Symphony uses this to append
* a "Create new" link to the Context div.
*/
public function appendSubheading($value, $actions = null)
{
if (!is_array($actions) && $actions) { // Backward compatibility
$actions = array($actions);
}
if (!empty($actions)) {
foreach ($actions as $a) {
$this->insertAction($a);
}
}
$this->Breadcrumbs->appendChild(new XMLElement('h2', $value, array('role' => 'heading', 'id' => 'symphony-subheading')));
}
/**
* This function allows a user to insert an Action button to the page.
* It accepts an `XMLElement` (which should be of the `Anchor` type),
* an optional parameter `$prepend`, which when `true` will add this
* action before any existing actions.
*
* @since Symphony 2.3
* @see core.Widget#Anchor
* @param XMLElement $action
* An Anchor element to add to the top of the page.
* @param boolean $append
* If true, this will add the `$action` after existing actions, otherwise
* it will be added before existing actions. By default this is `true`,
* which will add the `$action` after current actions.
*/
public function insertAction(XMLElement $action, $append = true)
{
$actions = $this->Context->getChildrenByName('ul');
// Actions haven't be added yet, create the element
if (empty($actions)) {
$ul = new XMLElement('ul', null, array('class' => 'actions'));
$this->Context->appendChild($ul);
} else {
$ul = current($actions);
$this->Context->replaceChildAt(1, $ul);
}
$li = new XMLElement('li', $action);
if ($append) {
$ul->prependChild($li);
} else {
$ul->appendChild($li);
}
}
/**
* Allows developers to specify a list of nav items that build the
* path to the current page or, in jargon, "breadcrumbs".
*
* @since Symphony 2.3
* @param array $values
* An array of `XMLElement`'s or strings that compose the path. If breadcrumbs
* already exist, any new item will be appended to the rightmost part of the
* path.
*/
public function insertBreadcrumbs(array $values)
{
if (empty($values)) {
return;
}
if ($this->Breadcrumbs instanceof XMLElement && count($this->Breadcrumbs->getChildrenByName('nav')) === 1) {
$nav = $this->Breadcrumbs->getChildrenByName('nav');
$nav = $nav[0];
$p = $nav->getChild(0);
} else {
$p = new XMLElement('p');
$nav = new XMLElement('nav');
$nav->appendChild($p);
$this->Breadcrumbs->prependChild($nav);
}
foreach ($values as $v) {
$p->appendChild($v);
$p->appendChild(new XMLElement('span', '›', array('class' => 'sep')));
}
}
/**
* Allows a Drawer element to added to the backend page in one of three
* positions, `horizontal`, `vertical-left` or `vertical-right`. The button
* to trigger the visibility of the drawer will be added after existing
* actions by default.
*
* @since Symphony 2.3
* @see core.Widget#Drawer
* @param XMLElement $drawer
* An XMLElement representing the drawer, use `Widget::Drawer` to construct
* @param string $position
* Where `$position` can be `horizontal`, `vertical-left` or
* `vertical-right`. Defaults to `horizontal`.
* @param string $button
* If not passed, a button to open/close the drawer will not be added
* to the interface. Accepts 'prepend' or 'append' values, which will
* add the button before or after existing buttons. Defaults to `prepend`.
* If any other value is passed, no button will be added.
* @throws InvalidArgumentException
*/
public function insertDrawer(XMLElement $drawer, $position = 'horizontal', $button = 'append')
{
$drawer->addClass($position);
$drawer->setAttribute('data-position', $position);
$drawer->setAttribute('role', 'complementary');
$this->Drawer[$position][] = $drawer;
if (in_array($button, array('prepend', 'append'))) {
$this->insertAction(
Widget::Anchor(
$drawer->getAttribute('data-label'),
'#' . $drawer->getAttribute('id'),
null,
'button drawer ' . $position
),
($button === 'append' ? true : false)
);
}
}
/**
* This function initialises a lot of the basic elements that make up a Symphony
* backend page such as the default stylesheets and scripts, the navigation and
* the footer. Any alerts are also appended by this function. `view()` is called to
* build the actual content of the page. The `InitialiseAdminPageHead` delegate
* allows extensions to add elements to the `<head>`. The `CanAccessPage` delegate
* allows extensions to restrict access to pages.
*
* @see view()
* @uses InitialiseAdminPageHead
* @uses CanAccessPage
* @param array $context
* An associative array describing this pages context. This
* can include the section handle, the current entry_id, the page
* name and any flags such as 'saved' or 'created'. This list is not exhaustive
* and extensions can add their own keys to the array.
* @throws InvalidArgumentException
* @throws SymphonyErrorPage
*/
public function build(array $context = array())
{
$this->_context = $context;
if (!$this->canAccessPage()) {
Administration::instance()->throwCustomError(
__('You are not authorised to access this page.'),
__('Access Denied'),
Page::HTTP_STATUS_UNAUTHORIZED
);
}
$this->Html->setDTD('<!DOCTYPE html>');
$this->Html->setAttribute('lang', Lang::get());
$this->addElementToHead(new XMLElement('meta', null, array('charset' => 'UTF-8')), 0);
$this->addElementToHead(new XMLElement('meta', null, array('http-equiv' => 'X-UA-Compatible', 'content' => 'IE=edge,chrome=1')), 1);
$this->addElementToHead(new XMLElement('meta', null, array('name' => 'viewport', 'content' => 'width=device-width, initial-scale=1')), 2);
// Add styles
$this->addStylesheetToHead(ASSETS_URL . '/css/symphony.min.css', 'screen', 2, false);
// Calculate timezone offset from UTC
$timezone = new DateTimeZone(DateTimeObj::getSetting('timezone'));
$datetime = new DateTime('now', $timezone);
$timezoneOffset = intval($timezone->getOffset($datetime)) / 60;
// Add scripts
$environment = array(
'root' => URL,
'symphony' => SYMPHONY_URL,
'path' => '/' . Symphony::Configuration()->get('admin-path', 'symphony'),
'route' => getCurrentPage(),
'version' => Symphony::Configuration()->get('version', 'symphony'),
'lang' => Lang::get(),
'user' => array(
'fullname' => Symphony::Author()->getFullName(),
'name' => Symphony::Author()->get('first_name'),
'type' => Symphony::Author()->get('user_type'),
'id' => Symphony::Author()->get('id')
),
'datetime' => array(
'formats' => DateTimeObj::getDateFormatMappings(),
'timezone-offset' => $timezoneOffset
),
'env' => array_merge(
array('page-namespace' => Symphony::getPageNamespace()),
$this->_context
)
);
$this->addElementToHead(
new XMLElement('script', json_encode($environment), array(
'type' => 'application/json',
'id' => 'environment'
)),
4
);
$this->addScriptToHead(ASSETS_URL . '/js/symphony.min.js', 6, false);
// Initialise page containers
$this->Wrapper = new XMLElement('div', null, array('id' => 'wrapper'));
$this->Header = new XMLElement('header', null, array('id' => 'header'));
$this->Context = new XMLElement('div', null, array('id' => 'context'));
$this->Breadcrumbs = new XMLElement('div', null, array('id' => 'breadcrumbs'));
$this->Contents = new XMLElement('div', null, array('id' => 'contents', 'role' => 'main'));
$this->Form = Widget::Form(Administration::instance()->getCurrentPageURL(), 'post', null, null, array('role' => 'form'));
/**
* Allows developers to insert items into the page HEAD. Use
* `Administration::instance()->Page` for access to the page object.
*
* @since In Symphony 2.3.2 this delegate was renamed from
* `InitaliseAdminPageHead` to the correct spelling of
* `InitialiseAdminPageHead`. The old delegate is supported
* until Symphony 3.0
*
* @delegate InitialiseAdminPageHead
* @param string $context
* '/backend/'
*/
Symphony::ExtensionManager()->notifyMembers('InitialiseAdminPageHead', '/backend/');
Symphony::ExtensionManager()->notifyMembers('InitaliseAdminPageHead', '/backend/');
$this->addHeaderToPage('Content-Type', 'text/html; charset=UTF-8');
$this->addHeaderToPage('Cache-Control', 'no-cache, must-revalidate, max-age=0');
$this->addHeaderToPage('Expires', 'Mon, 12 Dec 1982 06:14:00 GMT');
$this->addHeaderToPage('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT');
$this->addHeaderToPage('Pragma', 'no-cache');
// If not set by another extension, lock down the backend
if (!array_key_exists('x-frame-options', $this->headers())) {
$this->addHeaderToPage('X-Frame-Options', 'SAMEORIGIN');
}
if (!array_key_exists('x-content-type-options', $this->headers())) {
$this->addHeaderToPage('X-Content-Type-Options', 'nosniff');
}
if (!array_key_exists('x-xss-protection', $this->headers())) {
$this->addHeaderToPage('X-XSS-Protection', '1; mode=block');
}
if (!array_key_exists('referrer-policy', $this->headers())) {
$this->addHeaderToPage('Referrer-Policy', 'same-origin');
}
if (isset($_REQUEST['action'])) {
$this->action();
Symphony::Profiler()->sample('Page action run', PROFILE_LAP);
}
$h1 = new XMLElement('h1');
$h1->appendChild(Widget::Anchor(Symphony::Configuration()->get('sitename', 'general'), rtrim(URL, '/') . '/'));
$this->Header->appendChild($h1);
$this->appendUserLinks();
$this->appendNavigation();
// Add Breadcrumbs
$this->Context->prependChild($this->Breadcrumbs);
$this->Contents->appendChild($this->Form);
// Validate date time config
$dateFormat = defined('__SYM_DATE_FORMAT__') ? __SYM_DATE_FORMAT__ : null;
if (empty($dateFormat)) {
$this->pageAlert(
__('Your <code>%s</code> file does not define a date format', array(basename(CONFIG))),
Alert::NOTICE
);
}
$timeFormat = defined('__SYM_TIME_FORMAT__') ? __SYM_TIME_FORMAT__ : null;
if (empty($timeFormat)) {
$this->pageAlert(
__('Your <code>%s</code> file does not define a time format.', array(basename(CONFIG))),
Alert::NOTICE
);
}
$this->view();
$this->appendAlert();
Symphony::Profiler()->sample('Page content created', PROFILE_LAP);
}
/**
* Checks the current Symphony Author can access the current page.
* This check uses the `ASSETS . /xml/navigation.xml` file to determine
* if the current page (or the current page namespace) can be viewed
* by the currently logged in Author.
*
* @since Symphony 2.7.0
* It fires a delegate, CanAccessPage, to allow extensions to restrict access
* to the current page
*
* @uses CanAccessPage
*
* @link http://github.com/symphonycms/symphony-2/blob/master/symphony/assets/xml/navigation.xml
* @return boolean
* true if the Author can access the current page, false otherwise
*/
public function canAccessPage()
{
$nav = $this->getNavigationArray();
$page = '/' . trim(getCurrentPage(), '/') . '/';
$page_limit = 'author';
foreach ($nav as $item) {
if (
// If page directly matches one of the children
General::in_array_multi($page, $item['children'])
// If the page namespace matches one of the children (this will usually drop query
// string parameters such as /edit/1/)
|| General::in_array_multi(Symphony::getPageNamespace() . '/', $item['children'])
) {
if (is_array($item['children'])) {
foreach ($item['children'] as $c) {
if ($c['link'] === $page && isset($c['limit'])) {
$page_limit = $c['limit'];
// TODO: break out of the loop here in Symphony 3.0.0
}
}
}
if (isset($item['limit']) && $page_limit !== 'primary') {
if ($page_limit === 'author' && $item['limit'] === 'developer') {
$page_limit = 'developer';
}
}
} elseif (isset($item['link']) && $page === $item['link'] && isset($item['limit'])) {
$page_limit = $item['limit'];
}
}
$hasAccess = $this->doesAuthorHaveAccess($page_limit);
if ($hasAccess) {
$page_context = $this->getContext();
$section_handle = !isset($page_context['section_handle']) ? null : $page_context['section_handle'];
/**
* Immediately after the core access rules allowed access to this page
* (i.e. not called if the core rules denied it).
* Extension developers must only further restrict access to it.
* Extension developers must also take care of checking the current value
* of the allowed parameter in order to prevent conflicts with other extensions.
* `$context['allowed'] = $context['allowed'] && customLogic();`
*
* @delegate CanAccessPage
* @since Symphony 2.7.0
* @see doesAuthorHaveAccess()
* @param string $context
* '/backend/'
* @param bool $allowed
* A flag to further restrict access to the page, passed by reference
* @param string $page_limit
* The computed page limit for the current page
* @param string $page_url
* The computed page url for the current page
* @param int $section.id
* The id of the section for this url
* @param string $section.handle
* The handle of the section for this url
*/
Symphony::ExtensionManager()->notifyMembers('CanAccessPage', '/backend/', array(
'allowed' => &$hasAccess,
'page_limit' => $page_limit,
'page_url' => $page,
'section' => array(
'id' => !$section_handle ? 0 : SectionManager::fetchIDFromHandle($section_handle),
'handle' => $section_handle
),
));
}
return $hasAccess;
}
/**
* Given the limit of the current navigation item or page, this function
* returns if the current Author can access that item or not.
*
* @since Symphony 2.5.1
* @param string $item_limit
* @return boolean
*/
public function doesAuthorHaveAccess($item_limit = null)
{
$can_access = false;
if (!isset($item_limit) || $item_limit === 'author') {
$can_access = true;
} elseif ($item_limit === 'developer' && Symphony::Author()->isDeveloper()) {
$can_access = true;
} elseif ($item_limit === 'manager' && (Symphony::Author()->isManager() || Symphony::Author()->isDeveloper())) {
$can_access = true;
} elseif ($item_limit === 'primary' && Symphony::Author()->isPrimaryAccount()) {
$can_access = true;
}
return $can_access;
}
/**
* Appends the `$this->Header`, `$this->Context` and `$this->Contents`
* to `$this->Wrapper` before adding the ID and class attributes for
* the `<body>` element. This function will also place any Drawer elements
* in their relevant positions in the page. After this has completed the
* parent `generate()` is called which will convert the `XMLElement`'s
* into strings ready for output.
*
* @see core.HTMLPage#generate()
* @param null $page
* @return string
*/
public function generate($page = null)
{
$this->Wrapper->appendChild($this->Header);
// Add horizontal drawers (inside #context)
if (isset($this->Drawer['horizontal'])) {
$this->Context->appendChildArray($this->Drawer['horizontal']);
}
$this->Wrapper->appendChild($this->Context);
// Add vertical-left drawers (between #context and #contents)
if (isset($this->Drawer['vertical-left'])) {
$this->Contents->appendChildArray($this->Drawer['vertical-left']);
}
// Add vertical-right drawers (after #contents)
if (isset($this->Drawer['vertical-right'])) {
$this->Contents->appendChildArray($this->Drawer['vertical-right']);
}
$this->Wrapper->appendChild($this->Contents);
$this->Body->appendChild($this->Wrapper);
$this->__appendBodyId();
$this->__appendBodyClass($this->_context);
/**
* This is just prior to the page headers being rendered, and is suitable for changing them
* @delegate PreRenderHeaders
* @since Symphony 2.7.0
* @param string $context
* '/backend/'
*/
Symphony::ExtensionManager()->notifyMembers('PreRenderHeaders', '/backend/');
return parent::generate($page);
}
/**
* Uses this pages PHP classname as the `<body>` ID attribute.
* This function removes 'content' from the start of the classname
* and converts all uppercase letters to lowercase and prefixes them
* with a hyphen.
*/
private function __appendBodyId()
{
// trim "content" from beginning of class name
$body_id = preg_replace("/^content/", '', get_class($this));
// lowercase any uppercase letters and prefix with a hyphen
$body_id = trim(
preg_replace_callback(
"/([A-Z])/",
function($id) {
return "-" . strtolower($id[0]);
},
$body_id
),
'-'
);
if (!empty($body_id)) {
$this->Body->setAttribute('id', trim($body_id));
}
}
/**
* Given the context of the current page, which is an associative
* array, this function will append the values to the page's body as
* classes. If an context value is numeric it will be prepended by 'id-',
* otherwise all classes will be prefixed by the context key.
*
* @param array $context
*/
private function __appendBodyClass(array $context = array())
{
$body_class = '';
foreach ($context as $key => $value) {
if (is_numeric($value)) {
$value = 'id-' . $value;
// Add prefixes to all context values by making the
// class be {key}-{value}. #1397 ^BA
} elseif (!is_numeric($key) && isset($value)) {
// Skip arrays
if (is_array($value)) {
$value = null;
} else {
$value = str_replace('_', '-', $key) . '-'. $value;
}
}
if ($value !== null) {
$body_class .= trim($value) . ' ';
}
}
$classes = array_merge(explode(' ', trim($body_class)), explode(' ', trim($this->_body_class)));
$body_class = trim(implode(' ', $classes));
if (!empty($body_class)) {
$this->Body->setAttribute('class', $body_class);
}
}
/**
* Called to build the content for the page. This function immediately calls
* `__switchboard()` which acts a bit of a controller to show content based on
* off a type, such as 'view' or 'action'. `AdministrationPages` can override this
* function to just display content if they do not need the switchboard functionality
*
* @see __switchboard()
*/
public function view()
{
$this->__switchboard();
}
/**
* This function is called when `$_REQUEST` contains a key of 'action'.
* Any logic that needs to occur immediately for the action to complete
* should be contained within this function. By default this calls the
* `__switchboard` with the type set to 'action'.
*
* @see __switchboard()
*/
public function action()
{
$this->__switchboard('action');
}
/**
* The `__switchboard` function acts as a controller to display content
* based off the $type. By default, the `$type` is 'view' but it can be set
* also set to 'action'. The `$type` is prepended by __ and the context is
* append to the $type to create the name of the function that will provide
* that logic. For example, if the $type was action and the context of the
* current page was new, the resulting function to be called would be named
* `__actionNew()`. If an action function is not provided by the Page, this function
* returns nothing, however if a view function is not provided, a 404 page
* will be returned.
*
* @param string $type
* Either 'view' or 'action', by default this will be 'view'
* @throws SymphonyErrorPage
*/
public function __switchboard($type = 'view')
{
if (!isset($this->_context[0]) || trim($this->_context[0]) === '') {
$context = 'index';
} else {
$context = $this->_context[0];
}
$function = ($type == 'action' ? '__action' : '__view') . ucfirst($context);
if (!method_exists($this, $function)) {
// If there is no action function, just return without doing anything
if ($type == 'action') {
return;
}
Administration::instance()->errorPageNotFound();
}
$this->$function(null);
}
/**
* If `$this->Alert` is set, it will be added to this page. The
* `AppendPageAlert` delegate is fired to allow extensions to provide their
* their own Alert messages for this page. Since Symphony 2.3, there may be
* more than one `Alert` per page. Alerts are displayed in the order of
* severity, with Errors first, then Success alerts followed by Notices.
*
* @uses AppendPageAlert
*/
public function appendAlert()
{
/**
* Allows for appending of alerts. Administration::instance()->Page->Alert is way to tell what
* is currently in the system
*
* @delegate AppendPageAlert
* @param string $context
* '/backend/'
*/
Symphony::ExtensionManager()->notifyMembers('AppendPageAlert', '/backend/');
if (!is_array($this->Alert) || empty($this->Alert)) {
return;
}
usort($this->Alert, array($this, 'sortAlerts'));
// Using prependChild ruins our order (it's backwards, but with most
// recent notices coming after oldest notices), so reversing the array
// fixes this. We need to prepend so that without Javascript the notices
// are at the top of the markup. See #1312
$this->Alert = array_reverse($this->Alert);
foreach ($this->Alert as $alert) {
$this->Header->prependChild($alert->asXML());
}
}
// Errors first, success next, then notices.
public function sortAlerts($a, $b)
{
if ($a->{'type'} === $b->{'type'}) {
return 0;
}
if (
($a->{'type'} === Alert::ERROR && $a->{'type'} !== $b->{'type'})
|| ($a->{'type'} === Alert::SUCCESS && $b->{'type'} === Alert::NOTICE)
) {
return -1;
}
return 1;
}
/**
* This function will append the Navigation to the AdministrationPage.
* It fires a delegate, NavigationPreRender, to allow extensions to manipulate
* the navigation. Extensions should not use this to add their own navigation,
* they should provide the navigation through their fetchNavigation function.
* Note with the Section navigation groups, if there is only one section in a group
* and that section is set to visible, the group will not appear in the navigation.
*
* @uses NavigationPreRender
* @see getNavigationArray()
* @see toolkit.Extension#fetchNavigation()
*/
public function appendNavigation()
{
$nav = $this->getNavigationArray();
/**
* Immediately before displaying the admin navigation. Provided with the
* navigation array. Manipulating it will alter the navigation for all pages.
*
* @delegate NavigationPreRender
* @param string $context
* '/backend/'
* @param array $nav
* An associative array of the current navigation, passed by reference
*/
Symphony::ExtensionManager()->notifyMembers('NavigationPreRender', '/backend/', array(
'navigation' => &$nav,
));
$navElement = new XMLElement('nav', null, array('id' => 'nav', 'role' => 'navigation'));
$contentNav = new XMLElement('ul', null, array('class' => 'content', 'role' => 'menubar'));
$structureNav = new XMLElement('ul', null, array('class' => 'structure', 'role' => 'menubar'));
foreach ($nav as $n) {
if (isset($n['visible']) && $n['visible'] === 'no') {
continue;
}
$item_limit = isset($n['limit']) ? $n['limit'] : null;
if ($this->doesAuthorHaveAccess($item_limit)) {
$xGroup = new XMLElement('li', General::sanitize($n['name']), array('role' => 'presentation'));
if (isset($n['class']) && trim($n['name']) !== '') {
$xGroup->setAttribute('class', $n['class']);
}
$hasChildren = false;
$xChildren = new XMLElement('ul', null, array('role' => 'menu'));
if (is_array($n['children']) && !empty($n['children'])) {
foreach ($n['children'] as $c) {
// adapt for Yes and yes
if (strtolower($c['visible']) !== 'yes') {
continue;
}
$child_item_limit = isset($c['limit']) ? $c['limit'] : null;
if ($this->doesAuthorHaveAccess($child_item_limit)) {
$xChild = new XMLElement('li');
$xChild->setAttribute('role', 'menuitem');
$linkChild = Widget::Anchor(General::sanitize($c['name']), SYMPHONY_URL . $c['link']);
if (isset($c['target'])) {
$linkChild->setAttribute('target', $c['target']);
}
$xChild->appendChild($linkChild);
$xChildren->appendChild($xChild);
$hasChildren = true;
}
}
if ($hasChildren) {
$xGroup->setAttribute('aria-haspopup', 'true');
$xGroup->appendChild($xChildren);
if ($n['type'] === 'content') {
$contentNav->appendChild($xGroup);
} elseif ($n['type'] === 'structure') {
$structureNav->prependChild($xGroup);
}
}
}
}
}
$navElement->appendChild($contentNav);
$navElement->appendChild($structureNav);
$this->Header->appendChild($navElement);
Symphony::Profiler()->sample('Navigation Built', PROFILE_LAP);
}
/**
* Returns the `$_navigation` variable of this Page. If it is empty,
* it will be built by `__buildNavigation`
*
* When it calls `__buildNavigation`, it fires a delegate, NavigationPostBuild,
* to allow extensions to manipulate the navigation.
*
* @uses NavigationPostBuild
* @see __buildNavigation()
* @return array
*/
public function getNavigationArray()
{
if (empty($this->_navigation)) {
$this->__buildNavigation();
}
return $this->_navigation;
}
/**
* This method fills the `$nav` array with value
* from the `ASSETS/xml/navigation.xml` file
*
* @link http://github.com/symphonycms/symphony-2/blob/master/symphony/assets/xml/navigation.xml
*
* @since Symphony 2.3.2
*
* @param array $nav
* The navigation array that will receive nav nodes
*/
private function buildXmlNavigation(&$nav)
{
$xml = simplexml_load_file(ASSETS . '/xml/navigation.xml');
// Loop over the default Symphony navigation file, converting
// it into an associative array representation
foreach ($xml->xpath('/navigation/group') as $n) {
$index = (string)$n->attributes()->index;
$children = $n->xpath('children/item');
$content = $n->attributes();
// If the index is already set, increment the index and check again.
// Rinse and repeat until the index is not set.
if (isset($nav[$index])) {
do {
$index++;
} while (isset($nav[$index]));
}
$nav[$index] = array(
'name' => __(strval($content->name)),
'type' => 'structure',
'index' => $index,