/
Submission.php
238 lines (208 loc) · 4.94 KB
/
Submission.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
<?php
namespace Statamic\Forms;
use Carbon\Carbon;
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\Forms\Submission as SubmissionContract;
use Statamic\Data\ContainsData;
use Statamic\Data\HasAugmentedData;
use Statamic\Events\SubmissionDeleted;
use Statamic\Events\SubmissionSaved;
use Statamic\Facades\File;
use Statamic\Facades\YAML;
use Statamic\Forms\Uploaders\AssetsUploader;
use Statamic\Support\Arr;
use Statamic\Support\Traits\FluentlyGetsAndSets;
class Submission implements SubmissionContract, Augmentable
{
use ContainsData, FluentlyGetsAndSets, HasAugmentedData;
/**
* @var string
*/
private $id;
/**
* @var Form
*/
public $form;
public function __construct()
{
$this->data = collect();
$this->supplements = collect();
}
/**
* Get or set the ID.
*
* @param mixed|null
* @return mixed
*/
public function id($id = null)
{
return $this->fluentlyGetOrSet('id')
->getter(function ($id) {
return $this->id = $id ?: str_replace(',', '.', microtime(true));
})
->args(func_get_args());
}
/**
* Get or set the form.
*
* @param Form|null $form
* @return Form
*/
public function form($form = null)
{
return $this->fluentlyGetOrSet('form')->args(func_get_args());
}
/**
* Get the form fields.
*
* @return array
*/
public function fields()
{
return $this->form()->fields()->map->toArray();
}
/**
* Get or set the columns.
*
* @return array
*/
public function columns()
{
return $this->form()->blueprint()->columns();
}
/**
* Get the date when this was submitted.
*
* @return Carbon
*/
public function date()
{
return Carbon::createFromTimestamp($this->id());
}
/**
* Get the date, formatted by what's specified in the form config.
*
* @return string
*/
public function formattedDate()
{
return $this->date()->format(
$this->form()->dateFormat()
);
}
/**
* Get or set the data.
*
* @param array|null $data
* @return array
*/
public function data($data = null)
{
if (func_num_args() === 0) {
return $this->data;
}
$data = collect($data)->intersectByKeys($this->fields())->all();
$this->data = $data;
return $this;
}
/**
* Upload files.
*/
public function uploadFiles()
{
collect($this->fields())
->filter(function ($config, $handle) {
return Arr::get($config, 'type') === 'assets' && request()->hasFile($handle);
})
->each(function ($config, $handle) {
Arr::set($this->data, $handle, AssetsUploader::field($config)->upload(request()->file($handle)));
});
return $this;
}
/**
* Whether the submissin has the given key.
*
* @return bool
*/
public function has($field)
{
return array_has($this->data(), $field);
}
/**
* Get a value of a field.
*
* @param string $key
* @return mixed
*/
public function get($field)
{
return array_get($this->data(), $field);
}
/**
* Set a value of a field.
*
* @param string $field
* @param mixed $value
* @return void
*/
public function set($field, $value)
{
array_set($this->data, $field, $value);
}
/**
* Save the submission.
*/
public function save()
{
File::put($this->getPath(), YAML::dump($this->data()));
SubmissionSaved::dispatch($this);
}
/**
* Delete this submission.
*/
public function delete()
{
File::delete($this->getPath());
SubmissionDeleted::dispatch($this);
}
/**
* Get the path to the file.
*
* @return string
*/
public function getPath()
{
return config('statamic.forms.submissions').'/'.$this->form()->handle().'/'.$this->id().'.yaml';
}
/**
* Convert to an array.
*
* @return array
*/
public function toArray()
{
$data = $this->data();
return $this->form()->fields()->keys()->flip()
->reject(function ($field, $key) {
return in_array($key, ['id', 'date', 'form']);
})
->map(function ($field, $key) use ($data) {
return $data[$key] ?? null;
})
->merge([
'id' => $this->id(),
'date' => $this->date(),
])
->all();
}
public function augmentedArrayData()
{
return array_merge($this->toArray(), [
'form' => $this->form,
]);
}
public function blueprint()
{
return $this->form->blueprint();
}
}