|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.newicon.net/neon |
|
4
|
|
|
* @copyright Copyright (c) 18/10/2016 Newicon Ltd |
|
5
|
|
|
* @license http://www.newicon.net/neon/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace neon\core\form\fields; |
|
9
|
|
|
|
|
10
|
|
|
use neon\core\helpers\Arr; |
|
11
|
|
|
use neon\core\helpers\Html; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Choice |
|
15
|
|
|
* Allows one item to be saved from a selection of choices |
|
16
|
|
|
* @author Steve O'Brien - Newicon Ltd |
|
17
|
|
|
* @package neon\core\form\fields |
|
18
|
|
|
*/ |
|
19
|
|
|
class Choice extends Field |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @inheritdoc |
|
23
|
|
|
*/ |
|
24
|
|
|
public $ddsDataType = 'choice'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Whether the select box allows the user to clear the current selection |
|
28
|
|
|
* It's useful to turn this off when a selection must be made, a default is set, and null is not permitted |
|
29
|
|
|
* For example: select the start day of your work week day, where the default is Monday - clearing this selection |
|
30
|
|
|
* makes no sense as you would leave it as monday |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
public $allowClear = true; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Whether a user can create new options based on what they type in the box |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
public $create = false; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getFieldHtml() |
|
45
|
|
|
{ |
|
46
|
|
|
return Html::tag('neon-select', '', [ |
|
47
|
|
|
'v-bind' => $this->toJson(), |
|
48
|
|
|
'name' => $this->getInputName() |
|
49
|
|
|
]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getFilterField() |
|
56
|
|
|
{ |
|
57
|
|
|
$field = parent::getFilterField(); |
|
58
|
|
|
// if the select option has the creation ability - we want this off when used in filters. |
|
59
|
|
|
$field['create'] = false; |
|
60
|
|
|
// without this a select box always has a value meaning the grid will always filter by it |
|
61
|
|
|
$field['allowClear'] = true; |
|
62
|
|
|
return $field; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var array - store the choice items |
|
67
|
|
|
* [ 'key' => 'The display value'] ] |
|
68
|
|
|
*/ |
|
69
|
|
|
private $_items = []; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set the choice items |
|
73
|
|
|
* This is key => value where the value is what is displayed in the choice and the key is the value stored against the field |
|
74
|
|
|
* *Note* this also support groups where an item is an array - the key will be used as the group label |
|
75
|
|
|
* *Note* this also supports an array of associative arrays in the format ['key' => 'the key', 'value' => 'The Value'] |
|
76
|
|
|
* @param array $items |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function setItems($items) |
|
80
|
|
|
{ |
|
81
|
2 |
|
$test = current($items); |
|
82
|
2 |
|
if (is_array($test) && isset($test['key']) && isset($test['value'])) { |
|
83
|
2 |
|
$this->_items = Arr::map($items, 'key', 'value'); |
|
84
|
|
|
} else { |
|
85
|
2 |
|
$this->_items = $items; |
|
86
|
|
|
} |
|
87
|
2 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* The array of choices for e.g. `[ 'key' => 'The display value' ]` |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function getItems() |
|
95
|
|
|
{ |
|
96
|
2 |
|
return $this->_items; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Clear the items |
|
101
|
|
|
* @return array the previously set items |
|
102
|
|
|
*/ |
|
103
|
|
|
public function clearItems() |
|
104
|
|
|
{ |
|
105
|
|
|
$items = $this->_items; |
|
106
|
|
|
$this->_items = []; |
|
107
|
|
|
return $items; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the items suitable for display in standard html elements such as checkbox |
|
112
|
|
|
* lists and radio sets. |
|
113
|
|
|
* |
|
114
|
|
|
* This maps items to key=>value pairs and adds some extra html around the |
|
115
|
|
|
* displayed text to enable enhanced css handling. Use neonField_labelText to |
|
116
|
|
|
* access this in CSS |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getItemsDisplay() |
|
119
|
|
|
{ |
|
120
|
|
|
$items = $this->getItems(); |
|
121
|
|
|
foreach ($items as $key => $value) |
|
122
|
|
|
$items[$key] = '<span class="neonField_labelText">'.$value.'</span>'; |
|
123
|
|
|
return $items; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* The value from the database is the uuid of the linked field |
|
128
|
|
|
* @param array $value in format: ['key' => 'key', 'value' => 'Human Readable String', 'type' => 'choice'] |
|
129
|
|
|
* @inheritDoc |
|
130
|
|
|
*/ |
|
131
|
2 |
|
public function setValueFromDb($value) |
|
132
|
|
|
{ |
|
133
|
|
|
// A choice field received an array of information from Daedalus |
|
134
|
2 |
|
if (is_array($value) && isset($value['key'])) { |
|
135
|
2 |
|
parent::setValueFromDb($value['key']); |
|
136
|
|
|
} else { |
|
137
|
|
|
// if it is not array assume this is the key of the field |
|
138
|
|
|
parent::setValueFromDb($value); |
|
139
|
|
|
} |
|
140
|
2 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getProperties() |
|
147
|
|
|
{ |
|
148
|
|
|
return array_merge(parent::getProperties(), ['items', 'allowClear', 'create']); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get a displayable representation of the fields value |
|
153
|
|
|
* Assumes a single value for the choice |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
2 |
|
public function getValueDisplay($context='') |
|
157
|
|
|
{ |
|
158
|
2 |
|
$items = $this->getItems(); |
|
159
|
2 |
|
return isset($items[$this->getValue()]) ? $items[$this->getValue()] : ''; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Check if the items (array) is in ('key' => 'value') |
|
164
|
|
|
* if not then get it mapped via yii Arr::map helper |
|
165
|
|
|
* |
|
166
|
|
|
* @return array |
|
167
|
|
|
* @deprecated |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getItemsMapped() |
|
170
|
|
|
{ |
|
171
|
|
|
$items = $this->getItems(); |
|
172
|
|
|
if (Arr::isIndexed($items, true)) { |
|
173
|
|
|
$items = Arr::map($items, 'key', 'value'); |
|
174
|
|
|
} |
|
175
|
|
|
return $items; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @return array |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function extractChoices() |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->getItems(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @inheritdoc |
|
188
|
|
|
*/ |
|
189
|
|
|
public function getComponentDetails() |
|
190
|
|
|
{ |
|
191
|
|
|
return [ |
|
192
|
|
|
'label' => 'Choice (Abstract)', 'group' => 'Do Not Use', 'icon' => 'fa fa-list', 'order' => 1500, |
|
193
|
|
|
]; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @inheritdoc |
|
198
|
|
|
*/ |
|
199
|
4 |
|
public function getValue() |
|
200
|
|
|
{ |
|
201
|
4 |
|
$value = parent::getValue(); |
|
202
|
4 |
|
return is_array($value) ? array_filter($value) : $value; |
|
203
|
|
|
} |
|
204
|
|
|
} |