forked from medienbaecker/kirby-link-field
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·126 lines (125 loc) · 5.24 KB
/
index.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
<?php
Kirby::plugin('medienbaecker/link', [
'fields' => [
'link' => [
'props' => [
'value' => function($value = null) {
return Yaml::decode($value);
},
'options' => function($options = ["url", "email", "page", "file"]) {
return $options;
}
],
'methods' => [
'pageResponse' => function ($page) {
$thumb = [
'width' => 100,
'height' => 100
];
$image = $page->panelImage($this->image, $thumb);
$model = $this->model();
return [
'text' => $page->toString($this->text ?? '{{ page.title }}'),
'link' => $page->panelUrl(true),
'id' => $page->id(),
'info' => $page->toString($this->info ?? false),
'image' => $image,
'icon' => $page->panelIcon($image),
'hasChildren' => $page->hasChildren(),
];
},
'fileResponse' => function ($file) {
$thumb = [
'width' => 100,
'height' => 100
];
$image = $file->panelImage($this->image, $thumb);
$model = $this->model();
$uuid = $file->parent() === $model ? $file->filename() : $file->id();
return [
'filename' => $file->filename(),
'text' => $file->toString($this->text),
'link' => $file->panelUrl(true),
'id' => $file->id(),
'uuid' => $uuid,
'url' => $file->url(),
'info' => $file->toString($this->info ?? false),
'image' => $image,
'icon' => $file->panelIcon($image),
'type' => $file->type(),
];
}
],
'api' => function() {
return [
[
'pattern' => '/get/(:any)',
'action' => function ($type) {
if($type == "pages") {
$field = $this->field();
if (!$parent = $this->site()->find($this->requestQuery('parent'))) {
$parent = $this->site();
}
$pages = $parent->children();
$model = [
'id' => null,
'title' => t('link-field.page')
];
$children = [];
foreach ($pages as $index => $page) {
if ($page->isReadable() === true) {
$children[] = $field->pageResponse($page);
}
}
return [
'model' => $model,
'pages' => $children
];
}
if($type == "files") {
$field = $this->field();
$model = [
'id' => null,
'title' => t('link-field.file')
];
$files = $field->model()->query("page.files", 'Kirby\Cms\Files');
$data = [];
foreach ($files as $index => $file) {
$data[] = $field->fileResponse($file);
}
return $data;
}
}
]
];
}
]
],
'translations' => [
'en' => require_once __DIR__ . '/languages/en.php',
'de' => require_once __DIR__ . '/languages/de.php'
],
'fieldMethods' => [
'toHref' => function ($field) {
if($field->isNotEmpty()) {
$type = $field->yaml()["type"];
$link = $field->yaml()["link"];
$href = "";
if($type == "email"){
$href .= "mailto:";
}
if($type == "phone") {
$href .= "tel:";
}
if($type == "page" AND $linkPage = page($link)) {
$link = $linkPage->url();
}
if($type == "file" AND $linkFile = $field->parent()->file($link)) {
$link = $linkFile->url();
}
$href .= $link;
return $href;
}
}
]
]);