-
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathForm.php
More file actions
472 lines (395 loc) · 11.1 KB
/
Form.php
File metadata and controls
472 lines (395 loc) · 11.1 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<?php
namespace Statamic\Forms;
use Illuminate\Contracts\Support\Arrayable;
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\Data\Augmented;
use Statamic\Contracts\Forms\Form as FormContract;
use Statamic\Contracts\Forms\Submission;
use Statamic\Contracts\Forms\SubmissionQueryBuilder;
use Statamic\Contracts\Query\ContainsQueryableValues;
use Statamic\Data\ContainsData;
use Statamic\Data\HasAugmentedInstance;
use Statamic\Events\FormBlueprintFound;
use Statamic\Events\FormCreated;
use Statamic\Events\FormCreating;
use Statamic\Events\FormDeleted;
use Statamic\Events\FormDeleting;
use Statamic\Events\FormSaved;
use Statamic\Events\FormSaving;
use Statamic\Facades\Blueprint;
use Statamic\Facades\File;
use Statamic\Facades\Form as FormFacade;
use Statamic\Facades\FormSubmission;
use Statamic\Facades\YAML;
use Statamic\Forms\Exceptions\BlueprintUndefinedException;
use Statamic\Forms\Exporters\Exporter;
use Statamic\Statamic;
use Statamic\Support\Arr;
use Statamic\Support\Str;
use Statamic\Support\Traits\FluentlyGetsAndSets;
class Form implements Arrayable, Augmentable, ContainsQueryableValues, FormContract
{
use ContainsData, FluentlyGetsAndSets, HasAugmentedInstance;
protected $handle;
protected $title;
protected $blueprint;
protected $honeypot;
protected $store;
protected $email;
protected $metrics;
protected $afterSaveCallbacks = [];
protected $withEvents = true;
public function __construct()
{
$this->data = collect();
$this->supplements = collect();
}
public function __clone()
{
$this->data = clone $this->data;
$this->supplements = clone $this->supplements;
}
/**
* Get or set the handle.
*
* @param mixed $handle
* @return mixed
*/
public function handle($handle = null)
{
return $this->fluentlyGetOrSet('handle')->args(func_get_args());
}
/**
* Get or set the title.
*
* @param mixed $title
* @return mixed
*/
public function title($title = null)
{
return $this->fluentlyGetOrSet('title')->args(func_get_args());
}
/**
* Get the blueprint.
*
* @return mixed
*/
public function blueprint()
{
$blueprint = Blueprint::find('forms.'.$this->handle())
?? Blueprint::makeFromFields([])->setHandle($this->handle())->setNamespace('forms');
FormBlueprintFound::dispatch($blueprint, $this);
return $blueprint;
}
/**
* Get or set the honeypot field.
*
* @param mixed $honeypot
* @return mixed
*/
public function honeypot($honeypot = null)
{
return $this->fluentlyGetOrSet('honeypot')
->getter(function ($honeypot) {
return $honeypot ?? 'honeypot';
})
->setter(function ($honeypot) {
return $honeypot === 'honeypot' ? null : $honeypot;
})
->args(func_get_args());
}
/**
* Get or set the store field.
*
* @param mixed $store
* @return mixed
*/
public function store($store = null)
{
return $this->fluentlyGetOrSet('store')
->getter(function ($store) {
return $store !== false;
})
->setter(function ($store) {
return $store === false ? false : null;
})
->args(func_get_args());
}
/**
* Get or set the email field.
*
* @param mixed $emails
* @return mixed
*/
public function email($emails = null)
{
return $this->fluentlyGetOrSet('email')->args(func_get_args());
}
/**
* Get the form fields off the blueprint.
*
* @return \Illuminate\Support\Collection
*/
public function fields()
{
if (! $blueprint = $this->blueprint()) {
throw BlueprintUndefinedException::create($this);
}
return $blueprint->fields()->all();
}
/**
* Get path.
*
* @return string
*/
public function path()
{
return config('statamic.forms.forms')."/{$this->handle()}.yaml";
}
public function afterSave($callback)
{
$this->afterSaveCallbacks[] = $callback;
return $this;
}
public function saveQuietly()
{
$this->withEvents = false;
return $this->save();
}
/**
* Save form.
*/
public function save()
{
$isNew = is_null(FormFacade::find($this->handle()));
$withEvents = $this->withEvents;
$this->withEvents = true;
$afterSaveCallbacks = $this->afterSaveCallbacks;
$this->afterSaveCallbacks = [];
if ($withEvents) {
if ($isNew && FormCreating::dispatch($this) === false) {
return false;
}
if (FormSaving::dispatch($this) === false) {
return false;
}
}
$data = $this->data->merge(collect([
'title' => $this->title,
'honeypot' => $this->honeypot,
'email' => collect(isset($this->email['to']) ? [$this->email] : $this->email)->map(function ($email) {
$email['markdown'] = Arr::get($email, 'markdown') === true ? true : null;
$email['attachments'] = Arr::get($email, 'attachments') === true ? true : null;
return Arr::removeNullValues($email);
})->all(),
'metrics' => $this->metrics,
]))->filter()->all();
if ($this->store === false) {
$data['store'] = false;
}
File::put($this->path(), YAML::dump($data));
foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}
if ($withEvents) {
if ($isNew) {
FormCreated::dispatch($this);
}
FormSaved::dispatch($this);
}
}
public function deleteQuietly()
{
$this->withEvents = false;
return $this->delete();
}
/**
* Delete form and associated submissions.
*/
public function delete()
{
$withEvents = $this->withEvents;
$this->withEvents = true;
if ($withEvents && FormDeleting::dispatch($this) === false) {
return false;
}
$this->submissions()->each->delete();
File::delete($this->path());
if ($withEvents) {
FormDeleted::dispatch($this);
}
return true;
}
/**
* Hydrate form from file data.
*
* @return $this
*/
public function hydrate()
{
$contents = YAML::parse(File::get($this->path()));
$methods = [
'title',
'honeypot',
'store',
'email',
];
$this->merge(collect($contents)->except($methods));
collect($contents)
->filter(function ($value, $property) use ($methods) {
return in_array($property, $methods);
})
->each(function ($value, $property) {
$this->{$property}($value);
});
return $this;
}
// TODO: Reimplement metrics()
public function metrics($metrics = null)
{
return collect();
// if (! is_null($metrics)) {
// return $this->formset()->set('metrics', $metrics);
// }
// $metrics = [];
// foreach ($this->formset()->get('metrics', []) as $config) {
// $name = Str::studly($config['type']);
// $class = "Statamic\\Forms\\Metrics\\{$name}Metric";
// if (! class_exists($class)) {
// $class = "Statamic\\Addons\\{$name}\\{$name}Metric";
// }
// if (! class_exists($class)) {
// \Log::error("Metric [{$config['type']}] does not exist.");
// continue;
// }
// $metrics[] = new $class($this, $config);
// }
// return $metrics;
}
/**
* Get the submissions.
*
* @return \Illuminate\Support\Collection
*/
public function submissions()
{
return FormSubmission::whereForm($this->handle());
}
public function querySubmissions(): SubmissionQueryBuilder
{
return FormSubmission::query()->where('form', $this->handle());
}
/**
* Get a submission.
*
* @param string $id
* @return Submission
*/
public function submission($id)
{
return FormSubmission::find($id);
}
/**
* Make a form submission.
*
* @return Submission
*/
public function makeSubmission()
{
$submission = FormSubmission::make();
$submission->form($this);
return $submission;
}
/**
* The URL to view form submissions in the CP.
*
* @return string
*/
public function showUrl()
{
return cp_route('forms.show', $this->handle());
}
/**
* The URL to edit this in the CP.
*
* @return string
*/
public function editUrl()
{
return cp_route('forms.edit', $this->handle());
}
/**
* The URL to delete this in the CP.
*
* @return string
*/
public function deleteUrl()
{
return cp_route('forms.destroy', $this->handle());
}
/**
* Get the date format.
*
* @return string
*/
public function dateFormat()
{
return Statamic::isCpRoute() ? Statamic::cpDateTimeFormat() : Statamic::dateTimeFormat();
}
public function hasFiles()
{
return $this->fields()->filter(function ($field) {
return in_array($field->fieldtype()->handle(), ['assets', 'files']);
})->isNotEmpty();
}
public function newAugmentedInstance(): Augmented
{
return new AugmentedForm($this);
}
public function shallowAugmentedArrayKeys()
{
return ['handle', 'title', 'api_url'];
}
public function apiUrl()
{
return Statamic::apiRoute('forms.show', $this->handle());
}
/**
* Get the form action url.
*
* @return string
*/
public function actionUrl()
{
return route('statamic.forms.submit', $this->handle());
}
public function exporters()
{
return FormFacade::exporters()
->all()
->filter->allowedOnForm($this)
->each->setForm($this);
}
public function exporter(string $handle): ?Exporter
{
return $this->exporters()->get($handle);
}
public function getQueryableValue(string $field)
{
if (in_array($method = Str::camel($field), $this->queryableMethods())) {
return $this->{$method}();
}
$value = $this->get($field);
if (! $field = $this->blueprint()->field($field)) {
return $value;
}
return $field->fieldtype()->toQueryableValue($value);
}
private function queryableMethods(): array
{
return [
'actionUrl', 'apiUrl', 'blueprint', 'dateFormat', 'editUrl', 'fields',
'handle', 'honeypot', 'path', 'submissions', 'title',
];
}
}