|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yaro\Jarboe\Table\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Yaro\Jarboe\Table\Fields\Traits\Ajax; |
|
8
|
|
|
use Yaro\Jarboe\Table\Fields\Traits\Nullable; |
|
9
|
|
|
use Yaro\Jarboe\Table\Fields\Traits\Orderable; |
|
10
|
|
|
use Yaro\Jarboe\Table\Fields\Traits\Relations; |
|
11
|
|
|
|
|
12
|
|
|
class Select extends AbstractField |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
use Orderable; |
|
15
|
|
|
use Relations; |
|
16
|
|
|
use Nullable; |
|
17
|
|
|
use Ajax; |
|
18
|
|
|
|
|
19
|
|
|
const SIMPLE = 'simple'; |
|
20
|
|
|
const SELECT_2 = 'select2'; |
|
21
|
|
|
|
|
22
|
|
|
const ALLOWED_TYPES = [ |
|
23
|
|
|
self::SIMPLE, |
|
24
|
|
|
self::SELECT_2, |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
protected $multiple = false; |
|
28
|
|
|
protected $type = self::SIMPLE; |
|
29
|
|
|
|
|
30
|
|
|
public function isSelect2Type() |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->type == self::SELECT_2; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function isSimpleType() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->type == self::SIMPLE; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function type($type) |
|
41
|
|
|
{ |
|
42
|
|
|
$type = strtolower($type); |
|
43
|
|
|
if (!in_array($type, self::ALLOWED_TYPES)) { |
|
44
|
|
|
throw new \RuntimeException(sprintf('Not allowed type [%s] for SelectField', $type)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->type = $type; |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getType() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->type; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function multiple(bool $multiple = true) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->multiple = $multiple; |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function isMultiple() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->multiple; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function isCurrentOption($option, $model = null, $relationIndex = 0): bool |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->hasOld()) { |
|
72
|
|
|
if ($this->isGroupedRelation()) { |
|
73
|
|
|
$option = crc32($this->relations[$relationIndex]['group']) .'~~~'. $option; |
|
74
|
|
|
} |
|
75
|
|
|
if ($this->isMultiple()) { |
|
76
|
|
|
return in_array($option, $this->old()); |
|
77
|
|
|
} |
|
78
|
|
|
return $this->old() == $option; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (is_null($model)) { |
|
82
|
|
|
return (string) $option === (string) $this->getDefault(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($this->isMultiple() && !$this->isRelationField()) { |
|
86
|
|
|
return in_array($option, $model->{$this->name}); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($this->isRelationField()) { |
|
90
|
|
|
$related = $model->{$this->getRelationMethod($relationIndex)}; |
|
91
|
|
|
if ($related) { |
|
92
|
|
|
$relatedModelClass = get_class($model->{$this->getRelationMethod($relationIndex)}()->getRelated()); |
|
93
|
|
|
$freshRelatedModel = new $relatedModelClass; |
|
94
|
|
|
$collection = $related; |
|
95
|
|
|
if (!is_a($related, Collection::class)) { |
|
96
|
|
|
$collection = collect([$related]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $collection->contains($freshRelatedModel->getKeyName(), $option); |
|
100
|
|
|
} |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return (string) $option === (string) $model->{$this->name}; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function value(Request $request) |
|
108
|
|
|
{ |
|
109
|
|
|
$value = $request->get($this->name()); |
|
110
|
|
|
|
|
111
|
|
|
if ($this->isMultiple() && !$this->isRelationField()) { |
|
112
|
|
|
$value = $value ?: []; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (!$value && $this->isNullable()) { |
|
116
|
|
|
return null; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $value; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function afterStore($model, Request $request) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->afterUpdate($model, $request); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function afterUpdate($model, Request $request) |
|
128
|
|
|
{ |
|
129
|
|
|
if (!$this->isRelationField()) { |
|
130
|
|
|
return; |
|
131
|
|
|
} |
|
132
|
|
|
if ($this->isReadonly()) { |
|
133
|
|
|
return; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$this->syncRelations($model, $request->get($this->name())); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function getListView($model) |
|
140
|
|
|
{ |
|
141
|
|
|
return view('jarboe::crud.fields.select.list', [ |
|
142
|
|
|
'model' => $model, |
|
143
|
|
|
'field' => $this, |
|
144
|
|
|
'options' => $this->getOptions(), |
|
145
|
|
|
]); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function getEditFormView($model) |
|
149
|
|
|
{ |
|
150
|
|
|
$template = $this->isReadonly() ? 'readonly' : 'edit'; |
|
151
|
|
|
|
|
152
|
|
|
return view('jarboe::crud.fields.select.'. $template, [ |
|
153
|
|
|
'model' => $model, |
|
154
|
|
|
'field' => $this, |
|
155
|
|
|
]); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function getCreateFormView() |
|
159
|
|
|
{ |
|
160
|
|
|
return view('jarboe::crud.fields.select.create', [ |
|
161
|
|
|
'field' => $this, |
|
162
|
|
|
]); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
private function filterValuesForMorphToManyRelation($ids, $relationHash) |
|
166
|
|
|
{ |
|
167
|
|
|
$filtered = []; |
|
168
|
|
|
foreach ($ids as $id) { |
|
169
|
|
|
if (strpos($id, $relationHash .'~~~') !== false) { |
|
170
|
|
|
$id = substr($id, 13); |
|
171
|
|
|
$filtered[] = $id; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $filtered; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths