|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Thinktomorrow\Chief\Fields\Types\Field; |
|
8
|
|
|
use Thinktomorrow\Chief\Fields\Types\FieldType; |
|
9
|
|
|
|
|
10
|
|
|
trait SavingFields |
|
11
|
|
|
{ |
|
12
|
|
|
// If there is a save<key>Field this has priority over the set<Key>Field methods |
|
13
|
|
|
protected $saveAssistantMethods = []; |
|
14
|
|
|
protected $saveMethods = []; |
|
15
|
|
|
|
|
16
|
|
|
protected $queued_translations = []; |
|
17
|
|
|
|
|
18
|
84 |
|
public function saveFields(Request $request) |
|
19
|
|
|
{ |
|
20
|
84 |
|
foreach ($this->fieldsWithAssistantFields() as $field) { |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
// Custom save methods |
|
23
|
84 |
|
if ($this->detectCustomSaveMethods($field)) { |
|
24
|
68 |
|
continue; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// Media fields are treated separately |
|
28
|
84 |
|
if ($field->ofType(FieldType::MEDIA, FieldType::DOCUMENT)) { |
|
29
|
76 |
|
if (! isset($this->saveMethods['_files'])) { |
|
30
|
76 |
|
$this->saveMethods['_files'] = ['field' => new Fields([$field]), 'method' => 'uploadMedia']; |
|
31
|
76 |
|
continue; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
30 |
|
$this->saveMethods['_files']['field'][] = $field; |
|
35
|
30 |
|
continue; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Custom set methods - default is the generic setField() method. |
|
39
|
77 |
|
$methodName = 'set'. ucfirst(Str::camel($field->key())) . 'Field'; |
|
40
|
77 |
|
(method_exists($this, $methodName)) |
|
41
|
31 |
|
? $this->$methodName($field, $request) |
|
42
|
77 |
|
: $this->setField($field, $request); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// Save the model |
|
46
|
84 |
|
$this->saveQueuedFields(); |
|
47
|
|
|
|
|
48
|
84 |
|
$this->saveQueuedMethods($request); |
|
49
|
|
|
|
|
50
|
|
|
// Attach the updated model to our manager. |
|
51
|
84 |
|
$this->manage($this->model); |
|
|
|
|
|
|
52
|
84 |
|
} |
|
53
|
|
|
|
|
54
|
84 |
|
protected function detectCustomSaveMethods(Field $field): bool |
|
55
|
|
|
{ |
|
56
|
84 |
|
$saveMethodName = 'save'. ucfirst(Str::camel($field->key())) . 'Field'; |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
// Custom save method on assistant |
|
59
|
84 |
|
foreach ($this->assistants() as $assistant) { |
|
|
|
|
|
|
60
|
68 |
|
if (method_exists($assistant, $saveMethodName)) { |
|
61
|
68 |
|
$this->saveAssistantMethods[$field->key()] = ['field' => $field, 'method' => $saveMethodName, 'assistant' => $assistant]; |
|
62
|
68 |
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Custom save method on manager class |
|
67
|
84 |
|
if (method_exists($this, $saveMethodName)) { |
|
68
|
41 |
|
$this->saveMethods[$field->key()] = ['field' => $field, 'method' => $saveMethodName]; |
|
69
|
41 |
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
84 |
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
84 |
|
private function saveQueuedMethods($request) |
|
76
|
|
|
{ |
|
77
|
84 |
|
foreach ($this->saveAssistantMethods as $data) { |
|
78
|
68 |
|
$method = $data['method']; |
|
79
|
68 |
|
$data['assistant']->$method($data['field'], $request); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
84 |
|
foreach ($this->saveMethods as $data) { |
|
83
|
78 |
|
$method = $data['method']; |
|
84
|
78 |
|
$this->$method($data['field'], $request); |
|
85
|
|
|
} |
|
86
|
84 |
|
} |
|
87
|
|
|
|
|
88
|
77 |
|
public function setField(Field $field, Request $request) |
|
89
|
|
|
{ |
|
90
|
|
|
// Is field set as translatable? |
|
91
|
77 |
|
if ($field->isTranslatable()) { |
|
92
|
75 |
|
if (!$this->requestContainsTranslations($request)) { |
|
|
|
|
|
|
93
|
24 |
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Make our media fields able to be translatable as well... |
|
97
|
52 |
|
if ($field->ofType(FieldType::MEDIA, FieldType::DOCUMENT)) { |
|
98
|
|
|
throw new \Exception('Cannot process the ' . $field->key . ' media field. Currently no support for translatable media files. We should fix this!'); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// Okay so this is a bit odd but since all translations are expected to be inside the trans |
|
102
|
|
|
// array, we can add all these translations at once. Just make sure to keep track of the |
|
103
|
|
|
// keys since this is what our translation engine requires as well for proper update. |
|
104
|
52 |
|
$this->queued_translations = $request->get('trans'); |
|
105
|
52 |
|
$this->translation_columns[] = $field->column(); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
52 |
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// By default we assume the key matches the attribute / column naming |
|
111
|
39 |
|
$this->model->{$field->column()} = $request->get($field->key()); |
|
112
|
39 |
|
} |
|
113
|
|
|
|
|
114
|
84 |
|
private function saveQueuedFields() |
|
115
|
|
|
{ |
|
116
|
84 |
|
$this->model->save(); |
|
117
|
|
|
|
|
118
|
|
|
// Translations |
|
119
|
84 |
|
if (!empty($this->queued_translations)) { |
|
120
|
50 |
|
$this->saveTranslations($this->queued_translations, $this->model, $this->translation_columns); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
84 |
|
return (new static($this->registration))->manage($this->model); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|