-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
160 lines (152 loc) · 7.33 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
require_once __DIR__ . '/lib/functions.php';
Kirby::plugin('rd/data-importer', [
'options' => [
/* Default import mode: String / Value: »update« or »skip« */
/* If options field is hidden or value is unset */
'default_import_mode' => 'skip',
/* Field separator in CSV file: String */
/* Image name separator in CSV file is »,« (without space) */
/* Do NOT use »,« here! */
'delimiter' => ";"
],
'fields' => [
'import_data' => [
'mixins' => ['filepicker'],
'props' => [
'button_headline' => function(string $button_headline = null) {
if($button_headline == null) {
$default_button_headline = ["de"=>"Daten importieren", "en"=>"Data import"];
return I18n::translate($default_button_headline);
}
return I18n::translate($button_headline, $button_headline);
},
'button_label' => function(string $button_label = null) {
if($button_label == NULL) {
$default_button_label = ["de"=>"Datei auswählen...", "en"=>"Select data file ..."];
return I18n::translate($default_button_label);
}
return I18n::translate($button_label, $button_label);
},
'button_help' => function(string $button_help = null) {
if($button_help == NULL) {
return '';
}
return I18n::translate($button_help, $button_help);
},
'options_headline' => function(string $options_headline = null) {
if($options_headline == null) {
$default_options_headline = ["de"=>"Optionen", "en"=>"Options"];
return I18n::translate($default_options_headline);
}
return I18n::translate($options_headline, $options_headline);
},
'options_help' => function(string $options_help = null) {
if($options_help == null) {
return '';
}
return I18n::translate($options_help, $options_help);
},
'subpage_section' => function(string $subpage_section = '') {
return $subpage_section;
},
'subpage_template' => function(string $subpage_template = 'default') {
return $subpage_template;
},
'subpage_status' => function(string $subpage_status = 'draft') {
return $subpage_status;
},
'title_key_array' => function(array $title_key_array) {
return $title_key_array;
},
'image_page_slug' => function(string $image_page_slug = '') {
return $image_page_slug;
},
'image_field_name' => function(string $image_field_name = '') {
return $image_field_name;
},
'options_disabled' => function(bool $options_disabled = false) {
return $options_disabled;
},
'default_import_mode' => function(string $default_import_mode = null) {
if($default_import_mode == null) {
$default_import_mode = option('rd.data-importer.default_import_mode');
}
if($default_import_mode === 'update' || $default_import_mode === 'skip') {
return $default_import_mode;
} else {
throw new Exception('This option mode is not allowed: ' . $default_import_mode . '. Allowed values: »update« or »skip«');
}
}
],
'computed' => [
'options_text' => function() {
$option_update_text = I18n::translate(["de"=>"Aktualisieren", "en"=>"Update"]);
$option_skip_text = I18n::translate(["de"=>"Vorhandene überspringen", "en"=>"Skip existing records"]);
$data = '[
{ "value": "update", "text": "' . $option_update_text . '" },
{ "value": "skip", "text": "' . $option_skip_text . '" }
]';
return json_decode($data);
}
],
/* Field API */
'api' => function () {
return [
[
'pattern' => '/get/files',
'method' => 'GET',
'action' => function() {
return $this->field()->filepicker([
'query' => 'page.files',
]);
}
],
[
'pattern' => '/import/data',
'method' => 'POST',
'action' => function() {
$file_url = get('url');
$page_name = get('page');
$template = get('template');
$status = get('status');
$title_key_array = get('title_key_array');
$image_page_slug = get('image_page_slug');
$image_field_name = get('image_field_name');
$update = get('update');
/* Get parent page */
try {
$parent_page = $this->page($page_name);
} catch(Exception $error) {
throw new Exception("Error: " . $error->getMessage());
return ['data' => []];
}
/* Read file */
$delimiter = option('rd.data-importer.delimiter');
$data_array = __get_data($file_url, $delimiter);
if(empty($data_array)) {
throw new Exception("Error: Selected file contains no data.");
return ['data' => []];
}
/* Create subpages */
$create_pages_result = __create_pages(
$parent_page,
$data_array,
$update,
$template,
$status,
$title_key_array,
$image_page_slug,
$image_field_name
);
if($create_pages_result == null) {
return ['data' => []];
}
return ['data' => $data_array];
}
],
];
}
]
]
]);