-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
95 lines (82 loc) · 3.05 KB
/
api.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
<?php
use Kirby\Data\Json;
use Kirby\Filesystem\Dir;
use Kirby\Filesystem\F;
use Kirby\Http\Response;
use Kirby\Toolkit\Collection;
use Kirby\Toolkit\Str;
$contentRoot = kirby()->root('content');
$path = $contentRoot.DS.'localizer';
$localisations = $contentRoot.DS.'localizer'.DS."localisations.json";
return [
'routes' => [
[
'pattern' => '/localizer',
'method' => 'get',
'action' => function () use ($path) {
try {
$files = Dir::read($path);
$data = [];
foreach ($files as $file) {
$content = F::read($path.DS.$file);
if (F::is($file, 'json')) {
$data[] = Json::decode($content);
}
}
return Response::json($data);
} catch (Exception $exception) {
return Response::json([]);
}
},
],
[
'pattern' => '/localizer',
'method' => 'post',
'action' => function () use ($path) {
$translations = kirby()->request()->body()->data();
$languages = [];
foreach ($translations as $translation) {
$id = $translation['language']['id'];
Json::write($path.DS."$id.json", $translation);
// save language id for deletion check
$languages[] = $id;
}
// delete removed languages groups
$files = Dir::read($path);
foreach ($files as $file) {
$filename = F::name($file);
if (!in_array($filename, $languages)) {
F::remove($path.DS.$file);
}
}
return Response::json($translations);
},
],
[
'pattern' => '/localizer/translations/(:alpha)',
'method' => 'get',
'action' => function ($code) {
$page = get('page', 1);
$limit = get('limit', 10);
$q = trim(get('q'));
$translations = kirby()->translations()->find($code ?? 'en');
if (!empty($q)) {
$filteredTranslations = array_filter(
$translations->data(),
fn($v, $k) => Str::contains($k, $q) || Str::contains($v, $q),
ARRAY_FILTER_USE_BOTH
);
}
$collection = new Collection($filteredTranslations ?? $translations->data());
$pagedCollection = $collection->paginate([
'page' => $page,
'limit' => $limit,
]);
return Response::json([
'data' => $pagedCollection->toArray(),
'pagination' => $pagedCollection->pagination()->toArray(),
]);
},
],
],
];