/
actorfield.class.php
276 lines (257 loc) · 10 KB
/
actorfield.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
<?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 GPLv3+ http://www.gnu.org/licenses/gpl.txt
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/
class PluginFormcreatorActorField extends PluginFormcreatorField
{
const IS_MULTIPLE = true;
public static function getName() {
return _n('Actor', 'Actors', 1, 'formcreator');
}
public function displayField($canEdit = true) {
global $CFG_GLPI;
$readonly = $canEdit ? 'false' : 'true';
if (isset($this->fields['answer'])) {
$value = $this->sanitizeValue($this->fields['answer']);
} else {
$value = $this->sanitizeValue($this->fields['default_values']);
}
$initialValue = [];
foreach ($value as $id => $item) {
$initialValue[] = [
'id' => $id,
'text' => $item,
];
}
$initialValue = json_encode($initialValue);
// Value needs to be non empty to allow execition of select2's initSelection
if (version_compare(GLPI_VERSION, "9.3") >= 0) {
echo '<select multiple
name="formcreator_field_' . $this->fields['id'] . '[]"
id="formcreator_field_' . $this->fields['id']. '"
value="" />';
echo Html::scriptBlock('$(function() {
$("#formcreator_field_' . $this->fields['id']. '").select2({
tokenSeparators: [",", ";"],
minimumInputLength: 0,
ajax: {
url: "' . $CFG_GLPI['root_doc'] . '/ajax/getDropdownUsers.php",
type: "POST",
dataType: "json",
data: function (params, page) {
return {
entity_restrict: -1,
searchText: params.term,
page_limit: 100,
page: page
}
},
results: function (data, page) {
var more = (data.count >= 100);
return {results: data.results, pagination: {"more": more}};
}
},
createSearchChoice: function itemCreator(term, data) {
if ($(data).filter(function() {
return this.text.localeCompare(term) === 0;
}).length === 0) {
return { id: term, text: term };
}
},
initSelection: function (element, callback) {
callback(JSON.parse(\'' . $initialValue . '\'));
}
})
$("#formcreator_field_' . $this->fields['id'] . '").on("change", function(e) {
var selectedValues = $("#formcreator_field_' . $this->fields['id'] . '").val();
formcreatorChangeValueOf (' . $this->fields['id']. ', selectedValues);
});
});');
} else {
echo '<input
type="hidden"
name="formcreator_field_' . $this->fields['id'] . '"
id="formcreator_field_' . $this->fields['id']. '"
value="" />';
echo Html::scriptBlock('$(function() {
$("#formcreator_field_' . $this->fields['id']. '").select2({
multiple: true,
tokenSeparators: [",", ";"],
minimumInputLength: 0,
ajax: {
url: "' . $CFG_GLPI['root_doc'] . '/ajax/getDropdownUsers.php",
type: "POST",
dataType: "json",
data: function (term, page) {
return {
entity_restrict: -1,
searchText: term,
page_limit: 100,
page: page
}
},
results: function (data, page) {
var more = (data.count >= 100);
return {results: data.results, more: more};
}
},
createSearchChoice: function itemCreator(term, data) {
if ($(data).filter(function() {
return this.text.localeCompare(term) === 0;
}).length === 0) {
return { id: term, text: term };
}
},
initSelection: function (element, callback) {
callback(JSON.parse(\'' . $initialValue . '\'));
}
})
$("#formcreator_field_' . $this->fields['id'] . '").on("change", function(e) {
var selectedValues = $("#formcreator_field_' . $this->fields['id'] . '").val();
formcreatorChangeValueOf (' . $this->fields['id']. ', selectedValues);
});
});');
}
}
public function serializeValue($value) {
$serialized = [];
$value = explode("\r\n", $value);
foreach ($value as $item) {
if (filter_var($item, FILTER_VALIDATE_EMAIL)) {
// a single email address
$serialized[$item] = $item;
} else {
$user = new User();
$user->getFromDBbyName($item);
if (!$user->isNewItem()) {
// A user known in the DB
$serialized[$user->getID()] = $item;
}
}
}
return implode(',', (array_keys($serialized)));
}
public function deserializeValue($value) {
$deserialized = [];
$serialized = explode(',', $value);
if ($serialized !== null) {
foreach ($serialized as $item) {
$item = trim($item);
if (filter_var($item, FILTER_VALIDATE_EMAIL) !== false) {
$deserialized[$item] = $item;
} else if (!empty($item) && ctype_digit($item) && intval($item)) {
$user = new User();
$user->getFromDB($item);
if (!$user->isNewItem()) {
// A user known in the DB
$deserialized[$user->getID()] = $user->getField('name');
}
}
}
}
return implode("\r\n", $deserialized);
}
protected function sanitizeValue($value) {
$value = trim($value);
if (is_array(json_decode($value))) {
// For GLPI 9.3+
// the HTML field is no longer an HTML input
// it is now a HTML select
$answerValue = array_filter(json_decode($value));
} else {
// for GLPI < 9.3 and default values from DB regardless GLPI version
$answerValue = array_filter(explode(',', $value));
}
$unknownUsers = [];
$knownUsers = [];
$idToCheck = [];
foreach ($answerValue as $item) {
$item = trim($item);
if (filter_var($item, FILTER_VALIDATE_EMAIL) !== false) {
$unknownUsers[$item] = $item;
} else if (!empty($item) && ctype_digit($item) && intval($item)) {
$user = new User();
$user->getFromDB($item);
if (!$user->isNewItem()) {
// A user known in the DB
$knownUsers[$user->getID()] = $user->getField('name');
}
}
}
return $knownUsers + $unknownUsers;
}
public function isValid($value) {
$sanitized = $this->sanitizeValue($value);
// Ignore empty values
$value = trim($value);
$value = array_filter(explode(',', $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;
}
// If an item has been removed by sanitization, then the data is not valid
if (count($sanitized) != count($value)) {
Session::addMessageAfterRedirect(__('Invalid value:', 'formcreator') . ' ' . $this->getLabel(), false, ERROR);
return false;
}
return true;
}
public function getValue() {
if (isset($this->fields['answer'])) {
$value = $this->sanitizeValue($this->fields['answer']);
} else {
$value = $this->sanitizeValue($this->fields['default_values']);
}
return implode(',', $value);
}
public static function getPrefs() {
return [
'required' => 1,
'default_values' => 1,
'values' => 0,
'range' => 0,
'show_empty' => 0,
'regex' => 0,
'show_type' => 0,
'dropdown_value' => 0,
'glpi_objects' => 0,
'ldap_values' => 0,
];
}
public static function getJSFields() {
$prefs = self::getPrefs();
return "tab_fields_fields['actor'] = 'showFields(" . implode(', ', $prefs) . ");';";
}
public function prepareQuestionValuesForEdit($input) {
return $this->deserializeValue($input);
}
}