-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathextension.driver.php
310 lines (263 loc) · 8.81 KB
/
extension.driver.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
<?php
Class extension_order_entries extends Extension {
private $pagination_maximum_rows = null;
private $force_sort = false;
private $field_id = 0;
private $direction = 'asc';
private $dsFilters;
/**
* {@inheritDoc}
*/
public function getSubscribedDelegates(){
return array(
array(
'page' => '/backend/',
'delegate' => 'InitialiseAdminPageHead',
'callback' => 'prepareIndex'
),
array(
'page' => '/backend/',
'delegate' => 'AdminPagePreGenerate',
'callback' => 'adjustTable'
),
array(
'page' => '/backend/',
'delegate' => 'AdminPagePostGenerate',
'callback' => 'resetPagination'
),
array(
'page' => '/frontend/',
'delegate' => 'DataSourcePreExecute',
'callback' => 'saveFilterContext'
)
);
}
/**
* Save the Datasource Filter Context so it can be used for ordering
*/
public function saveFilterContext($context) {
$this->dsFilters = $context['datasource']->dsParamFILTERS;
}
/**
* get filters using filterable field ids and the section denoting where the filters are
*/
public function getFilters($filterableFields,$section_id){
//if no need to filter return empty filters
if (empty($filterableFields)) return array();
if (isset(Symphony::Engine()->Page)){
$context = method_exists(Symphony::Engine()->Page, 'getContext') ? Symphony::Engine()->Page->getContext() : array();
$filters = $context['filters'];
if (!isset($filters)) $filters = array();
// check if the filters are used for entry ordering and switch from name to id
foreach ($filters as $field_name => $value) {
$filtered_field_id = FieldManager::fetchFieldIDFromElementName($field_name,$section_id);
if (in_array($filtered_field_id, $filterableFields)){
//ensuring that capitalization will never be an issue
$filters[$filtered_field_id] = strtolower(General::sanitize($value));
}
unset($filters[$field_name]);
}
} else {
$filters = $this->dsFilters;
if (empty($filters)) return array();
// check if the filters are used for entry ordering otherwise remove from list
foreach ($filters as $filtered_field_id => $value) {
if (!in_array($filtered_field_id, $filterableFields)){
unset($filters[$filtered_field_id]);
} else {
$filters[$filtered_field_id] = strtolower(General::sanitize($value));
}
}
}
return $filters;
}
/**
* Prepare publish index for manual entry ordering
*/
public function prepareIndex($context) {
$callback = Symphony::Engine()->getPageCallback();
if($callback['driver'] == 'publish' && $callback['context']['page'] == 'index') {
// Fetch sort settings for this section (sort field ID and direction)
$section_id = SectionManager::fetchIDFromHandle($callback['context']['section_handle']);
// Fetch sorting field
if($section_id) {
$section = SectionManager::fetch($section_id);
$field = FieldManager::fetch($section->getSortingField());
// Check sorting field
if($field && $field->get('type') == 'order_entries') {
$this->force_sort = $field->get('force_sort');
$this->field_id = $field->get('id');
$this->direction = $section->getSortingOrder();
// Initialise manual ordering
$this->addComponents();
if ($field->get('disable_pagination') == 'yes'){
$this->disablePagination();
}
}
}
}
}
/**
* Force manual sorting
*/
public function adjustTable($context) {
if (!Symphony::Engine()->isLoggedIn()) {
return;
}
$callback = Symphony::Engine()->getPageCallback();
if($callback['driver'] == 'publish' && $callback['context']['page'] == 'index') {
$contents = $context['oPage']->Contents->getChildren();
// check every child, since the
// form may not always be the first element
foreach ($contents as $child) {
$form = $child->getChildrenByName('table');
// use current here since the keys can change somehow
$table = current($form);
if(!empty($table)) {
$table->setAttribute('data-order-entries-id', $this->field_id);
$table->setAttribute('data-order-entries-direction', $this->direction);
if($this->force_sort == 'yes') {
$table->setAttribute('data-order-entries-force', 'true');
}
$field = FieldManager::fetch($this->field_id);
if ($field && $field->get('show_column') == 'no'){
// sort order is not provided by field, so add manually
$tbody = $table->getChildByName('tbody',0);
//not looping as only the first row is required for sorting and is far more efficient
$tr = $tbody->getChildByName('tr',0);
$entry_id = str_replace('id-', '', $tr->getAttribute('id'));
if ($entry_id){
$entry = current(EntryManager::fetch($entry_id));
$data = $entry->getData($this->field_id);
$order = $field->getParameterPoolValue($data);
$tr->setAttribute('data-order',$order);
}
}
break;
}
}
}
}
/**
* Add components for manual entry ordering
*/
public function addComponents() {
// get filter data
$filters = $_REQUEST['filter'];
if (is_array($filters)){
$generatedFilters = array();
foreach ($filters as $field => $value) {
$generatedFilters[$field] = $value;
}
}
// add pagination and filter data on the form element
Administration::instance()->Page->Form->setAttribute(
'data-order-entries-filter',
empty($generatedFilters) ? '' : json_encode($generatedFilters)
);
Administration::instance()->Page->Form->setAttribute(
'data-order-entries-pagination-max-rows',
Symphony::Configuration()->get('pagination_maximum_rows', 'symphony')
);
Administration::instance()->Page->Form->setAttribute(
'data-order-entries-pagination-current',
(isset($_REQUEST['pg']) && is_numeric($_REQUEST['pg']) ? max(1, intval($_REQUEST['pg'])) : 1)
);
Administration::instance()->Page->addScriptToHead(
URL . '/extensions/order_entries/assets/order_entries.publish.js'
);
}
/**
* Contextually adjust maximum pagination rows
*/
public function disablePagination() {
$this->pagination_maximum_rows = Symphony::Configuration()->get('pagination_maximum_rows', 'symphony');
Symphony::Configuration()->set('pagination_maximum_rows', 99999, 'symphony');
}
/**
* Reset maximum pagination rows
*/
public function resetPagination() {
if($this->pagination_maximum_rows !== null) {
Symphony::Configuration()->set('pagination_maximum_rows', $this->pagination_maximum_rows, 'symphony');
}
}
/**
* {@inheritDoc}
*/
public function uninstall() {
Symphony::Database()->query("DROP TABLE `tbl_fields_order_entries`");
}
/**
* {@inheritDoc}
*/
public function update($previousVersion = false) {
$status = array();
// Prior version 1.6
if(version_compare($previousVersion, '1.6', '<')) {
$status[] = Symphony::Database()->query("
ALTER TABLE `tbl_fields_order_entries`
ADD `force_sort` enum('yes','no')
DEFAULT 'no'
");
}
// Prior version 1.8
if(version_compare($previousVersion, '1.8', '<')) {
$status[] = Symphony::Database()->query("
ALTER TABLE `tbl_fields_order_entries`
ADD `hide` enum('yes','no')
DEFAULT 'no'
");
}
// Prior version 2.1.4
if(version_compare($previousVersion, '2.1.4', '<')) {
$status[] = Symphony::Database()->query("
ALTER TABLE `tbl_fields_order_entries`
ADD `disable_pagination` enum('yes','no')
DEFAULT 'yes'
");
}
// Prior version 2.2
if(version_compare($previousVersion, '2.2', '<')) {
$status[] = Symphony::Database()->query("
ALTER TABLE `tbl_fields_order_entries`
ADD `filtered_fields` varchar(255) DEFAULT NULL
");
$fields = Symphony::Database()->fetchCol('field_id',"SELECT field_id FROM `tbl_fields_order_entries`");
foreach ($fields as $key => $field) {
$status[] = Symphony::Database()->query("
ALTER TABLE `tbl_entries_data_{$field}`
DROP INDEX `entry_id`
");
$status[] = Symphony::Database()->query("
ALTER TABLE `tbl_entries_data_{$field}`
ADD UNIQUE `unique`(`entry_id`)
");
}
}
// Report status
if(in_array(false, $status, true)) {
return false;
}
else {
return true;
}
}
/**
* {@inheritDoc}
*/
public function install() {
return Symphony::Database()->query("
CREATE TABLE `tbl_fields_order_entries` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
`force_sort` enum('yes','no') default 'no',
`hide` enum('yes','no') default 'no',
`disable_pagination` enum('yes','no') default 'no',
`filtered_fields` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `field_id` (`field_id`)
) TYPE=MyISAM
");
}
}