/
field.class.php
217 lines (193 loc) · 6.69 KB
/
field.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
<?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
* ---------------------------------------------------------------------
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
require_once(realpath(dirname(__FILE__ ) . '/../../../inc/includes.php'));
abstract class PluginFormcreatorField implements PluginFormcreatorFieldInterface
{
const IS_MULTIPLE = false;
protected $fields = [];
/**
* @param unknown $fields
* @param array $data
*/
public function __construct($fields, $data = []) {
$this->fields = $fields;
$this->fields['answer'] = $data;
}
/**
* Transform input to properly save it in the database
*
* @param array $input data to transform before save
*
* @return array input data to save as is
*/
public function prepareQuestionInputForSave($input) {
return $input;
}
/**
* Prepares an answer value for output in a target object
* @param string|array $input the answer to format for a target (ticket or change)
* @return string
*/
public function prepareQuestionInputForTarget($input) {
return Toolbox::addslashes_deep($input);
}
/**
* Prepares a default value or set of values for question edition
*
* @param string $input
* @return string
*/
public function prepareQuestionValuesForEdit($input) {
return $input;
}
/**
* Output HTML to display the field
* @param boolean $canEdit is the field editable ?
*/
public function show($canEdit = true) {
$required = ($canEdit && $this->fields['required']) ? ' required' : '';
echo '<div class="form-group ' . $required . '" id="form-group-formcreator_field_' . $this->fields['id'] . '">';
echo '<label for="formcreator_field_' . $this->fields['id'] . '">';
echo $this->getLabel();
if ($canEdit && $this->fields['required']) {
echo ' <span class="red">*</span>';
}
echo '</label>';
echo '<div class="help-block">' . html_entity_decode($this->fields['description']) . '</div>';
echo '<div class="form_field">';
$this->displayField($canEdit);
echo '</div>';
echo '</div>';
$value = is_array($this->getAnswer()) ? json_encode($this->getAnswer()) : $this->getAnswer();
// $value = json_encode($this->getAnswer());
if ($this->fields['fieldtype'] == 'dropdown') {
echo Html::scriptBlock('$(function() {
formcreatorAddValueOf(' . $this->fields['id'] . ', "'
. str_replace("\r\n", "\\r\\n", addslashes($this->fields['answer'])) . '");
})');
} else {
echo Html::scriptBlock('$(function() {
formcreatorAddValueOf(' . $this->fields['id'] . ', "'
. str_replace("\r\n", "\\r\\n", addslashes(html_entity_decode($value))) . '");
})');
}
}
/**
* Outputs the HTML representing the field
* @param string $canEdit
*/
public function displayField($canEdit = true) {
if ($canEdit) {
echo '<input type="text" class="form-control"
name="formcreator_field_' . $this->fields['id'] . '"
id="formcreator_field_' . $this->fields['id'] . '"
value="' . $this->getAnswer() . '"
onchange="formcreatorChangeValueOf(' . $this->fields['id'] . ', this.value);" />';
} else {
echo $this->getAnswer();
}
}
public function getLabel() {
return $this->fields['name'];
}
public function getField() {
}
public function getValue() {
if (isset($this->fields['answer'])) {
if (!is_array($this->fields['answer']) && is_array(json_decode($this->fields['answer']))) {
return json_decode($this->fields['answer']);
}
return $this->fields['answer'];
} else {
if (static::IS_MULTIPLE) {
return explode("\r\n", $this->fields['default_values']);
}
if (!$this->fields['show_empty'] && empty($this->fields['default_values'])) {
$availableValues = $this->getAvailableValues();
return array_shift($availableValues);
}
return $this->fields['default_values'];
}
}
public function getAnswer() {
return $this->getValue();
}
/**
* Gets the available values for the field
*
* @return array
*/
public function getAvailableValues() {
return explode("\r\n", $this->fields['values']);
}
/**
* Is the field valid for the given value?
*
* @param string $value
*
* @return boolean True if the field has a valid value, false otherwise
*/
public function isValid($value) {
// If the field is required it can't be empty
if ($this->isRequired() && empty($value)) {
Session::addMessageAfterRedirect(
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
false,
ERROR);
return false;
}
// All is OK
return true;
}
/**
* Is the field required?
*
* @return boolean
*/
public function isRequired() {
return $this->fields['required'];
}
/**
* trim values separated by \r\n
* @param string $value a value or default value
* @return string
*/
protected function trimValue($value) {
$value = explode('\\r\\n', $value);
$value = array_map('trim', $value);
return implode('\\r\\n', $value);
}
}