-
Notifications
You must be signed in to change notification settings - Fork 2
/
Plugin.php
executable file
·262 lines (232 loc) · 9.86 KB
/
Plugin.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
namespace OFFLINE\Forms;
use Backend\Classes\NavigationManager;
use Backend\Facades\Backend;
use Illuminate\Support\Facades\Event;
use OFFLINE\Forms\Classes\Contexts;
use OFFLINE\Forms\Classes\Events;
use OFFLINE\Forms\Models\Form;
use OFFLINE\Forms\Models\Submission;
use System\Classes\PluginBase;
use System\Models\File;
class Plugin extends PluginBase
{
public function boot()
{
// Add a submenu item for each Form in the backend menu.
Event::listen('backend.menu.extendItems', function (NavigationManager $navigationManager) {
$forms = Form::where('is_archived', false)->get();
// Change the URL of the main menu item to the first form.
$item = $navigationManager->getMainMenuItem('OFFLINE.Forms', 'offline-forms-main-menu');
if ($item && $firstForm = $forms->first()) {
$item->url = Backend::url('offline/forms/forms/submissions/' . $firstForm->id);
}
$forms->each(function (Form $form) use ($navigationManager) {
$navigationManager->addSideMenuItems('OFFLINE.Forms', 'offline-forms-main-menu', [
'submissions-' . $form->id => [
'label' => $form->name,
'icon' => 'icon-list',
'permissions' => ['offline.forms::can_see_forms'],
'url' => Backend::url('offline/forms/submissions/index/' . $form->id),
],
]);
});
});
// Add fields to the submission form.
Event::listen('backend.form.extendFields', function (\Backend\Widgets\Form $widget) {
if (!$widget->model instanceof Submission) {
return;
}
// Allow other plugins to modify the fields.
Event::fire(Events::FORM_EXTEND, [$widget->model->form, Contexts::FIELDS, $widget]);
collect($widget->model->form->fields)->each(function (array $field) use ($widget) {
$args = [];
switch ($field['_field_type'] ?? '') {
case 'section':
$args['type'] = 'section';
$args['span'] = 'full';
$args['comment'] = $field['text'];
break;
case 'fileupload':
$allowedExtensions = array_get($field, 'allowed_extensions');
$args['type'] = 'fileupload';
$args['mode'] = array_intersect(explode(',', $allowedExtensions), ['jpg', 'jpeg', 'png', 'gif']) ? 'image' : 'file';
$args['fileTypes'] = $allowedExtensions;
break;
case 'textarea':
$args['type'] = 'textarea';
$args['size'] = 'small';
break;
case 'checkboxlist':
$args['type'] = 'repeater';
$args['showDuplicate'] = false;
$args['showReorder'] = false;
$args['itemsExpanded'] = false;
$args['form'] = [
'fields' => [
'value' => [
'span' => 'full',
],
],
];
break;
default:
$type = array_get($field, 'type', 'text');
switch ($type) {
case 'date':
$args['type'] = 'datepicker';
$args['mode'] = 'date';
break;
case 'time':
$args['type'] = 'datepicker';
$args['mode'] = 'time';
break;
default:
$args['type'] = $type ?? 'text';
break;
}
break;
}
$widget->addTabFields([
$field['name'] => [
'label' => $field['label'] ?? $field['name'],
'span' => 'auto',
'disabled' => $field['disabled'] ?? false,
'tab' => 'offline.forms::lang.fields',
...$args,
],
]);
});
});
// Add columns to the submission list.
Event::listen('backend.list.extendColumns', function (\Backend\Widgets\Lists $widget) {
if (!$widget->model instanceof Submission) {
return;
}
$form = $widget->getController()->getFormModel();
if (!$form) {
return;
}
// Allow other plugins to modify the fields.
Event::fire(Events::FORM_EXTEND, [$form, Contexts::COLUMNS, $widget]);
collect($form->fields)->each(function (array $field) use ($widget) {
$args = [];
switch ($field['_field_type'] ?? '') {
case 'section':
return;
case 'fileupload':
$args['type'] = 'partial';
$args['path'] = '$/offline/forms/controllers/submissions/_fileupload_column.php';
$args['clickable'] = false;
break;
case 'checkboxlist':
$args['type'] = 'partial';
$args['path'] = '$/offline/forms/controllers/submissions/_checkboxlist_column.php';
break;
default:
$type = array_get($field, 'type', 'text');
$args['type'] = match ($type) {
'date' => 'date',
'time' => 'time',
default => 'text',
};
break;
}
$widget->addColumns([
$field['name'] => [
'label' => $field['label'] ?? $field['name'],
'sortable' => false,
'_searchable' => true,
...$args,
],
]);
// Add the default columns to the end.
$widget->removeColumn('created_at');
$widget->addColumns([
'ip_hash' => [
'label' => 'offline.forms::lang.ip_hash',
'type' => 'string',
'invisible' => true,
],
'port' => [
'label' => 'offline.forms::lang.port',
'type' => 'string',
'invisible' => true,
],
'created_at' => [
'label' => 'offline.forms::lang.created_at',
'type' => 'date',
],
'updated_at' => [
'label' => 'offline.forms::lang.updated_at',
'type' => 'date',
'invisible' => 'true',
],
'deleted_at' => [
'label' => 'offline.forms::lang.deleted_at',
'type' => 'date',
'invisible' => 'true',
],
]);
});
});
// Override the export column value for file uploads.
Event::listen('backend.list.overrideColumnValueRaw', function ($listWidget, $record, $column, &$value) {
if (!$listWidget->model instanceof Submission) {
return;
}
if ($listWidget->getController()->getAction() !== 'export') {
return;
}
// Transform the upload to a URL.
if (array_get($column->config, 'path') === '$/offline/forms/controllers/submissions/_fileupload_column.php') {
$value = $value->implode(fn (File $file) => "{$file->getPath()} ({$file->getFilename()})", ',');
}
// Transform checkbox list array values to a string.
if (array_get($column->config, 'path') === '$/offline/forms/controllers/submissions/_checkboxlist_column.php') {
if (is_array($value)) {
$value = collect($value)->pluck('value')->implode(', ');
} else {
$value = '';
}
}
});
// Hide the Captcha Option if the Laravel Captcha plugin is not installed.
Event::listen('backend.form.extendFields', function (\Backend\Widgets\Form $form) {
if (!$form->model instanceof Form || class_exists(\Mews\Captcha\CaptchaServiceProvider::class)) {
return;
}
$form->removeField('spam_use_captcha');
});
// Set sensible defaults for the Laravel Captcha plugin.
if (class_exists(\Mews\Captcha\CaptchaServiceProvider::class)) {
config()->set('captcha.default', [
'length' => 5,
'width' => 180,
'height' => 60,
'quality' => 90,
]);
}
}
/**
* registerComponents used by the frontend.
*/
public function registerComponents()
{
return [
Components\RenderForm::class => 'renderForm',
];
}
/**
* registerSettings used by the backend.
*/
public function registerSettings()
{
}
public function registerMailTemplates()
{
return [
'offline.forms::mail.submission',
];
}
}