|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package toolkit |
|
5
|
|
|
*/ |
|
6
|
|
|
/** |
|
7
|
|
|
* The `SectionManager` is responsible for managing all Sections in a Symphony |
|
8
|
|
|
* installation by exposing basic CRUD operations. Sections are stored in the |
|
9
|
|
|
* database in `tbl_sections`. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
class SectionManager |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* An array of all the objects that the Manager is responsible for. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
* Defaults to an empty array. |
|
19
|
|
|
*/ |
|
20
|
|
|
protected static $_pool = array(); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Takes an associative array of Section settings and creates a new |
|
24
|
|
|
* entry in the `tbl_sections` table, returning the ID of the Section. |
|
25
|
|
|
* The ID of the section is generated using auto_increment and returned |
|
26
|
|
|
* as the Section ID. |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $settings |
|
29
|
|
|
* An associative of settings for a section with the key being |
|
30
|
|
|
* a column name from `tbl_sections` |
|
31
|
|
|
* @throws DatabaseException |
|
32
|
|
|
* @return integer |
|
33
|
|
|
* The newly created Section's ID |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function add(array $settings) |
|
36
|
|
|
{ |
|
37
|
|
|
$defaults = array(); |
|
38
|
|
|
$defaults['creation_date'] = $defaults['modification_date'] = DateTimeObj::get('Y-m-d H:i:s'); |
|
39
|
|
|
$defaults['creation_date_gmt'] = $defaults['modification_date_gmt'] = DateTimeObj::getGMT('Y-m-d H:i:s'); |
|
40
|
|
|
$defaults['author_id'] = 1; |
|
41
|
|
|
$defaults['modification_author_id'] = 1; |
|
42
|
|
|
$settings = array_replace($defaults, $settings); |
|
43
|
|
|
if (!Symphony::Database()->insert($settings, 'tbl_sections')) { |
|
44
|
|
|
return false; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return Symphony::Database()->getInsertID(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Updates an existing Section given it's ID and an associative |
|
52
|
|
|
* array of settings. The array does not have to contain all the |
|
53
|
|
|
* settings for the Section as there is no deletion of settings |
|
54
|
|
|
* prior to updating the Section |
|
55
|
|
|
* |
|
56
|
|
|
* @param integer $section_id |
|
57
|
|
|
* The ID of the Section to edit |
|
58
|
|
|
* @param array $settings |
|
59
|
|
|
* An associative of settings for a section with the key being |
|
60
|
|
|
* a column name from `tbl_sections` |
|
61
|
|
|
* @throws DatabaseException |
|
62
|
|
|
* @return boolean |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function edit($section_id, array $settings) |
|
65
|
|
|
{ |
|
66
|
|
|
$defaults = array(); |
|
67
|
|
|
$defaults['modification_date'] = DateTimeObj::get('Y-m-d H:i:s'); |
|
68
|
|
|
$defaults['modification_date_gmt'] = DateTimeObj::getGMT('Y-m-d H:i:s'); |
|
69
|
|
|
$defaults['author_id'] = 1; |
|
70
|
|
|
$defaults['modification_author_id'] = 1; |
|
71
|
|
|
$settings = array_replace($defaults, $settings); |
|
72
|
|
|
if (!Symphony::Database()->update($settings, 'tbl_sections', sprintf(" `id` = %d", $section_id))) { |
|
|
|
|
|
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Deletes a Section by Section ID, removing all entries, fields, the |
|
81
|
|
|
* Section and any Section Associations in that order |
|
82
|
|
|
* |
|
83
|
|
|
* @param integer $section_id |
|
84
|
|
|
* The ID of the Section to delete |
|
85
|
|
|
* @throws DatabaseException |
|
86
|
|
|
* @throws Exception |
|
87
|
|
|
* @return boolean |
|
88
|
|
|
* Returns true when completed |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function delete($section_id) |
|
91
|
|
|
{ |
|
92
|
|
|
$details = Symphony::Database()->fetchRow(0, sprintf(" |
|
|
|
|
|
|
93
|
|
|
SELECT `sortorder` FROM tbl_sections WHERE `id` = %d", |
|
94
|
|
|
$section_id |
|
95
|
|
|
)); |
|
96
|
|
|
|
|
97
|
|
|
// Delete all the entries |
|
98
|
|
|
$entries = Symphony::Database()->fetchCol('id', "SELECT `id` FROM `tbl_entries` WHERE `section_id` = '$section_id'"); |
|
|
|
|
|
|
99
|
|
|
EntryManager::delete($entries); |
|
100
|
|
|
|
|
101
|
|
|
// Delete all the fields |
|
102
|
|
|
$fields = FieldManager::fetch(null, $section_id); |
|
103
|
|
|
|
|
104
|
|
|
if (is_array($fields) && !empty($fields)) { |
|
105
|
|
|
foreach ($fields as $field) { |
|
106
|
|
|
FieldManager::delete($field->get('id')); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// Delete the section |
|
111
|
|
|
Symphony::Database()->delete('tbl_sections', sprintf(" |
|
|
|
|
|
|
112
|
|
|
`id` = %d", $section_id |
|
113
|
|
|
)); |
|
114
|
|
|
|
|
115
|
|
|
// Update the sort orders |
|
116
|
|
|
Symphony::Database()->query(sprintf(" |
|
|
|
|
|
|
117
|
|
|
UPDATE tbl_sections |
|
118
|
|
|
SET `sortorder` = (`sortorder` - 1) |
|
119
|
|
|
WHERE `sortorder` > %d", |
|
120
|
|
|
$details['sortorder'] |
|
121
|
|
|
)); |
|
122
|
|
|
|
|
123
|
|
|
// Delete the section associations |
|
124
|
|
|
Symphony::Database()->delete('tbl_sections_association', sprintf(" |
|
|
|
|
|
|
125
|
|
|
`parent_section_id` = %d", $section_id |
|
126
|
|
|
)); |
|
127
|
|
|
|
|
128
|
|
|
return true; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Returns a Section object by ID, or returns an array of Sections |
|
133
|
|
|
* if the Section ID was omitted. If the Section ID is omitted, it is |
|
134
|
|
|
* possible to sort the Sections by providing a sort order and sort |
|
135
|
|
|
* field. By default, Sections will be order in ascending order by |
|
136
|
|
|
* their name |
|
137
|
|
|
* |
|
138
|
|
|
* @param integer|array $section_id |
|
139
|
|
|
* The ID of the section to return, or an array of ID's. Defaults to null |
|
140
|
|
|
* @param string $order |
|
141
|
|
|
* If `$section_id` is omitted, this is the sortorder of the returned |
|
142
|
|
|
* objects. Defaults to ASC, other options id DESC |
|
143
|
|
|
* @param string $sortfield |
|
144
|
|
|
* The name of the column in the `tbl_sections` table to sort |
|
145
|
|
|
* on. Defaults to name |
|
146
|
|
|
* @throws DatabaseException |
|
147
|
|
|
* @return Section|array |
|
148
|
|
|
* A Section object or an array of Section objects |
|
149
|
|
|
*/ |
|
150
|
|
|
public static function fetch($section_id = null, $order = 'ASC', $sortfield = 'name') |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
$returnSingle = false; |
|
153
|
|
|
$section_ids = array(); |
|
154
|
|
|
|
|
155
|
|
|
if (!is_null($section_id)) { |
|
156
|
|
|
if (!is_array($section_id)) { |
|
157
|
|
|
$returnSingle = true; |
|
158
|
|
|
$section_ids = array($section_id); |
|
159
|
|
|
} else { |
|
160
|
|
|
$section_ids = $section_id; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
if ($returnSingle && isset(self::$_pool[$section_id])) { |
|
165
|
|
|
return self::$_pool[$section_id]; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
// Ensure they are always an ID |
|
169
|
|
|
$section_ids = array_map('intval', $section_ids); |
|
170
|
|
|
$sql = sprintf( |
|
171
|
|
|
"SELECT `s`.* |
|
|
|
|
|
|
172
|
|
|
FROM `tbl_sections` AS `s` |
|
173
|
|
|
%s |
|
174
|
|
|
%s", |
|
175
|
|
|
!empty($section_id) ? " WHERE `s`.`id` IN (" . implode(',', $section_ids) . ") " : "", |
|
|
|
|
|
|
176
|
|
|
empty($section_id) ? " ORDER BY `s`.`$sortfield` $order" : "" |
|
|
|
|
|
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
|
|
if (!$sections = Symphony::Database()->fetch($sql)) { |
|
180
|
|
|
return ($returnSingle ? false : array()); |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$ret = array(); |
|
184
|
|
|
|
|
185
|
|
|
foreach ($sections as $s) { |
|
186
|
|
|
$obj = self::create(); |
|
187
|
|
|
|
|
188
|
|
|
foreach ($s as $name => $value) { |
|
189
|
|
|
$obj->set($name, $value); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$obj->set('creation_date', DateTimeObj::get('c', $obj->get('creation_date'))); |
|
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
$modDate = $obj->get('modification_date'); |
|
195
|
|
|
if (!empty($modDate)) { |
|
196
|
|
|
$obj->set('modification_date', DateTimeObj::get('c', $obj->get('modification_date'))); |
|
197
|
|
|
} else { |
|
198
|
|
|
$obj->set('modification_date', $obj->get('creation_date')); |
|
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
self::$_pool[$obj->get('id')] = $obj; |
|
202
|
|
|
|
|
203
|
|
|
$ret[] = $obj; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return (count($ret) == 1 && $returnSingle ? $ret[0] : $ret); |
|
|
|
|
|
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Return a Section ID by the handle |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $handle |
|
213
|
|
|
* The handle of the section |
|
214
|
|
|
* @return integer |
|
215
|
|
|
* The Section ID |
|
216
|
|
|
*/ |
|
217
|
|
|
public static function fetchIDFromHandle($handle) |
|
218
|
|
|
{ |
|
219
|
|
|
$handle = Symphony::Database()->cleanValue($handle); |
|
220
|
|
|
return Symphony::Database()->fetchVar('id', 0, "SELECT `id` FROM `tbl_sections` WHERE `handle` = '$handle' LIMIT 1"); |
|
|
|
|
|
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Work out the next available sort order for a new section |
|
225
|
|
|
* |
|
226
|
|
|
* @return integer |
|
227
|
|
|
* Returns the next sort order |
|
228
|
|
|
*/ |
|
229
|
|
|
public static function fetchNextSortOrder() |
|
230
|
|
|
{ |
|
231
|
|
|
$next = Symphony::Database()->fetchVar( |
|
232
|
|
|
"next", |
|
|
|
|
|
|
233
|
|
|
0, |
|
234
|
|
|
"SELECT |
|
|
|
|
|
|
235
|
|
|
MAX(p.sortorder) + 1 AS `next` |
|
236
|
|
|
FROM |
|
237
|
|
|
`tbl_sections` AS p |
|
238
|
|
|
LIMIT 1" |
|
239
|
|
|
); |
|
240
|
|
|
|
|
241
|
|
|
return ($next ? (int)$next : 1); |
|
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Returns a new Section object, using the SectionManager |
|
246
|
|
|
* as the Section's $parent. |
|
247
|
|
|
* |
|
248
|
|
|
* @return Section |
|
249
|
|
|
*/ |
|
250
|
|
|
public static function create() |
|
251
|
|
|
{ |
|
252
|
|
|
$obj = new Section; |
|
253
|
|
|
return $obj; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Create an association between a section and a field. |
|
258
|
|
|
* |
|
259
|
|
|
* @since Symphony 2.3 |
|
260
|
|
|
* @param integer $parent_section_id |
|
261
|
|
|
* The linked section id. |
|
262
|
|
|
* @param integer $child_field_id |
|
263
|
|
|
* The field ID of the field that is creating the association |
|
264
|
|
|
* @param integer $parent_field_id (optional) |
|
265
|
|
|
* The field ID of the linked field in the linked section |
|
266
|
|
|
* @param boolean $show_association (optional) |
|
267
|
|
|
* Whether of not the link should be shown on the entries table of the |
|
268
|
|
|
* linked section. This defaults to true. |
|
269
|
|
|
* @throws DatabaseException |
|
270
|
|
|
* @throws Exception |
|
271
|
|
|
* @return boolean |
|
272
|
|
|
* true if the association was successfully made, false otherwise. |
|
273
|
|
|
*/ |
|
274
|
|
|
public static function createSectionAssociation($parent_section_id = null, $child_field_id = null, $parent_field_id = null, $show_association = true, $interface = null, $editor = null) |
|
|
|
|
|
|
275
|
|
|
{ |
|
276
|
|
|
if (is_null($parent_section_id) && (is_null($parent_field_id) || !$parent_field_id)) { |
|
277
|
|
|
return false; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
if (is_null($parent_section_id)) { |
|
281
|
|
|
$parent_field = FieldManager::fetch($parent_field_id); |
|
282
|
|
|
$parent_section_id = $parent_field->get('parent_section'); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
$child_field = FieldManager::fetch($child_field_id); |
|
286
|
|
|
$child_section_id = $child_field->get('parent_section'); |
|
287
|
|
|
|
|
288
|
|
|
$fields = array( |
|
289
|
|
|
'parent_section_id' => $parent_section_id, |
|
290
|
|
|
'parent_section_field_id' => $parent_field_id, |
|
291
|
|
|
'child_section_id' => $child_section_id, |
|
292
|
|
|
'child_section_field_id' => $child_field_id, |
|
293
|
|
|
'hide_association' => ($show_association ? 'no' : 'yes'), |
|
|
|
|
|
|
294
|
|
|
'interface' => $interface, |
|
295
|
|
|
'editor' => $editor |
|
296
|
|
|
); |
|
297
|
|
|
|
|
298
|
|
|
return Symphony::Database()->insert($fields, 'tbl_sections_association'); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Permanently remove a section association for this field in the database. |
|
303
|
|
|
* |
|
304
|
|
|
* @since Symphony 2.3 |
|
305
|
|
|
* @param integer $field_id |
|
306
|
|
|
* the field ID of the linked section's linked field. |
|
307
|
|
|
* @throws DatabaseException |
|
308
|
|
|
* @return boolean |
|
309
|
|
|
*/ |
|
310
|
|
|
public static function removeSectionAssociation($field_id) |
|
311
|
|
|
{ |
|
312
|
|
|
return Symphony::Database()->delete('tbl_sections_association', sprintf( |
|
313
|
|
|
'`child_section_field_id` = %1$d OR `parent_section_field_id` = %1$d', |
|
314
|
|
|
$field_id |
|
315
|
|
|
)); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Returns the association settings for the given field id. This is to be used |
|
320
|
|
|
* when configuring the field so we can correctly show the association setting |
|
321
|
|
|
* the UI. |
|
322
|
|
|
* |
|
323
|
|
|
* @since Symphony 2.6.0 |
|
324
|
|
|
* @param integer $field_id |
|
325
|
|
|
* @return string |
|
326
|
|
|
*/ |
|
327
|
|
|
public static function getSectionAssociationSetting($field_id) |
|
328
|
|
|
{ |
|
329
|
|
|
// We must inverse the setting. The database stores 'hide', whereas the UI |
|
330
|
|
|
// refers to 'show'. Hence if the database says 'yes', it really means, hide |
|
331
|
|
|
// the association. In the UI, this needs to be flipped to 'no' so the checkbox |
|
332
|
|
|
// won't be checked. |
|
333
|
|
|
return Symphony::Database()->fetchVar('show_association', 0, sprintf(' |
|
334
|
|
|
SELECT |
|
335
|
|
|
CASE hide_association WHEN "no" THEN "yes" ELSE "no" END as show_association |
|
336
|
|
|
FROM `tbl_sections_association` |
|
337
|
|
|
WHERE `child_section_field_id` = %d |
|
338
|
|
|
', $field_id)); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* Returns any section associations this section has with other sections |
|
343
|
|
|
* linked using fields. Has an optional parameter, `$respect_visibility` that |
|
344
|
|
|
* will only return associations that are deemed visible by a field that |
|
345
|
|
|
* created the association. eg. An articles section may link to the authors |
|
346
|
|
|
* section, but the field that links these sections has hidden this association |
|
347
|
|
|
* so an Articles column will not appear on the Author's Publish Index |
|
348
|
|
|
* |
|
349
|
|
|
* @deprecated This function will be removed in Symphony 3.0. Use `fetchChildAssociations` instead. |
|
350
|
|
|
* @since Symphony 2.3 |
|
351
|
|
|
* @param integer $section_id |
|
352
|
|
|
* The ID of the section |
|
353
|
|
|
* @param boolean $respect_visibility |
|
354
|
|
|
* Whether to return all the section associations regardless of if they |
|
355
|
|
|
* are deemed visible or not. Defaults to false, which will return all |
|
356
|
|
|
* associations. |
|
357
|
|
|
* @return array |
|
358
|
|
|
*/ |
|
359
|
|
|
public static function fetchAssociatedSections($section_id, $respect_visibility = false) |
|
|
|
|
|
|
360
|
|
|
{ |
|
361
|
|
|
if (Symphony::Log()) { |
|
362
|
|
|
Symphony::Log()->pushDeprecateWarningToLog('SectionManager::fetchAssociatedSections()', 'SectionManager::fetchChildAssociations()'); |
|
363
|
|
|
} |
|
|
|
|
|
|
364
|
|
|
self::fetchChildAssociations($section_id, $respect_visibility); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* Returns any section associations this section has with other sections |
|
369
|
|
|
* linked using fields, and where this section is the parent in the association. |
|
370
|
|
|
* Has an optional parameter, `$respect_visibility` that |
|
371
|
|
|
* will only return associations that are deemed visible by a field that |
|
372
|
|
|
* created the association. eg. An articles section may link to the authors |
|
373
|
|
|
* section, but the field that links these sections has hidden this association |
|
374
|
|
|
* so an Articles column will not appear on the Author's Publish Index |
|
375
|
|
|
* |
|
376
|
|
|
* @since Symphony 2.3.3 |
|
377
|
|
|
* @param integer $section_id |
|
378
|
|
|
* The ID of the section |
|
379
|
|
|
* @param boolean $respect_visibility |
|
380
|
|
|
* Whether to return all the section associations regardless of if they |
|
381
|
|
|
* are deemed visible or not. Defaults to false, which will return all |
|
382
|
|
|
* associations. |
|
383
|
|
|
* @throws DatabaseException |
|
384
|
|
|
* @return array |
|
385
|
|
|
*/ |
|
386
|
|
|
public static function fetchChildAssociations($section_id, $respect_visibility = false) |
|
|
|
|
|
|
387
|
|
|
{ |
|
388
|
|
|
return Symphony::Database()->fetch(sprintf(" |
|
|
|
|
|
|
389
|
|
|
SELECT * |
|
390
|
|
|
FROM `tbl_sections_association` AS `sa`, `tbl_sections` AS `s` |
|
391
|
|
|
WHERE `sa`.`parent_section_id` = %d |
|
392
|
|
|
AND `s`.`id` = `sa`.`child_section_id` |
|
393
|
|
|
%s |
|
394
|
|
|
ORDER BY `s`.`sortorder` ASC", |
|
395
|
|
|
$section_id, |
|
396
|
|
|
($respect_visibility) ? "AND `sa`.`hide_association` = 'no'" : "" |
|
|
|
|
|
|
397
|
|
|
)); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* Returns any section associations this section has with other sections |
|
402
|
|
|
* linked using fields, and where this section is the child in the association. |
|
403
|
|
|
* Has an optional parameter, `$respect_visibility` that |
|
404
|
|
|
* will only return associations that are deemed visible by a field that |
|
405
|
|
|
* created the association. eg. An articles section may link to the authors |
|
406
|
|
|
* section, but the field that links these sections has hidden this association |
|
407
|
|
|
* so an Articles column will not appear on the Author's Publish Index |
|
408
|
|
|
* |
|
409
|
|
|
* @since Symphony 2.3.3 |
|
410
|
|
|
* @param integer $section_id |
|
411
|
|
|
* The ID of the section |
|
412
|
|
|
* @param boolean $respect_visibility |
|
413
|
|
|
* Whether to return all the section associations regardless of if they |
|
414
|
|
|
* are deemed visible or not. Defaults to false, which will return all |
|
415
|
|
|
* associations. |
|
416
|
|
|
* @throws DatabaseException |
|
417
|
|
|
* @return array |
|
418
|
|
|
*/ |
|
419
|
|
|
public static function fetchParentAssociations($section_id, $respect_visibility = false) |
|
|
|
|
|
|
420
|
|
|
{ |
|
421
|
|
|
return Symphony::Database()->fetch(sprintf( |
|
422
|
|
|
"SELECT * |
|
|
|
|
|
|
423
|
|
|
FROM `tbl_sections_association` AS `sa`, `tbl_sections` AS `s` |
|
424
|
|
|
WHERE `sa`.`child_section_id` = %d |
|
425
|
|
|
AND `s`.`id` = `sa`.`parent_section_id` |
|
426
|
|
|
%s |
|
427
|
|
|
ORDER BY `s`.`sortorder` ASC", |
|
428
|
|
|
$section_id, |
|
429
|
|
|
($respect_visibility) ? "AND `sa`.`hide_association` = 'no'" : "" |
|
|
|
|
|
|
430
|
|
|
)); |
|
431
|
|
|
} |
|
432
|
|
|
} |
|
433
|
|
|
|