-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuggestPicker.php
228 lines (177 loc) · 6.53 KB
/
SuggestPicker.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
<?php
namespace SuggestPicker;
use \Nette\Web\Html;
use \Nette\Templates\Template;
/**
* Multiple items picker with suggestion
*
* @copyright Copyright (c) 2009, 2010 David Šabata
*/
class SuggestPicker extends \Nette\Forms\FormControl {
/** @var array $items always a value=>title array; $useKeys is respected in getters/setters */
protected $items = array();
/** @var array $value always a value=>title array; $useKeys is respected in getters/setters */
protected $value = array();
/** @var array $addedValues values entered by user which are not present in $items; only when $allowAdding=TRUE */
protected $addedValues = array();
/** @var string $startText text to display when the input field is empty */
public $startText = '';
/** @var string $emptyText text to display when there are no search results */
public $emptyText = "No Results";
/** @var string $limitText text to display when the number of selection has reached it's limit */
public $limitText = "No More Selections Are Allowed";
/** @var int|FALSE $selectionLimit max number of selected items */
public $selectionLimit = FALSE; // BUG: when limit's reached, autoSuggest only stop suggesting but does not prevent adding new values
/** @var bool $matchCase match case when suggesting */
public $matchCase = FALSE;
/** @var bool $supportFilesIncluded has support (js,css) files already been included? */
private static $supportFilesIncluded = FALSE;
/**
* If allowed users can type in new values, these can be retrieved by $control->getAddedValues().
* Otherwise the values are ignored from user.
* Setting invalid keys/values with $control->setValue() never minds this setting!
* @var bool $allowAdding
*/
protected $allowAdding = TRUE;
/**
* FALSE for getting/setting/defining/showing items as values,
* TRUE for getting/setting keys, defining keys=>values and showing values
* @var bool $useKeys
*/
protected $useKeys = TRUE;
/**
* @param mixed $label
* @param array $items
* @param bool $useKeys
* @return SuggestPicker
*/
public function __construct($label = NULL, array $items = NULL, $useKeys = TRUE) {
parent::__construct($label);
$this->useKeys = $useKeys;
if ($items !== NULL)
$this->setItems($items);
}
/**
* Allow or deny adding new values by user
* @param bool $val
* @return SuggestPicker
*/
public function setAddingAllowed($val) {
$this->allowAdding = (bool)$val;
return $this;
}
/**
* @return bool
*/
public function isAddingAllowed() {
return $this->allowAdding;
}
/**
* @return array
*/
public function getAddedValues() {
return $this->addedValues;
}
/**
* Sets items from which to choose
* @param array $items
* @return SuggestPicker
*/
public function setItems(array $items) {
$this->items = $items;
foreach ($items as $key => $value) {
if (!is_scalar($value)) {
throw new \InvalidArgumentException("All items must be scalars.");
}
}
return $this;
}
/**
* Returns items from which to choose
* @return array
*/
public function getItems() {
return $this->items;
}
/**
* Returns items in form of array of arrays('value'=>key, 'title'=>value)
* @return array
*/
protected function formatItems(array $items) {
$new = array();
foreach ($items as $k => $v)
$new[] = array('value' => $k, 'title' => $v);
return $new;
}
/**
* @return Html
*/
public function getControl() {
$box = Html::el();
$box->add( parent::getControl() );
// add JS functionality
$template = new Template(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'SuggestPicker.phtml');
$template->registerFilter(new \Nette\Templates\LatteFilter());
$template->data = $this->formatItems( $this->getItems() );
$template->preFill = $this->formatItems( $this->value );
$template->htmlId = $this->getHtmlId();
$template->startText = $this->startText;
$template->emptyText = $this->emptyText;
$template->limitText = $this->limitText;
$template->selectionLimit = $this->selectionLimit;
$template->matchCase = $this->matchCase;
$template->incl = !self::$supportFilesIncluded;
self::$supportFilesIncluded = TRUE;
$box->add((string)$template);
return $box;
}
/**
* Respects $control->useKeys when setting an array
* @param mixed $value array or comma separated keys string
* @return SuggestPicker
*/
public function setValue($value) {
/** @var bool $forceUsingKeys used to bypass the $useKey settings; autoSuggest plugin returns always keys */
$forceUsingKeys = FALSE;
// comma separated keys string support - needed by autoSuggest script
// BUG: autoSuggest doesn't make a difference between selected key and newly added value
if (!is_array($value) && $this->getForm() && $this->getForm()->isSubmitted()) {
$resultKeys = explode(',', trim($value, ','));
$forceUsingKeys = TRUE;
$value = array();
foreach($resultKeys as $key)
$value[] = trim($key);
}
// setValue is called with array of keys to be set (values are searched in $control->items)
if ($this->useKeys || $forceUsingKeys) {
$keys = $value;
foreach ($keys as $key) {
if ( isset($this->items[$key]) )
$this->value[$key] = $this->items[$key];
elseif ($forceUsingKeys && $this->isAddingAllowed())
$this->addedValues[] = $key;
elseif (!$forceUsingKeys)
throw new \InvalidArgumentException("Can't set value '$key', key is not set in control items.");
}
}
// setValue is called with array of values to be set (keys are searched in $control->items)
else {
$values = $value;
foreach ($values as $val) {
if ( ($key = array_search($val, $this->items)) !== FALSE )
$this->value[$key] = $val;
else
throw new \InvalidArgumentException("Can't set value '$val', value is not present in control items.");
}
}
return $this;
}
/**
* Respects $control->useKeys
* @return array
*/
public function getValue() {
return $this->useKeys ? array_keys($this->value) : array_values($this->value);
}
}
?>