1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Element; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
|
8
|
|
|
class MultiSelect extends Select |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var bool |
12
|
|
|
*/ |
13
|
|
|
protected $taggable = false; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Closure |
17
|
|
|
*/ |
18
|
|
|
protected $pivotCallback; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $deleteRelatedItem = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $view = 'form.element.select'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getName() |
34
|
|
|
{ |
35
|
|
|
return parent::getName().'[]'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function getValueFromModel() |
42
|
|
|
{ |
43
|
|
|
$value = parent::getValueFromModel(); |
44
|
|
|
if ($value instanceof Collection && $value->count() > 0) { |
45
|
|
|
$value = $value->pluck($value->first()->getKeyName())->all(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($value instanceof Collection) { |
49
|
|
|
$value = $value->toArray(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $value; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function isTaggable() |
59
|
|
|
{ |
60
|
|
|
return $this->taggable; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function taggable() |
67
|
|
|
{ |
68
|
|
|
$this->taggable = true; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return \Closure |
75
|
|
|
*/ |
76
|
|
|
public function getPivotCallback() |
77
|
|
|
{ |
78
|
|
|
return $this->pivotCallback; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \Closure $callable |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function setPivotCallback(\Closure $callable) |
86
|
|
|
{ |
87
|
|
|
$this->pivotCallback = $callable; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function isDeleteRelatedItem() |
96
|
|
|
{ |
97
|
|
|
return $this->deleteRelatedItem; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
|
public function deleteRelatedItem() |
104
|
|
|
{ |
105
|
|
|
$this->deleteRelatedItem = true; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function toArray() |
114
|
|
|
{ |
115
|
|
|
$this->setHtmlAttributes([ |
116
|
|
|
'id' => $this->getName(), |
117
|
|
|
'class' => 'form-control input-select', |
118
|
|
|
'multiple', |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
if ($this->isTaggable()) { |
122
|
|
|
$this->setHtmlAttribute('class', 'input-taggable'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ($this->isReadonly()) { |
126
|
|
|
$this->setHtmlAttribute('disabled', 'disabled'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return [ |
130
|
|
|
'tagable' => $this->isTaggable(), |
131
|
|
|
'attributes' => $this->getHtmlAttributes(), |
132
|
|
|
] + parent::toArray(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param \Illuminate\Http\Request $request |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
public function save(\Illuminate\Http\Request $request) |
141
|
|
|
{ |
142
|
|
|
if (is_null($this->getModelForOptions())) { |
143
|
|
|
parent::save($request); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param \Illuminate\Http\Request $request |
149
|
|
|
* |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
public function afterSave(\Illuminate\Http\Request $request) |
153
|
|
|
{ |
154
|
|
|
if (is_null($this->getModelForOptions())) { |
155
|
|
|
return; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$attribute = $this->getModelAttributeKey(); |
159
|
|
|
|
160
|
|
|
if (is_null($request->input($this->getPath()))) { |
161
|
|
|
$values = []; |
162
|
|
|
} else { |
163
|
|
|
$values = $this->getValueFromModel(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$relation = $this->getModel()->{$attribute}(); |
167
|
|
|
|
168
|
|
|
if ($relation instanceof \Illuminate\Database\Eloquent\Relations\BelongsToMany) { |
169
|
|
|
$this->syncBelongsToManyRelation($relation, $values); |
170
|
|
|
} elseif ($relation instanceof \Illuminate\Database\Eloquent\Relations\HasMany) { |
171
|
|
|
$this->deleteOldItemsFromHasManyRelation($relation, $values); |
172
|
|
|
$this->attachItemsToHasManyRelation($relation, $values); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation |
178
|
|
|
* @param array $values |
179
|
|
|
* |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
|
|
protected function syncBelongsToManyRelation( |
183
|
|
|
\Illuminate\Database\Eloquent\Relations\BelongsToMany $relation, |
184
|
|
|
array $values |
185
|
|
|
) { |
186
|
|
|
foreach ($values as $i => $value) { |
187
|
|
|
if (! array_key_exists($value, $this->getOptions()) and $this->isTaggable()) { |
|
|
|
|
188
|
|
|
$model = clone $this->getModelForOptions(); |
189
|
|
|
$model->{$this->getDisplay()} = $value; |
190
|
|
|
$model->save(); |
191
|
|
|
|
192
|
|
|
$values[$i] = $model->getKey(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (is_callable($this->getPivotCallback())) { |
197
|
|
|
$values = call_user_func($this->pivotCallback, $values) ?: $values; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$relation->sync($values); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\HasMany $relation |
205
|
|
|
* @param array $values |
206
|
|
|
*/ |
207
|
|
|
protected function deleteOldItemsFromHasManyRelation( |
208
|
|
|
\Illuminate\Database\Eloquent\Relations\HasMany $relation, |
209
|
|
|
array $values |
210
|
|
|
) { |
211
|
|
|
$items = $relation->get(); |
212
|
|
|
|
213
|
|
|
foreach ($items as $item) { |
214
|
|
|
if (! in_array($item->getKey(), $values)) { |
215
|
|
|
if ($this->isDeleteRelatedItem()) { |
216
|
|
|
$item->delete(); |
217
|
|
|
} else { |
218
|
|
|
$item->{$this->getForeignKeyNameFromRelation($relation)} = null; |
219
|
|
|
$item->save(); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\HasMany $relation |
227
|
|
|
* @param array $values |
228
|
|
|
*/ |
229
|
|
|
protected function attachItemsToHasManyRelation( |
230
|
|
|
\Illuminate\Database\Eloquent\Relations\HasMany $relation, |
231
|
|
|
array $values |
232
|
|
|
) { |
233
|
|
|
foreach ($values as $i => $value) { |
234
|
|
|
/** @var Model $model */ |
235
|
|
|
$model = clone $this->getModelForOptions(); |
236
|
|
|
$item = $model->find($value); |
237
|
|
|
|
238
|
|
|
if (is_null($item)) { |
239
|
|
|
if (! $this->isTaggable()) { |
240
|
|
|
continue; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$model->{$this->getDisplay()} = $value; |
244
|
|
|
$item = $model; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$relation->save($item); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.