|
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 bool |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $deleteRelatedItem = false; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $view = 'form.element.select'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getName() |
|
29
|
|
|
{ |
|
30
|
|
|
return parent::getName().'[]'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getValueFromModel() |
|
37
|
|
|
{ |
|
38
|
|
|
$value = parent::getValueFromModel(); |
|
39
|
|
|
if ($value instanceof Collection && $value->count() > 0) { |
|
40
|
|
|
$value = $value->pluck($value->first()->getKeyName())->all(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ($value instanceof Collection) { |
|
44
|
|
|
$value = $value->toArray(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $value; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public function isTaggable() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->taggable; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return $this |
|
60
|
|
|
*/ |
|
61
|
|
|
public function taggable() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->taggable = true; |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
|
public function isDeleteRelatedItem() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->deleteRelatedItem; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
|
|
public function deleteRelatedItem() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->deleteRelatedItem = true; |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
public function toArray() |
|
90
|
|
|
{ |
|
91
|
|
|
$attributes = [ |
|
92
|
|
|
'id' => $this->getName(), |
|
93
|
|
|
'class' => 'form-control input-select', |
|
94
|
|
|
'multiple', |
|
95
|
|
|
]; |
|
96
|
|
|
|
|
97
|
|
|
if ($this->isTaggable()) { |
|
98
|
|
|
$attributes['class'] .= ' input-taggable'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ($this->isReadonly()) { |
|
102
|
|
|
$attributes['disabled'] = 'disabled'; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return [ |
|
106
|
|
|
'tagable' => $this->isTaggable(), |
|
107
|
|
|
'attributes' => $attributes, |
|
108
|
|
|
] + parent::toArray(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param \Illuminate\Http\Request $request |
|
113
|
|
|
* |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
|
|
public function save(\Illuminate\Http\Request $request) |
|
117
|
|
|
{ |
|
118
|
|
|
if (is_null($this->getModelForOptions())) { |
|
119
|
|
|
parent::save($request); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param \Illuminate\Http\Request $request |
|
125
|
|
|
* |
|
126
|
|
|
* @return void |
|
127
|
|
|
*/ |
|
128
|
|
|
public function afterSave(\Illuminate\Http\Request $request) |
|
129
|
|
|
{ |
|
130
|
|
|
if (is_null($this->getModelForOptions())) { |
|
131
|
|
|
return; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$attribute = $this->getModelAttributeKey(); |
|
135
|
|
|
|
|
136
|
|
|
if (is_null($request->input($this->getPath()))) { |
|
137
|
|
|
$values = []; |
|
138
|
|
|
} else { |
|
139
|
|
|
$values = $this->getValueFromModel(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$relation = $this->getModel()->{$attribute}(); |
|
143
|
|
|
|
|
144
|
|
|
if ($relation instanceof \Illuminate\Database\Eloquent\Relations\BelongsToMany) { |
|
145
|
|
|
$this->syncBelongsToManyRelation($relation, $values); |
|
146
|
|
|
} elseif ($relation instanceof \Illuminate\Database\Eloquent\Relations\HasMany) { |
|
147
|
|
|
$this->deleteOldItemsFromHasManyRelation($relation, $values); |
|
148
|
|
|
$this->attachItemsToHasManyRelation($relation, $values); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation |
|
154
|
|
|
* @param array $values |
|
155
|
|
|
* |
|
156
|
|
|
* @return void |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function syncBelongsToManyRelation(\Illuminate\Database\Eloquent\Relations\BelongsToMany $relation, array $values) |
|
159
|
|
|
{ |
|
160
|
|
|
foreach ($values as $i => $value) { |
|
161
|
|
|
if (! array_key_exists($value, $this->getOptions()) and $this->isTaggable()) { |
|
|
|
|
|
|
162
|
|
|
$model = clone $this->getModelForOptions(); |
|
163
|
|
|
$model->{$this->getDisplay()} = $value; |
|
164
|
|
|
$model->save(); |
|
165
|
|
|
|
|
166
|
|
|
$values[$i] = $model->getKey(); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$relation->sync($values); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\HasMany $relation |
|
175
|
|
|
* @param array $values |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function deleteOldItemsFromHasManyRelation(\Illuminate\Database\Eloquent\Relations\HasMany $relation, array $values) |
|
178
|
|
|
{ |
|
179
|
|
|
$items = $relation->get(); |
|
180
|
|
|
|
|
181
|
|
|
foreach ($items as $item) { |
|
182
|
|
|
if (! in_array($item->getKey(), $values)) { |
|
183
|
|
|
if ($this->isDeleteRelatedItem()) { |
|
184
|
|
|
$item->delete(); |
|
185
|
|
|
} else { |
|
186
|
|
|
$item->{$relation->getPlainForeignKey()} = null; |
|
187
|
|
|
$item->save(); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\HasMany $relation |
|
195
|
|
|
* @param array $values |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function attachItemsToHasManyRelation(\Illuminate\Database\Eloquent\Relations\HasMany $relation, array $values) |
|
198
|
|
|
{ |
|
199
|
|
|
foreach ($values as $i => $value) { |
|
200
|
|
|
/** @var Model $model */ |
|
201
|
|
|
$model = clone $this->getModelForOptions(); |
|
202
|
|
|
$item = $model->find($value); |
|
203
|
|
|
|
|
204
|
|
|
if (is_null($item)) { |
|
205
|
|
|
if (! $this->isTaggable()) { |
|
206
|
|
|
continue; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$model->{$this->getDisplay()} = $value; |
|
210
|
|
|
$item = $model; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$relation->save($item); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
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
dieintroduces 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 withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.