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