We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php |
||
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 = array_pluck($this->getRelationFieldsWithPivot(), 'name'); |
||
33 | $data = array_except($data, $nn_relationships); |
||
34 | $updated = $item->update($data); |
||
35 | |||
36 | return $item; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Get all fields needed for the EDIT ENTRY form. |
||
41 | * |
||
42 | * @param int $id The id of the entry that is being edited. |
||
43 | * |
||
44 | * @return array The fields with attributes, fake attributes and values. |
||
45 | */ |
||
46 | public function getUpdateFields($id = false) |
||
47 | { |
||
48 | $fields = $this->fields(); |
||
49 | $entry = ($id != false) ? $this->getEntry($id) : $this->getCurrentEntry(); |
||
50 | |||
51 | foreach ($fields as &$field) { |
||
52 | // set the value |
||
53 | if (! isset($field['value'])) { |
||
54 | if (isset($field['subfields'])) { |
||
55 | $field['value'] = []; |
||
56 | foreach ($field['subfields'] as $subfield) { |
||
57 | $field['value'][] = $entry->{$subfield['name']}; |
||
58 | } |
||
59 | |||
60 | // handle fake fields |
||
61 | } elseif (! empty($field['fake'])) { |
||
62 | // determine the stored-in attribute |
||
63 | $fakeStoredInAttribute = $field['store_in'] ?? 'extras'; |
||
64 | // check if the fake stored-in attribute exists |
||
65 | if (! empty($entry->{$fakeStoredInAttribute})) { |
||
66 | $fakeStoredInArray = $entry->{$fakeStoredInAttribute}; |
||
67 | // check if it's a string, decode it |
||
68 | // otherwise, it should be an array |
||
69 | if (is_string($fakeStoredInArray)) { |
||
70 | // decode it |
||
71 | $fakeStoredInArray = json_decode($fakeStoredInArray, true); |
||
72 | } |
||
73 | |||
74 | + if (! empty($fakeStoredInArray) && is_array($fakeStoredInArray) && isset($fakeStoredInArray[$field['name']])) { |
||
|
|||
75 | $field['value'] = $fakeStoredInArray[$field['name']]; |
||
76 | } |
||
77 | } |
||
78 | } else { |
||
79 | $field['value'] = $this->getModelAttributeValue($entry, $field); |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | |||
84 | // always have a hidden input for the entry id |
||
85 | if (! array_key_exists('id', $fields)) { |
||
86 | $fields['id'] = [ |
||
87 | 'name' => $entry->getKeyName(), |
||
88 | 'value' => $entry->getKey(), |
||
89 | 'type' => 'hidden', |
||
90 | ]; |
||
91 | } |
||
92 | |||
93 | return $fields; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get the value of the 'name' attribute from the declared relation model in the given field. |
||
98 | * |
||
99 | * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model. |
||
100 | * @param array $field The CRUD field array. |
||
101 | * |
||
102 | * @return mixed The value of the 'name' attribute from the relation model. |
||
103 | */ |
||
104 | private function getModelAttributeValue($model, $field) |
||
105 | { |
||
106 | if (isset($field['entity'])) { |
||
107 | $relationArray = explode('.', $field['entity']); |
||
108 | $relatedModel = array_reduce(array_splice($relationArray, 0, -1), function ($obj, $method) { |
||
109 | return $obj->{$method} ? $obj->{$method} : $obj; |
||
110 | }, $model); |
||
111 | |||
112 | $relationMethod = end($relationArray); |
||
113 | if ($relatedModel->{$relationMethod} && $relatedModel->{$relationMethod}() instanceof HasOneOrMany) { |
||
114 | return $relatedModel->{$relationMethod}->{$field['name']}; |
||
115 | } else { |
||
134 |