-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
122 lines (106 loc) · 2.71 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
<?php
namespace distantnative\CsvField;
use Kirby\Cms\App;
use Kirby\Content\Field;
load([
'distantnative\CsvField\Csv' => __DIR__ . '/Csv.php'
]);
App::plugin('distantnative/kirby-csv-field', [
'fields' => [
'csv' => [
'extends' => 'files',
'props' => [
/**
* Unset inherited props
*/
'default' => null,
'empty' => null,
'image' => null,
'info' => null,
'layout' => null,
'link' => null,
'max' => null,
'query' => null,
'search' => null,
'size' => null,
'text' => null,
'columns' => function (array $columns = null) {
return $columns;
},
'delimiter' => function (string $delimiter = ',') {
return $delimiter;
},
'limit' => function (int $limit = null) {
return $limit;
},
'multiple' => function (bool $multiple = null) {
return false;
},
'uploads' => function ($uploads = []) {
if ($uploads === false) {
return false;
}
if (is_string($uploads) === true) {
$uploads = ['template' => $uploads];
}
if (is_array($uploads) === false) {
$uploads = [];
}
$uploads['accept'] = '.csv';
return $uploads;
},
],
'api' => fn () => [
...(require $this->kirby()->core()->fields()['files'])['api'](),
[
'pattern' => 'preview',
'method' => 'GET',
'action' => function () {
$field = $this->field();
$file = $this->requestQuery('file');
// read file and turn into Csv collection
$csv = Csv::for(
$field->model()->file($file)->root(),
$field->delimiter()
);
// gather columns or use
// manually configured columns
$columns = [];
foreach ($field->columns() ?? $csv->columns() as $key => $column) {
$key = match (true) {
is_array($column) => $column['key'] ?? $key,
is_string($key) => $key,
default => $column
};
$columns[$key] = match (true) {
is_array($column) => $column,
default => ['label' => $column]
};
}
// paginate rows
if ($limit = $field->limit()) {
$csv = $csv->paginate([
'page' => $this->requestQuery('page', 1),
'limit' => $limit
]);
}
return [
'columns' => $columns,
'rows' => $csv->rows(),
'pagination' => $csv->pagination()?->toArray() ?? false
];
}
]
]
]
],
'fieldMethods' => [
'toCsv' => function (Field $field, string $delimiter = ','): Csv|null {
if ($file = $field->toFiles()->first()) {
return Csv::for($file->root(), $delimiter);
}
return null;
}
],
'translations' => require_once __DIR__ . '/i18n/index.php'
]);