|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package toolkit |
|
5
|
|
|
*/ |
|
6
|
|
|
/** |
|
7
|
|
|
* The Section class represents a Symphony Section object. A section is a model |
|
8
|
|
|
* of a data structure using one or more Fields. Sections are stored in the database |
|
9
|
|
|
* and are used as repositories for Entry objects, which are a model for this data |
|
10
|
|
|
* structure. This class contains functions for finding Fields within a Section and |
|
11
|
|
|
* saving a Section's settings. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
class Section |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* An array of the Section's settings |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $_data = array(); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* A setter function that will save a section's setting into |
|
24
|
|
|
* the poorly named `$this->_data` variable |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $setting |
|
27
|
|
|
* The setting name |
|
28
|
|
|
* @param string $value |
|
29
|
|
|
* The setting value |
|
30
|
|
|
*/ |
|
31
|
|
|
public function set($setting, $value) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->_data[$setting] = $value; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* An accessor function for this Section's settings. If the |
|
38
|
|
|
* $setting param is omitted, an array of all settings will |
|
39
|
|
|
* be returned. Otherwise it will return the data for |
|
40
|
|
|
* the setting given. |
|
41
|
|
|
* |
|
42
|
|
|
* @param null|string $setting |
|
43
|
|
|
* @return array|string |
|
44
|
|
|
* If setting is provided, returns a string, if setting is omitted |
|
45
|
|
|
* returns an associative array of this Section's settings |
|
46
|
|
|
*/ |
|
47
|
|
|
public function get($setting = null) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
if (is_null($setting)) { |
|
50
|
|
|
return $this->_data; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->_data[$setting]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns the default field this Section will be sorted by. |
|
58
|
|
|
* This is determined by the first visible field that is allowed to |
|
59
|
|
|
* to be sorted (defined by the field's `isSortable()` function). |
|
60
|
|
|
* If no fields exist or none of them are visible in the entries table, |
|
61
|
|
|
* 'id' is returned instead. |
|
62
|
|
|
* |
|
63
|
|
|
* @since Symphony 2.3 |
|
64
|
|
|
* @throws Exception |
|
65
|
|
|
* @return string |
|
66
|
|
|
* Either the field ID or the string 'id'. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getDefaultSortingField() |
|
69
|
|
|
{ |
|
70
|
|
|
$fields = $this->fetchVisibleColumns(); |
|
71
|
|
|
|
|
72
|
|
|
foreach ($fields as $field) { |
|
73
|
|
|
if (!$field->isSortable()) { |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $field->get('id'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return 'id'; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns the field this Section will be sorted by, or calls |
|
85
|
|
|
* `getDefaultSortingField()` if the configuration file doesn't |
|
86
|
|
|
* contain any settings for that Section. |
|
87
|
|
|
* |
|
88
|
|
|
* @since Symphony 2.3 |
|
89
|
|
|
* @throws Exception |
|
90
|
|
|
* @return string |
|
91
|
|
|
* Either the field ID or the string 'id'. |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getSortingField() |
|
94
|
|
|
{ |
|
95
|
|
|
$result = Symphony::Configuration()->get('section_' . $this->get('handle') . '_sortby', 'sorting'); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
return (is_null($result) ? $this->getDefaultSortingField() : $result); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Returns the sort order for this Section. Defaults to 'asc'. |
|
102
|
|
|
* |
|
103
|
|
|
* @since Symphony 2.3 |
|
104
|
|
|
* @return string |
|
105
|
|
|
* Either 'asc' or 'desc'. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getSortingOrder() |
|
108
|
|
|
{ |
|
109
|
|
|
$result = Symphony::Configuration()->get('section_' . $this->get('handle') . '_order', 'sorting'); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
return (is_null($result) ? 'asc' : $result); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Saves the new field this Section will be sorted by. |
|
116
|
|
|
* |
|
117
|
|
|
* @since Symphony 2.3 |
|
118
|
|
|
* @param string $sort |
|
119
|
|
|
* The field ID or the string 'id'. |
|
120
|
|
|
* @param boolean $write |
|
121
|
|
|
* If false, the new settings won't be written on the configuration file. |
|
122
|
|
|
* Defaults to true. |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setSortingField($sort, $write = true) |
|
|
|
|
|
|
125
|
|
|
{ |
|
126
|
|
|
Symphony::Configuration()->set('section_' . $this->get('handle') . '_sortby', $sort, 'sorting'); |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
if ($write) { |
|
129
|
|
|
Symphony::Configuration()->write(); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Saves the new sort order for this Section. |
|
135
|
|
|
* |
|
136
|
|
|
* @since Symphony 2.3 |
|
137
|
|
|
* @param string $order |
|
138
|
|
|
* Either 'asc' or 'desc'. |
|
139
|
|
|
* @param boolean $write |
|
140
|
|
|
* If false, the new settings won't be written on the configuration file. |
|
141
|
|
|
* Defaults to true. |
|
142
|
|
|
*/ |
|
143
|
|
|
public function setSortingOrder($order, $write = true) |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
|
|
Symphony::Configuration()->set('section_' . $this->get('handle') . '_order', $order, 'sorting'); |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
if ($write) { |
|
148
|
|
|
Symphony::Configuration()->write(); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Returns any section associations this section has with other sections |
|
154
|
|
|
* linked using fields. Has an optional parameter, `$respect_visibility` that |
|
155
|
|
|
* will only return associations that are deemed visible by a field that |
|
156
|
|
|
* created the association. eg. An articles section may link to the authors |
|
157
|
|
|
* section, but the field that links these sections has hidden this association |
|
158
|
|
|
* so an Articles column will not appear on the Author's Publish Index |
|
159
|
|
|
* |
|
160
|
|
|
* @deprecated This function will be removed in Symphony 3.0. Use `SectionManager::fetchChildAssociations` instead. |
|
161
|
|
|
* @param boolean $respect_visibility |
|
162
|
|
|
* Whether to return all the section associations regardless of if they |
|
163
|
|
|
* are deemed visible or not. Defaults to false, which will return all |
|
164
|
|
|
* associations. |
|
165
|
|
|
* @return array |
|
166
|
|
|
*/ |
|
167
|
|
|
public function fetchAssociatedSections($respect_visibility = false) |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
if (Symphony::Log()) { |
|
170
|
|
|
Symphony::Log()->pushDeprecateWarningToLog('Section::fetchAssociatedSections()', 'SectionManager::fetchChildAssociations()'); |
|
171
|
|
|
} |
|
|
|
|
|
|
172
|
|
|
return SectionManager::fetchChildAssociations($this->get('id'), $respect_visibility); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Returns any section associations this section has with other sections |
|
177
|
|
|
* linked using fields, and where this section is the parent in the association. |
|
178
|
|
|
* Has an optional parameter, `$respect_visibility` that |
|
179
|
|
|
* will only return associations that are deemed visible by a field that |
|
180
|
|
|
* created the association. eg. An articles section may link to the authors |
|
181
|
|
|
* section, but the field that links these sections has hidden this association |
|
182
|
|
|
* so an Articles column will not appear on the Author's Publish Index |
|
183
|
|
|
* |
|
184
|
|
|
* @since Symphony 2.3.3 |
|
185
|
|
|
* @param boolean $respect_visibility |
|
186
|
|
|
* Whether to return all the section associations regardless of if they |
|
187
|
|
|
* are deemed visible or not. Defaults to false, which will return all |
|
188
|
|
|
* associations. |
|
189
|
|
|
* @return array |
|
190
|
|
|
*/ |
|
191
|
|
|
public function fetchChildAssociations($respect_visibility = false) |
|
|
|
|
|
|
192
|
|
|
{ |
|
193
|
|
|
return SectionManager::fetchChildAssociations($this->get('id'), $respect_visibility); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Returns any section associations this section has with other sections |
|
198
|
|
|
* linked using fields, and where this section is the child in the association. |
|
199
|
|
|
* Has an optional parameter, `$respect_visibility` that |
|
200
|
|
|
* will only return associations that are deemed visible by a field that |
|
201
|
|
|
* created the association. eg. An articles section may link to the authors |
|
202
|
|
|
* section, but the field that links these sections has hidden this association |
|
203
|
|
|
* so an Articles column will not appear on the Author's Publish Index |
|
204
|
|
|
* |
|
205
|
|
|
* @since Symphony 2.3.3 |
|
206
|
|
|
* @param boolean $respect_visibility |
|
207
|
|
|
* Whether to return all the section associations regardless of if they |
|
208
|
|
|
* are deemed visible or not. Defaults to false, which will return all |
|
209
|
|
|
* associations. |
|
210
|
|
|
* @return array |
|
211
|
|
|
*/ |
|
212
|
|
|
public function fetchParentAssociations($respect_visibility = false) |
|
|
|
|
|
|
213
|
|
|
{ |
|
214
|
|
|
return SectionManager::fetchParentAssociations($this->get('id'), $respect_visibility); |
|
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Returns an array of all the fields in this section that are to be displayed |
|
219
|
|
|
* on the entries table page ordered by the order in which they appear |
|
220
|
|
|
* in the Section Editor interface |
|
221
|
|
|
* |
|
222
|
|
|
* @throws Exception |
|
223
|
|
|
* @return array |
|
224
|
|
|
*/ |
|
225
|
|
|
public function fetchVisibleColumns() |
|
226
|
|
|
{ |
|
227
|
|
|
return FieldManager::fetch(null, $this->get('id'), 'ASC', 'sortorder', null, null, " AND t1.show_column = 'yes' "); |
|
|
|
|
|
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Returns an array of all the fields in this section optionally filtered by |
|
232
|
|
|
* the field type or it's location within the section. |
|
233
|
|
|
* |
|
234
|
|
|
* @param string $type |
|
235
|
|
|
* The field type (it's handle as returned by `$field->handle()`) |
|
236
|
|
|
* @param string $location |
|
237
|
|
|
* The location of the fields in the entry creator, whether they are |
|
238
|
|
|
* 'main' or 'sidebar' |
|
239
|
|
|
* @throws Exception |
|
240
|
|
|
* @return array |
|
241
|
|
|
*/ |
|
242
|
|
|
public function fetchFields($type = null, $location = null) |
|
|
|
|
|
|
243
|
|
|
{ |
|
244
|
|
|
return FieldManager::fetch(null, $this->get('id'), 'ASC', 'sortorder', $type, $location); |
|
|
|
|
|
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Returns an array of all the fields that can be filtered. |
|
249
|
|
|
* |
|
250
|
|
|
* @param string $location |
|
251
|
|
|
* The location of the fields in the entry creator, whether they are |
|
252
|
|
|
* 'main' or 'sidebar' |
|
253
|
|
|
* @throws Exception |
|
254
|
|
|
* @return array |
|
255
|
|
|
*/ |
|
256
|
|
|
public function fetchFilterableFields($location = null) |
|
|
|
|
|
|
257
|
|
|
{ |
|
258
|
|
|
return FieldManager::fetch(null, $this->get('id'), 'ASC', 'sortorder', null, $location, null, Field::__FILTERABLE_ONLY__); |
|
|
|
|
|
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Returns an array of all the fields that can be toggled. This function |
|
263
|
|
|
* is used to help build the With Selected drop downs on the Publish |
|
264
|
|
|
* Index pages |
|
265
|
|
|
* |
|
266
|
|
|
* @param string $location |
|
267
|
|
|
* The location of the fields in the entry creator, whether they are |
|
268
|
|
|
* 'main' or 'sidebar' |
|
269
|
|
|
* @throws Exception |
|
270
|
|
|
* @return array |
|
271
|
|
|
*/ |
|
272
|
|
|
public function fetchToggleableFields($location = null) |
|
|
|
|
|
|
273
|
|
|
{ |
|
274
|
|
|
return FieldManager::fetch(null, $this->get('id'), 'ASC', 'sortorder', null, $location, null, Field::__TOGGLEABLE_ONLY__); |
|
|
|
|
|
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Returns the Schema of this section which includes all this sections |
|
279
|
|
|
* fields and their settings. |
|
280
|
|
|
* |
|
281
|
|
|
* @return array |
|
282
|
|
|
*/ |
|
283
|
|
|
public function fetchFieldsSchema() |
|
284
|
|
|
{ |
|
285
|
|
|
return FieldManager::fetchFieldsSchema($this->get('id')); |
|
|
|
|
|
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Commit the settings of this section from the section editor to |
|
290
|
|
|
* create an instance of this section in `tbl_sections`. This function |
|
291
|
|
|
* loops of each of the fields in this section and calls their commit |
|
292
|
|
|
* function. |
|
293
|
|
|
* |
|
294
|
|
|
* @see toolkit.Field#commit() |
|
295
|
|
|
* @return boolean |
|
296
|
|
|
* true if the commit was successful, false otherwise. |
|
297
|
|
|
*/ |
|
298
|
|
|
public function commit() |
|
299
|
|
|
{ |
|
300
|
|
|
$settings = $this->_data; |
|
301
|
|
|
|
|
302
|
|
|
if (isset($settings['id'])) { |
|
303
|
|
|
$id = $settings['id']; |
|
304
|
|
|
unset($settings['id']); |
|
305
|
|
|
$section_id = SectionManager::edit($id, $settings); |
|
306
|
|
|
|
|
307
|
|
|
if ($section_id) { |
|
308
|
|
|
$section_id = $id; |
|
309
|
|
|
} |
|
310
|
|
|
} else { |
|
311
|
|
|
$section_id = SectionManager::add($settings); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
if (is_numeric($section_id) && $section_id !== false) { |
|
315
|
|
|
for ($ii = 0, $length = count($this->_fields); $ii < $length; $ii++) { |
|
|
|
|
|
|
316
|
|
|
$this->_fields[$ii]->set('parent_section', $section_id); |
|
317
|
|
|
$this->_fields[$ii]->commit(); |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
|