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