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