|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
|
|
7
|
|
|
trait Update |
|
8
|
|
|
{ |
|
9
|
|
|
/* |
|
10
|
|
|
|-------------------------------------------------------------------------- |
|
11
|
|
|
| UPDATE |
|
12
|
|
|
|-------------------------------------------------------------------------- |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Update a row in the database. |
|
17
|
|
|
* |
|
18
|
|
|
* @param int $id The entity's id |
|
19
|
|
|
* @param array $data All inputs to be updated. |
|
20
|
|
|
* |
|
21
|
|
|
* @return object |
|
22
|
|
|
*/ |
|
23
|
|
|
public function update($id, $data) |
|
24
|
|
|
{ |
|
25
|
|
|
$data = $this->decodeJsonCastedAttributes($data); |
|
|
|
|
|
|
26
|
|
|
$data = $this->compactFakeFields($data); |
|
|
|
|
|
|
27
|
|
|
$item = $this->model->findOrFail($id); |
|
28
|
|
|
|
|
29
|
|
|
$this->createRelations($item, $data); |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
// omit the n-n relationships when updating the eloquent item |
|
32
|
|
|
$nn_relationships = Arr::pluck($this->getRelationFieldsWithPivot(), 'name'); |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
$data = Arr::except($data, $nn_relationships); |
|
35
|
|
|
|
|
36
|
|
|
$updated = $item->update($data); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
return $item; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get all fields needed for the EDIT ENTRY form. |
|
43
|
|
|
* |
|
44
|
|
|
* @param int $id The id of the entry that is being edited. |
|
45
|
|
|
* |
|
46
|
|
|
* @return array The fields with attributes, fake attributes and values. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getUpdateFields($id = false) |
|
49
|
|
|
{ |
|
50
|
|
|
$fields = $this->fields(); |
|
|
|
|
|
|
51
|
|
|
$entry = ($id != false) ? $this->getEntry($id) : $this->getCurrentEntry(); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
foreach ($fields as &$field) { |
|
54
|
|
|
// set the value |
|
55
|
|
|
if (! isset($field['value'])) { |
|
56
|
|
|
if (isset($field['subfields'])) { |
|
57
|
|
|
$field['value'] = []; |
|
58
|
|
|
foreach ($field['subfields'] as $subfield) { |
|
59
|
|
|
$field['value'][] = $entry->{$subfield['name']}; |
|
60
|
|
|
} |
|
61
|
|
|
} else { |
|
62
|
|
|
$field['value'] = $this->getModelAttributeValue($entry, $field); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// always have a hidden input for the entry id |
|
68
|
|
|
if (! array_key_exists('id', $fields)) { |
|
69
|
|
|
$fields['id'] = [ |
|
70
|
|
|
'name' => $entry->getKeyName(), |
|
71
|
|
|
'value' => $entry->getKey(), |
|
72
|
|
|
'type' => 'hidden', |
|
73
|
|
|
]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $fields; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get the value of the 'name' attribute from the declared relation model in the given field. |
|
81
|
|
|
* |
|
82
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model The current CRUD model. |
|
83
|
|
|
* @param array $field The CRUD field array. |
|
84
|
|
|
* |
|
85
|
|
|
* @return mixed The value of the 'name' attribute from the relation model. |
|
86
|
|
|
*/ |
|
87
|
|
|
private function getModelAttributeValue($model, $field) |
|
88
|
|
|
{ |
|
89
|
|
|
if (isset($field['entity'])) { |
|
90
|
|
|
$relational_entity = $this->parseRelationFieldNamesFromHtml([$field])[0]['name']; |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
$relation_array = explode('.', $relational_entity); |
|
93
|
|
|
|
|
94
|
|
|
$relatedModel = $relatedModel = array_reduce(array_splice($relation_array, 0, -1), function ($obj, $method) { |
|
|
|
|
|
|
95
|
|
|
return $obj->{$method} ? $obj->{$method} : $obj; |
|
96
|
|
|
}, $model); |
|
97
|
|
|
|
|
98
|
|
|
$relationMethod = Arr::last($relation_array); |
|
99
|
|
|
|
|
100
|
|
|
if (method_exists($relatedModel, $relationMethod) && $relatedModel->{$relationMethod}() instanceof HasOne) { |
|
|
|
|
|
|
101
|
|
|
return $relatedModel->{$relationMethod}->{Arr::last(explode('.', $relational_entity))}; |
|
102
|
|
|
} else { |
|
103
|
|
|
// if pivot is true and there is `fields` array in this field we are trying to sync a pivot with |
|
104
|
|
|
// extra attributes on it. It's a Repeatable Field so its values are sent as json. |
|
105
|
|
|
if (isset($field['pivot']) && $field['pivot'] && isset($field['fields']) && is_array($field['fields'])) { |
|
106
|
|
|
//we remove the first field from repeatable because it is our relation. |
|
107
|
|
|
$pivot_fields = Arr::where($field['fields'], function ($item) use ($field) { |
|
108
|
|
|
return $field['name'] != $item['name']; |
|
109
|
|
|
}); |
|
110
|
|
|
|
|
111
|
|
|
//we grab the related models |
|
112
|
|
|
$related_models = $relatedModel->{$relationMethod}; |
|
113
|
|
|
$return = []; |
|
114
|
|
|
|
|
115
|
|
|
//for any given model, we grab the attributes that belong to our pivot table. |
|
116
|
|
|
foreach ($related_models as $related_model) { |
|
117
|
|
|
$item[$field['name']] = $related_model->getKey(); |
|
118
|
|
|
//for any given related model, we attach the pivot fields. |
|
119
|
|
|
foreach ($pivot_fields as $pivot_field) { |
|
120
|
|
|
$item[$pivot_field['name']] = $related_model->pivot->{$pivot_field['name']}; |
|
121
|
|
|
} |
|
122
|
|
|
$return[] = $item; |
|
123
|
|
|
} |
|
124
|
|
|
//we return the json encoded result as expected by repeatable field. |
|
125
|
|
|
return json_encode($return); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $relatedModel->{$relationMethod}; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if (is_string($field['name'])) { |
|
133
|
|
|
return $model->{$field['name']}; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if (is_array($field['name'])) { |
|
137
|
|
|
$result = []; |
|
138
|
|
|
foreach ($field['name'] as $key => $value) { |
|
139
|
|
|
$result = $model->{$value}; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $result; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|