-
Notifications
You must be signed in to change notification settings - Fork 5
/
imageradio-options.php
109 lines (100 loc) · 3.48 KB
/
imageradio-options.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
<?php
use Kirby\Form\Options;
class ImageRadioOptions extends Options {
public static function api($api, $model = null): array {
$model = $model ?? App::instance()->site();
$fetch = null;
$text = null;
$value = null;
$image = null;
if (is_array($api) === true) {
$fetch = $api['fetch'] ?? null;
$text = $api['text'] ?? null;
$value = $api['value'] ?? null;
$image = $api['image'] ?? null;
$url = $api['url'] ?? null;
} else {
$url = $api;
}
$optionsApi = new OptionsApi([
'data' => static::data($model),
'fetch' => $fetch,
'url' => $url,
'text' => $text,
'value' => $value,
'image' => $image
]);
return $optionsApi->options();
}
public static function factory($options, array $props = [], $model = null): array {
switch ($options) {
case 'api':
$options = static::api($props['api']);
break;
case 'query':
$options = static::query($props['query'], $model);
break;
case 'children':
case 'grandChildren':
case 'siblings':
case 'index':
case 'files':
case 'images':
case 'documents':
case 'videos':
case 'audio':
case 'code':
case 'archives':
$options = static::query('page.' . $options, $model);
break;
case 'pages':
$options = static::query('site.index', $model);
break;
}
if (is_array($options) === false) {
return [];
}
$assets = kirby()->url('assets') . '/images';
$result = [];
foreach ($options as $key => $option) {
if (is_array($option) === false || isset($option['value']) === false) {
$option = [
'value' => is_int($key) ? $option : $key,
'text' => $option['text'],
'image' => $assets . '/' . $option['image'],
];
}
elseif (is_array($option) === false || isset($option['value']) === true) {
$option = [
'value' => $option['value'],
'text' => $option['text'],
'image' => $option['image'],
];
}
// translate the option text
$option['text'] = I18n::translate($option['text'], $option['text']);
// add the option to the list
$result[] = $option;
}
return $result;
}
public static function query($query, $model = null): array {
$model = $model ?? App::instance()->site();
// resolve array query setup
if (is_array($query) === true) {
$text = $query['label'] ?? '{{ file.filename }}';
$value = $query['value'] ?? '{{ file.id }}';
$image = $query['image'] ?? '{{ file.url }}';
$query = $query['fetch'] ?? null;
}
$optionsQuery = new ImageRadioOptionsQuery([
'aliases' => static::aliases(),
'data' => static::data($model),
'query' => $query,
'text' => $text,
'value' => $value,
'image' => $image,
]);
return $optionsQuery->options();
}
}