|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Repositories\Behaviors; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
|
|
10
|
|
|
trait HandleRepeaters |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param \A17\Twill\Models\Model $object |
|
14
|
|
|
* @param array $fields |
|
15
|
|
|
* @return void |
|
16
|
|
|
*/ |
|
17
|
|
|
public function afterSaveHandleRepeaters($object, $fields) |
|
18
|
|
|
{ |
|
19
|
|
|
if (property_exists($this, 'repeaters')) { |
|
20
|
|
|
foreach ($this->repeaters as $module) { |
|
21
|
|
|
if (is_string($module)) { |
|
22
|
|
|
$model = Str::studly(Str::singular($module)); |
|
23
|
|
|
$repeaterName = Str::singular($module); |
|
24
|
|
|
$this->updateRepeater($object, $fields, $module, $model, $repeaterName); |
|
25
|
|
|
} elseif (is_array($module)) { |
|
26
|
|
|
$relation = !empty($module['relation']) ? $module['relation'] : key($module); |
|
27
|
|
|
$model = isset($module['model']) ? $module['model'] : Str::studly(Str::singular(key($module))); |
|
28
|
|
|
$repeaterName = !empty($module['repeaterName']) ? $module['repeaterName'] : Str::singular(key($module)); |
|
29
|
|
|
$this->updateRepeater($object, $fields, $relation, $model, $repeaterName); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param \A17\Twill\Models\Model $object |
|
37
|
|
|
* @param array $fields |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getFormFieldsHandleRepeaters($object, $fields) |
|
41
|
|
|
{ |
|
42
|
|
|
if (property_exists($this, 'repeaters')) { |
|
43
|
|
|
foreach ($this->repeaters as $module) { |
|
44
|
|
|
if (is_string($module)) { |
|
45
|
|
|
$model = Str::studly(Str::singular($module)); |
|
46
|
|
|
$repeaterName = Str::singular($module); |
|
47
|
|
|
$fields = $this->getFormFieldsForRepeater($object, $fields, $module, $model, $repeaterName); |
|
48
|
|
|
} elseif (is_array($module)) { |
|
49
|
|
|
$model = isset($module['model']) ? $module['model'] : Str::studly(Str::singular(key($module))); |
|
50
|
|
|
$relation = !empty($module['relation']) ? $module['relation'] : key($module); |
|
51
|
|
|
$repeaterName = !empty($module['repeaterName']) ? $module['repeaterName'] : Str::singular(key($module)); |
|
52
|
|
|
$fields = $this->getFormFieldsForRepeater($object, $fields, $relation, $model, $repeaterName); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $fields; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function updateRepeaterMany($object, $fields, $relation, $keepExisting = true, $model = null) |
|
61
|
|
|
{ |
|
62
|
|
|
$relationFields = $fields['repeaters'][$relation] ?? []; |
|
63
|
|
|
$relationRepository = $this->getModelRepository($relation, $model); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
if (!$keepExisting) { |
|
66
|
|
|
$object->$relation()->each(function ($repeaterElement) { |
|
67
|
|
|
$repeaterElement->forceDelete(); |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
foreach ($relationFields as $relationField) { |
|
72
|
|
|
$newRelation = $relationRepository->create($relationField); |
|
73
|
|
|
$object->$relation()->attach($newRelation->id); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function updateRepeater($object, $fields, $relation, $model = null, $repeaterName = null) |
|
78
|
|
|
{ |
|
79
|
|
|
if (!$repeaterName) { |
|
80
|
|
|
$repeaterName = $relation; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$relationFields = $fields['repeaters'][$repeaterName] ?? []; |
|
84
|
|
|
|
|
85
|
|
|
$relationRepository = $this->getModelRepository($relation, $model); |
|
86
|
|
|
|
|
87
|
|
|
// if no relation field submitted, soft deletes all associated rows |
|
88
|
|
|
if (!$relationFields) { |
|
89
|
|
|
$relationRepository->updateBasic(null, [ |
|
90
|
|
|
'deleted_at' => Carbon::now(), |
|
91
|
|
|
], [ |
|
92
|
|
|
$this->model->getForeignKey() => $object->id, |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// keep a list of updated and new rows to delete (soft delete?) old rows that were deleted from the frontend |
|
97
|
|
|
$currentIdList = []; |
|
98
|
|
|
|
|
99
|
|
|
foreach ($relationFields as $index => $relationField) { |
|
100
|
|
|
$relationField['position'] = $index + 1; |
|
101
|
|
|
if (isset($relationField['id']) && starts_with($relationField['id'], $relation)) { |
|
|
|
|
|
|
102
|
|
|
// row already exists, let's update |
|
103
|
|
|
$id = str_replace($relation . '-', '', $relationField['id']); |
|
104
|
|
|
$relationRepository->update($id, $relationField); |
|
105
|
|
|
$currentIdList[] = $id; |
|
106
|
|
|
} else { |
|
107
|
|
|
// new row, let's attach to our object and create |
|
108
|
|
|
$relationField[$this->model->getForeignKey()] = $object->id; |
|
109
|
|
|
unset($relationField['id']); |
|
110
|
|
|
$newRelation = $relationRepository->create($relationField); |
|
111
|
|
|
$currentIdList[] = $newRelation['id']; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
foreach ($object->$relation->pluck('id') as $id) { |
|
116
|
|
|
if (!in_array($id, $currentIdList)) { |
|
117
|
|
|
$relationRepository->updateBasic(null, [ |
|
118
|
|
|
'deleted_at' => Carbon::now(), |
|
119
|
|
|
], [ |
|
120
|
|
|
'id' => $id, |
|
121
|
|
|
]); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function getFormFieldsForRepeater($object, $fields, $relation, $model = null, $repeaterName = null) |
|
127
|
|
|
{ |
|
128
|
|
|
if (!$repeaterName) { |
|
129
|
|
|
$repeaterName = $relation; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$repeaters = []; |
|
133
|
|
|
$repeatersFields = []; |
|
134
|
|
|
$repeatersBrowsers = []; |
|
135
|
|
|
$repeatersMedias = []; |
|
136
|
|
|
$repeatersFiles = []; |
|
137
|
|
|
$relationRepository = $this->getModelRepository($relation, $model); |
|
138
|
|
|
$repeatersConfig = config('twill.block_editor.repeaters'); |
|
139
|
|
|
|
|
140
|
|
|
foreach ($object->$relation as $relationItem) { |
|
141
|
|
|
$repeaters[] = [ |
|
142
|
|
|
'id' => $relation . '-' . $relationItem->id, |
|
143
|
|
|
'type' => $repeatersConfig[$repeaterName]['component'], |
|
144
|
|
|
'title' => $repeatersConfig[$repeaterName]['title'], |
|
145
|
|
|
]; |
|
146
|
|
|
|
|
147
|
|
|
$relatedItemFormFields = $relationRepository->getFormFields($relationItem); |
|
148
|
|
|
$translatedFields = []; |
|
149
|
|
|
|
|
150
|
|
|
if (isset($relatedItemFormFields['translations'])) { |
|
151
|
|
|
foreach ($relatedItemFormFields['translations'] as $key => $values) { |
|
152
|
|
|
$repeatersFields[] = [ |
|
153
|
|
|
'name' => "blocks[$relation-$relationItem->id][$key]", |
|
154
|
|
|
'value' => $values, |
|
155
|
|
|
]; |
|
156
|
|
|
|
|
157
|
|
|
$translatedFields[] = $key; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if (isset($relatedItemFormFields['medias'])) { |
|
162
|
|
|
if (config('twill.media_library.translated_form_fields', false)) { |
|
163
|
|
|
Collection::make($relatedItemFormFields['medias'])->each(function ($rolesWithMedias, $locale) use (&$repeatersMedias, $relation, $relationItem) { |
|
164
|
|
|
$repeatersMedias[] = Collection::make($rolesWithMedias)->mapWithKeys(function ($medias, $role) use ($locale, $relation, $relationItem) { |
|
165
|
|
|
return [ |
|
166
|
|
|
"blocks[$relation-$relationItem->id][$role][$locale]" => $medias, |
|
167
|
|
|
]; |
|
168
|
|
|
})->toArray(); |
|
169
|
|
|
}); |
|
170
|
|
|
} else { |
|
171
|
|
|
foreach ($relatedItemFormFields['medias'] as $key => $values) { |
|
172
|
|
|
$repeatersMedias["blocks[$relation-$relationItem->id][$key]"] = $values; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if (isset($relatedItemFormFields['files'])) { |
|
178
|
|
|
Collection::make($relatedItemFormFields['files'])->each(function ($rolesWithFiles, $locale) use (&$repeatersFiles, $relation, $relationItem) { |
|
179
|
|
|
$repeatersFiles[] = Collection::make($rolesWithFiles)->mapWithKeys(function ($files, $role) use ($locale, $relation, $relationItem) { |
|
180
|
|
|
return [ |
|
181
|
|
|
"blocks[$relation-$relationItem->id][$role][$locale]" => $files, |
|
182
|
|
|
]; |
|
183
|
|
|
})->toArray(); |
|
184
|
|
|
}); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
if (isset($relatedItemFormFields['browsers'])) { |
|
188
|
|
|
foreach ($relatedItemFormFields['browsers'] as $key => $values) { |
|
189
|
|
|
$repeatersBrowsers["blocks[$relation-$relationItem->id][$key]"] = $values; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$itemFields = method_exists($relationItem, 'toRepeaterArray') ? $relationItem->toRepeaterArray() : Arr::except($relationItem->attributesToArray(), $translatedFields); |
|
194
|
|
|
|
|
195
|
|
|
foreach ($itemFields as $key => $value) { |
|
196
|
|
|
$repeatersFields[] = [ |
|
197
|
|
|
'name' => "blocks[$relation-$relationItem->id][$key]", |
|
198
|
|
|
'value' => $value, |
|
199
|
|
|
]; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
if (!empty($repeatersMedias) && config('twill.media_library.translated_form_fields', false)) { |
|
205
|
|
|
$repeatersMedias = call_user_func_array('array_merge', $repeatersMedias); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
if (!empty($repeatersFiles)) { |
|
209
|
|
|
$repeatersFiles = call_user_func_array('array_merge', $repeatersFiles); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$fields['repeaters'][$repeaterName] = $repeaters; |
|
213
|
|
|
$fields['repeaterFields'][$repeaterName] = $repeatersFields; |
|
214
|
|
|
$fields['repeaterMedias'][$repeaterName] = $repeatersMedias; |
|
215
|
|
|
$fields['repeaterFiles'][$repeaterName] = $repeatersFiles; |
|
216
|
|
|
$fields['repeaterBrowsers'][$repeaterName] = $repeatersBrowsers; |
|
217
|
|
|
|
|
218
|
|
|
return $fields; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|