1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package data-sources |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The `AuthorDatasource` extends the base `Datasource` class and allows |
9
|
|
|
* the retrieval of Author information from the current Symphony installation. |
10
|
|
|
* |
11
|
|
|
* @since Symphony 2.3 |
12
|
|
|
*/ |
13
|
|
|
class AuthorDatasource extends Datasource |
14
|
|
|
{ |
15
|
|
|
public function execute(array &$param_pool = null) |
16
|
|
|
{ |
17
|
|
|
$author_ids = array(); |
18
|
|
|
|
19
|
|
|
if (is_array($this->dsParamFILTERS) && !empty($this->dsParamFILTERS)) { |
20
|
|
|
foreach ($this->dsParamFILTERS as $field => $value) { |
|
|
|
|
21
|
|
|
if (!is_array($value) && trim($value) === '') { |
22
|
|
|
continue; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$ret = $this->__processAuthorFilter($field, $value); |
26
|
|
|
|
27
|
|
|
if (empty($ret)) { |
28
|
|
|
$author_ids = array(); |
29
|
|
|
break; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (empty($author_ids)) { |
33
|
|
|
$author_ids = $ret; |
34
|
|
|
continue; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$author_ids = array_intersect($author_ids, $ret); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$authors = AuthorManager::fetchByID(array_values($author_ids)); |
41
|
|
|
} else { |
42
|
|
|
$authors = AuthorManager::fetch($this->dsParamSORT, $this->dsParamORDER); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ((!is_array($authors) || empty($authors)) && $this->dsParamREDIRECTONEMPTY === 'yes') { |
|
|
|
|
46
|
|
|
throw new FrontendPageNotFoundException; |
47
|
|
|
} elseif (!is_array($authors) || empty($authors)) { |
48
|
|
|
$result = $this->emptyXMLSet(); |
49
|
|
|
|
50
|
|
|
return $result; |
51
|
|
|
} else { |
52
|
|
|
if ($this->_negate_result === true) { |
53
|
|
|
return $this->negateXMLSet(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!$this->_param_output_only) { |
57
|
|
|
$result = new XMLElement($this->dsParamROOTELEMENT); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$singleParam = false; |
61
|
|
|
$key = 'ds-' . $this->dsParamROOTELEMENT; |
62
|
|
|
|
63
|
|
|
if (isset($this->dsParamPARAMOUTPUT)) { |
64
|
|
|
if (!is_array($this->dsParamPARAMOUTPUT)) { |
65
|
|
|
$this->dsParamPARAMOUTPUT = array($this->dsParamPARAMOUTPUT); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$singleParam = count($this->dsParamPARAMOUTPUT) === 1; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
foreach ($authors as $author) { |
72
|
|
|
if (isset($this->dsParamPARAMOUTPUT)) { |
73
|
|
|
foreach ($this->dsParamPARAMOUTPUT as $param) { |
74
|
|
|
// The new style of paramater is `ds-datasource-handle.field-handle` |
75
|
|
|
$param_key = $key . '.' . str_replace(':', '-', $param); |
76
|
|
|
|
77
|
|
|
if (!is_array($param_pool[$param_key])) { |
78
|
|
|
$param_pool[$param_key] = array(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$param_pool[$param_key][] = ($param === 'name' ? $author->getFullName() : $author->get($param)); |
82
|
|
|
|
83
|
|
|
if ($singleParam) { |
84
|
|
|
if (!is_array($param_pool[$key])) { |
85
|
|
|
$param_pool[$key] = array(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$param_pool[$key][] = ($param === 'name' ? $author->getFullName() : $author->get($param)); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($this->_param_output_only) { |
94
|
|
|
continue; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$xAuthor = new XMLElement('author'); |
98
|
|
|
$xAuthor->setAttributeArray(array( |
99
|
|
|
'id' => $author->get('id'), |
100
|
|
|
'user-type' => $author->get('user_type'), |
101
|
|
|
'primary-account' => $author->get('primary') |
102
|
|
|
)); |
103
|
|
|
|
104
|
|
|
// No included elements, so just create the Author XML |
105
|
|
|
if (!isset($this->dsParamINCLUDEDELEMENTS) || !is_array($this->dsParamINCLUDEDELEMENTS) || empty($this->dsParamINCLUDEDELEMENTS)) { |
106
|
|
|
$result->appendChild($xAuthor); |
|
|
|
|
107
|
|
|
} else { |
108
|
|
|
// Name |
109
|
|
|
if (in_array('name', $this->dsParamINCLUDEDELEMENTS)) { |
|
|
|
|
110
|
|
|
$xAuthor->appendChild( |
111
|
|
|
new XMLElement('name', $author->getFullName()) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Username |
116
|
|
|
if (in_array('username', $this->dsParamINCLUDEDELEMENTS)) { |
117
|
|
|
$xAuthor->appendChild( |
118
|
|
|
new XMLElement('username', $author->get('username')) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Email |
123
|
|
|
if (in_array('email', $this->dsParamINCLUDEDELEMENTS)) { |
124
|
|
|
$xAuthor->appendChild( |
125
|
|
|
new XMLElement('email', $author->get('email')) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Author Token |
130
|
|
|
if (in_array('author-token', $this->dsParamINCLUDEDELEMENTS) && $author->isTokenActive()) { |
131
|
|
|
$xAuthor->appendChild( |
132
|
|
|
new XMLElement('author-token', $author->createAuthToken()) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// Default Area |
137
|
|
|
if (in_array('default-area', |
138
|
|
|
$this->dsParamINCLUDEDELEMENTS) && !is_null($author->get('default_area')) |
139
|
|
|
) { |
140
|
|
|
// Section |
141
|
|
|
if ($section = SectionManager::fetch($author->get('default_area'))) { |
142
|
|
|
$default_area = new XMLElement('default-area', $section->get('name')); |
143
|
|
|
$default_area->setAttributeArray(array( |
144
|
|
|
'id' => $section->get('id'), |
145
|
|
|
'handle' => $section->get('handle'), |
146
|
|
|
'type' => 'section' |
147
|
|
|
)); |
148
|
|
|
$xAuthor->appendChild($default_area); |
149
|
|
|
|
150
|
|
|
// Pages |
151
|
|
|
} else { |
152
|
|
|
$default_area = new XMLElement('default-area', $author->get('default_area')); |
153
|
|
|
$default_area->setAttribute('type', 'page'); |
154
|
|
|
$xAuthor->appendChild($default_area); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$result->appendChild($xAuthor); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $result; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function __processAuthorFilter($field, $filter) |
167
|
|
|
{ |
168
|
|
|
if (!is_array($filter)) { |
169
|
|
|
$bits = preg_split('/,\s*/', $filter, -1, PREG_SPLIT_NO_EMPTY); |
170
|
|
|
$bits = array_map('trim', $bits); |
171
|
|
|
} else { |
172
|
|
|
$bits = $filter; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$q = Database::addPlaceholders($bits); |
176
|
|
|
$authors = Symphony::Database()->fetchCol('id', sprintf(" |
177
|
|
|
SELECT `id` FROM `tbl_authors` |
178
|
|
|
WHERE `%s` IN (%s)", |
179
|
|
|
$field, |
180
|
|
|
$q |
181
|
|
|
)); |
182
|
|
|
|
183
|
|
|
return (is_array($authors) && !empty($authors) ? $authors : null); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
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: