|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package data-sources |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* The `NavigationDatasource` outputs the Symphony page structure as XML. |
|
9
|
|
|
* This datasource supports filtering to narrow down the results to only |
|
10
|
|
|
* show pages that match a particular page type, have a specific parent, etc. |
|
11
|
|
|
* |
|
12
|
|
|
* @since Symphony 2.3 |
|
13
|
|
|
*/ |
|
14
|
|
|
class NavigationDatasource extends Datasource |
|
15
|
|
|
{ |
|
16
|
|
|
public function execute(array &$param_pool = null) |
|
17
|
|
|
{ |
|
18
|
|
|
$result = new XMLElement($this->dsParamROOTELEMENT); |
|
|
|
|
|
|
19
|
|
|
$type_sql = $parent_sql = null; |
|
20
|
|
|
|
|
21
|
|
|
if (trim($this->dsParamFILTERS['type']) !== '') { |
|
22
|
|
|
$type_sql = $this->__processNavigationTypeFilter($this->dsParamFILTERS['type'], |
|
|
|
|
|
|
23
|
|
|
Datasource::determineFilterType($this->dsParamFILTERS['type'])); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
if (trim($this->dsParamFILTERS['parent']) !== '') { |
|
27
|
|
|
$parent_sql = $this->__processNavigationParentFilter($this->dsParamFILTERS['parent']); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
// Build the Query appending the Parent and/or Type WHERE clauses |
|
31
|
|
|
$pages = Symphony::Database()->fetch(sprintf( |
|
32
|
|
|
"SELECT DISTINCT p.id, p.title, p.handle, (SELECT COUNT(id) FROM `tbl_pages` WHERE parent = p.id) AS children |
|
33
|
|
|
FROM `tbl_pages` AS p |
|
34
|
|
|
LEFT JOIN `tbl_pages_types` AS pt ON (p.id = pt.page_id) |
|
35
|
|
|
WHERE 1 = 1 |
|
36
|
|
|
%s |
|
37
|
|
|
%s |
|
38
|
|
|
ORDER BY p.`sortorder` ASC", |
|
39
|
|
|
// Add Parent SQL |
|
40
|
|
|
!is_null($parent_sql) ? $parent_sql : " AND p.parent IS null ", |
|
41
|
|
|
// Add Types SQL |
|
42
|
|
|
!is_null($type_sql) ? $type_sql : "" |
|
43
|
|
|
)); |
|
44
|
|
|
|
|
45
|
|
|
if ((!is_array($pages) || empty($pages))) { |
|
46
|
|
|
if ($this->dsParamREDIRECTONEMPTY === 'yes') { |
|
|
|
|
|
|
47
|
|
|
throw new FrontendPageNotFoundException; |
|
48
|
|
|
} |
|
49
|
|
|
$result->appendChild($this->__noRecordsFound()); |
|
50
|
|
|
} else { |
|
51
|
|
|
// Build an array of all the types so that the page's don't have to do |
|
52
|
|
|
// individual lookups. |
|
53
|
|
|
$page_types = PageManager::fetchAllPagesPageTypes(); |
|
54
|
|
|
|
|
55
|
|
|
foreach ($pages as $page) { |
|
56
|
|
|
$result->appendChild($this->__buildPageXML($page, $page_types)); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $result; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function __processNavigationTypeFilter($filter, $filter_type = Datasource::FILTER_OR) |
|
64
|
|
|
{ |
|
65
|
|
|
$types = preg_split('/' . ($filter_type === Datasource::FILTER_AND ? '\+' : '(?<!\\\\),') . '\s*/', $filter, |
|
66
|
|
|
-1, PREG_SPLIT_NO_EMPTY); |
|
67
|
|
|
$types = array_map('trim', $types); |
|
68
|
|
|
|
|
69
|
|
|
$types = array_map(array('Datasource', 'removeEscapedCommas'), $types); |
|
70
|
|
|
|
|
71
|
|
|
if ($filter_type === Datasource::FILTER_OR) { |
|
72
|
|
|
$type_sql = " AND pt.type IN ('" . implode("', '", $types) . "')"; |
|
73
|
|
|
} else { |
|
74
|
|
|
foreach ($types as $type) { |
|
75
|
|
|
$type_sql = " AND pt.type = '" . $type . "'"; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $type_sql; |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function __processNavigationParentFilter($parent) |
|
83
|
|
|
{ |
|
84
|
|
|
$parent_paths = preg_split('/,\s*/', $parent, -1, PREG_SPLIT_NO_EMPTY); |
|
85
|
|
|
$parent_paths = array_map(function ($a) { |
|
86
|
|
|
return trim($a, ' /'); |
|
87
|
|
|
}, $parent_paths); |
|
88
|
|
|
|
|
89
|
|
|
return (is_array($parent_paths) && !empty($parent_paths) ? " AND p.`path` IN ('" . implode("', '", |
|
90
|
|
|
$parent_paths) . "')" : null); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function __buildPageXML($page, $page_types) |
|
94
|
|
|
{ |
|
95
|
|
|
$oPage = new XMLElement('page'); |
|
96
|
|
|
$oPage->setAttribute('handle', $page['handle']); |
|
97
|
|
|
$oPage->setAttribute('id', $page['id']); |
|
98
|
|
|
$oPage->appendChild(new XMLElement('name', General::sanitize($page['title']))); |
|
99
|
|
|
|
|
100
|
|
|
if (in_array($page['id'], array_keys($page_types))) { |
|
101
|
|
|
$xTypes = new XMLElement('types'); |
|
102
|
|
|
|
|
103
|
|
|
foreach ($page_types[$page['id']] as $type) { |
|
104
|
|
|
$xTypes->appendChild(new XMLElement('type', $type)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$oPage->appendChild($xTypes); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if ($page['children'] !== '0') { |
|
111
|
|
|
if ($children = PageManager::fetch(false, array('id, handle, title'), |
|
112
|
|
|
array(sprintf('`parent` = %d', $page['id']))) |
|
113
|
|
|
) { |
|
114
|
|
|
foreach ($children as $c) { |
|
115
|
|
|
$oPage->appendChild($this->__buildPageXML($c, $page_types)); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $oPage; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: