This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathactions.php
158 lines (128 loc) · 4.42 KB
/
actions.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
<?php
$app->on('collections.save.before', function($name, $entry, $isUpdate) use ($app) {
$config = $app->config['helpers'];
if (empty($config['uniqueFields']) || empty($config['uniqueFields'][$name])) {
return;
}
$uniqueFields = $config['uniqueFields'][$name];
foreach ($uniqueFields as $field) {
if (!isset($entry[$field]) || !$entry[$field]) continue;
$filter = [];
$filter[$field] = $entry[$field];
if ($isUpdate && isset($entry['_id'])) {
if ($app->storage->type === 'mongodb') {
$filter['_id'] = ['$ne' => new MongoDB\BSON\ObjectId($entry['_id'])];
}
else {
$filter['_id'] = ['$not' => $entry['_id']];
}
}
$check = $app->module('collections')->findOne($name, $filter);
if ($check) {
$app->stop(['error' => "<strong>{$field}</strong> must be unique!"], 412);
}
}
});
/**
* Recheck collection structure and remove fields that doesnt exist anymore.
*/
$app->on('collections.save.after', function($name, &$entry, $isUpdate) use($app) {
// Handle max revisions.
if ($isUpdate) {
$app->module('helpers')->removeMaxRevisions('collections', $name, $entry['_id']);
}
// Enforce collection schema update if there are changes.
$config = $app->config['helpers'];
$collection = $app->module('collections')->collection($name);
if (empty($config['checkSchema']) || !$config['checkSchema']) {
return;
}
$core_fields = ['_id', '_mby', '_by', '_modified', '_created', '_o', '_pid'];
$collection_fields = array_map(function($item) {
return $item['name'];
}, $collection['fields']);
// We retrieve list of languages because field may contain locale key.
// E.g. field_jp or field_example_fr.
$locales = [];
foreach ($app->retrieve('config/languages', []) as $key => $val) {
if (is_numeric($key)) $key = $val;
$locales[] = $key;
}
$update = FALSE;
foreach ($entry as $name => $value) {
if (in_array($name, $core_fields)) {
continue;
}
// Since we need to be sure that we don't remove service field with _slug, _locale or _locale_slug.
// Clean field from services parts above.
// E.g. field_example_slug -> field_example,
// another_field_example_jp_slug -> another_field_example.
$pure_field = $name;
$field_parts = explode("_", $name);
if (count($field_parts) > 1) {
$pure_field = implode(
array_filter($field_parts, function($part) use ($locales) {
return $part !== "slug" && !in_array($part, $locales);
}
), "_");
}
if (!in_array($pure_field, $collection_fields)) {
$update = TRUE;
unset($entry[$name]);
}
}
// Since core update performs an array merge between old and new data.
// We need to remove and re-insert the entry (the id will not change).
if ($update) {
// Remove entry.
$app->storage->remove("collections/{$collection['_id']}", ['_id' => $entry['_id']]);
// Reinsert entry with removed fields.
$app->storage->insert("collections/{$collection['_id']}", $entry);
}
});
/**
* Recheck singleton structure and remove fields that doesnt exist anymore.
*/
$app->on('singleton.saveData.before', function($singleton, &$data) use($app) {
$config = $app->config['helpers'];
if (empty($config['checkSchema']) || !$config['checkSchema']) {
return;
}
if (empty($data) || !is_array($data)) {
return;
}
$core_fields = ['_mby', '_by'];
$singleton_fields = array_map(function($item) {
return $item['name'];
}, $singleton['fields']);
$locales = [];
foreach ($app->retrieve('config/languages', []) as $key => $val) {
if (is_numeric($key)) $key = $val;
$locales[] = $key;
}
foreach ($data as $name => $value) {
if (in_array($name, $core_fields)) {
continue;
}
$pure_field = $name;
$field_parts = explode("_", $name);
if (count($field_parts) > 1) {
$pure_field = implode(
array_filter($field_parts, function($part) use ($locales) {
return $part !== "slug" && !in_array($part, $locales);
}
), "_");
}
if (!in_array($pure_field, $singleton_fields)) {
unset($data[$name]);
}
}
});
/**
* Handle singletons on saveData after.
*/
$app->on('singleton.saveData.after', function($singleton, $data) use($app) {
if (!empty($singleton['_id'])) {
$app->module('helpers')->removeMaxRevisions('singletons', $singleton['name'], $singleton['_id']);
}
});