1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @package toolkit |
||
5 | */ |
||
6 | /** |
||
7 | * The `ResourcesPage` abstract class controls the way "Datasource" |
||
8 | * and "Events" index pages are displayed in the backend. It extends the |
||
9 | * `AdministrationPage` class. |
||
10 | * |
||
11 | * @since Symphony 2.3 |
||
12 | * @see toolkit.AdministrationPage |
||
13 | */ |
||
14 | |||
15 | abstract class ResourcesPage extends AdministrationPage |
||
16 | { |
||
17 | /** |
||
18 | * This method is invoked from the `Sortable` class and it contains the |
||
19 | * logic for sorting (or unsorting) the resource index. It provides a basic |
||
20 | * wrapper to the `ResourceManager`'s `fetch()` method. |
||
21 | * |
||
22 | * @see toolkit.ResourceManager#getSortingField |
||
23 | * @see toolkit.ResourceManager#getSortingOrder |
||
24 | * @see toolkit.ResourceManager#fetch |
||
25 | * @param string $sort |
||
26 | * The field to sort on which should match one of the table's column names. |
||
27 | * If this is not provided the default will be determined by |
||
28 | * `ResourceManager::getSortingField` |
||
29 | * @param string $order |
||
30 | * The direction to sort in, either 'asc' or 'desc'. If this is not provided |
||
31 | * the value will be determined by `ResourceManager::getSortingOrder`. |
||
32 | * @param array $params |
||
33 | * An associative array of params (usually populated from the URL) that this |
||
34 | * function uses. The current implementation will use `type` and `unsort` keys |
||
35 | * @throws Exception |
||
36 | * @throws SymphonyErrorPage |
||
37 | * @return array |
||
38 | * An associative of the resource as determined by `ResourceManager::fetch` |
||
39 | */ |
||
40 | public function sort(&$sort, &$order, array $params) |
||
41 | { |
||
42 | $type = $params['type']; |
||
43 | |||
44 | if (!is_null($sort)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
45 | General::sanitize($sort); |
||
46 | } |
||
47 | |||
48 | // If `?unsort` is appended to the URL, then sorting information are reverted |
||
49 | // to their defaults |
||
50 | if (isset($params['unsort'])) { |
||
51 | ResourceManager::setSortingField($type, 'name', false); |
||
52 | ResourceManager::setSortingOrder($type, 'asc'); |
||
53 | |||
54 | redirect(Administration::instance()->getCurrentPageURL()); |
||
55 | } |
||
56 | |||
57 | // By default, sorting information are retrieved from |
||
58 | // the filesystem and stored inside the `Configuration` object |
||
59 | if (is_null($sort) && is_null($order)) { |
||
0 ignored issues
–
show
|
|||
60 | $sort = ResourceManager::getSortingField($type); |
||
61 | $order = ResourceManager::getSortingOrder($type); |
||
62 | |||
63 | // If the sorting field or order differs from what is saved, |
||
64 | // update the config file and reload the page |
||
65 | } elseif ($sort !== ResourceManager::getSortingField($type) || $order !== ResourceManager::getSortingOrder($type)) { |
||
66 | ResourceManager::setSortingField($type, $sort, false); |
||
67 | ResourceManager::setSortingOrder($type, $order); |
||
68 | |||
69 | redirect(Administration::instance()->getCurrentPageURL()); |
||
70 | } |
||
71 | |||
72 | return ResourceManager::fetch($params['type'], array(), array(), $sort . ' ' . $order); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * This function creates an array of all page titles in the system. |
||
77 | * |
||
78 | * @return array |
||
79 | * An array of page titles |
||
80 | */ |
||
81 | public function pagesFlatView() |
||
82 | { |
||
83 | $pages = PageManager::fetch(false, array('id')); |
||
84 | |||
85 | foreach ($pages as &$p) { |
||
86 | $p['title'] = PageManager::resolvePageTitle($p['id']); |
||
87 | } |
||
88 | |||
89 | return $pages; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * This function contains the minimal amount of logic for generating the |
||
94 | * index table of a given `$resource_type`. The table has name, source, pages |
||
95 | * release date and author columns. The values for these columns are determined |
||
96 | * by the resource's `about()` method. |
||
97 | * |
||
98 | * As Datasources types can be installed using Providers, the Source column |
||
99 | * can be overridden with a Datasource's `getSourceColumn` method (if it exists). |
||
100 | * |
||
101 | * @param integer $resource_type |
||
102 | * Either `ResourceManager::RESOURCE_TYPE_EVENT` or `ResourceManager::RESOURCE_TYPE_DATASOURCE` |
||
103 | * @throws InvalidArgumentException |
||
104 | */ |
||
105 | public function __viewIndex($resource_type) |
||
106 | { |
||
107 | $manager = ResourceManager::getManagerFromType($resource_type); |
||
108 | $friendly_resource = ($resource_type === ResourceManager::RESOURCE_TYPE_EVENT) ? __('Event') : __('DataSource'); |
||
109 | |||
110 | $this->setPageType('table'); |
||
111 | |||
112 | Sortable::initialize($this, $resources, $sort, $order, array( |
||
113 | 'type' => $resource_type, |
||
114 | )); |
||
115 | |||
116 | $columns = array( |
||
117 | array( |
||
118 | 'label' => __('Name'), |
||
119 | 'sortable' => true, |
||
120 | 'handle' => 'name' |
||
121 | ), |
||
122 | array( |
||
123 | 'label' => __('Source'), |
||
124 | 'sortable' => true, |
||
125 | 'handle' => 'source' |
||
126 | ), |
||
127 | array( |
||
128 | 'label' => __('Pages'), |
||
129 | 'sortable' => false, |
||
130 | ), |
||
131 | array( |
||
132 | 'label' => __('Author'), |
||
133 | 'sortable' => true, |
||
134 | 'handle' => 'author' |
||
135 | ) |
||
136 | ); |
||
137 | |||
138 | $aTableHead = Sortable::buildTableHeaders($columns, $sort, $order, (isset($_REQUEST['filter']) ? '&filter=' . $_REQUEST['filter'] : '')); |
||
139 | |||
140 | $aTableBody = array(); |
||
141 | |||
142 | if (!is_array($resources) || empty($resources)) { |
||
143 | $aTableBody = array(Widget::TableRow(array(Widget::TableData(__('None found.'), 'inactive', null, count($aTableHead))), 'odd')); |
||
144 | } else { |
||
145 | $context = Administration::instance()->getPageCallback(); |
||
146 | |||
147 | foreach ($resources as $r) { |
||
148 | $action = 'edit'; |
||
149 | $status = null; |
||
150 | $locked = null; |
||
151 | |||
152 | // Locked resources |
||
153 | if ( |
||
0 ignored issues
–
show
|
|||
154 | isset($r['can_parse']) && $r['can_parse'] !== true || |
||
0 ignored issues
–
show
|
|||
155 | ($resource_type === ResourceManager::RESOURCE_TYPE_DS && $r['source']['name'] === 'Dynamic_xml') |
||
156 | ) { |
||
157 | $action = 'info'; |
||
158 | $status = 'status-notice'; |
||
159 | $locked = array( |
||
160 | 'data-status' => ' — ' . __('read only') |
||
161 | ); |
||
162 | } |
||
163 | |||
164 | $name = Widget::TableData( |
||
165 | Widget::Anchor( |
||
166 | stripslashes($r['name']), |
||
167 | SYMPHONY_URL . $context['pageroot'] . $action . '/' . $r['handle'] . '/', |
||
0 ignored issues
–
show
|
|||
168 | $r['handle'], |
||
169 | 'resource-' . $action, |
||
170 | null, |
||
171 | $locked |
||
172 | ) |
||
173 | ); |
||
174 | |||
175 | $name->appendChild(Widget::Label(__('Select ' . $friendly_resource . ' %s', array($r['name'])), null, 'accessible', null, array( |
||
176 | 'for' => 'resource-' . $r['handle'] |
||
177 | ))); |
||
178 | $name->appendChild(Widget::Input('items['.$r['handle'].']', 'on', 'checkbox', array( |
||
179 | 'id' => 'resource-' . $r['handle'] |
||
180 | ))); |
||
181 | |||
182 | // Resource type/source |
||
183 | if (isset($r['source'], $r['source']['id'])) { |
||
184 | $section = Widget::TableData( |
||
185 | Widget::Anchor( |
||
186 | $r['source']['name'], |
||
187 | SYMPHONY_URL . '/blueprints/sections/edit/' . $r['source']['id'] . '/', |
||
188 | $r['source']['handle'] |
||
189 | ) |
||
190 | ); |
||
191 | } elseif (isset($r['source']) && class_exists($r['source']['name']) && method_exists($r['source']['name'], 'getSourceColumn')) { |
||
192 | $class = call_user_func(array($manager, '__getClassName'), $r['handle']); |
||
193 | $section = Widget::TableData(call_user_func(array($class, 'getSourceColumn'), $r['handle'])); |
||
194 | } elseif (isset($r['source'], $r['source']['name'])) { |
||
195 | $section = Widget::TableData(stripslashes($r['source']['name'])); |
||
196 | } else { |
||
197 | $section = Widget::TableData(__('Unknown'), 'inactive'); |
||
198 | } |
||
199 | |||
200 | // Attached pages |
||
201 | $pages = ResourceManager::getAttachedPages($resource_type, $r['handle']); |
||
202 | |||
203 | $pagelinks = array(); |
||
204 | $i = 0; |
||
205 | |||
206 | foreach ($pages as $p) { |
||
207 | ++$i; |
||
208 | $pagelinks[] = Widget::Anchor( |
||
209 | General::sanitize($p['title']), |
||
210 | SYMPHONY_URL . '/blueprints/pages/edit/' . $p['id'] . '/' |
||
211 | )->generate() . (count($pages) > $i ? (($i % 10) == 0 ? '<br />' : ', ') : ''); |
||
0 ignored issues
–
show
|
|||
212 | } |
||
213 | |||
214 | $pages = implode('', $pagelinks); |
||
215 | |||
216 | if ($pages == '') { |
||
217 | $pagelinks = Widget::TableData(__('None'), 'inactive'); |
||
218 | } else { |
||
219 | $pagelinks = Widget::TableData($pages, 'pages'); |
||
220 | } |
||
221 | |||
222 | // Authors |
||
223 | $author = $r['author']['name']; |
||
224 | |||
225 | if ($author) { |
||
226 | if (isset($r['author']['website'])) { |
||
227 | $author = Widget::Anchor($r['author']['name'], General::validateURL($r['author']['website'])); |
||
228 | } elseif (isset($r['author']['email'])) { |
||
229 | $author = Widget::Anchor($r['author']['name'], 'mailto:' . $r['author']['email']); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | $author = Widget::TableData($author); |
||
234 | |||
235 | $aTableBody[] = Widget::TableRow(array($name, $section, $pagelinks, $author), $status); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | $table = Widget::Table( |
||
240 | Widget::TableHead($aTableHead), |
||
241 | null, |
||
242 | Widget::TableBody($aTableBody), |
||
243 | 'selectable', |
||
244 | null, |
||
245 | array('role' => 'directory', 'aria-labelledby' => 'symphony-subheading', 'data-interactive' => 'data-interactive') |
||
246 | ); |
||
247 | |||
248 | $this->Form->appendChild($table); |
||
249 | |||
250 | $version = new XMLElement('p', 'Symphony ' . Symphony::Configuration()->get('version', 'symphony'), array( |
||
251 | 'id' => 'version' |
||
252 | )); |
||
253 | $this->Form->appendChild($version); |
||
254 | |||
255 | $tableActions = new XMLElement('div'); |
||
256 | $tableActions->setAttribute('class', 'actions'); |
||
257 | |||
258 | $options = array( |
||
259 | array(null, false, __('With Selected...')), |
||
260 | array('delete', false, __('Delete'), 'confirm'), |
||
261 | ); |
||
262 | |||
263 | $pages = $this->pagesFlatView(); |
||
264 | |||
265 | $group_attach = array('label' => __('Attach to Page'), 'options' => array()); |
||
266 | $group_detach = array('label' => __('Detach from Page'), 'options' => array()); |
||
267 | |||
268 | $group_attach['options'][] = array('attach-all-pages', false, __('All')); |
||
269 | $group_detach['options'][] = array('detach-all-pages', false, __('All')); |
||
270 | |||
271 | foreach ($pages as $p) { |
||
272 | $group_attach['options'][] = array('attach-to-page-' . $p['id'], false, General::sanitize($p['title'])); |
||
273 | $group_detach['options'][] = array('detach-from-page-' . $p['id'], false, General::sanitize($p['title'])); |
||
274 | } |
||
275 | |||
276 | $options[] = $group_attach; |
||
277 | $options[] = $group_detach; |
||
278 | |||
279 | /** |
||
280 | * Allows an extension to modify the existing options for this page's |
||
281 | * With Selected menu. If the `$options` parameter is an empty array, |
||
282 | * the 'With Selected' menu will not be rendered. |
||
283 | * |
||
284 | * @delegate AddCustomActions |
||
285 | * @since Symphony 2.3.2 |
||
286 | * @param string $context |
||
287 | * '/blueprints/datasources/' or '/blueprints/events/' |
||
288 | * @param array $options |
||
289 | * An array of arrays, where each child array represents an option |
||
290 | * in the With Selected menu. Options should follow the same format |
||
291 | * expected by `Widget::__SelectBuildOption`. Passed by reference. |
||
292 | */ |
||
293 | Symphony::ExtensionManager()->notifyMembers('AddCustomActions', $context['pageroot'], array( |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
294 | 'options' => &$options |
||
295 | )); |
||
296 | |||
297 | if (!empty($options)) { |
||
298 | $tableActions->appendChild(Widget::Apply($options)); |
||
299 | $this->Form->appendChild($tableActions); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * This function is called from the resources index when a user uses the |
||
305 | * With Selected, or Apply, menu. The type of resource is given by |
||
306 | * `$resource_type`. At this time the only two valid values, |
||
307 | * `ResourceManager::RESOURCE_TYPE_EVENT` or `ResourceManager::RESOURCE_TYPE_DATASOURCE`. |
||
308 | * |
||
309 | * The function handles 'delete', 'attach', 'detach', 'attach all', |
||
310 | * 'detach all' actions. |
||
311 | * |
||
312 | * @param integer $resource_type |
||
313 | * Either `ResourceManager::RESOURCE_TYPE_EVENT` or `ResourceManager::RESOURCE_TYPE_DATASOURCE` |
||
314 | * @throws Exception |
||
315 | */ |
||
316 | public function __actionIndex($resource_type) |
||
317 | { |
||
318 | $manager = ResourceManager::getManagerFromType($resource_type); |
||
319 | $checked = (is_array($_POST['items'])) ? array_keys($_POST['items']) : null; |
||
320 | $context = Administration::instance()->getPageCallback(); |
||
321 | |||
322 | if (isset($_POST['action']) && is_array($_POST['action'])) { |
||
323 | /** |
||
324 | * Extensions can listen for any custom actions that were added |
||
325 | * through `AddCustomPreferenceFieldsets` or `AddCustomActions` |
||
326 | * delegates. |
||
327 | * |
||
328 | * @delegate CustomActions |
||
329 | * @since Symphony 2.3.2 |
||
330 | * @param string $context |
||
331 | * '/blueprints/datasources/' or '/blueprints/events/' |
||
332 | * @param array $checked |
||
333 | * An array of the selected rows. The value is usually the ID of the |
||
334 | * the associated object. |
||
335 | */ |
||
336 | Symphony::ExtensionManager()->notifyMembers('CustomActions', $context['pageroot'], array( |
||
337 | 'checked' => $checked |
||
338 | )); |
||
339 | |||
340 | if (is_array($checked) && !empty($checked)) { |
||
341 | if ($_POST['with-selected'] == 'delete') { |
||
342 | $canProceed = true; |
||
343 | |||
344 | foreach ($checked as $handle) { |
||
345 | $path = call_user_func(array($manager, '__getDriverPath'), $handle); |
||
346 | |||
347 | // Don't allow Extension resources to be deleted. RE: #2027 |
||
348 | if (stripos($path, EXTENSIONS) === 0) { |
||
0 ignored issues
–
show
|
|||
349 | continue; |
||
350 | } elseif (!General::deleteFile($path)) { |
||
351 | $folder = str_replace(DOCROOT, '', $path); |
||
352 | $folder = str_replace('/' . basename($path), '', $folder); |
||
353 | |||
354 | $this->pageAlert( |
||
355 | __('Failed to delete %s.', array('<code>' . basename($path) . '</code>')) |
||
356 | . ' ' . __('Please check permissions on %s', array('<code>' . $folder . '</code>')), |
||
357 | Alert::ERROR |
||
358 | ); |
||
359 | $canProceed = false; |
||
360 | } else { |
||
361 | $pages = ResourceManager::getAttachedPages($resource_type, $handle); |
||
362 | |||
363 | foreach ($pages as $page) { |
||
364 | ResourceManager::detach($resource_type, $handle, $page['id']); |
||
365 | } |
||
366 | } |
||
367 | } |
||
368 | |||
369 | if ($canProceed) { |
||
370 | redirect(Administration::instance()->getCurrentPageURL()); |
||
371 | } |
||
372 | } elseif (preg_match('/^(at|de)?tach-(to|from)-page-/', $_POST['with-selected'])) { |
||
373 | if (substr($_POST['with-selected'], 0, 6) == 'detach') { |
||
374 | $page = str_replace('detach-from-page-', '', $_POST['with-selected']); |
||
375 | |||
376 | foreach ($checked as $handle) { |
||
377 | ResourceManager::detach($resource_type, $handle, $page); |
||
378 | } |
||
379 | } else { |
||
380 | $page = str_replace('attach-to-page-', '', $_POST['with-selected']); |
||
381 | |||
382 | foreach ($checked as $handle) { |
||
383 | ResourceManager::attach($resource_type, $handle, $page); |
||
384 | } |
||
385 | } |
||
386 | |||
387 | if ($canProceed) { |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
388 | redirect(Administration::instance()->getCurrentPageURL()); |
||
389 | } |
||
390 | } elseif (preg_match('/^(at|de)?tach-all-pages$/', $_POST['with-selected'])) { |
||
391 | $pages = PageManager::fetch(false, array('id')); |
||
392 | |||
393 | if (substr($_POST['with-selected'], 0, 6) == 'detach') { |
||
394 | foreach ($checked as $handle) { |
||
395 | foreach ($pages as $page) { |
||
396 | ResourceManager::detach($resource_type, $handle, $page['id']); |
||
397 | } |
||
398 | } |
||
399 | } else { |
||
400 | foreach ($checked as $handle) { |
||
401 | foreach ($pages as $page) { |
||
402 | ResourceManager::attach($resource_type, $handle, $page['id']); |
||
403 | } |
||
404 | } |
||
405 | } |
||
406 | |||
407 | redirect(Administration::instance()->getCurrentPageURL()); |
||
408 | } |
||
409 | } |
||
410 | } |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Returns the path to the component-template by looking at the |
||
415 | * `WORKSPACE/template/` directory, then at the `TEMPLATES` |
||
416 | * directory for the convention `*.tpl`. If the template |
||
417 | * is not found, false is returned |
||
418 | * |
||
419 | * @param string $name |
||
420 | * Name of the template |
||
421 | * @return mixed |
||
422 | * String, which is the path to the template if the template is found, |
||
423 | * false otherwise |
||
424 | */ |
||
425 | protected function getTemplate($name) |
||
426 | { |
||
427 | $format = '%s/%s.tpl'; |
||
428 | |||
429 | if (file_exists($template = sprintf($format, WORKSPACE . '/template', $name))) { |
||
0 ignored issues
–
show
|
|||
430 | return $template; |
||
431 | } elseif (file_exists($template = sprintf($format, TEMPLATE, $name))) { |
||
0 ignored issues
–
show
|
|||
432 | return $template; |
||
433 | } else { |
||
434 | return false; |
||
435 | } |
||
436 | } |
||
437 | } |
||
438 |