-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathForm.php
More file actions
82 lines (65 loc) · 2.06 KB
/
Form.php
File metadata and controls
82 lines (65 loc) · 2.06 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
<?php
namespace Statamic\Eloquent\Forms;
use Illuminate\Database\Eloquent\Model;
use Statamic\Contracts\Forms\Form as Contract;
use Statamic\Events\FormDeleted;
use Statamic\Events\FormSaved;
use Statamic\Facades\Blink;
use Statamic\Forms\Form as FileEntry;
class Form extends FileEntry
{
protected $model;
public static function fromModel(Model $model)
{
return (new static)
->title($model->title)
->handle($model->handle)
->store($model->settings['store'] ?? null)
->email($model->settings['email'] ?? null)
->honeypot($model->settings['honeypot'] ?? null)
->data($model->settings['data'] ?? [])
->model($model);
}
public function toModel()
{
return self::makeModelFromContract($this);
}
public static function makeModelFromContract(Contract $source)
{
$class = app('statamic.eloquent.forms.model');
return $class::firstOrNew(['handle' => $source->handle()])->fill([
'title' => $source->title() ?? $source->handle(),
'settings' => [
'store' => $source->store(),
'email' => $source->email(),
'honeypot' => $source->honeypot(),
'data' => $source->data()->filter(fn ($v) => $v !== null),
],
]);
}
public function model($model = null)
{
if (func_num_args() === 0) {
return $this->model;
}
$this->model = $model;
return $this;
}
public function save()
{
$model = $this->toModel();
$model->save();
$this->model($model->fresh());
Blink::forget("eloquent-forms-{$this->handle()}");
Blink::forget('eloquent-forms');
FormSaved::dispatch($this);
}
public function delete()
{
$this->submissions()->each->delete();
$this->model()->delete();
Blink::forget("eloquent-forms-{$this->handle()}");
Blink::forget('eloquent-forms');
FormDeleted::dispatch($this);
}
}