1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Admin\Action\Selectize; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
|
7
|
|
|
// From 'charcoal-factory' |
8
|
|
|
use Charcoal\Factory\FactoryInterface; |
9
|
|
|
|
10
|
|
|
// From 'charcoal-property' |
11
|
|
|
use Charcoal\Property\PropertyInterface; |
12
|
|
|
|
13
|
|
|
// From 'charcoal-admin' |
14
|
|
|
use Charcoal\Admin\Property\Input\SelectizeInput; |
15
|
|
|
use Charcoal\Admin\Property\PropertyInputInterface; |
16
|
|
|
use Charcoal\Admin\Service\SelectizeRenderer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
trait SelectizeRendererAwareTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var SelectizeRenderer |
25
|
|
|
*/ |
26
|
|
|
protected $selectizeRenderer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Store the factory instance. |
30
|
|
|
* |
31
|
|
|
* @var FactoryInterface |
32
|
|
|
*/ |
33
|
|
|
protected $propertyInputFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Store the factory instance. |
37
|
|
|
* |
38
|
|
|
* @var FactoryInterface |
39
|
|
|
*/ |
40
|
|
|
protected $propertyFactory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $selectizeObjType; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $selectizePropIdent; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var PropertyInterface |
54
|
|
|
*/ |
55
|
|
|
protected $selectizeProperty; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return PropertyInterface |
59
|
|
|
*/ |
60
|
|
|
protected function selectizeProperty() |
61
|
|
|
{ |
62
|
|
|
if ($this->selectizeProperty) { |
63
|
|
|
return $this->selectizeProperty; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$objType = $this->selectizeObjType(); |
67
|
|
|
$propertyIdent = $this->selectizePropIdent(); |
68
|
|
|
|
69
|
|
|
if ($objType && $propertyIdent) { |
70
|
|
|
$model = $this->modelFactory()->create($objType); |
71
|
|
|
$prop = $model->property($propertyIdent); |
72
|
|
|
|
73
|
|
|
$this->selectizeProperty = $prop; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->selectizeProperty; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $struct The property as string. |
81
|
|
|
*/ |
82
|
|
|
public function setSelectizeProperty($struct) |
83
|
|
|
{ |
84
|
|
|
$struct = json_decode($struct, true); |
85
|
|
|
|
86
|
|
|
$property = $this->propertyFactory()->create($struct['type']); |
87
|
|
|
$property->setIdent($this->selectizePropIdent()); |
88
|
|
|
$property->setData($struct); |
89
|
|
|
|
90
|
|
|
$this->selectizeProperty = $property; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return PropertyInputInterface |
97
|
|
|
*/ |
98
|
|
|
protected function selectizeInput() |
99
|
|
|
{ |
100
|
|
|
$prop = $this->selectizeProperty(); |
101
|
|
|
$type = isset($prop['input_type']) ? $prop['input_type'] : null; |
102
|
|
|
|
103
|
|
|
$input = $this->propertyInputFactory()->create($type); |
104
|
|
|
$input->setInputType($type); |
105
|
|
|
$input->setProperty($prop); |
106
|
|
|
$input->setData($prop->data()); |
107
|
|
|
|
108
|
|
|
return $input; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Retrieves the output from SelectizeInput::selectizeVal. |
113
|
|
|
* |
114
|
|
|
* @param mixed $val The value(s) to parse as selectize choices. |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
protected function selectizeVal($val) |
118
|
|
|
{ |
119
|
|
|
if ($val === null) { |
120
|
|
|
return []; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$input = $this->selectizeInput(); |
124
|
|
|
$choices = []; |
125
|
|
|
|
126
|
|
|
if ($input instanceof SelectizeInput) { |
127
|
|
|
$choices = $input->selectizeVal($val); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $choices; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// ========================================================================== |
134
|
|
|
// SUPPORT |
135
|
|
|
// ========================================================================== |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set a property control factory. |
139
|
|
|
* |
140
|
|
|
* @param FactoryInterface $factory The factory to create form controls for property values. |
141
|
|
|
* @return self |
142
|
|
|
*/ |
143
|
|
|
protected function setPropertyInputFactory(FactoryInterface $factory) |
144
|
|
|
{ |
145
|
|
|
$this->propertyInputFactory = $factory; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Retrieve the property control factory. |
152
|
|
|
* |
153
|
|
|
* @throws RuntimeException If the property control factory is missing. |
154
|
|
|
* @return FactoryInterface |
155
|
|
|
*/ |
156
|
|
|
public function propertyInputFactory() |
157
|
|
|
{ |
158
|
|
|
if (!isset($this->propertyInputFactory)) { |
159
|
|
|
throw new RuntimeException(sprintf( |
160
|
|
|
'Property Control Factory is not defined for [%s]', |
161
|
|
|
get_class($this) |
162
|
|
|
)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this->propertyInputFactory; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set a property control factory. |
170
|
|
|
* |
171
|
|
|
* @param FactoryInterface $factory The factory to create form controls for property values. |
172
|
|
|
* @return self |
173
|
|
|
*/ |
174
|
|
|
protected function setPropertyFactory(FactoryInterface $factory) |
175
|
|
|
{ |
176
|
|
|
$this->propertyFactory = $factory; |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Retrieve the property control factory. |
183
|
|
|
* |
184
|
|
|
* @throws RuntimeException If the property control factory is missing. |
185
|
|
|
* @return FactoryInterface |
186
|
|
|
*/ |
187
|
|
|
public function propertyFactory() |
188
|
|
|
{ |
189
|
|
|
if (!isset($this->propertyFactory)) { |
190
|
|
|
throw new RuntimeException(sprintf( |
191
|
|
|
'Property Control Factory is not defined for [%s]', |
192
|
|
|
get_class($this) |
193
|
|
|
)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $this->propertyFactory; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// ========================================================================== |
200
|
|
|
// GETTERS AND SETTERS |
201
|
|
|
// ========================================================================== |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $data The data set by setData(). |
205
|
|
|
* @return $this |
206
|
|
|
*/ |
207
|
|
|
public function setSelectizeObjType($data) |
208
|
|
|
{ |
209
|
|
|
$this->selectizeObjType = $data; |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param string $data The data set by setData(). |
216
|
|
|
* @return $this |
217
|
|
|
*/ |
218
|
|
|
public function setSelectizePropIdent($data) |
219
|
|
|
{ |
220
|
|
|
$this->selectizePropIdent = $data; |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return mixed |
227
|
|
|
*/ |
228
|
|
|
public function selectizeObjType() |
229
|
|
|
{ |
230
|
|
|
return $this->selectizeObjType; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return mixed |
235
|
|
|
*/ |
236
|
|
|
public function selectizePropIdent() |
237
|
|
|
{ |
238
|
|
|
return $this->selectizePropIdent; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return SelectizeRenderer |
243
|
|
|
*/ |
244
|
|
|
public function selectizeRenderer() |
245
|
|
|
{ |
246
|
|
|
return $this->selectizeRenderer; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param SelectizeRenderer $selectizeRenderer Selectize renderer. |
251
|
|
|
* @return self |
252
|
|
|
*/ |
253
|
|
|
public function setSelectizeRenderer(SelectizeRenderer $selectizeRenderer) |
254
|
|
|
{ |
255
|
|
|
$this->selectizeRenderer = $selectizeRenderer; |
256
|
|
|
|
257
|
|
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @throws Exception If the model factory was not set before being accessed. |
262
|
|
|
* @return FactoryInterface |
263
|
|
|
*/ |
264
|
|
|
abstract protected function modelFactory(); |
265
|
|
|
} |
266
|
|
|
|