1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
trait Relationships |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* From the field entity we get the relation instance. |
11
|
|
|
* |
12
|
|
|
* @param array $entity |
13
|
|
|
* @return object |
14
|
|
|
*/ |
15
|
|
|
public function getRelationInstance($field) |
16
|
|
|
{ |
17
|
|
|
$entity = $this->getOnlyRelationEntity($field); |
|
|
|
|
18
|
|
|
$entity_array = explode('.', $entity); |
19
|
|
|
$relation_model = $this->getRelationModel($entity); |
|
|
|
|
20
|
|
|
|
21
|
|
|
$related_method = Arr::last($entity_array); |
22
|
|
|
if (count(explode('.', $entity)) == count(explode('.', $field['entity']))) { |
23
|
|
|
$relation_model = $this->getRelationModel($entity, -1); |
24
|
|
|
} |
25
|
|
|
$relation_model = new $relation_model(); |
26
|
|
|
|
27
|
|
|
//if counts are diferent means that last element of entity is the field in relation. |
28
|
|
|
if (count(explode('.', $entity)) != count(explode('.', $field['entity']))) { |
29
|
|
|
if (in_array($related_method, $relation_model->getFillable())) { |
30
|
|
|
if (count($entity_array) > 1) { |
31
|
|
|
$related_method = $entity_array[(count($entity_array) - 2)]; |
32
|
|
|
$relation_model = $this->getRelationModel($entity, -2); |
33
|
|
|
} else { |
34
|
|
|
$relation_model = $this->model; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
if (count($entity_array) == 1) { |
39
|
|
|
if (method_exists($this->model, $related_method)) { |
40
|
|
|
return $this->model->{$related_method}(); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $relation_model->{$related_method}(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the fields with specific relation types. |
49
|
|
|
* |
50
|
|
|
* @param array|string $relation_types |
51
|
|
|
* |
52
|
|
|
* @return array The fields with corresponding relation types. |
53
|
|
|
*/ |
54
|
|
|
public function getFieldsWithRelationType($relation_types): array |
55
|
|
|
{ |
56
|
|
|
$relation_types = is_array($relation_types) ?: (array) $relation_types; |
57
|
|
|
|
58
|
|
|
return collect($this->fields()) |
|
|
|
|
59
|
|
|
->where('model') |
60
|
|
|
->whereIn('relation_type', $relation_types) |
61
|
|
|
->toArray(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Grabs an relation instance and returns the class name of the related model. |
66
|
|
|
* |
67
|
|
|
* @param array $field |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function inferFieldModelFromRelationship($field) |
71
|
|
|
{ |
72
|
|
|
$relation = $this->getRelationInstance($field); |
73
|
|
|
|
74
|
|
|
return get_class($relation->getRelated()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return the relation type from a given field: BelongsTo, HasOne ... etc. |
79
|
|
|
* |
80
|
|
|
* @param array $field |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function inferRelationTypeFromRelationship($field) |
84
|
|
|
{ |
85
|
|
|
$relation = $this->getRelationInstance($field); |
86
|
|
|
|
87
|
|
|
return Arr::last(explode('\\', get_class($relation))); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Parse the field name back to the related entity after the form is submited. |
92
|
|
|
* Its called in getAllFieldNames(). |
93
|
|
|
* |
94
|
|
|
* @param array $fields |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function parseRelationFieldNamesFromHtml($fields) |
98
|
|
|
{ |
99
|
|
|
foreach ($fields as &$field) { |
100
|
|
|
//we only want to parse fields that has a relation type and their name contains [ ] used in html. |
101
|
|
|
if (isset($field['relation_type']) && preg_match('/[\[\]]/', $field['name']) !== 0) { |
102
|
|
|
$chunks = explode('[', $field['name']); |
103
|
|
|
|
104
|
|
|
foreach ($chunks as &$chunk) { |
105
|
|
|
if (strpos($chunk, ']')) { |
106
|
|
|
$chunk = str_replace(']', '', $chunk); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
$field['name'] = implode('.', $chunks); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $fields; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Based on relation type returns the default field type. |
118
|
|
|
* |
119
|
|
|
* @param string $relation_type |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function inferFieldTypeFromFieldRelation($field) |
123
|
|
|
{ |
124
|
|
|
switch ($field['relation_type']) { |
125
|
|
|
case 'BelongsToMany': |
126
|
|
|
case 'HasMany': |
127
|
|
|
case 'HasManyThrough': |
128
|
|
|
case 'MorphMany': |
129
|
|
|
case 'MorphToMany': |
130
|
|
|
case 'BelongsTo': |
131
|
|
|
return 'relationship'; |
|
|
|
|
132
|
|
|
|
133
|
|
|
default: |
134
|
|
|
return 'text'; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Based on relation type returns if relation allows multiple entities. |
140
|
|
|
* |
141
|
|
|
* @param string $relation_type |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function guessIfFieldHasMultipleFromRelationType($relation_type) |
145
|
|
|
{ |
146
|
|
|
switch ($relation_type) { |
147
|
|
|
case 'BelongsToMany': |
148
|
|
|
case 'HasMany': |
149
|
|
|
case 'HasManyThrough': |
150
|
|
|
case 'HasOneOrMany': |
151
|
|
|
case 'MorphMany': |
152
|
|
|
case 'MorphOneOrMany': |
153
|
|
|
case 'MorphToMany': |
154
|
|
|
return true; |
155
|
|
|
|
156
|
|
|
default: |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Based on relation type returns if relation has a pivot table. |
163
|
|
|
* |
164
|
|
|
* @param string $relation_type |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function guessIfFieldHasPivotFromRelationType($relation_type) |
168
|
|
|
{ |
169
|
|
|
switch ($relation_type) { |
170
|
|
|
case 'BelongsToMany': |
171
|
|
|
case 'HasManyThrough': |
172
|
|
|
case 'MorphMany': |
173
|
|
|
case 'MorphOneOrMany': |
174
|
|
|
case 'MorphToMany': |
175
|
|
|
return true; |
176
|
|
|
default: |
177
|
|
|
return false; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Associate and dissociate the BelongsTo relations in primary model |
183
|
|
|
* |
184
|
|
|
* @param Model |
185
|
|
|
* @param array The form data. |
|
|
|
|
186
|
|
|
* @return Model Model with relationships set up. |
|
|
|
|
187
|
|
|
*/ |
188
|
|
|
public function associateBelongsToRelations($item, array $data) |
189
|
|
|
{ |
190
|
|
|
$belongsToFields = $this->getFieldsWithRelationType('BelongsTo'); |
191
|
|
|
|
192
|
|
|
foreach ($belongsToFields as $relationField) { |
193
|
|
|
if(method_exists($item, $this->getOnlyRelationEntity($relationField))) { |
194
|
|
|
$relatedId = Arr::get($data, $relationField['name']); |
195
|
|
|
$related = $relationField['model']::find($relatedId); |
196
|
|
|
|
197
|
|
|
$item->{$this->getOnlyRelationEntity($relationField)}()->associate($related); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $item; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
|