|
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 void |
|
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
|
|
|
return collect($this->fields()) |
|
|
|
|
|
|
58
|
|
|
->where('model') |
|
59
|
|
|
->whereIn('relation_type', $relation_types) |
|
60
|
|
|
->toArray(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Grabs an relation instance and returns the class name of the related model. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $field |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function inferFieldModelFromRelationship($field) |
|
70
|
|
|
{ |
|
71
|
|
|
$relation = $this->getRelationInstance($field); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
return get_class($relation->getRelated()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Return the relation type from a given field: BelongsTo, HasOne ... etc. |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $field |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function inferRelationTypeFromRelationship($field) |
|
83
|
|
|
{ |
|
84
|
|
|
$relation = $this->getRelationInstance($field); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
return Arr::last(explode('\\', get_class($relation))); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Parse the field name back to the related entity after the form is submited. |
|
91
|
|
|
* Its called in getAllFieldNames(). |
|
92
|
|
|
* |
|
93
|
|
|
* @param array $fields |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public function parseRelationFieldNamesFromHtml($fields) |
|
97
|
|
|
{ |
|
98
|
|
|
foreach ($fields as &$field) { |
|
99
|
|
|
//we only want to parse fields that has a relation type and their name contains [ ] used in html. |
|
100
|
|
|
if (isset($field['relation_type']) && preg_match('/[\[\]]/', $field['name']) !== 0) { |
|
101
|
|
|
$chunks = explode('[', $field['name']); |
|
102
|
|
|
|
|
103
|
|
|
foreach ($chunks as &$chunk) { |
|
104
|
|
|
if (strpos($chunk, ']')) { |
|
105
|
|
|
$chunk = str_replace(']', '', $chunk); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
$field['name'] = implode('.', $chunks); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $fields; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Based on relation type returns the default field type. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $relation_type |
|
119
|
|
|
* @return bool |
|
120
|
|
|
*/ |
|
121
|
|
|
public function inferFieldTypeFromFieldRelation($field) |
|
122
|
|
|
{ |
|
123
|
|
|
switch ($field['relation_type']) { |
|
124
|
|
|
case 'BelongsToMany': |
|
125
|
|
|
case 'HasMany': |
|
126
|
|
|
case 'HasManyThrough': |
|
127
|
|
|
case 'MorphMany': |
|
128
|
|
|
case 'MorphToMany': |
|
129
|
|
|
case 'BelongsTo': |
|
130
|
|
|
return 'relationship'; |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
default: |
|
133
|
|
|
return 'text'; |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Based on relation type returns if relation allows multiple entities. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $relation_type |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
public function guessIfFieldHasMultipleFromRelationType($relation_type) |
|
144
|
|
|
{ |
|
145
|
|
|
switch ($relation_type) { |
|
146
|
|
|
case 'BelongsToMany': |
|
147
|
|
|
case 'HasMany': |
|
148
|
|
|
case 'HasManyThrough': |
|
149
|
|
|
case 'HasOneOrMany': |
|
150
|
|
|
case 'MorphMany': |
|
151
|
|
|
case 'MorphOneOrMany': |
|
152
|
|
|
case 'MorphToMany': |
|
153
|
|
|
return true; |
|
154
|
|
|
|
|
155
|
|
|
default: |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Based on relation type returns if relation has a pivot table. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $relation_type |
|
164
|
|
|
* @return bool |
|
165
|
|
|
*/ |
|
166
|
|
|
public function guessIfFieldHasPivotFromRelationType($relation_type) |
|
167
|
|
|
{ |
|
168
|
|
|
switch ($relation_type) { |
|
169
|
|
|
case 'BelongsToMany': |
|
170
|
|
|
case 'HasManyThrough': |
|
171
|
|
|
case 'MorphMany': |
|
172
|
|
|
case 'MorphOneOrMany': |
|
173
|
|
|
case 'MorphToMany': |
|
174
|
|
|
return true; |
|
175
|
|
|
default: |
|
176
|
|
|
return false; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|