/
class.section.php
321 lines (292 loc) · 10.8 KB
/
class.section.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
<?php
/**
* @package toolkit
*/
/**
* The Section class represents a Symphony Section object. A section is a model
* of a data structure using one or more Fields. Sections are stored in the database
* and are used as repositories for Entry objects, which are a model for this data
* structure. This class contains functions for finding Fields within a Section and
* saving a Section's settings.
*/
class Section
{
/**
* An array of the Section's settings
* @var array
*/
protected $_data = array();
/**
* A setter function that will save a section's setting into
* the poorly named `$this->_data` variable
*
* @param string $setting
* The setting name
* @param string $value
* The setting value
*/
public function set($setting, $value)
{
$this->_data[$setting] = $value;
}
/**
* An accessor function for this Section's settings. If the
* $setting param is omitted, an array of all settings will
* be returned. Otherwise it will return the data for
* the setting given.
*
* @param null|string $setting
* @return array|string
* If setting is provided, returns a string, if setting is omitted
* returns an associative array of this Section's settings
*/
public function get($setting = null)
{
if (is_null($setting)) {
return $this->_data;
}
return $this->_data[$setting];
}
/**
* Returns the default field this Section will be sorted by.
* This is determined by the first visible field that is allowed to
* to be sorted (defined by the field's `isSortable()` function).
* If no fields exist or none of them are visible in the entries table,
* 'id' is returned instead.
*
* @since Symphony 2.3
* @throws Exception
* @return string
* Either the field ID or the string 'id'.
*/
public function getDefaultSortingField()
{
$fields = $this->fetchVisibleColumns();
foreach ($fields as $field) {
if (!$field->isSortable()) {
continue;
}
return $field->get('id');
}
return 'id';
}
/**
* Returns the field this Section will be sorted by, or calls
* `getDefaultSortingField()` if the configuration file doesn't
* contain any settings for that Section.
*
* @since Symphony 2.3
* @throws Exception
* @return string
* Either the field ID or the string 'id'.
*/
public function getSortingField()
{
$result = Symphony::Configuration()->get('section_' . $this->get('handle') . '_sortby', 'sorting');
return (is_null($result) ? $this->getDefaultSortingField() : $result);
}
/**
* Returns the sort order for this Section. Defaults to 'asc'.
*
* @since Symphony 2.3
* @return string
* Either 'asc' or 'desc'.
*/
public function getSortingOrder()
{
$result = Symphony::Configuration()->get('section_' . $this->get('handle') . '_order', 'sorting');
return (is_null($result) ? 'asc' : $result);
}
/**
* Saves the new field this Section will be sorted by.
*
* @since Symphony 2.3
* @param string $sort
* The field ID or the string 'id'.
* @param boolean $write
* If false, the new settings won't be written on the configuration file.
* Defaults to true.
*/
public function setSortingField($sort, $write = true)
{
Symphony::Configuration()->set('section_' . $this->get('handle') . '_sortby', $sort, 'sorting');
if ($write) {
Symphony::Configuration()->write();
}
}
/**
* Saves the new sort order for this Section.
*
* @since Symphony 2.3
* @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 function setSortingOrder($order, $write = true)
{
Symphony::Configuration()->set('section_' . $this->get('handle') . '_order', $order, 'sorting');
if ($write) {
Symphony::Configuration()->write();
}
}
/**
* Returns any section associations this section has with other sections
* linked using fields. Has an optional parameter, `$respect_visibility` that
* will only return associations that are deemed visible by a field that
* created the association. eg. An articles section may link to the authors
* section, but the field that links these sections has hidden this association
* so an Articles column will not appear on the Author's Publish Index
*
* @deprecated This function will be removed in Symphony 3.0. Use `SectionManager::fetchChildAssociations` instead.
* @param boolean $respect_visibility
* Whether to return all the section associations regardless of if they
* are deemed visible or not. Defaults to false, which will return all
* associations.
* @return array
*/
public function fetchAssociatedSections($respect_visibility = false)
{
if (Symphony::Log()) {
Symphony::Log()->pushDeprecateWarningToLog('Section::fetchAssociatedSections()', 'SectionManager::fetchChildAssociations()');
}
return SectionManager::fetchChildAssociations($this->get('id'), $respect_visibility);
}
/**
* Returns any section associations this section has with other sections
* linked using fields, and where this section is the parent in the association.
* Has an optional parameter, `$respect_visibility` that
* will only return associations that are deemed visible by a field that
* created the association. eg. An articles section may link to the authors
* section, but the field that links these sections has hidden this association
* so an Articles column will not appear on the Author's Publish Index
*
* @since Symphony 2.3.3
* @param boolean $respect_visibility
* Whether to return all the section associations regardless of if they
* are deemed visible or not. Defaults to false, which will return all
* associations.
* @return array
*/
public function fetchChildAssociations($respect_visibility = false)
{
return SectionManager::fetchChildAssociations($this->get('id'), $respect_visibility);
}
/**
* Returns any section associations this section has with other sections
* linked using fields, and where this section is the child in the association.
* Has an optional parameter, `$respect_visibility` that
* will only return associations that are deemed visible by a field that
* created the association. eg. An articles section may link to the authors
* section, but the field that links these sections has hidden this association
* so an Articles column will not appear on the Author's Publish Index
*
* @since Symphony 2.3.3
* @param boolean $respect_visibility
* Whether to return all the section associations regardless of if they
* are deemed visible or not. Defaults to false, which will return all
* associations.
* @return array
*/
public function fetchParentAssociations($respect_visibility = false)
{
return SectionManager::fetchParentAssociations($this->get('id'), $respect_visibility);
}
/**
* Returns an array of all the fields in this section that are to be displayed
* on the entries table page ordered by the order in which they appear
* in the Section Editor interface
*
* @throws Exception
* @return array
*/
public function fetchVisibleColumns()
{
return FieldManager::fetch(null, $this->get('id'), 'ASC', 'sortorder', null, null, " AND t1.show_column = 'yes' ");
}
/**
* Returns an array of all the fields in this section optionally filtered by
* the field type or it's location within the section.
*
* @param string $type
* The field type (it's handle as returned by `$field->handle()`)
* @param string $location
* The location of the fields in the entry creator, whether they are
* 'main' or 'sidebar'
* @throws Exception
* @return array
*/
public function fetchFields($type = null, $location = null)
{
return FieldManager::fetch(null, $this->get('id'), 'ASC', 'sortorder', $type, $location);
}
/**
* Returns an array of all the fields that can be filtered.
*
* @param string $location
* The location of the fields in the entry creator, whether they are
* 'main' or 'sidebar'
* @throws Exception
* @return array
*/
public function fetchFilterableFields($location = null)
{
return FieldManager::fetch(null, $this->get('id'), 'ASC', 'sortorder', null, $location, null, Field::__FILTERABLE_ONLY__);
}
/**
* Returns an array of all the fields that can be toggled. This function
* is used to help build the With Selected drop downs on the Publish
* Index pages
*
* @param string $location
* The location of the fields in the entry creator, whether they are
* 'main' or 'sidebar'
* @throws Exception
* @return array
*/
public function fetchToggleableFields($location = null)
{
return FieldManager::fetch(null, $this->get('id'), 'ASC', 'sortorder', null, $location, null, Field::__TOGGLEABLE_ONLY__);
}
/**
* Returns the Schema of this section which includes all this sections
* fields and their settings.
*
* @return array
*/
public function fetchFieldsSchema()
{
return FieldManager::fetchFieldsSchema($this->get('id'));
}
/**
* Commit the settings of this section from the section editor to
* create an instance of this section in `tbl_sections`. This function
* loops of each of the fields in this section and calls their commit
* function.
*
* @see toolkit.Field#commit()
* @return boolean
* true if the commit was successful, false otherwise.
*/
public function commit()
{
$settings = $this->_data;
if (isset($settings['id'])) {
$id = $settings['id'];
unset($settings['id']);
$section_id = SectionManager::edit($id, $settings);
if ($section_id) {
$section_id = $id;
}
} else {
$section_id = SectionManager::add($settings);
}
if (is_numeric($section_id) && $section_id !== false) {
for ($ii = 0, $length = count($this->_fields); $ii < $length; $ii++) {
$this->_fields[$ii]->set('parent_section', $section_id);
$this->_fields[$ii]->commit();
}
}
}
}