Repository navigation
Expand file tree
/
Copy pathadmin.php
More file actions
55 lines (54 loc) · 1.89 KB
/
Copy pathadmin.php
File metadata and controls
55 lines (54 loc) · 1.89 KB
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
<?php
$app->on('admin.init', function () {
$this->helper('admin')->addAssets('cockpitspecificationsfield:assets/components/field-specifications.tag');
$this->helper('admin')->addAssets('cockpitspecificationsfield:assets/specifications.js');
});
$app->on('admin.init', function () use ($app) {
$this->bind('/specifications/templates/:collectionName', function ($params) use ($app) {
$collectionName = $params['collectionName'];
if (!$collectionName) {
$this->response->status = 400;
return ["error" => "collectionName is missing"];
}
if (!$app->module('collections')->hasaccess($collectionName, 'entries_view')) {
return $this->stop('{"error": "Unauthorized"}', 401);
}
$criteria = [];
$templates = $app->module('collections')->find($collectionName, $criteria);
$output = [];
foreach ($templates as $template) {
$attributes = [];
foreach ($template['attributes'] as $attribute) {
$attributes[] = $attribute['value'];
}
$output['templates'][] = [
"id" => $template['id'],
"name" => $template['name'],
"attributes" => $attributes
];
}
return $output;
});
$this->bind('/specifications/values/:collection/:field', function ($params) use ($app) {
$collectionName = $params['collection'];
if (!$app->module('collections')->hasaccess($collectionName, 'entries_view')) {
return $this->stop('{"error": "Unauthorized"}', 401);
}
$fieldName = $params['field'];
$criteria = [
"fields" => [
$fieldName => 1
]
];
$entries = $app->module('collections')->find($collectionName, $criteria);
$values = [];
foreach ($entries as $entry) {
foreach ($entry[$fieldName]['values'] as $id => $value) {
if (!$values[$id] || !in_array($value, $values[$id])) {
$values[$id][] = $value;
}
}
}
return $values;
});
});