/
content.blueprintspages.php
971 lines (807 loc) · 38.3 KB
/
content.blueprintspages.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
<?php
/**
* @package content
*/
/**
* Developers can create new Frontend pages from this class. It provides
* an index view of all the pages in this Symphony install as well as the
* forms for the creation/editing of a Page
*/
class contentBlueprintsPages extends AdministrationPage
{
public $_errors = array();
protected $_hilights = array();
public function insertBreadcrumbsUsingPageIdentifier($page_id, $preserve_last = true)
{
if ($page_id == 0) {
return $this->insertBreadcrumbs(
array(Widget::Anchor(__('Pages'), SYMPHONY_URL . '/blueprints/pages/'))
);
}
$pages = PageManager::resolvePage($page_id, 'handle');
foreach ($pages as &$page) {
// If we are viewing the Page Editor, the Breadcrumbs should link
// to the parent's Page Editor.
if ($this->_context[0] == 'edit') {
$page = Widget::Anchor(
PageManager::fetchTitleFromHandle($page),
SYMPHONY_URL . '/blueprints/pages/edit/' . PageManager::fetchIDFromHandle($page) . '/'
);
// If the pages index is nested, the Breadcrumb should link to the
// Pages Index filtered by parent
} elseif (Symphony::Configuration()->get('pages_table_nest_children', 'symphony') == 'yes') {
$page = Widget::Anchor(
PageManager::fetchTitleFromHandle($page),
SYMPHONY_URL . '/blueprints/pages/?parent=' . PageManager::fetchIDFromHandle($page)
);
// If there is no nesting on the Pages Index, the breadcrumb is
// not a link, just plain text
} else {
$page = new XMLElement('span', PageManager::fetchTitleFromHandle($page));
}
}
if (!$preserve_last) {
array_pop($pages);
}
$this->insertBreadcrumbs(array_merge(
array(Widget::Anchor(__('Pages'), SYMPHONY_URL . '/blueprints/pages/')),
$pages
));
}
public function __viewIndex()
{
$this->setPageType('table');
$this->setTitle(__('%1$s – %2$s', array(__('Pages'), __('Symphony'))));
$nesting = Symphony::Configuration()->get('pages_table_nest_children', 'symphony') == 'yes';
if ($nesting && isset($_GET['parent']) && is_numeric($_GET['parent'])) {
$parent = PageManager::fetchPageByID((int)$_GET['parent'], array('title', 'id'));
}
$this->appendSubheading(isset($parent) ? $parent['title'] : __('Pages'), Widget::Anchor(
__('Create New'), Administration::instance()->getCurrentPageURL() . 'new/' . ($nesting && isset($parent) ? "?parent={$parent['id']}" : null),
__('Create a new page'), 'create button', null, array('accesskey' => 'c')
));
if (isset($parent)) {
$this->insertBreadcrumbsUsingPageIdentifier($parent['id'], false);
}
$aTableHead = array(
array(__('Name'), 'col'),
array(__('Template'), 'col'),
array('<abbr title="' . __('Universal Resource Locator') . '">' . __('URL') . '</abbr>', 'col'),
array(__('Parameters'), 'col'),
array(__('Type'), 'col')
);
$aTableBody = array();
if ($nesting) {
$aTableHead[] = array(__('Children'), 'col');
$where = array(
'parent ' . (isset($parent) ? " = {$parent['id']} " : ' IS NULL ')
);
} else {
$where = array();
}
$pages = PageManager::fetch(true, array('*'), $where);
if (!is_array($pages) || empty($pages)) {
$aTableBody = array(Widget::TableRow(array(
Widget::TableData(__('None found.'), 'inactive', null, count($aTableHead))
), 'odd'));
} else {
foreach ($pages as $page) {
$class = array();
$page_title = ($nesting ? $page['title'] : PageManager::resolvePageTitle($page['id']));
$page_url = URL . '/' . PageManager::resolvePagePath($page['id']) . '/';
$page_edit_url = Administration::instance()->getCurrentPageURL() . 'edit/' . $page['id'] . '/';
$page_template = PageManager::createFilePath($page['path'], $page['handle']);
$col_title = Widget::TableData(Widget::Anchor($page_title, $page_edit_url, $page['handle']));
$col_title->appendChild(Widget::Label(__('Select Page %s', array($page_title)), null, 'accessible', null, array(
'for' => 'page-' . $page['id']
)));
$col_title->appendChild(Widget::Input('items['.$page['id'].']', 'on', 'checkbox', array(
'id' => 'page-' . $page['id']
)));
$col_template = Widget::TableData($page_template . '.xsl');
$col_url = Widget::TableData(Widget::Anchor($page_url, $page_url));
if ($page['params']) {
$col_params = Widget::TableData(trim($page['params'], '/'));
} else {
$col_params = Widget::TableData(__('None'), 'inactive');
}
if (!empty($page['type'])) {
$col_types = Widget::TableData(implode(', ', $page['type']));
} else {
$col_types = Widget::TableData(__('None'), 'inactive');
}
if (in_array($page['id'], $this->_hilights)) {
$class[] = 'failed';
}
$columns = array($col_title, $col_template, $col_url, $col_params, $col_types);
if ($nesting) {
if (PageManager::hasChildPages($page['id'])) {
$col_children = Widget::TableData(
Widget::Anchor(PageManager::getChildPagesCount($page['id']) . ' →',
SYMPHONY_URL . '/blueprints/pages/?parent=' . $page['id'])
);
} else {
$col_children = Widget::TableData(__('None'), 'inactive');
}
$columns[] = $col_children;
}
$aTableBody[] = Widget::TableRow(
$columns,
implode(' ', $class)
);
}
}
$table = Widget::Table(
Widget::TableHead($aTableHead), null,
Widget::TableBody($aTableBody), 'orderable selectable',
null, array('role' => 'directory', 'aria-labelledby' => 'symphony-subheading', 'data-interactive' => 'data-interactive')
);
$this->Form->appendChild($table);
$version = new XMLElement('p', 'Symphony ' . Symphony::Configuration()->get('version', 'symphony'), array(
'id' => 'version'
));
$this->Form->appendChild($version);
$tableActions = new XMLElement('div');
$tableActions->setAttribute('class', 'actions');
$options = array(
array(null, false, __('With Selected...')),
array('delete', false, __('Delete'), 'confirm', null, array(
'data-message' => __('Are you sure you want to delete the selected pages?')
))
);
/**
* Allows an extension to modify the existing options for this page's
* With Selected menu. If the `$options` parameter is an empty array,
* the 'With Selected' menu will not be rendered.
*
* @delegate AddCustomActions
* @since Symphony 2.3.2
* @param string $context
* '/blueprints/pages/'
* @param array $options
* An array of arrays, where each child array represents an option
* in the With Selected menu. Options should follow the same format
* expected by `Widget::__SelectBuildOption`. Passed by reference.
*/
Symphony::ExtensionManager()->notifyMembers('AddCustomActions', '/blueprints/pages/', array(
'options' => &$options
));
if (!empty($options)) {
$tableActions->appendChild(Widget::Apply($options));
$this->Form->appendChild($tableActions);
}
}
public function __viewNew()
{
$this->__viewEdit();
}
public function __viewEdit()
{
$this->setPageType('form');
$fields = array("title"=>null, "handle"=>null, "parent"=>null, "params"=>null, "type"=>null, "data_sources"=>null);
$existing = $fields;
$nesting = (Symphony::Configuration()->get('pages_table_nest_children', 'symphony') == 'yes');
// Verify page exists:
if ($this->_context[0] === 'edit') {
if (!$page_id = (int)$this->_context[1]) {
redirect(SYMPHONY_URL . '/blueprints/pages/');
}
$existing = PageManager::fetchPageByID($page_id);
if (!$existing) {
Administration::instance()->errorPageNotFound();
} else {
$existing['type'] = PageManager::fetchPageTypes($page_id);
}
}
// Status message:
if (isset($this->_context[2])) {
$flag = $this->_context[2];
$parent_link_suffix = $message = '';
$time = Widget::Time();
if (isset($_REQUEST['parent']) && is_numeric($_REQUEST['parent'])) {
$parent_link_suffix = "?parent=" . $_REQUEST['parent'];
} elseif ($nesting && isset($existing) && !is_null($existing['parent'])) {
$parent_link_suffix = '?parent=' . $existing['parent'];
}
switch ($flag) {
case 'saved':
$message = __('Page updated at %s.', array($time->generate()));
break;
case 'created':
$message = __('Page created at %s.', array($time->generate()));
}
$this->pageAlert(
$message
. ' <a href="' . SYMPHONY_URL . '/blueprints/pages/new/' . $parent_link_suffix . '" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/blueprints/pages/" accesskey="a">'
. __('View all Pages')
. '</a>',
Alert::SUCCESS
);
}
// Find values:
if (isset($_POST['fields'])) {
$fields = $_POST['fields'];
} elseif ($this->_context[0] == 'edit') {
$fields = $existing;
if (!is_null($fields['type'])) {
$fields['type'] = implode(', ', $fields['type']);
}
$fields['data_sources'] = preg_split('/,/i', $fields['data_sources'], -1, PREG_SPLIT_NO_EMPTY);
$fields['events'] = preg_split('/,/i', $fields['events'], -1, PREG_SPLIT_NO_EMPTY);
} elseif (isset($_REQUEST['parent']) && is_numeric($_REQUEST['parent'])) {
$fields['parent'] = $_REQUEST['parent'];
}
$title = $fields['title'];
if (trim($title) == '') {
$title = $existing['title'];
}
$this->setTitle(__(
($title ? '%1$s – %2$s – %3$s' : '%2$s – %3$s'),
array(
$title,
__('Pages'),
__('Symphony')
)
));
$page_id = isset($page_id) ? $page_id : null;
if (!empty($title)) {
$page_url = URL . '/' . PageManager::resolvePagePath($page_id) . '/';
$this->appendSubheading($title, array(
Widget::Anchor(__('View Page'), $page_url, __('View Page on Frontend'), 'button', null, array('target' => '_blank', 'accesskey' => 'v'))
));
} else {
$this->appendSubheading(!empty($title) ? $title : __('Untitled'));
}
if (isset($page_id)) {
$this->insertBreadcrumbsUsingPageIdentifier($page_id, false);
} else {
$_GET['parent'] = isset($_GET['parent']) ? $_GET['parent'] : null;
$this->insertBreadcrumbsUsingPageIdentifier((int)$_GET['parent'], true);
}
// Title --------------------------------------------------------------
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', __('Page Settings')));
$label = Widget::Label(__('Name'));
$label->appendChild(Widget::Input(
'fields[title]', General::sanitize($fields['title'])
));
if (isset($this->_errors['title'])) {
$label = Widget::Error($label, $this->_errors['title']);
}
$fieldset->appendChild($label);
// Handle -------------------------------------------------------------
$group = new XMLElement('div');
$group->setAttribute('class', 'two columns');
$column = new XMLElement('div');
$column->setAttribute('class', 'column');
$label = Widget::Label(__('Handle'));
$label->appendChild(Widget::Input(
'fields[handle]', $fields['handle']
));
if (isset($this->_errors['handle'])) {
$label = Widget::Error($label, $this->_errors['handle']);
}
$column->appendChild($label);
// Parent ---------------------------------------------------------
$label = Widget::Label(__('Parent Page'));
$where = array(
sprintf('id != %d', $page_id)
);
$pages = PageManager::fetch(false, array('id'), $where, 'title ASC');
$options = array(
array('', false, '/')
);
if (!empty($pages)) {
foreach ($pages as $page) {
$options[] = array(
$page['id'], $fields['parent'] == $page['id'],
'/' . PageManager::resolvePagePath($page['id'])
);
}
usort($options, array($this, '__compare_pages'));
}
$label->appendChild(Widget::Select(
'fields[parent]', $options
));
$column->appendChild($label);
$group->appendChild($column);
// Parameters ---------------------------------------------------------
$column = new XMLElement('div');
$column->setAttribute('class', 'column');
$label = Widget::Label(__('Parameters'));
$label->appendChild(Widget::Input(
'fields[params]', $fields['params'], 'text', array('placeholder' => 'param1/param2')
));
$column->appendChild($label);
// Type -----------------------------------------------------------
$label = Widget::Label(__('Type'));
$label->appendChild(Widget::Input('fields[type]', $fields['type']));
if (isset($this->_errors['type'])) {
$label = Widget::Error($label, $this->_errors['type']);
}
$column->appendChild($label);
$tags = new XMLElement('ul');
$tags->setAttribute('class', 'tags');
$tags->setAttribute('data-interactive', 'data-interactive');
$types = PageManager::fetchAvailablePageTypes();
foreach ($types as $type) {
$tags->appendChild(new XMLElement('li', $type));
}
$column->appendChild($tags);
$group->appendChild($column);
$fieldset->appendChild($group);
$this->Form->appendChild($fieldset);
// Events -------------------------------------------------------------
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', __('Page Resources')));
$group = new XMLElement('div');
$group->setAttribute('class', 'two columns');
$label = Widget::Label(__('Events'));
$label->setAttribute('class', 'column');
$events = ResourceManager::fetch(ResourceManager::RESOURCE_TYPE_EVENT, array(), array(), 'name ASC');
$options = array();
if (is_array($events) && !empty($events)) {
if (!isset($fields['events'])) {
$fields['events'] = array();
}
foreach ($events as $name => $about) {
$options[] = array(
$name, in_array($name, $fields['events']), $about['name']
);
}
}
$label->appendChild(Widget::Select('fields[events][]', $options, array('multiple' => 'multiple')));
$group->appendChild($label);
// Data Sources -------------------------------------------------------
$label = Widget::Label(__('Data Sources'));
$label->setAttribute('class', 'column');
$datasources = ResourceManager::fetch(ResourceManager::RESOURCE_TYPE_DS, array(), array(), 'name ASC');
$options = array();
if (is_array($datasources) && !empty($datasources)) {
if (!isset($fields['data_sources'])) {
$fields['data_sources'] = array();
}
foreach ($datasources as $name => $about) {
$options[] = array(
$name, in_array($name, $fields['data_sources']), $about['name']
);
}
}
$label->appendChild(Widget::Select('fields[data_sources][]', $options, array('multiple' => 'multiple')));
$group->appendChild($label);
$fieldset->appendChild($group);
$this->Form->appendChild($fieldset);
// Controls -----------------------------------------------------------
/**
* After all Page related Fields have been added to the DOM, just before the
* actions.
*
* @delegate AppendPageContent
* @param string $context
* '/blueprints/pages/'
* @param XMLElement $form
* @param array $fields
* @param array $errors
*/
Symphony::ExtensionManager()->notifyMembers(
'AppendPageContent',
'/blueprints/pages/',
array(
'form' => &$this->Form,
'fields' => &$fields,
'errors' => $this->_errors
)
);
$div = new XMLElement('div');
$div->setAttribute('class', 'actions');
$div->appendChild(Widget::Input(
'action[save]', ($this->_context[0] == 'edit' ? __('Save Changes') : __('Create Page')),
'submit', array('accesskey' => 's')
));
if ($this->_context[0] == 'edit') {
$button = new XMLElement('button', __('Delete'));
$button->setAttributeArray(array('name' => 'action[delete]', 'class' => 'button confirm delete', 'title' => __('Delete this page'), 'accesskey' => 'd', 'data-message' => __('Are you sure you want to delete this page?')));
$div->appendChild($button);
}
$this->Form->appendChild($div);
if (isset($_REQUEST['parent']) && is_numeric($_REQUEST['parent'])) {
$this->Form->appendChild(new XMLElement('input', null, array('type' => 'hidden', 'name' => 'parent', 'value' => $_REQUEST['parent'])));
}
}
public function __compare_pages($a, $b)
{
return strnatcasecmp($a[2], $b[2]);
}
public function __actionIndex()
{
$checked = (is_array($_POST['items'])) ? array_keys($_POST['items']) : null;
if (is_array($checked) && !empty($checked)) {
/**
* Extensions can listen for any custom actions that were added
* through `AddCustomPreferenceFieldsets` or `AddCustomActions`
* delegates.
*
* @delegate CustomActions
* @since Symphony 2.3.2
* @param string $context
* '/blueprints/pages/'
* @param array $checked
* An array of the selected rows. The value is usually the ID of the
* the associated object.
*/
Symphony::ExtensionManager()->notifyMembers('CustomActions', '/blueprints/pages/', array(
'checked' => $checked
));
switch ($_POST['with-selected']) {
case 'delete':
$this->__actionDelete($checked, SYMPHONY_URL . '/blueprints/pages/');
break;
}
}
}
public function __actionTemplate()
{
$filename = $this->_context[1] . '.xsl';
$file_abs = PAGES . '/' . $filename;
$fields = $_POST['fields'];
$this->_errors = array();
if (!isset($fields['body']) || trim($fields['body']) == '') {
$this->_errors['body'] = __('This is a required field.');
} elseif (!General::validateXML($fields['body'], $errors, false, new XSLTProcess())) {
$this->_errors['body'] = __('This document is not well formed.') . ' ' . __('The following error was returned:') . ' <code>' . $errors[0]['message'] . '</code>';
}
if (empty($this->_errors)) {
/**
* Just before a Page Template is about to written to disk
*
* @delegate PageTemplatePreEdit
* @since Symphony 2.2.2
* @param string $context
* '/blueprints/pages/template/'
* @param string $file
* The path to the Page Template file
* @param string $contents
* The contents of the `$fields['body']`, passed by reference
*/
Symphony::ExtensionManager()->notifyMembers('PageTemplatePreEdit', '/blueprints/pages/template/', array('file' => $file_abs, 'contents' => &$fields['body']));
if (!PageManager::writePageFiles($file_abs, $fields['body'])) {
$this->pageAlert(
__('Page Template could not be written to disk.')
. ' ' . __('Please check permissions on %s.', array('<code>/workspace/pages</code>')),
Alert::ERROR
);
} else {
/**
* Just after a Page Template has been edited and written to disk
*
* @delegate PageTemplatePostEdit
* @since Symphony 2.2.2
* @param string $context
* '/blueprints/pages/template/'
* @param string $file
* The path to the Page Template file
*/
Symphony::ExtensionManager()->notifyMembers('PageTemplatePostEdit', '/blueprints/pages/template/', array('file' => $file_abs));
redirect(SYMPHONY_URL . '/blueprints/pages/template/' . $this->_context[1] . '/saved/');
}
}
}
public function __actionNew()
{
$this->__actionEdit();
}
public function __actionEdit()
{
if ($this->_context[0] != 'new' && !$page_id = (integer)$this->_context[1]) {
redirect(SYMPHONY_URL . '/blueprints/pages/');
}
$parent_link_suffix = null;
if (isset($_REQUEST['parent']) && is_numeric($_REQUEST['parent'])) {
$parent_link_suffix = '?parent=' . $_REQUEST['parent'];
}
if (@array_key_exists('delete', $_POST['action'])) {
$this->__actionDelete($page_id, SYMPHONY_URL . '/blueprints/pages/' . $parent_link_suffix);
}
if (@array_key_exists('save', $_POST['action'])) {
$fields = $_POST['fields'];
$this->_errors = array();
$autogenerated_handle = false;
if (!isset($fields['title']) || trim($fields['title']) == '') {
$this->_errors['title'] = __('This is a required field');
}
if (trim($fields['type']) != '' && preg_match('/(index|404|403)/i', $fields['type'])) {
$types = preg_split('/\s*,\s*/', strtolower($fields['type']), -1, PREG_SPLIT_NO_EMPTY);
if (in_array('index', $types) && PageManager::hasPageTypeBeenUsed($page_id, 'index')) {
$this->_errors['type'] = __('An index type page already exists.');
} elseif (in_array('404', $types) && PageManager::hasPageTypeBeenUsed($page_id, '404')) {
$this->_errors['type'] = __('A 404 type page already exists.');
} elseif (in_array('403', $types) && PageManager::hasPageTypeBeenUsed($page_id, '403')) {
$this->_errors['type'] = __('A 403 type page already exists.');
}
}
if (trim($fields['handle']) == '') {
$fields['handle'] = $fields['title'];
$autogenerated_handle = true;
}
$fields['handle'] = PageManager::createHandle($fields['handle']);
if (empty($fields['handle']) && !isset($this->_errors['title'])) {
$this->_errors['handle'] = __('Please ensure handle contains at least one Latin-based character.');
}
/**
* Just after the Symphony validation has run, allows Developers
* to run custom validation logic on a Page
*
* @delegate PagePostValidate
* @since Symphony 2.2
* @param string $context
* '/blueprints/pages/'
* @param array $fields
* The `$_POST['fields']` array. This should be read-only and not changed
* through this delegate.
* @param array $errors
* An associative array of errors, with the key matching a key in the
* `$fields` array, and the value being the string of the error. `$errors`
* is passed by reference.
*/
Symphony::ExtensionManager()->notifyMembers('PagePostValidate', '/blueprints/pages/', array('fields' => $fields, 'errors' => &$errors));
if (empty($this->_errors)) {
$autogenerated_handle = false;
if ($fields['params']) {
$fields['params'] = trim(preg_replace('@\/{2,}@', '/', $fields['params']), '/');
}
// Clean up type list
$types = preg_split('/\s*,\s*/', $fields['type'], -1, PREG_SPLIT_NO_EMPTY);
$types = @array_map('trim', $types);
unset($fields['type']);
$fields['parent'] = ($fields['parent'] != __('None') ? $fields['parent'] : null);
$fields['data_sources'] = is_array($fields['data_sources']) ? implode(',', $fields['data_sources']) : null;
$fields['events'] = is_array($fields['events']) ? implode(',', $fields['events']) : null;
$fields['path'] = null;
if ($fields['parent']) {
$fields['path'] = PageManager::resolvePagePath((integer)$fields['parent']);
}
// Check for duplicates:
$current = PageManager::fetchPageByID($page_id);
if (empty($current)) {
$fields['sortorder'] = PageManager::fetchNextSortOrder();
}
$where = array();
if (!empty($current)) {
$where[] = "p.id != {$page_id}";
}
$where[] = "p.handle = '" . $fields['handle'] . "'";
$where[] = (is_null($fields['path']))
? "p.path IS null"
: "p.path = '" . $fields['path'] . "'";
$duplicate = PageManager::fetch(false, array('*'), $where);
// If duplicate
if (!empty($duplicate)) {
if ($autogenerated_handle) {
$this->_errors['title'] = __('A page with that title already exists');
} else {
$this->_errors['handle'] = __('A page with that handle already exists');
}
// Create or move files:
} else {
// New page?
if (empty($current)) {
$file_created = PageManager::createPageFiles(
$fields['path'],
$fields['handle']
);
// Existing page, potentially rename files
} else {
$file_created = PageManager::createPageFiles(
$fields['path'],
$fields['handle'],
$current['path'],
$current['handle']
);
}
// If the file wasn't created, it's usually permissions related
if (!$file_created) {
$redirect = null;
return $this->pageAlert(
__('Page Template could not be written to disk.')
. ' ' . __('Please check permissions on %s.', array('<code>/workspace/pages</code>')),
Alert::ERROR
);
}
// Insert the new data:
if (empty($current)) {
/**
* Just prior to creating a new Page record in `tbl_pages`, provided
* with the `$fields` associative array. Use with caution, as no
* duplicate page checks are run after this delegate has fired
*
* @delegate PagePreCreate
* @since Symphony 2.2
* @param string $context
* '/blueprints/pages/'
* @param array $fields
* The `$_POST['fields']` array passed by reference
*/
Symphony::ExtensionManager()->notifyMembers('PagePreCreate', '/blueprints/pages/', array('fields' => &$fields));
if (!$page_id = PageManager::add($fields)) {
$this->pageAlert(
__('Unknown errors occurred while attempting to save.')
. '<a href="' . SYMPHONY_URL . '/system/log/">'
. __('Check your activity log')
. '</a>.',
Alert::ERROR
);
} else {
/**
* Just after the creation of a new page in `tbl_pages`
*
* @delegate PagePostCreate
* @since Symphony 2.2
* @param string $context
* '/blueprints/pages/'
* @param integer $page_id
* The ID of the newly created Page
* @param array $fields
* An associative array of data that was just saved for this page
*/
Symphony::ExtensionManager()->notifyMembers('PagePostCreate', '/blueprints/pages/', array('page_id' => $page_id, 'fields' => &$fields));
$redirect = "/blueprints/pages/edit/{$page_id}/created/{$parent_link_suffix}";
}
// Update existing:
} else {
/**
* Just prior to updating a Page record in `tbl_pages`, provided
* with the `$fields` associative array. Use with caution, as no
* duplicate page checks are run after this delegate has fired
*
* @delegate PagePreEdit
* @since Symphony 2.2
* @param string $context
* '/blueprints/pages/'
* @param integer $page_id
* The ID of the Page that is about to be updated
* @param array $fields
* The `$_POST['fields']` array passed by reference
*/
Symphony::ExtensionManager()->notifyMembers('PagePreEdit', '/blueprints/pages/', array('page_id' => $page_id, 'fields' => &$fields));
if (!PageManager::edit($page_id, $fields, true)) {
return $this->pageAlert(
__('Unknown errors occurred while attempting to save.')
. '<a href="' . SYMPHONY_URL . '/system/log/">'
. __('Check your activity log')
. '</a>.',
Alert::ERROR
);
} else {
/**
* Just after updating a page in `tbl_pages`
*
* @delegate PagePostEdit
* @since Symphony 2.2
* @param string $context
* '/blueprints/pages/'
* @param integer $page_id
* The ID of the Page that was just updated
* @param array $fields
* An associative array of data that was just saved for this page
*/
Symphony::ExtensionManager()->notifyMembers('PagePostEdit', '/blueprints/pages/', array('page_id' => $page_id, 'fields' => $fields));
$redirect = "/blueprints/pages/edit/{$page_id}/saved/{$parent_link_suffix}";
}
}
}
// Only proceed if there was no errors saving/creating the page
if (empty($this->_errors)) {
/**
* Just before the page's types are saved into `tbl_pages_types`.
* Use with caution as no further processing is done on the `$types`
* array to prevent duplicate `$types` from occurring (ie. two index
* page types). Your logic can use the PageManger::hasPageTypeBeenUsed
* function to perform this logic.
*
* @delegate PageTypePreCreate
* @since Symphony 2.2
* @see toolkit.PageManager#hasPageTypeBeenUsed
* @param string $context
* '/blueprints/pages/'
* @param integer $page_id
* The ID of the Page that was just created or updated
* @param array $types
* An associative array of the types for this page passed by reference.
*/
Symphony::ExtensionManager()->notifyMembers('PageTypePreCreate', '/blueprints/pages/', array('page_id' => $page_id, 'types' => &$types));
// Assign page types:
PageManager::addPageTypesToPage($page_id, $types);
// Find and update children:
if ($this->_context[0] == 'edit') {
PageManager::editPageChildren($page_id, $fields['path'] . '/' . $fields['handle']);
}
if ($redirect) {
redirect(SYMPHONY_URL . $redirect);
}
}
}
// If there was any errors, either with pre processing or because of a
// duplicate page, return.
if (is_array($this->_errors) && !empty($this->_errors)) {
return $this->pageAlert(
__('An error occurred while processing this form. See below for details.'),
Alert::ERROR
);
}
}
}
public function __actionDelete($pages, $redirect)
{
$success = true;
$deleted_page_ids = array();
if (!is_array($pages)) {
$pages = array($pages);
}
/**
* Prior to deleting Pages
*
* @delegate PagePreDelete
* @since Symphony 2.2
* @param string $context
* '/blueprints/pages/'
* @param array $page_ids
* An array of Page ID's that are about to be deleted, passed
* by reference
* @param string $redirect
* The absolute path that the Developer will be redirected to
* after the Pages are deleted
*/
Symphony::ExtensionManager()->notifyMembers('PagePreDelete', '/blueprints/pages/', array('page_ids' => &$pages, 'redirect' => &$redirect));
foreach ($pages as $page_id) {
$page = PageManager::fetchPageByID($page_id);
if (empty($page)) {
$success = false;
$this->pageAlert(
__('Page could not be deleted because it does not exist.'),
Alert::ERROR
);
break;
}
if (PageManager::hasChildPages($page_id)) {
$this->_hilights[] = $page['id'];
$success = false;
$this->pageAlert(
__('Page could not be deleted because it has children.'),
Alert::ERROR
);
continue;
}
if (!PageManager::deletePageFiles($page['path'], $page['handle'])) {
$this->_hilights[] = $page['id'];
$success = false;
$this->pageAlert(
__('One or more pages could not be deleted.')
. ' ' . __('Please check permissions on %s.', array('<code>/workspace/pages</code>')),
Alert::ERROR
);
continue;
}
if (PageManager::delete($page_id, false)) {
$deleted_page_ids[] = $page_id;
}
}
if ($success) {
/**
* Fires after all Pages have been deleted
*
* @delegate PagePostDelete
* @since Symphony 2.3
* @param string $context
* '/blueprints/pages/'
* @param array $page_ids
* The page ID's that were just deleted
*/
Symphony::ExtensionManager()->notifyMembers('PagePostDelete', '/blueprints/pages/', array('page_ids' => $deleted_page_ids));
redirect($redirect);
}
}
}