1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Related\Forms; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use SleepingOwl\Admin\Form\Columns\Column; |
8
|
|
|
use SleepingOwl\Admin\Form\Related\Select; |
9
|
|
|
use SleepingOwl\Admin\Form\Columns\Columns; |
10
|
|
|
use SleepingOwl\Admin\Form\Related\Elements; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ManyToMany. |
15
|
|
|
* |
16
|
|
|
* @method BelongsToMany getEmptyRelation() |
17
|
|
|
*/ |
18
|
|
|
class ManyToMany extends Elements |
19
|
|
|
{ |
20
|
|
|
protected $primaries; |
21
|
|
|
|
22
|
|
|
protected $relatedElement; |
23
|
|
|
|
24
|
|
|
protected $relatedElementDisplayName; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Columns |
28
|
|
|
*/ |
29
|
|
|
protected $relatedWrapper; |
30
|
|
|
|
31
|
|
|
public function __construct($relationName, array $elements = []) |
32
|
|
|
{ |
33
|
|
|
$elements = array_prepend($elements, $this->relatedElement = new Select('__new_element__')); |
34
|
|
|
|
35
|
|
|
parent::__construct($relationName, $elements); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getPrimaries() |
39
|
|
|
{ |
40
|
|
|
return $this->primaries ?: [ |
41
|
|
|
$this->getEmptyRelation()->getForeignPivotKeyName(), |
42
|
|
|
$this->getRelatedForeignKeyName(), |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Sets primaries of relation. |
48
|
|
|
* |
49
|
|
|
* @param array $primaries |
50
|
|
|
*/ |
51
|
|
|
public function setPrimaries(array $primaries) |
52
|
|
|
{ |
53
|
|
|
$this->primaries = $primaries; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns related element (first select if it's important). |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function getRelatedElement() |
62
|
|
|
{ |
63
|
|
|
return $this->relatedElement; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Initializes all elements. |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function initializeElements() |
72
|
|
|
{ |
73
|
|
|
$this->initializeRelatedElement(); |
74
|
|
|
parent::initializeElements(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Initializes related element. First element (select) is created by default because it's many-to-many, you know. |
79
|
|
|
*/ |
80
|
|
|
protected function initializeRelatedElement() |
81
|
|
|
{ |
82
|
|
|
$select = $this->getRelatedElement(); |
83
|
|
|
|
84
|
|
|
$select->setName($name = $this->getRelatedForeignKeyName()); |
85
|
|
|
$select->setModelAttributeKey($name); |
86
|
|
|
$select->setPath($this->getEmptyRelation()->getRelated()->getKeyName()); |
87
|
|
|
$select->setModelForOptions(get_class($this->getEmptyRelation()->getRelated())); |
88
|
|
|
$select->setModel($this->getModelForElements()); |
89
|
|
|
$select->setDisplay($this->getRelatedElementDisplayName()); |
90
|
|
|
$select->required(); |
91
|
|
|
|
92
|
|
|
$this->unique([$name], trans('sleeping_owl::lang.form.unique')); |
|
|
|
|
93
|
|
|
|
94
|
|
|
if ($this->relatedWrapper) { |
95
|
|
|
$this->getElements()->forget(0); |
96
|
|
|
$column = new Column([$select]); |
97
|
|
|
$this->relatedWrapper->getElements()->prepend($column); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Retrieves related values from given query. |
103
|
|
|
* |
104
|
|
|
* @param $query |
105
|
|
|
* |
106
|
|
|
* @return Collection |
107
|
|
|
*/ |
108
|
|
|
protected function retrieveRelationValuesFromQuery($query) |
109
|
|
|
{ |
110
|
|
|
return $query->get()->pluck('pivot')->keyBy(function ($item) { |
111
|
|
|
return $this->getKeyFromItem($item); |
112
|
|
|
})->forget($this->toRemove->all()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $item |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
protected function getKeyFromItem($item) |
121
|
|
|
{ |
122
|
|
|
return $this->getCompositeKey($item, $this->getPrimaries()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param \Illuminate\Support\Collection $values |
127
|
|
|
* |
128
|
|
|
* @return Collection |
129
|
|
|
*/ |
130
|
|
|
protected function buildRelatedMap(Collection $values) |
131
|
|
|
{ |
132
|
|
|
$relatedKey = $this->getRelatedForeignKeyName(); |
133
|
|
|
|
134
|
|
|
return $values->mapWithKeys(function ($attributes) use ($relatedKey) { |
135
|
|
|
return [ |
136
|
|
|
array_get($attributes, $relatedKey) => array_except($attributes, [ |
137
|
|
|
$relatedKey, |
138
|
|
|
$this->getEmptyRelation()->getForeignPivotKeyName(), |
139
|
|
|
]), |
140
|
|
|
]; |
141
|
|
|
}); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns related foreign key name. |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
protected function getRelatedForeignKeyName() |
150
|
|
|
{ |
151
|
|
|
return $this->getEmptyRelation()->getRelatedPivotKeyName(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function proceedSave(Request $request) |
155
|
|
|
{ |
156
|
|
|
// By this time getModel method will always return existed model object, not empty |
157
|
|
|
// so wee need to fresh it, because if it's new model creating relation will throw |
158
|
|
|
// exception 'call relation method on null' |
159
|
|
|
$relation = $this->getRelation(); |
160
|
|
|
$relation->sync($this->buildRelatedMap($this->relatedValues)); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param array $data |
165
|
|
|
* |
166
|
|
|
* @return mixed|void |
167
|
|
|
*/ |
168
|
|
|
protected function prepareRelatedValues(array $data) |
169
|
|
|
{ |
170
|
|
|
$elements = $this->flatNamedElements($this->getNewElements()); |
171
|
|
|
foreach ($data as $key => $attributes) { |
172
|
|
|
$related = $this->addOrGetRelated($key); |
173
|
|
|
|
174
|
|
|
foreach ($elements as $index => $element) { |
175
|
|
|
$attribute = $element->getModelAttributeKey(); |
176
|
|
|
|
177
|
|
|
$element->setModel($related); |
178
|
|
|
$element->setPath($attribute); |
179
|
|
|
$element->setValueSkipped(false); |
180
|
|
|
$element->setModelAttribute(array_get($attributes, $attribute)); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Pivot |
187
|
|
|
*/ |
188
|
|
|
protected function getModelForElements() |
189
|
|
|
{ |
190
|
|
|
return $this->getEmptyRelation()->newPivot(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Wraps first element into given columns. It's useful when you have Columns in your form and want the related |
195
|
|
|
* element to be inside this columns. |
196
|
|
|
* |
197
|
|
|
* @param \SleepingOwl\Admin\Form\Columns\Columns $columns |
198
|
|
|
*/ |
199
|
|
|
public function wrapRelatedInto(Columns $columns) |
200
|
|
|
{ |
201
|
|
|
$this->relatedWrapper = $columns; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Cancels related element wrapping. |
206
|
|
|
*/ |
207
|
|
|
public function dontWrap() |
208
|
|
|
{ |
209
|
|
|
$this->relatedWrapper = null; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Proxies method call to related element. |
214
|
|
|
* |
215
|
|
|
* @param $name |
216
|
|
|
* @param $arguments |
217
|
|
|
*/ |
218
|
|
|
public function __call($name, $arguments) |
219
|
|
|
{ |
220
|
|
|
if (method_exists($this->getRelatedElement(), $name)) { |
221
|
|
|
$this->getRelatedElement()->$name(...$arguments); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
throw new \RuntimeException("Method {$name} doesn't exist."); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Returns fresh instance of model for each element in form. |
229
|
|
|
* |
230
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
231
|
|
|
*/ |
232
|
|
|
protected function getFreshModelForElements() |
233
|
|
|
{ |
234
|
|
|
return $this->getModelForElements(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
|
|
public function getRelatedElementDisplayName() |
241
|
|
|
{ |
242
|
|
|
return $this->relatedElementDisplayName; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param string $value |
247
|
|
|
* @return $this |
248
|
|
|
*/ |
249
|
|
|
public function setRelatedElementDisplayName($value) |
250
|
|
|
{ |
251
|
|
|
$this->relatedElementDisplayName = $value; |
252
|
|
|
|
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|