-
Notifications
You must be signed in to change notification settings - Fork 125
/
questiondependency.class.php
223 lines (189 loc) · 7.43 KB
/
questiondependency.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
<?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/>.
* ---------------------------------------------------------------------
* @copyright Copyright © 2011 - 2019 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
* ---------------------------------------------------------------------
*/
use GlpiPlugin\Formcreator\Exception\ImportFailureException;
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
/**
* A question parameter to handle a depdency to an other question. For example
* the content og the question A is computed from the content of the question B. In
* this case the question A has this parameter to maitnain the dependency to the
* question B
*/
class PluginFormcreatorQuestionDependency
extends PluginFormcreatorQuestionParameter
{
/** @var string $fieldtype type of field useable for the dependency */
protected $fieldType;
protected $domId = 'plugin_formcreator_questionDependency';
/**
* @param PluginFormcreatorFieldInterface $field Field
* @param array $options
* - fieldName: name of the HTML input tag
* - label : label for the parameter
* - fieldType: array of field types the dependency should filter
*/
public function __construct(PluginFormcreatorFieldInterface $field, array $options) {
parent::__construct($field, $options);
$this->fieldtype = isset($options['fieldType']) ? $options['fieldType'] : [];
}
public function getParameterFormSize() {
return 0;
}
public function getParameterForm(PluginFormcreatorForm $form, PluginFormcreatorQuestion $question) {
// get questions of type text in the form
$eligibleQuestions = [];
$criteria = ['fieldtype' => $this->fieldtype];
foreach ($question->getQuestionsFromForm($form->getID(), $criteria) as $item) {
$eligibleQuestions[$item->getID()] = $item->getField('name');
}
// get the name of the HTML input field
$name = '_parameters[' . $this->field->getFieldTypeName() . '][' . $this->fieldName . ']';
// get the selected value in the dropdown
$selected = 0;
if (!$question->isNewItem()) {
$this->getFromDBByCrit([
'plugin_formcreator_questions_id' => $question->getID(),
'fieldname' => $this->fieldName,
]);
if (!$this->isNewItem()) {
$selected = $this->fields['plugin_formcreator_questions_id_2'];
}
}
// get the HTML for the dropdown
$questionsDropdown = Dropdown::showFromArray(
$name . '[plugin_formcreator_questions_id_2]',
$eligibleQuestions,
[
'display' => false,
'display_emptychoice' => true,
'value' => $selected,
'used' => [$question->getID() => ''],
]
);
// build HTML code
$selector = $this->domId;
$out = '';
$out.= '<td id="' . $selector . '">' . $this->label . '</td>';
$out.= '<td>' . $questionsDropdown . '</td>';
return $out;
}
public function getJsShowHideSelector() {
return "#" . $this->domId;
}
public function prepareInputForAdd($input) {
$input = parent::prepareInputForAdd($input);
$input['fieldname'] = $this->fieldName;
return $input;
}
public function getFieldName() {
return $this->fieldName;
}
public function post_getEmpty() {
$this->fields['plugin_formcreator_questions_id_2'] = '0';
}
public static function import(PluginFormcreatorLinker $linker, $input = [], $containerId = 0) {
global $DB;
if (!isset($input['uuid']) && !isset($input['id'])) {
throw new ImportFailureException('UUID or ID is mandatory');
}
$questionFk = PluginFormcreatorQuestion::getForeignKeyField();
$input[$questionFk] = $containerId;
$question = new PluginFormcreatorQuestion();
$question->getFromDB($containerId);
$field = PluginFormcreatorFields::getFieldInstance(
$question->fields['fieldtype'],
$question
);
$item = $field->getEmptyParameters();
$item = $item[$input['fieldname']];
// Find an existing condition to update, only if an UUID is available
$itemId = false;
/** @var string $idKey key to use as ID (id or uuid) */
$idKey = 'id';
if (isset($input['uuid'])) {
// Try to find an existing item to update
$idKey = 'uuid';
$itemId = plugin_formcreator_getFromDBByField(
$item,
'uuid',
$input['plugin_formcreator_questions_id_2']
);
}
// escape text fields
foreach (['fieldname'] as $key) {
$input[$key] = $DB->escape($input[$key]);
}
// set ID for linked objects
$linked = $linker->getObject($input['plugin_formcreator_questions_id_2'], PluginFormcreatorQuestion::class);
if ($linked === false) {
$linker->postpone($input[$idKey], $item->getType(), $input, $containerId);
return false;
}
$input['plugin_formcreator_questions_id_2'] = $linked->getID();
// Add or update condition
$originalId = $input[$idKey];
if ($itemId !== false) {
$input['id'] = $itemId;
$item->update($input);
} else {
unset($input['id']);
$itemId = $item->add($input);
}
if ($itemId === false) {
$typeName = strtolower(self::getTypeName());
throw new ImportFailureException(sprintf(__('failed to add or update the %1$s %2$s', 'formceator'), $typeName, $input['name']));
}
// add the parameter to the linker
$linker->addObject($originalId, $item);
return $itemId;
}
public function export($remove_uuid = false) {
if ($this->isNewItem()) {
return false;
}
$parameter = $this->fields;
$questionFk = PluginFormcreatorQuestion::getForeignKeyField();
unset($parameter[$questionFk]);
// remove ID or UUID
$idToRemove = 'id';
if ($remove_uuid) {
$idToRemove = 'uuid';
} else {
// Convert IDs into UUIDs
$question = new PluginFormcreatorQuestion();
$question->getFromDB($parameter['plugin_formcreator_questions_id_2']);
$parameter['plugin_formcreator_questions_id_2'] = $question->fields['uuid'];
}
unset($parameter[$idToRemove]);
return $parameter;
}
}