-
Notifications
You must be signed in to change notification settings - Fork 125
/
checkboxesfield.class.php
283 lines (248 loc) · 8.64 KB
/
checkboxesfield.class.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
<?php
/**
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Formcreator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
* @author Thierry Bugier
* @author Jérémy Moreau
* @copyright Copyright © 2011 - 2018 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/
class PluginFormcreatorCheckboxesField extends PluginFormcreatorField
{
public function isPrerequisites() {
return true;
}
public function displayField($canEdit = true) {
if ($canEdit) {
$id = $this->fields['id'];
$rand = mt_rand();
$fieldName = 'formcreator_field_' . $id;
$domId = $fieldName . '_' . $rand;
$values = [];
$values = $this->getAvailableValues();
if (!empty($values)) {
echo '<div class="checkboxes">';
$i = 0;
foreach ($values as $value) {
if ((trim($value) != '')) {
$i++;
$current_value = $this->value;
echo "<div class='checkbox'>";
echo Html::getCheckbox([
'title' => htmlentities($value, ENT_QUOTES),
'id' => $domId.'_'.$i,
'name' => htmlentities($fieldName, ENT_QUOTES) . '[]',
'value' => htmlentities($value, ENT_QUOTES),
'zero_on_empty' => false,
'checked' => in_array($value, $this->value)
]);
echo '<label for="' . $domId . '_' . $i . '">';
echo ' ' . $value;
echo '</label>';
echo "</div>";
}
}
echo '</div>';
}
echo Html::scriptBlock("$(function() {
pluginFormcreatorInitializeCheckboxes('$fieldName', '$rand');
});");
} else {
if (count($this->value)) {
echo implode('<br />', $this->value);
} else {
echo '';
}
}
}
/**
* Gets the available values for the field
* @return array available values
*/
public function getAvailableValues() {
return explode("\r\n", $this->fields['values']);
}
public function serializeValue() {
if ($this->value === null || $this->value === '') {
return '';
}
return implode("\r\n", $this->value);
}
public function deserializeValue($value) {
$deserialized = [];
$this->value = ($value !== null && $value !== '')
? explode("\r\n", $value)
: [];
}
public function getValueForDesign() {
if ($this->value === null) {
return '';
}
return implode("\r\n", $this->value);
}
public function parseAnswerValues($input) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!isset($input[$key])) {
$input[$key] = [];
} else {
if (!is_array($input[$key])) {
return false;
}
}
$this->value = $input[$key];
return true;
}
public function isValid() {
$value = $this->value;
if (is_null($value)) {
$value = [];
}
// If the field is required it can't be empty
if ($this->isRequired() && count($value) <= 0) {
Session::addMessageAfterRedirect(
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
false,
ERROR);
return false;
}
return $this->isValidValue($value);
}
private function isValidValue($value) {
$parameters = $this->getParameters();
// Check the field matches the format regex
if (!$parameters['range']->isNewItem()) {
$rangeMin = $parameters['range']->fields['range_min'];
$rangeMax = $parameters['range']->fields['range_max'];
if (strlen($rangeMin) > 0 && count($value) < $rangeMin) {
$message = sprintf(__('The following question needs of at least %d answers', 'formcreator'), $rangeMin);
Session::addMessageAfterRedirect($message . ' ' . $this->getLabel(), false, ERROR);
return false;
}
if (strlen($rangeMax) > 0 && count($value) > $rangeMax) {
$message = sprintf(__('The following question does not accept more than %d answers', 'formcreator'), $rangeMax);
Session::addMessageAfterRedirect($message . ' ' . $this->getLabel(), false, ERROR);
return false;
}
}
return true;
}
public static function getName() {
return __('Checkboxes', 'formcreator');
}
public function prepareQuestionInputForSave($input) {
if (isset($input['values'])) {
if (empty($input['values'])) {
Session::addMessageAfterRedirect(
__('The field value is required:', 'formcreator') . ' ' . $input['name'],
false,
ERROR);
return [];
} else {
$input['values'] = $this->trimValue($input['values']);
}
}
if (isset($input['default_values'])) {
$this->value = explode('\r\n', $input['default_values']);
$this->value = array_map('trim', $this->value);
$this->value = array_filter($this->value, function($value) {
return ($value !== '');
});
$input['default_values'] = implode('\r\n', $this->value);
}
return $input;
}
public function getValueForTargetText() {
global $CFG_GLPI;
$value = [];
$values = $this->getAvailableValues();
if (count($this->value) === 0) {
return '';
}
foreach ($this->value as $input) {
if (in_array($input, $values)) {
$value[] = addslashes($input);
}
}
if (version_compare(PluginFormcreatorCommon::getGlpiVersion(), 9.4) >= 0 || $CFG_GLPI['use_rich_text']) {
$value = '<br />' . implode('<br />', $value);
} else {
$value = '\r\n' . implode('\r\n', $value);
}
return $value;
}
public function getDocumentsForTarget() {
return [];;
}
public static function getPrefs() {
return [
'required' => 1,
'default_values' => 1,
'values' => 1,
'range' => 1,
'show_empty' => 0,
'regex' => 0,
'show_type' => 1,
'dropdown_value' => 0,
'glpi_objects' => 0,
'ldap_values' => 0,
];
}
public static function getJSFields() {
$prefs = self::getPrefs();
return "tab_fields_fields['checkboxes'] = 'showFields(" . implode(', ', $prefs) . ");';";
}
public function getEmptyParameters() {
return [
'range' => new PluginFormcreatorQuestionRange(
$this,
[
'fieldName' => 'range',
'label' => __('Range', 'formcreator'),
'fieldType' => ['text'],
]
),
];
}
public function equals($value) {
if (!is_array($this->value)) {
// No checkbox enabled
return ($value === '');
}
return in_array($value, $this->value);
}
public function notEquals($value) {
return !$this->equals($value);
}
public function greaterThan($value) {
throw new PluginFormcreatorComparisonException('Meaningless comparison');
}
public function lessThan($value) {
throw new PluginFormcreatorComparisonException('Meaningless comparison');
}
public function isAnonymousFormCompatible() {
return true;
}
}