-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextension.driver.php
462 lines (387 loc) · 14.2 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
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
456
457
458
459
460
461
462
<?php
Class extension_default_event_values extends Extension{
public function getSubscribedDelegates() {
return array(
array(
'page' => '/backend/',
'delegate' => 'AdminPagePreGenerate',
'callback' => 'AdminPagePreGenerate'
),
array(
'page' => '/blueprints/events/',
'delegate' => 'EventPreCreate',
'callback' => 'saveEssentials'
),
array(
'page' => '/blueprints/events/',
'delegate' => 'EventPreEdit',
'callback' => 'saveEssentials'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPreSaveFilter',
'callback' => 'setDefaultValues'
),
);
}
/*-------------------------------------------------------------------------
Delegate Callbacks:
-------------------------------------------------------------------------*/
/**
* @uses AdminPagePreGenerate
*/
public function AdminPagePreGenerate(&$context) {
if($context['oPage'] instanceof contentBlueprintsEvents) {
$callback = Administration::instance()->getPageCallback();
if(in_array($callback['context'][0], array('edit', 'new'))) {
$page = $context['oPage'];
// Get the children of the Contents wrapper
$xChildren = $page->Contents->getChildren();
// Pop off the current <form> array and get it's children
$form = array_shift($xChildren);
// Inject our custom goodness, using `prependChild` so that it's
// at the start of the form
$this->injectFields($form, $callback);
}
}
}
/**
* @uses EventPreEdit
* @uses EventPostEdit
*/
public function saveEssentials(&$context) {
$default_values = 'public $eDefaultValues = array(';
if(is_array($_POST['default_event_values'])) {
foreach($_POST['default_event_values'] as $field => $dv) {
if($field == 'custom_value') {
$max = count($_POST['default_event_values']['custom_value']);
for($i = 0; $i <= $max; $i++) {
if(!isset($_POST['default_event_values']['custom_value'][$i])) continue;
$default_values .= self::addCustomDefaultValue($_POST['default_event_values']['custom_value'][$i]);
}
}
else {
$default_values .= self::addDefaultValue($field, $dv);
}
}
}
$default_values .= PHP_EOL . str_repeat("\t", 2) . ');';
$default_values .= PHP_EOL . PHP_EOL . str_repeat("\t", 2) . 'public $eParamFILTERS';
$context['contents'] = preg_replace('/public \$eParamFILTERS/i', $default_values, $context['contents']);
}
/**
* @uses EventPreSaveFilter
*/
public function setDefaultValues(&$context) {
if(!isset($context['event']->eDefaultValues) || !is_array($context['event']->eDefaultValues)) return;
// Create a Datasource class, which has the logic for finding Parameters
// and turning them into the values.
$datasource = new Datasource(null, false);
// Fake an environment to find Parameters in
$env = array(
'env' => Frontend::instance()->Page()->Env(),
'param' => Frontend::instance()->Page()->Params()
);
// Loop over the Default Values, setting them in $_POST or `$context['fields']`
// as appropriate.
foreach($context['event']->eDefaultValues as $field => $dv) {
$value = $datasource->__processParametersInString($dv['value'], $env);
// Custom field, this will set $_POST instead of the `$context['fields']`
// as `$context['fields']` only contains things inside $_POST['fields']
if($dv['custom'] == 'yes') {
$matches = preg_split('/\[/U', $field);
foreach($matches as $key => $match) {
$matches[$key] = trim($match, ']');
}
if(count($matches) == 1) {
self::setArrayValue($_POST, $field, $value, ($dv['override'] == 'yes'));
}
// We'll need to build out the relevant $_POST array
else {
$tree = self::addKey($matches, $value);
// If the DV is an override, set it regardless
// DV is not an override, so only set if it hasn't already been set
if(($dv['override'] == 'no') && !self::checkArrayForTree($_POST, $tree)) {
$_POST = array_merge_recursive($_POST, $tree);
}
else if($dv['override'] == 'yes') {
$_POST = array_replace_recursive($_POST, $tree);
}
}
continue;
}
self::setArrayValue($_POST['fields'], $field, $value, ($dv['override'] == 'yes'));
self::setArrayValue($context['fields'], $field, $value, ($dv['override'] == 'yes'));
}
}
/*-------------------------------------------------------------------------
Helpers:
-------------------------------------------------------------------------*/
/**
* Given a flat array, build this out to be an associative array setting
* the last key to the `$value`.
*
* @param array $array
* @param string $value
* @return array
*/
private static function addKey(&$array, $value = null) {
return ($key = array_pop($array))
? self::addKey($array, array($key => $value))
: $value;
}
/**
* Given an array, this function will set the `$value` at the `$key`.
* If `$override` is set to the true, the value will be set regardless,
* if not it will only be set if the key doesn't already exist
*
* @param array $array
* @param string $key
* @param string $value
* @param boolean $override
*/
private static function setArrayValue(&$array, $key, $value, $override) {
if($override || !isset($array[$key])) {
$array[$key] = $value;
}
}
/**
* Given one `$array` structure, this functions checks to see if the `$tree`
* structure exists in the `$array` returning boolean
*
* @param array $array
* @param array $tree
* @return boolean
* True if the structure does exist, false otherwise
*/
private static function checkArrayForTree(&$array, $tree) {
// If either parameter is now not an array, return true
// as the structure exists
if(!is_array($array) || !is_array($tree)) {
return true;
}
// Get keys in the tree
foreach(array_keys($tree) as $key) {
// Check to see if the key exists in $array
if(in_array($key, array_keys($array))) {
// If it exists, move down the tree and check the next one
return self::checkArrayForTree($array[$key], $tree[$key]);
}
// If it doesn't, return false
return false;
}
}
private static function addCustomDefaultValue($custom) {
return self::addDefaultValue($custom['key'], array(
'value' => $custom['value'],
'override' => $custom['override'],
'custom' => 'yes'
));
}
private static function addDefaultValue($name, $value) {
return sprintf('
"%s" => array(
%s
%s
%s
),',
$name,
isset($value['value']) ? "'value' => '" . $value['value'] . "'," : null,
isset($value['override']) ? "'override' => '" . $value['override'] . "'," : null,
isset($value['custom']) ? "'custom' => '" . $value['custom'] . "'" : null
);
}
/*-------------------------------------------------------------------------
Event Editor:
-------------------------------------------------------------------------*/
private function injectFields(XMLElement &$form, array $callback) {
// skip when creating new events
if ($callback['context'][0] == 'new') return $this->injectDefault($form);
$event = EventManager::create($callback['context'][1]);
$event_source = null;
if(method_exists($event, 'getSource')) {
$event_source = $event->getSource();
}
// This isn't a typical event, so return
if(!is_numeric($event_source)) return null;
$section = SectionManager::fetch($event_source);
// For whatever reason, the Section doesn't exist anymore
if(!$section instanceof Section) return null;
$this->injectDefaultValues($form, $event, $section);
}
private function injectDefault(XMLElement &$form) {
// Create the Default Values fieldset
$fieldset = new XMLElement('fieldset', null, array('class' => 'settings'));
$fieldset->appendChild(
new XMLElement('legend', __('Default Values'))
);
$div = new XMLElement('div', null);
$div->appendChild(
new XMLElement('p', __('Default values can be set after this event has been created.'), array('class' => 'label'))
);
$fieldset->appendChild($div);
$form->insertChildAt(1, $fieldset);
}
private function injectDefaultValues(XMLElement &$form, Event $event, Section $section) {
// Create the Default Values fieldset
$fieldset = new XMLElement('fieldset', null, array('class' => 'settings'));
$fieldset->appendChild(
new XMLElement('legend', __('Default Values'))
);
$fieldset->appendChild(
new XMLElement('p', __('Use Default Values to set field values without having them in your Frontend markup. Use <code>{$param}</code> syntax to use page parameters.'), array(
'class' => 'help'
))
);
$div = new XMLElement('div', null);
$div->appendChild(
new XMLElement('p', __('Add Default Value'), array('class' => 'label'))
);
// Create Duplicators
$ol = new XMLElement('ol');
$ol->setAttribute('class', 'filters-duplicator');
$custom_default_values = $event->eDefaultValues;
// Loop over this event's section's fields
foreach($section->fetchFields() as $field) {
// Remove this from the `custom_default_values` array
unset($custom_default_values[$field->get('element_name')]);
// Add template
$this->createDuplicatorTemplate($ol, $field->get('label'), $field->get('element_name'));
// Create real instance with real data
if(isset($event->eDefaultValues[$field->get('element_name')])) {
$filter = $event->eDefaultValues[$field->get('element_name')];
$this->createDuplicatorTemplate($ol, $field->get('label'), $field->get('element_name'), $filter);
}
}
$this->createCustomValueDuplicatorTemplate($ol);
if(is_array($custom_default_values)) {
$custom_default_values = array_filter($custom_default_values);
if(!empty($custom_default_values)) {
foreach($custom_default_values as $name => $values) {
$this->createCustomValueDuplicatorTemplate($ol, $name, $values);
}
}
}
$div->appendChild($ol);
$fieldset->appendChild($div);
$form->insertChildAt(1, $fieldset);
}
/*-------------------------------------------------------------------------
Duplicator Utilities
-------------------------------------------------------------------------*/
private function createDuplicatorTemplate(XMLElement $wrapper, $label, $name, array $values = null) {
// Create duplicator template
$li = new XMLElement('li');
$li->setAttribute('data-type', $name);
$header = new XMLElement('header');
$header->appendChild(new XMLElement('h4', $label));
$li->appendChild($header);
if(is_null($values)) {
$li->setAttribute('class', 'unique template');
}
else {
$li->setAttribute('class', 'unique');
}
// Value
$xLabel = Widget::Label(__('Value'));
$xLabel->appendChild(
Widget::Input('default_event_values['.$name.'][value]', !is_null($values) ? $values['value'] : null)
);
$li->appendChild($xLabel);
// Will this value override?
$li->appendChild(
Widget::Input('default_event_values['.$name.'][override]', 'no', 'hidden')
);
$input = Widget::Input('default_event_values['.$name.'][override]', 'yes', 'checkbox');
if(isset($values['override']) && $values['override'] == 'yes') {
$input->setAttribute('checked', 'checked');
}
$xLabel = Widget::Label(
__('%s Value will override any value posted from the frontend', array($input->generate()))
);
$li->appendChild($xLabel);
// Add to the wrapper
$wrapper->appendChild($li);
}
private function createCustomValueDuplicatorTemplate(XMLElement $wrapper, $name = 'Custom', array $values = null) {
// Create duplicator template
$li = new XMLElement('li');
$li->setAttribute('data-type', 'custom');
$header = new XMLElement('header');
$header->appendChild(new XMLElement('h4', $name));
$li->appendChild($header);
if(is_null($values)) {
$li->setAttribute('class', 'template');
}
$group = new XMLElement('div', null, array('class' => 'two columns'));
// Column One
$col = new XMLElement('div');
$col->setAttribute('class', 'column');
// Custom Key
$xLabel = Widget::Label(__('Key'));
$xLabel->appendChild(
Widget::Input('default_event_values[custom_value][-1][key]', ($name !== 'Custom') ? $name : null)
);
$col->appendChild($xLabel);
$group->appendChild($col);
// Column Two
$col = new XMLElement('div');
$col->setAttribute('class', 'column');
// Value
$xLabel = Widget::Label(__('Value'));
$xLabel->appendChild(
Widget::Input('default_event_values[custom_value][-1][value]', !is_null($values) ? $values['value'] : null)
);
$col->appendChild($xLabel);
// Will this value override?
$li->appendChild(
Widget::Input('default_event_values[custom_value][-1][override]', 'no', 'hidden')
);
$input = Widget::Input('default_event_values[custom_value][-1][override]', 'yes', 'checkbox');
if(isset($values['override']) && $values['override'] == 'yes') {
$input->setAttribute('checked', 'checked');
}
$xLabel = Widget::Label(
__('%s Value will override any value posted from the frontend', array($input->generate()))
);
$col->appendChild($xLabel);
$group->appendChild($col);
$li->appendChild($group);
// Add to the wrapper
$wrapper->appendChild($li);
}
}
/**
* One hundred thousand man hugs and thanks to Gregor for this function
* @link http://www.php.net/manual/en/function.array-replace-recursive.php#92574
*/
if(!function_exists('array_replace_recursive')) {
function array_replace_recursive($array, $array1) {
function recurse($array, $array1) {
foreach ($array1 as $key => $value) {
// create new key in $array, if it is empty or not an array
if (!isset($array[$key]) || (isset($array[$key]) && !is_array($array[$key]))) {
$array[$key] = array();
}
// overwrite the value in the base array
if (is_array($value)) {
$value = recurse($array[$key], $value);
}
$array[$key] = $value;
}
return $array;
}
// handle the arguments, merge one by one
$args = func_get_args();
$array = $args[0];
if (!is_array($array)) {
return $array;
}
for ($i = 1; $i < count($args); $i++) {
if (is_array($args[$i])) {
$array = recurse($array, $args[$i]);
}
}
return $array;
}
}