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\Related\Elements; |
8
|
|
|
use SleepingOwl\Admin\Form\Columns\Column; |
9
|
|
|
use SleepingOwl\Admin\Form\Columns\Columns; |
10
|
|
|
use SleepingOwl\Admin\Form\Related\Select; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ManyToMany |
15
|
|
|
* |
16
|
|
|
* @package Admin\Form\Elements\RelatedElements\Forms |
17
|
|
|
* |
18
|
|
|
* @method BelongsToMany getEmptyRelation() |
19
|
|
|
*/ |
20
|
|
|
class ManyToMany extends Elements |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Relation name of the model. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $relationName; |
28
|
|
|
|
29
|
|
|
protected $primaries; |
30
|
|
|
|
31
|
|
|
protected $relatedElement; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Columns |
35
|
|
|
*/ |
36
|
|
|
protected $relatedWrapper; |
37
|
|
|
|
38
|
|
|
public function __construct($relationName, array $elements = []) |
39
|
|
|
{ |
40
|
|
|
$elements = array_prepend($elements, $this->relatedElement = new Select('__new_element__')); |
41
|
|
|
|
42
|
|
|
parent::__construct($relationName, $elements); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getPrimaries() |
46
|
|
|
{ |
47
|
|
|
return $this->primaries ?: [ |
48
|
|
|
$this->getEmptyRelation()->getForeignPivotKeyName(), |
49
|
|
|
$this->getRelatedForeignKeyName(), |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setPrimaries(array $primaries) |
54
|
|
|
{ |
55
|
|
|
$this->primaries = $primaries; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getRelatedElement() |
59
|
|
|
{ |
60
|
|
|
return $this->relatedElement; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function initializeElements() |
67
|
|
|
{ |
68
|
|
|
$this->initializeRelatedElement(); |
69
|
|
|
parent::initializeElements(); |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function initializeRelatedElement() |
74
|
|
|
{ |
75
|
|
|
$select = $this->getRelatedElement(); |
76
|
|
|
|
77
|
|
|
$select->setName($name = $this->getRelatedForeignKeyName()); |
78
|
|
|
$select->setModelAttributeKey($name); |
79
|
|
|
$select->setPath($this->getEmptyRelation()->getRelated()->getKeyName()); |
80
|
|
|
$select->setModelForOptions(get_class($this->getEmptyRelation()->getRelated())); |
81
|
|
|
$select->setModel($this->getModelForElements()); |
82
|
|
|
$select->required(); |
83
|
|
|
|
84
|
|
|
$this->unique([$name], trans('sleeping_owl::lang.form.unique')); |
|
|
|
|
85
|
|
|
|
86
|
|
|
if ($this->relatedWrapper) { |
87
|
|
|
$this->getElements()->forget(0); |
88
|
|
|
$column = new Column([$select]); |
89
|
|
|
$this->relatedWrapper->getElements()->prepend($column); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function retrieveRelationValuesFromQuery($query) |
94
|
|
|
{ |
95
|
|
|
return $query->get()->pluck('pivot')->keyBy(function ($item) { |
96
|
|
|
return $this->getKeyFromItem($item); |
97
|
|
|
})->forget($this->toRemove->all()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $item |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function getKeyFromItem($item) |
106
|
|
|
{ |
107
|
|
|
return $this->getCompositeKey($item, $this->getPrimaries()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param \Illuminate\Support\Collection $values |
112
|
|
|
* |
113
|
|
|
* @return Collection |
114
|
|
|
*/ |
115
|
|
|
protected function buildRelatedMap(Collection $values) |
116
|
|
|
{ |
117
|
|
|
$relatedKey = $this->getRelatedForeignKeyName(); |
118
|
|
|
|
119
|
|
|
return $values->mapWithKeys(function ($attributes) use ($relatedKey) { |
120
|
|
|
return [ |
121
|
|
|
array_get($attributes, $relatedKey) => array_except($attributes, [ |
122
|
|
|
$relatedKey, |
123
|
|
|
$this->getEmptyRelation()->getForeignPivotKeyName(), |
124
|
|
|
]), |
125
|
|
|
]; |
126
|
|
|
}); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function getRelatedForeignKeyName() |
130
|
|
|
{ |
131
|
|
|
return $this->getEmptyRelation()->getRelatedPivotKeyName(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function proceedSave(Request $request) |
135
|
|
|
{ |
136
|
|
|
// By this time getModel method will always return existed model object, not empty |
137
|
|
|
// so wee need to fresh it, because if it's new model creating relation will throw |
138
|
|
|
// exception 'call relation method on null' |
139
|
|
|
$relation = $this->getRelation(); |
140
|
|
|
$relation->sync($this->buildRelatedMap($this->relatedValues)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function prepareRelatedValues(array $data) |
144
|
|
|
{ |
145
|
|
|
$elements = $this->flatNamedElements($this->getNewElements()); |
146
|
|
|
foreach ($data as $key => $attributes) { |
147
|
|
|
$related = $this->addOrGetRelated($key); |
148
|
|
|
|
149
|
|
|
foreach ($elements as $index => $element) { |
150
|
|
|
$attribute = $element->getModelAttributeKey(); |
151
|
|
|
$value = $element->prepareValue(array_get($attributes, $attribute)); |
152
|
|
|
$related->setAttribute($attribute, $value); |
153
|
|
|
|
154
|
|
|
$element->setModel($related); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function getModelForElements() |
160
|
|
|
{ |
161
|
|
|
return $this->getEmptyRelation()->newPivot(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function wrapRelatedInto(Columns $columns) |
165
|
|
|
{ |
166
|
|
|
$this->relatedWrapper = $columns; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function dontWrap() |
170
|
|
|
{ |
171
|
|
|
$this->relatedWrapper = null; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function __call($name, $arguments) |
175
|
|
|
{ |
176
|
|
|
if (method_exists($this->getRelatedElement(), $name)) { |
177
|
|
|
$this->getRelatedElement()->$name(...$arguments); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
throw new \RuntimeException("Method {$name} doesn't exist."); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Returns fresh instance of model for each element in form. |
185
|
|
|
* |
186
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
187
|
|
|
*/ |
188
|
|
|
protected function getFreshModelForElements() |
189
|
|
|
{ |
190
|
|
|
return $this->getModelForElements(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|