/
class.resourcemanager.php
455 lines (390 loc) · 14.9 KB
/
class.resourcemanager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
/**
* @package toolkit
*/
/**
* The ResourcesManager is a class used to collect some methods for both
* Datasources and Events.
*
* @since Symphony 2.3
*/
class ResourceManager
{
/**
* The integer value for event-type resources.
* @var integer
*/
const RESOURCE_TYPE_EVENT = 20;
/**
* The integer value for datasource-type resources.
* @var integer
*/
const RESOURCE_TYPE_DS = 21;
/**
* A private method used to return the `tbl_pages` column related to
* the given resource type.
*
* @param integer $type
* The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS`
* @return string
* A string representing the `tbl_pages` column to target.
*/
private static function getColumnFromType($type)
{
switch($type) {
case ResourceManager::RESOURCE_TYPE_EVENT:
return 'events';
case ResourceManager::RESOURCE_TYPE_DS:
return 'data_sources';
}
}
/**
* A method used to return the Manager for the given resource type.
*
* @param integer $type
* The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS`
* @return string
* An string representing the name of the Manager class that handles the resource.
*/
public static function getManagerFromType($type)
{
switch($type) {
case ResourceManager::RESOURCE_TYPE_EVENT:
return 'EventManager';
case ResourceManager::RESOURCE_TYPE_DS:
return 'DatasourceManager';
}
}
/**
* Returns the axis a given resource type will be sorted by.
* The following handles are available: `name`, `source`, `release-date`
* and `author`. Defaults to 'name'.
*
* @param integer $type
* The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS`
* @return string
* The axis handle.
*/
public static function getSortingField($type)
{
$result = Symphony::Configuration()->get(self::getColumnFromType($type) . '_index_sortby', 'sorting');
return (is_null($result) ? 'name' : $result);
}
/**
* Returns the sort order for a given resource type. Defaults to 'asc'.
*
* @param integer $type
* The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS`
* @return string
* Either 'asc' or 'desc'.
*/
public static function getSortingOrder($type)
{
$result = Symphony::Configuration()->get(self::getColumnFromType($type) . '_index_order', 'sorting');
return (is_null($result) ? 'asc' : $result);
}
/**
* Saves the new axis a given resource type will be sorted by.
*
* @param integer $type
* The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS`
* @param string $sort
* The axis handle.
* @param boolean $write
* If false, the new settings won't be written on the configuration file.
* Defaults to true.
*/
public static function setSortingField($type, $sort, $write = true)
{
Symphony::Configuration()->set(self::getColumnFromType($type) . '_index_sortby', $sort , 'sorting');
if ($write) {
Symphony::Configuration()->write();
}
}
/**
* Saves the new sort order for a given resource type.
*
* @param integer $type
* The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS`
* @param string $order
* Either 'asc' or 'desc'.
* @param boolean $write
* If false, the new settings won't be written on the configuration file.
* Defaults to true.
*/
public static function setSortingOrder($type, $order, $write = true)
{
Symphony::Configuration()->set(self::getColumnFromType($type) . '_index_order', $order, 'sorting');
if ($write) {
Symphony::Configuration()->write();
}
}
/**
* This function will return an associative array of resource information. The
* information returned is defined by the `$select` parameter, which will allow
* a developer to restrict what information is returned about the resource.
* Optionally, `$where` (not implemented) and `$order_by` parameters allow a developer to
* further refine their query.
*
* @param integer $type
* The type of the resource (needed to retrieve the correct Manager)
* @param array $select (optional)
* Accepts an array of keys to return from the manager's `listAll()` method. If omitted,
* all keys will be returned.
* @param array $where (optional)
* Not implemented.
* @param string $order_by (optional)
* Allows a developer to return the resources in a particular order. The syntax is the
* same as other `fetch` methods. If omitted this will return resources ordered by `name`.
* @throws SymphonyErrorPage
* @throws Exception
* @return array
* An associative array of resource information, formatted in the same way as the resource's
* manager `listAll()` method.
*/
public static function fetch($type, array $select = array(), array $where = array(), $order_by = null)
{
$manager = self::getManagerFromType($type);
if (!isset($manager)) {
throw new Exception(__('Unable to find a Manager class for this resource.'));
}
$resources = call_user_func(array($manager, 'listAll'));
foreach ($resources as &$r) {
// If source is numeric, it's considered to be a Symphony Section
if (isset($r['source']) && $r['source'] > 0) {
$section = SectionManager::fetch($r['source']);
if ($section !== false) {
$r['source'] = array(
'name' => $section->get('name'),
'handle' => $section->get('handle'),
'id' => $r['source']
);
} else {
unset($r['source']);
}
// If source is set but no numeric, it's considered to be a Symphony Type (e.g. authors or navigation)
} elseif (isset($r['source'])) {
$r['source'] = array(
'name' => ucwords($r['source']),
'handle' => $r['source']
);
// Resource provided by extension?
} else {
$extension = self::__getExtensionFromHandle($type, $r['handle']);
if (!empty($extension)) {
$extension = Symphony::ExtensionManager()->about($extension);
$r['source'] = array(
'name' => $extension['name'],
'handle' => Lang::createHandle($extension['name'])
);
}
}
}
if (empty($select) && empty($where) && is_null($order_by)) {
return $resources;
}
if (!is_null($order_by) && !empty($resources)) {
$author = $label = $source = $name = array();
$order_by = array_map('strtolower', explode(' ', $order_by));
$order = ($order_by[1] == 'desc') ? SORT_DESC : SORT_ASC;
$sort = $order_by[0];
if ($sort == 'author') {
foreach ($resources as $key => $about) {
$author[$key] = $about['author']['name'];
$label[$key] = $key;
}
array_multisort($author, $order, $label, SORT_ASC, $resources);
} elseif ($sort == 'release-date') {
foreach ($resources as $key => $about) {
$author[$key] = $about['release-date'];
$label[$key] = $key;
}
array_multisort($author, $order, $label, SORT_ASC, $resources);
} elseif ($sort == 'source') {
foreach ($resources as $key => $about) {
$source[$key] = $about['source']['handle'];
$label[$key] = $key;
}
array_multisort($source, $order, $label, SORT_ASC, $resources);
} elseif ($sort == 'name') {
foreach ($resources as $key => $about) {
$name[$key] = strtolower($about['name']);
$label[$key] = $key;
}
array_multisort($name, $order, $label, SORT_ASC, $resources);
}
}
$data = array();
foreach ($resources as $i => $res) {
$data[$i] = array();
foreach ($res as $key => $value) {
// If $select is empty, we assume every field is requested
if (in_array($key, $select) || empty($select)) {
$data[$i][$key] = $value;
}
}
}
return $data;
}
/**
* Given the type and handle of a resource, return the extension it belongs to.
*
* @param integer $type
* The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS`
* @param string $r_handle
* The handle of the resource.
* @throws Exception
* @return string
* The extension handle.
*/
public static function __getExtensionFromHandle($type, $r_handle)
{
$manager = self::getManagerFromType($type);
if (!isset($manager)) {
throw new Exception(__('Unable to find a Manager class for this resource.'));
}
$type = str_replace('_', '-', self::getColumnFromType($type));
preg_match('/extensions\/(.*)\/' . $type . '/', call_user_func(array($manager, '__getClassPath'), $r_handle), $data);
$data = array_splice($data, 1);
if (empty($data)) {
return null;
} else {
return $data[0];
}
}
/**
* Given the resource handle, this function will return an associative
* array of Page information, filtered by the pages the resource is attached to.
*
* @param integer $type
* The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS`
* @param string $r_handle
* The handle of the resource.
* @return array
* An associative array of Page information, according to the pages the resource is attached to.
*/
public static function getAttachedPages($type, $r_handle)
{
$col = self::getColumnFromType($type);
$pages = PageManager::fetch(false, array('id'), array(sprintf(
'`%s` = "%s" OR `%s` REGEXP "%s"',
$col,
$r_handle,
$col,
'^' . $r_handle . ',|,' . $r_handle . ',|,' . $r_handle . '$'
)));
if (is_array($pages)) {
foreach ($pages as $key => &$page) {
$pages[$key] = array(
'id' => $page['id'],
'title' => PageManager::resolvePageTitle($page['id'])
);
}
}
return (is_null($pages) ? array() : $pages);
}
/**
* Given a resource type, a handle and a page, this function will attach
* the given handle (which represents either a datasource or event) to that page.
*
* @param integer $type
* The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS`
* @param string $r_handle
* The handle of the resource.
* @param integer $page_id
* The ID of the page.
* @return boolean
*/
public static function attach($type, $r_handle, $page_id)
{
$col = self::getColumnFromType($type);
$pages = PageManager::fetch(false, array($col), array(sprintf(
'`id` = %d',
$page_id
)));
if (is_array($pages) && count($pages) == 1) {
$result = $pages[0][$col];
if (!in_array($r_handle, explode(',', $result))) {
if (strlen($result) > 0) {
$result .= ',';
}
$result .= $r_handle;
return PageManager::edit($page_id, array(
$col => MySQL::cleanValue($result)
));
}
}
return false;
}
/**
* Given a resource type, a handle and a page, this function detaches
* the given handle (which represents either a datasource or event) to that page.
*
* @param integer $type
* The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS`
* @param string $r_handle
* The handle of the resource.
* @param integer $page_id
* The ID of the page.
* @return boolean
*/
public static function detach($type, $r_handle, $page_id)
{
$col = self::getColumnFromType($type);
$pages = PageManager::fetch(false, array($col), array(sprintf(
'`id` = %d',
$page_id
)));
if (is_array($pages) && count($pages) == 1) {
$result = $pages[0][$col];
$values = explode(',', $result);
$idx = array_search($r_handle, $values, false);
if ($idx !== false) {
array_splice($values, $idx, 1);
$result = implode(',', $values);
return PageManager::edit($page_id, array(
$col => MySQL::cleanValue($result)
));
}
}
return false;
}
/**
* Given a resource type, a handle and an array of pages, this function will
* ensure that the resource is attached to the given pages. Note that this
* function will also remove the resource from all pages that are not provided
* in the `$pages` parameter.
*
* @since Symphony 2.4
* @param integer $type
* The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS`
* @param string $r_handle
* The handle of the resource.
* @param array $pages
* An array of Page ID's to attach this resource to.
* @return boolean
*/
public static function setPages($type, $r_handle, $pages = array())
{
if (!is_array($pages)) {
$pages = array();
}
// Get attached pages
$attached_pages = ResourceManager::getAttachedPages($type, $r_handle);
$currently_attached_pages = array();
foreach ($attached_pages as $page) {
$currently_attached_pages[] = $page['id'];
}
// Attach this datasource to any page that is should be attached to
$diff_to_attach = array_diff($pages, $currently_attached_pages);
foreach ($diff_to_attach as $diff_page) {
ResourceManager::attach($type, $r_handle, $diff_page);
}
// Remove this datasource from any page where it once was, but shouldn't be anymore
$diff_to_detach = array_diff($currently_attached_pages, $pages);
foreach ($diff_to_detach as $diff_page) {
ResourceManager::detach($type, $r_handle, $diff_page);
}
return true;
}
}