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