-
-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathSubmission.php
More file actions
305 lines (256 loc) · 6.81 KB
/
Submission.php
File metadata and controls
305 lines (256 loc) · 6.81 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
<?php
namespace Statamic\Forms;
use Carbon\Carbon;
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\Forms\Submission as SubmissionContract;
use Statamic\Contracts\Query\ContainsQueryableValues;
use Statamic\Data\ContainsData;
use Statamic\Data\ExistsAsFile;
use Statamic\Data\HasAugmentedData;
use Statamic\Data\TracksQueriedColumns;
use Statamic\Data\TracksQueriedRelations;
use Statamic\Events\SubmissionCreated;
use Statamic\Events\SubmissionCreating;
use Statamic\Events\SubmissionDeleted;
use Statamic\Events\SubmissionSaved;
use Statamic\Events\SubmissionSaving;
use Statamic\Facades\Asset;
use Statamic\Facades\File;
use Statamic\Facades\FormSubmission;
use Statamic\Facades\Stache;
use Statamic\Forms\Uploaders\AssetsUploader;
use Statamic\Forms\Uploaders\FilesUploader;
use Statamic\Support\Str;
use Statamic\Support\Traits\FluentlyGetsAndSets;
class Submission implements Augmentable, ContainsQueryableValues, SubmissionContract
{
use ContainsData, ExistsAsFile, FluentlyGetsAndSets, HasAugmentedData, TracksQueriedColumns, TracksQueriedRelations;
/**
* @var string
*/
private $id;
/**
* @var Form
*/
public $form;
protected $afterSaveCallbacks = [];
protected $withEvents = true;
protected ?string $redirect = null;
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 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 \Illuminate\Support\Collection<string, array>
*/
public function fields()
{
return $this->form()->fields()->map->toArray();
}
/**
* Get the columns.
*
* @return \Statamic\CP\Columns
*/
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(), config('app.timezone'));
}
/**
* Get the date, formatted by what's specified in the form config.
*
* @return string
*/
public function formattedDate()
{
return $this->date()->format(
$this->form()->dateFormat()
);
}
/**
* Upload files and return asset IDs.
*
* @param array $uploadedFiles
* @return array
*/
public function uploadFiles($uploadedFiles)
{
return collect($uploadedFiles)->map(function ($files, $handle) {
$field = $this->fields()->get($handle);
return $field['type'] === 'files'
? FilesUploader::field($field)->upload($files)
: AssetsUploader::field($field)->upload($files);
})->all();
}
public function afterSave($callback)
{
$this->afterSaveCallbacks[] = $callback;
return $this;
}
public function saveQuietly()
{
$this->withEvents = false;
return $this->save();
}
/**
* Save the submission.
*/
public function save()
{
$isNew = is_null($this->form()->submission($this->id()));
$withEvents = $this->withEvents;
$this->withEvents = true;
$afterSaveCallbacks = $this->afterSaveCallbacks;
$this->afterSaveCallbacks = [];
if ($withEvents) {
if ($isNew && SubmissionCreating::dispatch($this) === false) {
return false;
}
if (SubmissionSaving::dispatch($this) === false) {
return false;
}
}
FormSubmission::save($this);
foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}
if ($withEvents) {
if ($isNew) {
SubmissionCreated::dispatch($this);
}
SubmissionSaved::dispatch($this);
}
}
public function deleteQuietly()
{
$this->withEvents = false;
return $this->delete();
}
/**
* Delete this submission.
*/
public function delete()
{
$withEvents = $this->withEvents;
$this->withEvents = true;
FormSubmission::delete($this);
if ($withEvents) {
SubmissionDeleted::dispatch($this);
}
return true;
}
/**
* Get the path to the file.
*
* @return string
*/
public function getPath()
{
return $this->path();
}
public function path()
{
return vsprintf('%s/%s/%s.yaml', [
rtrim(Stache::store('form-submissions')->directory(), '/'),
$this->form()->handle(),
$this->id(),
]);
}
/**
* 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();
}
public function fileData()
{
return $this->data()->all();
}
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 [
'blueprint', 'date', 'form', 'formattedDate', 'id', 'path',
];
}
public function __get($key)
{
return $this->get($key);
}
}