-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathroutes.php
92 lines (77 loc) · 2.61 KB
/
routes.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
<?php
use Kirby\Cms\Search;
use Kirby\Toolkit\Str;
function pageUrl(Kirby\Cms\Page $page, bool $qualified)
{
if ($qualified) {
return $page->url();
}
return parse_url($page->url(), PHP_URL_PATH);
}
function pageTitle(Kirby\Cms\Page $page): string
{
$query = option('gearsdigital.enhanced-toolbar-link-dialog.title', '{{ page.title }}');
return Str::template($query, [
'page' => $page,
'site' => site(),
'kirby' => kirby(),
]);
}
function customPageData(Kirby\Cms\Page $page): array
{
return [
'id' => $page->id(),
'text' => pageTitle($page),
'link' => pageUrl($page, option('gearsdigital.enhanced-toolbar-link-dialog.qualified', false)),
'info' => $page->slug(),
'image' => $page->panel()->image(),
];
}
function pagination(array $array, int $page, int $pageSize): array
{
$arrayChunks = array_chunk($array, $pageSize);
return $arrayChunks[$page - 1] ?? [];
}
return [
'routes' => [
[
'pattern' => 'enhanced-toolbar-link-dialog/pages',
'action' => function () {
$query = get('q', '*');
$page = get('page', 1);
// this option isn't documented because I don't want to break the layout if someone decided to show
// 30 pages per page but I'll keep it just in case
$limit = option('gearsdigital.enhanced-toolbar-link-dialog.limit', 5);
$pages = Search::pages($query);
if ($sortBy = option('gearsdigital.enhanced-toolbar-link-dialog.sort')) {
$pages = $pages->sortBy(...$sortBy);
}
if ($filter = option('gearsdigital.enhanced-toolbar-link-dialog.filter')) {
$pages = $pages->filterBy(...$filter);
}
foreach ($pages as $contentPage) {
$results[] = customPageData($contentPage);
}
return [
"data" => pagination($results ?? [], $page, $limit),
"pagination" => [
"total" => sizeof($results ?? []),
"page" => $page,
"limit" => $limit,
],
];
},
],
[
'pattern' => 'enhanced-toolbar-link-dialog/page',
'action' => function () {
$id = get('id');
$page = page($id);
if ($page) {
return customPageData($page);
}
return null;
},
],
],
];