-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.php
39 lines (36 loc) · 1.6 KB
/
bootstrap.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
<?php
/**
* Modify collection result to include null on collection lookup and save
*/
$this->on('collections.find.after', function ($name, &$entries) {
if ($collection = include(cockpit()->path("#storage:collections/{$name}.collection.php"))) {
foreach ($entries as $k => $v) {
handleEntry($entries[$k], $collection['fields']);
}
}
});
$this->on('collections.save.after', function ($name, &$entry) {
if ($collection = include(cockpit()->path("#storage:collections/{$name}.collection.php"))) {
handleEntry($entry, $collection['fields']);
}
});
function handleEntry(&$collectionEntry, $collectionFields) {
foreach ($collectionFields as $field) {
switch ($field['type']) {
case 'string':
case 'text':
$isNonEmptyString = is_string($collectionEntry[$field['name']]) && strlen(trim($collectionEntry[$field['name']])) !== 0;
$collectionEntry[$field['name']] = $isNonEmptyString ? $collectionEntry[$field['name']] : null;
break;
case 'boolean':
$collectionEntry[$field['name']] = is_bool($collectionEntry[$field['name']]) ? $collectionEntry[$field['name']] : false;
break;
case 'number':
$collectionEntry[$field['name']] = is_numeric($collectionEntry[$field['name']]) ? $collectionEntry[$field['name']] : 0;
break;
default:
$collectionEntry[$field['name']] = !empty($collectionEntry[$field['name']]) ? $collectionEntry[$field['name']] : null;
break;
}
}
}