-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubmissionHelper.php
110 lines (89 loc) · 2.97 KB
/
SubmissionHelper.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
<?php
namespace SchantlDev\Statamic\FormAttach\Forms;
use Illuminate\Support\Collection;
use Statamic\Facades\Antlers;
use Statamic\Facades\Config;
use Statamic\Facades\GlobalSet;
use Statamic\Fields\Blueprint;
use Statamic\Forms\Form;
use Statamic\Forms\Submission;
use Statamic\Sites\Site;
use Statamic\Support\Arr;
class SubmissionHelper
{
public ?Blueprint $blueprint;
public ?Form $form;
public Collection $data;
public function __construct(
public Submission $submission,
public Site $site,
public array $config,
array $data = []
) {
$this->form = $this->submission->form();
$this->blueprint = $this->form->blueprint();
$this->data = collect($data);
}
public function put(string $key, mixed $value): void
{
$this->data->put($key, $value);
}
public function get(string $key): mixed
{
return $this->data->get($key);
}
public function data(): array
{
$augmented = $this->submission->toAugmentedArray();
$fields = $this->getRenderableFieldData(Arr::except($augmented, ['id', 'date', 'form']));
if (Arr::has($this->config, 'attachments')) {
$fields = $fields->reject(fn ($field) => in_array($field['fieldtype'], ['assets', 'files']));
}
$data = array_merge($augmented, $this->getGlobalsData(), [
'config' => config()->all(),
'fields' => $fields,
'site_url' => Config::getSiteUrl(),
'date' => now(),
'now' => now(),
'today' => now(),
'site' => $this->site->handle(),
'locale' => $this->site->handle(),
]);
return array_merge($data, $this->data->toArray());
}
private function getRenderableFieldData($values)
{
return collect($values)->map(function ($value, $handle) {
$field = $value->field();
$display = $field->display();
$fieldtype = $field->type();
$config = $field->config();
return compact('display', 'handle', 'fieldtype', 'config', 'value');
})->values();
}
private function getGlobalsData()
{
$data = [];
foreach (GlobalSet::all() as $global) {
if (! $global->existsIn($this->site->handle())) {
continue;
}
$global = $global->in($this->site->handle());
$data[$global->handle()] = $global->toAugmentedArray();
}
return $data;
}
public function blueprintFields(): Collection
{
return $this->blueprint->fields()->items()->mapWithKeys(function ($field) {
return [$field['handle'] => [
'display' => $field['field']['display'] ?? 'Undefined name',
'width' => $field['field']['width'] ?? 100,
]];
});
}
public function subject(): string
{
return Antlers::parse(__($this->config['subject'] ?? 'Attachment'), $this->data());
}
}