forked from Olicek/form-selectize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selectize.php
327 lines (291 loc) · 7.38 KB
/
Selectize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* Copyright (c) 2014 Petr Olišar (http://olisar.eu)
*
* For the full copyright and license information, please view
* the file LICENSE.md that was distributed with this source code.
*/
namespace App\Form\Control;
use Doctrine\ORM\PersistentCollection;
use Kdyby\Doctrine\Collections\ReadOnlyCollectionWrapper;
use Nette;
use Nette\Forms\Form;
use Nette\Utils;
/**
* Description of Selectize
*
* @author Petr Olišar <[email protected]>
* @author <[email protected]>
* @author Tomáš Hofman <[email protected]>
*/
class Selectize extends Nette\Forms\Controls\BaseControl
{
private $entity = [];
private $labelName;
private $selectize;
private $selectizeBack;
private $options;
private $prompt = null;
public function __construct(?string $label = null, ?array $entity = null, ?array $options = null)
{
parent::__construct($label);
$this->entity = is_null($entity) ? [] : $entity;
$this->labelName = $label;
$this->options = $options;
}
public function setOptions(array $options)
{
foreach($options as $key => $value)
{
$this->options[$key] = $value;
}
return $this;
}
public function setMode($mode)
{
$this->options['mode'] = $mode;
return $this;
}
public function setCreate($create)
{
$this->options['create'] = $create;
return $this;
}
public function maxItems($items)
{
$this->options['maxItems'] = $items;
return $this;
}
public function setDelimiter($delimiter)
{
$this->options['delimiter'] = $delimiter;
return $this;
}
public function setPlugins(array $plugins)
{
$this->options['plugins'] = $plugins;
return $this;
}
public function setValueField($valueField)
{
$this->options['valueField'] = $valueField;
return $this;
}
public function setLabelField($labelField)
{
$this->options['labelField'] = $labelField;
return $this;
}
public function setSearchField($searchField)
{
$this->options['searchField'] = $searchField;
return $this;
}
public function setClass($class)
{
$this->options['class'] = $class;
return $this;
}
public function setAjaxURL($ajaxURL)
{
$this->options['ajaxURL'] = $ajaxURL;
return $this;
}
/**
* Sets first prompt item in select box.
* @param string
* @return self
*/
public function setPrompt($prompt)
{
$this->prompt = $prompt;
return $this;
}
/**
* Returns first prompt item?
* @return mixed
*/
public function getPrompt()
{
return $this->prompt;
}
/**
* Sets options and option groups from which to choose.
* @param array $items
* @return array
*/
public function setItems(array $items)
{
return $this->entity = $items;
}
/**
* Gets items
* @return array
*/
public function getItems() {
return $this->entity;
}
public function setValue($value)
{
if(!is_null($value))
{
if ($value instanceof Nette\Database\Table\Selection)
{
throw new Nette\InvalidArgumentException("Type must be array, instance of Nette\\Database\\Table\\Selection was given. Try Selection::fetchAssoc(\$key)");
}
if(is_array($value) || $value instanceof ReadOnlyCollectionWrapper || $value instanceof PersistentCollection)
{
$i = 0;
foreach($value as $key => $slug)
{
$i++;
$idName = $this->options['valueField'];
$this->selectizeBack .= isset($slug->$idName) ? $slug->$idName : $key;
if($i < count($value))
{
$this->selectizeBack .= $this->options['delimiter'];
}
}
} else
{
$this->selectizeBack = $value;
}
}
$this->selectize = $this->selectizeBack;
}
public function getValue()
{
if (is_array($this->selectize) && count($this->selectize) === 0) {
return null;
}
return $this->selectize;
}
public function loadHttpData(): void
{
if($this->options['mode'] === 'select')
{
$value = $this->getHttpData(Form::DATA_LINE);
if($value === "")
{
$value = NULL;
}
$this->selectizeBack = $this->selectize = $value;
} else
{
$this->prepareData();
}
}
public function getControl(): Utils\Html
{
$this->setOption('rendered', true);
$name = $this->getHtmlName();
$el = clone $this->control;
if (array_key_exists('ajaxURL', $this->options))
{
$this->entity = $this->findActiveValue($this->entity, $this->options['valueField'], $this->selectizeBack);
}
if($this->options['mode'] === 'full')
{
return $el->addAttributes([
'id' => $this->getHtmlId(),
'type' => 'text',
'name' => $name,
'class' => array(isset($this->options['class']) ? $this->options['class'] : 'selectize' . ' form-control text'),
'data-entity' => $this->entity,
'data-options' => $this->options,
'value' => $this->selectizeBack
]);
} elseif ($this->options['mode'] === 'select')
{
$this->entity = empty($this->prompt) ?
$this->entity : self::arrayUnshiftAssoc($this->entity, '', $this->translate($this->prompt));
return Nette\Forms\Helpers::createSelectBox($this->entity, [
'selected?' => $this->selectizeBack
])
->id($this->getHtmlId())
->name($name)
->data('entity', $this->entity)
->data('options', $this->options)
->class(isset($this->options['class']) ? $this->options['class'] : 'selectize' . ' form-control')
->addAttributes(parent::getControl()->attrs)
->setValue($this->selectizeBack);
}
}
private function findActiveValue($array, $key, $value)
{
$results = array();
if (is_array($array)) {
if (isset($array[$key]) && $array[$key] == $value) {
$results[] = $array;
}
foreach ($array as $subarray) {
$results = array_merge($results, $this->findActiveValue($subarray, $key, $value));
}
}
return $results;
}
private static function arrayUnshiftAssoc(&$arr, $key, $val)
{
$arr = array_reverse($arr, true);
$arr[$key] = $val;
return array_reverse($arr, true);
}
private function prepareData()
{
$this->selectize = $this->split($this->getHttpData(Form::DATA_LINE));
$this->selectizeBack = $this->getHttpData(Form::DATA_LINE);
$iteration = false;
foreach($this->selectize as $key => $value)
{
if(!$this->myInArray($this->entity, $value, $this->options['valueField']))
{
$iteration ?: $this->selectize['new'] = [];
array_push($this->entity, [$this->options['valueField'] => $value, 'name' => $value]);
array_push($this->selectize['new'], $value);
unset($this->selectize[$key]);
$iteration = true;
}
}
}
private function split($selectize)
{
$return = Nette\Utils\Strings::split(empty($selectize) ?
'' : $selectize, '~'.$this->options['delimiter'].'\s*~');
return $return[0] === "" ? [] : $return;
}
/**
*
* @author <[email protected]>
* @param array $array
* @param string|int $value
* @param string|int $key
* @return boolean
*/
private function myInArray(array $array, $value, $key)
{
if(isset($array[$key]) AND $array[$key]==$value)
{
return true;
}
foreach ($array as $val)
{
if (is_array($val))
{
if($this->myInArray($val,$value,$key))
{
return true;
}
}
}
return false;
}
public static function register($method = 'addSelectize', $config)
{
Nette\Forms\Container::extensionMethod($method, function(Nette\Forms\Container $container, $name, $label, $entity = null, array $options = null) use ($config)
{
$container[$name] = new Selectize($label, $entity, is_array($options) ?
array_replace($config, $options) : $config);
return $container[$name];
});
}
}