1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
|
9
|
|
|
trait Create |
10
|
|
|
{ |
11
|
|
|
/* |
12
|
|
|
|-------------------------------------------------------------------------- |
13
|
|
|
| CREATE |
14
|
|
|
|-------------------------------------------------------------------------- |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Insert a row in the database. |
19
|
|
|
* |
20
|
|
|
* @param array $input All input values to be inserted. |
21
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
22
|
|
|
*/ |
23
|
|
|
public function create($input) |
24
|
|
|
{ |
25
|
|
|
[$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($input); |
|
|
|
|
26
|
|
|
|
27
|
|
|
if ($this->get('create.useDatabaseTransactions') ?? config('backpack.base.useDatabaseTransactions', false)) { |
|
|
|
|
28
|
|
|
return DB::transaction(fn () => $this->createModelAndRelations($directInputs, $relationInputs)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $this->createModelAndRelations($directInputs, $relationInputs); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function createModelAndRelations(array $directInputs, array $relationInputs): Model |
35
|
|
|
{ |
36
|
|
|
$item = $this->model->create($directInputs); |
37
|
|
|
$this->createRelationsForItem($item, $relationInputs); |
38
|
|
|
|
39
|
|
|
return $item; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get all fields needed for the ADD NEW ENTRY form. |
44
|
|
|
* |
45
|
|
|
* @return array The fields with attributes and fake attributes. |
46
|
|
|
*/ |
47
|
|
|
public function getCreateFields() |
48
|
|
|
{ |
49
|
|
|
return $this->fields(); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get all fields with relation set (model key set on field). |
54
|
|
|
* |
55
|
|
|
* @param array $fields |
56
|
|
|
* @return array The fields with model key set. |
57
|
|
|
*/ |
58
|
|
|
public function getRelationFields($fields = []) |
59
|
|
|
{ |
60
|
|
|
if (empty($fields)) { |
61
|
|
|
$fields = $this->getCleanStateFields(); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$relationFields = []; |
65
|
|
|
|
66
|
|
|
foreach ($fields as $field) { |
67
|
|
|
if (isset($field['model']) && $field['model'] !== false && $field['entity'] !== false) { |
68
|
|
|
array_push($relationFields, $field); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// if a field has an array name AND subfields |
72
|
|
|
// then take those fields into account (check if they have relationships); |
73
|
|
|
// this is done in particular for the checklist_dependency field, |
74
|
|
|
// but other fields could use it too, in the future; |
75
|
|
|
if ($this->holdsMultipleInputs($field['name']) && |
|
|
|
|
76
|
|
|
isset($field['subfields']) && |
77
|
|
|
is_array($field['subfields'])) { |
78
|
|
|
foreach ($field['subfields'] as $subfield) { |
79
|
|
|
if (isset($subfield['model']) && $subfield['model'] !== false) { |
80
|
|
|
array_push($relationFields, $subfield); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $relationFields; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* --------------- |
91
|
|
|
* PRIVATE METHODS |
92
|
|
|
* ---------------. |
93
|
|
|
*/ |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create relations for the provided model. |
97
|
|
|
* |
98
|
|
|
* @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
99
|
|
|
* @param array $formattedRelations The form data. |
100
|
|
|
* @return bool|null |
101
|
|
|
*/ |
102
|
|
|
private function createRelationsForItem($item, $formattedRelations) |
103
|
|
|
{ |
104
|
|
|
// no relations to create |
105
|
|
|
if (empty($formattedRelations)) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
foreach ($formattedRelations as $relationMethod => $relationDetails) { |
110
|
|
|
$relation = $item->{$relationMethod}(); |
111
|
|
|
$relationType = $relationDetails['relation_type']; |
112
|
|
|
|
113
|
|
|
switch ($relationType) { |
114
|
|
|
case 'HasOne': |
115
|
|
|
case 'MorphOne': |
116
|
|
|
$this->createUpdateOrDeleteOneToOneRelation($relation, $relationMethod, $relationDetails); |
117
|
|
|
break; |
118
|
|
|
case 'HasMany': |
119
|
|
|
case 'MorphMany': |
120
|
|
|
$relationValues = $relationDetails['values'][$relationMethod]; |
121
|
|
|
// if relation values are null we can only attach, also we check if we sent |
122
|
|
|
// - a single dimensional array: [1,2,3] |
123
|
|
|
// - an array of arrays: [[1][2][3]] |
124
|
|
|
// if is as single dimensional array we can only attach. |
125
|
|
|
if ($relationValues === null || ! is_multidimensional_array($relationValues)) { |
126
|
|
|
$this->attachManyRelation($item, $relation, $relationDetails, $relationValues); |
127
|
|
|
} else { |
128
|
|
|
$this->createManyEntries($item, $relation, $relationMethod, $relationDetails); |
129
|
|
|
} |
130
|
|
|
break; |
131
|
|
|
case 'BelongsToMany': |
132
|
|
|
case 'MorphToMany': |
133
|
|
|
$values = $relationDetails['values'][$relationMethod] ?? []; |
134
|
|
|
$values = is_string($values) ? json_decode($values, true) : $values; |
135
|
|
|
|
136
|
|
|
// disabling ConvertEmptyStringsToNull middleware may return null from json_decode() if an empty string is used. |
137
|
|
|
// we need to make sure no null value can go foward so we reassure that values is not null after json_decode() |
138
|
|
|
$values = $values ?? []; |
139
|
|
|
|
140
|
|
|
$relationValues = []; |
141
|
|
|
|
142
|
|
|
if (is_array($values) && is_multidimensional_array($values)) { |
143
|
|
|
foreach ($values as $value) { |
144
|
|
|
if (isset($value[$relationMethod])) { |
145
|
|
|
$relationValues[$value[$relationMethod]] = Arr::except($value, $relationMethod); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// if there is no relation data, and the values array is single dimensional we have |
151
|
|
|
// an array of keys with no aditional pivot data. sync those. |
152
|
|
|
if (empty($relationValues)) { |
153
|
|
|
$relationValues = array_values($values); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$item->{$relationMethod}()->sync($relationValues); |
157
|
|
|
break; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Save the attributes of a given HasOne or MorphOne relationship on the |
164
|
|
|
* related entry, create or delete it, depending on what was sent in the form. |
165
|
|
|
* |
166
|
|
|
* For HasOne and MorphOne relationships, the dev might want to a few different things: |
167
|
|
|
* (A) save an attribute on the related entry (eg. passport.number) |
168
|
|
|
* (B) set an attribute on the related entry to NULL (eg. slug.slug) |
169
|
|
|
* (C) save an entire related entry (eg. passport) |
170
|
|
|
* (D) delete the entire related entry (eg. passport) |
171
|
|
|
* |
172
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\HasOne|\Illuminate\Database\Eloquent\Relations\MorphOne $relation |
173
|
|
|
* @param string $relationMethod The name of the relationship method on the main Model. |
174
|
|
|
* @param array $relationDetails Details about that relationship. For example: |
175
|
|
|
* [ |
176
|
|
|
* 'model' => 'App\Models\Passport', |
177
|
|
|
* 'parent' => 'App\Models\Pet', |
178
|
|
|
* 'entity' => 'passport', |
179
|
|
|
* 'attribute' => 'passport', |
180
|
|
|
* 'values' => **THE TRICKY BIT**, |
181
|
|
|
* ] |
182
|
|
|
* @return Model|null |
183
|
|
|
*/ |
184
|
|
|
private function createUpdateOrDeleteOneToOneRelation($relation, $relationMethod, $relationDetails) |
185
|
|
|
{ |
186
|
|
|
// Let's see which scenario we're treating, depending on the contents of $relationDetails: |
187
|
|
|
// - (A) ['number' => 1315, 'name' => 'Something'] (if passed using a text/number/etc field) |
188
|
|
|
// - (B) ['slug' => null] (if the 'slug' attribute on the 'slug' related entry needs to be cleared) |
189
|
|
|
// - (C) ['passport' => [['number' => 1314, 'name' => 'Something']]] (if passed using a repeatable field) |
190
|
|
|
// - (D) ['passport' => null] (if deleted from the repeatable field) |
191
|
|
|
|
192
|
|
|
// Scenario C or D |
193
|
|
|
if (array_key_exists($relationMethod, $relationDetails['values'])) { |
194
|
|
|
$relationMethodValue = $relationDetails['values'][$relationMethod]; |
195
|
|
|
|
196
|
|
|
// Scenario D |
197
|
|
|
if (is_null($relationMethodValue) && $relationDetails['entity'] === $relationMethod) { |
198
|
|
|
$relation->first()?->delete(); |
199
|
|
|
|
200
|
|
|
return null; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// Scenario C (when it's an array inside an array, because it's been added as one item inside a repeatable field) |
204
|
|
|
if (gettype($relationMethodValue) == 'array' && is_multidimensional_array($relationMethodValue)) { |
205
|
|
|
$relationMethodValue = $relationMethodValue[0]; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
// saving process |
209
|
|
|
$input = $relationMethodValue ?? $relationDetails['values']; |
210
|
|
|
[$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($input, $relationDetails, $relationMethod); |
211
|
|
|
|
212
|
|
|
$item = $relation->updateOrCreate([], $directInputs); |
213
|
|
|
|
214
|
|
|
$this->createRelationsForItem($item, $relationInputs); |
215
|
|
|
|
216
|
|
|
return $item; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* When using the HasMany/MorphMany relations as selectable elements we use this function to "mimic-sync" in those relations. |
221
|
|
|
* Since HasMany/MorphMany does not have the `sync` method, we manually re-create it. |
222
|
|
|
* Here we add the entries that developer added and remove the ones that are not in the list. |
223
|
|
|
* This removal process happens with the following rules: |
224
|
|
|
* - by default Backpack will behave like a `sync` from M-M relations: it deletes previous entries and add only the current ones. |
225
|
|
|
* - `force_delete` is configurable in the field, it's `true` by default. When false, if connecting column is nullable instead of deleting the row we set the column to null. |
226
|
|
|
* - `fallback_id` could be provided. In this case instead of deleting we set the connecting key to whatever developer gives us. |
227
|
|
|
* |
228
|
|
|
* @return mixed |
229
|
|
|
*/ |
230
|
|
|
private function attachManyRelation($item, $relation, $relationDetails, $relationValues) |
231
|
|
|
{ |
232
|
|
|
$modelInstance = $relation->getRelated(); |
233
|
|
|
$relationForeignKey = $relation->getForeignKeyName(); |
234
|
|
|
$relationLocalKey = $relation->getLocalKeyName(); |
235
|
|
|
|
236
|
|
|
if (empty($relationValues)) { |
237
|
|
|
// the developer cleared the selection |
238
|
|
|
// we gonna clear all related values by setting up the value to the fallback id, to null or delete. |
239
|
|
|
return $this->handleManyRelationItemRemoval($modelInstance, $relation, $relationDetails, $relationForeignKey); |
240
|
|
|
} |
241
|
|
|
// we add the new values into the relation, if it is HasMany we only update the foreign_key, |
242
|
|
|
// otherwise (it's a MorphMany) we need to update the morphs keys too |
243
|
|
|
$toUpdate[$relationForeignKey] = $item->{$relationLocalKey}; |
|
|
|
|
244
|
|
|
|
245
|
|
|
if ($relationDetails['relation_type'] === 'MorphMany') { |
246
|
|
|
$toUpdate[$relation->getQualifiedMorphType()] = $relation->getMorphClass(); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$modelInstance->whereIn($modelInstance->getKeyName(), $relationValues) |
250
|
|
|
->update($toUpdate); |
251
|
|
|
|
252
|
|
|
// we clear up any values that were removed from model relation. |
253
|
|
|
// if developer provided a fallback id, we use it |
254
|
|
|
// if column is nullable we set it to null if developer didn't specify `force_delete => true` |
255
|
|
|
// if none of the above we delete the model from database |
256
|
|
|
$removedEntries = $modelInstance->whereNotIn($modelInstance->getKeyName(), $relationValues) |
257
|
|
|
->where($relationForeignKey, $item->{$relationLocalKey}); |
258
|
|
|
|
259
|
|
|
// if relation is MorphMany we also match by morph type. |
260
|
|
|
if ($relationDetails['relation_type'] === 'MorphMany') { |
261
|
|
|
$removedEntries->where($relation->getQualifiedMorphType(), $relation->getMorphClass()); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $this->handleManyRelationItemRemoval($modelInstance, $removedEntries, $relationDetails, $relationForeignKey); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
private function handleManyRelationItemRemoval($modelInstance, $removedEntries, $relationDetails, $relationForeignKey) |
268
|
|
|
{ |
269
|
|
|
$relationColumnIsNullable = $modelInstance->isColumnNullable($relationForeignKey); |
270
|
|
|
$forceDelete = $relationDetails['force_delete'] ?? false; |
271
|
|
|
$fallbackId = $relationDetails['fallback_id'] ?? false; |
272
|
|
|
|
273
|
|
|
// developer provided a fallback_id he knows what he's doing, just use it. |
274
|
|
|
if ($fallbackId) { |
275
|
|
|
return $removedEntries->update([$relationForeignKey => $fallbackId]); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
// developer set force_delete => true, so we don't care if it's nullable or not, |
279
|
|
|
// we just follow developer's will |
280
|
|
|
if ($forceDelete) { |
281
|
|
|
return $removedEntries->lazy()->each->delete(); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
// get the default that could be set at database level. |
285
|
|
|
$dbColumnDefault = $modelInstance->getDbColumnDefault($relationForeignKey); |
286
|
|
|
|
287
|
|
|
// if column is not nullable in database, and there is no column default (null), |
288
|
|
|
// we will delete the entry from the database, otherwise it will throw and ugly DB error. |
289
|
|
|
if (! $relationColumnIsNullable && $dbColumnDefault === null) { |
290
|
|
|
return $removedEntries->lazy()->each->delete(); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
// if column is nullable we just set it to the column default (null when it does exist, or the default value when it does). |
294
|
|
|
return $removedEntries->update([$relationForeignKey => $dbColumnDefault]); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Handle HasMany/MorphMany relations when used as creatable entries in the crud. |
299
|
|
|
* By using repeatable field, developer can allow the creation of such entries |
300
|
|
|
* in the crud forms. |
301
|
|
|
* |
302
|
|
|
* @param $entry - eg: story |
|
|
|
|
303
|
|
|
* @param $relation - eg story HasMany monsters |
304
|
|
|
* @param $relationMethod - eg: monsters |
305
|
|
|
* @param $relationDetails - eg: info about relation including submited values |
306
|
|
|
* @return void |
307
|
|
|
*/ |
308
|
|
|
private function createManyEntries($entry, $relation, $relationMethod, $relationDetails) |
309
|
|
|
{ |
310
|
|
|
$items = $relationDetails['values'][$relationMethod]; |
311
|
|
|
|
312
|
|
|
$relatedModelLocalKey = $relation->getRelated()->getKeyName(); |
313
|
|
|
|
314
|
|
|
$relatedItemsSent = []; |
315
|
|
|
|
316
|
|
|
foreach ($items as $item) { |
317
|
|
|
[$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($item, $relationDetails, $relationMethod); |
318
|
|
|
// for each item we get the inputs to create and the relations of it. |
319
|
|
|
$relatedModelLocalKeyValue = $item[$relatedModelLocalKey] ?? null; |
320
|
|
|
|
321
|
|
|
// we either find the matched entry by local_key (usually `id`) |
322
|
|
|
// and update the values from the input |
323
|
|
|
// or create a new item from input |
324
|
|
|
$item = $entry->{$relationMethod}()->updateOrCreate([$relatedModelLocalKey => $relatedModelLocalKeyValue], $directInputs); |
325
|
|
|
|
326
|
|
|
// we store the item local key so we can match them with database and check if any item was deleted |
327
|
|
|
$relatedItemsSent[] = $item->{$relatedModelLocalKey}; |
328
|
|
|
|
329
|
|
|
// create the item relations if any. |
330
|
|
|
$this->createRelationsForItem($item, $relationInputs); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
// use the collection of sent ids to match against database ids, delete the ones not found in the submitted ids. |
334
|
|
|
if (! empty($relatedItemsSent)) { |
335
|
|
|
// we perform the cleanup of removed database items |
336
|
|
|
$entry->{$relationMethod}()->whereNotIn($relatedModelLocalKey, $relatedItemsSent)->lazy()->each->delete(); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|