Skip to content

Commit fe2867d

Browse files
committed
feat(glpiselect): add tree settings to entity"
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
1 parent daec8ad commit fe2867d

File tree

9 files changed

+144
-49
lines changed

9 files changed

+144
-49
lines changed

ajax/commontree.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,26 @@
5050
// Build the row content
5151
$rand = mt_rand();
5252
$additions = '<td>';
53-
$additions .= '<label for="dropdown_root_ticket_categories'.$rand.'" id="label_root_ticket_categories">';
53+
$additions .= '<label for="dropdown_show_tree_root'.$rand.'" id="label_show_tree_root">';
5454
$additions .= __('Subtree root', 'formcreator');
5555
$additions .= '</label>';
5656
$additions .= '</td>';
5757
$additions .= '<td>';
5858
$additions .= Dropdown::show($itemtype, [
59-
'name' => 'show_ticket_categories_root',
59+
'name' => 'show_tree_root',
6060
'value' => $root,
6161
'rand' => $rand,
6262
'display' => false,
6363
]);
6464
$additions .= '</td>';
6565
$additions .= '<td>';
66-
$additions .= '<label for="dropdown_show_ticket_categories_depth'.$rand.'" id="label_show_ticket_categories_depth">';
66+
$additions .= '<label for="dropdown_show_tree_depth'.$rand.'" id="label_show_tree_depth">';
6767
$additions .= __('Limit subtree depth', 'formcreator');
6868
$additions .= '</label>';
6969
$additions .= '</td>';
7070
$additions .= '<td>';
7171
$additions .= dropdown::showNumber(
72-
'show_ticket_categories_depth', [
72+
'show_tree_depth', [
7373
'rand' => $rand,
7474
'value' => $depth,
7575
'min' => 1,

inc/field/dropdownfield.class.php

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,8 @@ public function getDesignSpecializationField(): array {
7474
$itemtype = $decodedValues['itemtype'];
7575
}
7676

77-
$root = Dropdown::EMPTY_VALUE;
78-
if (isset($decodedValues['show_ticket_categories_root'])) {
79-
$root = $decodedValues['show_ticket_categories_root'];
80-
}
81-
82-
$maxDepth = Dropdown::EMPTY_VALUE;
83-
if (isset($decodedValues['show_ticket_categories_depth'])) {
84-
$maxDepth = $decodedValues['show_ticket_categories_depth'];
85-
}
77+
$root = $decodedValues['show_tree_root'] ?? Dropdown::EMPTY_VALUE;
78+
$maxDepth = $decodedValues['show_tree_depth'] ?? Dropdown::EMPTY_VALUE;
8679

8780
$optgroup = Dropdown::getStandardDropdownItemTypes();
8881

@@ -144,8 +137,8 @@ public function getDesignSpecializationField(): array {
144137
$additions .= "<input id='commonTreeDropdownRoot' type='hidden' value='$root'>";
145138
$additions .= "<input id='commonTreeDropdownMaxDepth' type='hidden' value='$maxDepth'>";
146139
$additions .= '</td>';
147-
$additions .= '</td>';
148140
$additions .= '<td>';
141+
$additions .= '</td>';
149142
$additions .= '</tr>';
150143
$additions .= '<tr class="plugin_formcreator_question_specific plugin_formcreator_dropdown">';
151144
// This row will be generated by an AJAX request
@@ -523,17 +516,8 @@ public function prepareQuestionInputForSave($input) {
523516
}
524517

525518
// Set default for depth setting
526-
if (!isset($input['show_ticket_categories_depth'])) {
527-
$input['show_ticket_categories_depth'] = -1;
528-
}
529-
if ($input['show_ticket_categories_depth'] != (int) $input['show_ticket_categories_depth']) {
530-
$input['values']['show_ticket_categories_depth'] = 0;
531-
} else {
532-
$input['values']['show_ticket_categories_depth'] = $input['show_ticket_categories_depth'];
533-
}
534-
$input['values']['show_ticket_categories_root'] = isset($input['show_ticket_categories_root'])
535-
? $input['show_ticket_categories_root']
536-
: '';
519+
$input['values']['show_tree_depth'] = (string) (int) ($input['show_tree_depth'] ?? '-1');
520+
$input['values']['show_tree_root'] = ($input['show_tree_root'] ?? '');
537521
} else if ($input['dropdown_values'] == SLA::getType()
538522
|| $input['dropdown_values'] == OLA::getType()
539523
) {
@@ -546,8 +530,8 @@ public function prepareQuestionInputForSave($input) {
546530
$input['default_values'] = isset($input['dropdown_default_value']) ? $input['dropdown_default_value'] : '';
547531
unset($input['dropdown_default_value']);
548532
unset($input['show_ticket_categories']);
549-
unset($input['show_ticket_categories_depth']);
550-
unset($input['show_ticket_categories_root']);
533+
unset($input['show_tree_depth']);
534+
unset($input['show_tree_root']);
551535
unset($input['dropdown_values']);
552536

553537
return $input;
@@ -811,7 +795,7 @@ public function isEditableField(): bool {
811795
*
812796
* @return string
813797
*/
814-
protected function getSubItemtype() {
798+
public function getSubItemtype() {
815799
return self::getSubItemtypeForValues($this->question->fields['values']);
816800
}
817801

inc/field/glpiselectfield.class.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
use PassiveDCEquipment;
6363
use PluginAppliancesAppliance;
6464
use Plugin;
65+
use CommonTreeDropdown;
6566

6667
use GlpiPlugin\Formcreator\Exception\ComparisonException;
6768

@@ -116,14 +117,25 @@ public function getDesignSpecializationField(): array {
116117
if ((new Plugin())->isActivated('appliances')) {
117118
$optgroup[__("Assets")][PluginAppliancesAppliance::class] = PluginAppliancesAppliance::getTypeName(2) . ' (' . _n('Plugin', 'Plugins', 1) . ')';
118119
}
120+
121+
$decodedValues = json_decode($this->question->fields['values'], JSON_OBJECT_AS_ARRAY);
122+
if ($decodedValues === null) {
123+
$itemtype = $this->question->fields['values'];
124+
} else {
125+
$itemtype = $decodedValues['itemtype'];
126+
}
127+
119128
array_unshift($optgroup, '---');
120129
$field = Dropdown::showFromArray('glpi_objects', $optgroup, [
121-
'value' => $this->question->fields['values'],
130+
'value' => $itemtype,
122131
'rand' => $rand,
123132
'on_change' => 'plugin_formcreator_changeGlpiObjectItemType();',
124133
'display' => false,
125134
]);
126135

136+
$root = $decodedValues['show_tree_root'] ?? '0';
137+
$maxDepth = $decodedValues['show_tree_depth'] ?? Dropdown::EMPTY_VALUE;
138+
127139
$additions = '<tr class="plugin_formcreator_question_specific">';
128140
$additions .= '<td>';
129141
$additions .= '<label for="dropdown_default_values' . $rand . '">';
@@ -132,10 +144,19 @@ public function getDesignSpecializationField(): array {
132144
$additions .= '</td>';
133145
$additions .= '<td id="dropdown_default_value_field">';
134146
$additions .= '</td>';
135-
$additions .= '<td></td>';
136-
$additions .= '<td></td>';
147+
$additions .= '<td>';
148+
$additions .= "<input id='commonTreeDropdownRoot' type='hidden' value='$root'>";
149+
$additions .= "<input id='commonTreeDropdownMaxDepth' type='hidden' value='$maxDepth'>";
150+
$additions .= '</td>';
151+
$additions .= '<td>';
152+
$additions .= '</td>';
137153
$additions .= '</tr>';
138154
$additions .= Html::scriptBlock("plugin_formcreator_changeGlpiObjectItemType($rand);");
155+
156+
$additions .= '<tr class="plugin_formcreator_question_specific plugin_formcreator_dropdown">';
157+
// This row will be generated by an AJAX request
158+
$additions .= '</tr>';
159+
139160
return [
140161
'label' => $label,
141162
'field' => $field,
@@ -168,9 +189,24 @@ public function prepareQuestionInputForSave($input) {
168189
return [];
169190
}
170191

171-
$input['values'] = $input['glpi_objects'];
192+
$itemtype = $input['glpi_objects'];
193+
$input['values'] = [
194+
'itemtype' => $itemtype
195+
];
172196
$input['default_values'] = isset($input['dropdown_default_value']) ? $input['dropdown_default_value'] : '';
197+
198+
// Params for CommonTreeDropdown fields
199+
if (is_a($itemtype, CommonTreeDropdown::class, true)) {
200+
// Set default for depth setting
201+
$input['values']['show_tree_depth'] = (int) ($input['show_tree_depth'] ?? '-1');
202+
$input['values']['show_tree_root'] = ($input['show_tree_root'] ?? '');
203+
}
204+
205+
$input['values'] = json_encode($input['values']);
206+
173207
unset($input['dropdown_default_value']);
208+
unset($input['show_tree_root']);
209+
unset($input['show_tree_depth']);
174210

175211
return $input;
176212
}

inc/question.class.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,13 +1208,13 @@ public static function getFullData($parents, $id = null) {
12081208

12091209
if (isset($data['_questions'])) {
12101210
foreach ($data['_questions'] as $key => $question) {
1211-
if ($question['fieldtype'] == "dropdown") {
1211+
if (in_array($question['fieldtype'], ['dropdown', 'glpiselect'])) {
12121212
$question = new PluginFormcreatorQuestion();
12131213
$question->getFromDB($key);
12141214

12151215
/** @var PluginFormcreatorDropdownField */
12161216
$field = PluginFormcreatorFields::getFieldInstance(
1217-
"dropdown",
1217+
$question['fieldtype'],
12181218
$question
12191219
);
12201220

@@ -1269,4 +1269,23 @@ public function deleteObsoleteItems(CommonDBTM $container, array $exclude) : boo
12691269
}
12701270
return $this->deleteByCriteria($keepCriteria);
12711271
}
1272+
1273+
/**
1274+
* Get the field object representing the question
1275+
* @return PluginFormcreatorFieldInterface|null
1276+
*/
1277+
public function getSubField(): PluginFormcreatorFieldInterface {
1278+
if ($this->isNewItem()) {
1279+
return null;
1280+
}
1281+
1282+
if ($this->field === null) {
1283+
$this->field = PluginFormcreatorFields::getFieldInstance(
1284+
$this->fields['fieldtype'],
1285+
$this
1286+
);
1287+
}
1288+
1289+
return $this->field;
1290+
}
12721291
}

inc/targetticket.class.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,9 @@ protected function setTargetAssociatedItem(array $data, PluginFormcreatorFormAns
10161016
$associateQuestion = $this->fields['associate_question'];
10171017
$question = new PluginFormcreatorQuestion();
10181018
$question->getFromDB($associateQuestion);
1019-
$itemtype = $question->fields['values'];
1019+
/** @var GlpiPlugin\Formcreator\Field\DropdownField */
1020+
$field = $question->getSubField();
1021+
$itemtype = $field->getSubItemtype();
10201022

10211023
// find the id of the associated item
10221024
$item = $DB->request([
@@ -1080,7 +1082,13 @@ protected function setTargetAssociatedItem(array $data, PluginFormcreatorFormAns
10801082

10811083
foreach ($answers as $answer) {
10821084
// Skip if the object type is not valid asset type
1083-
if (!in_array($answer['values'], $CFG_GLPI['asset_types'])) {
1085+
$question = new PluginFormcreatorQuestion();
1086+
$question->getFromDB($answer[PluginFormcreatorQuestion::getForeignKeyField()]);
1087+
/** @var GlpiPlugin\Formcreator\Field\DropdownField */
1088+
$field = $question->getSubField();
1089+
$field->deserializeValue($answer['answer']);
1090+
$itemtype = $field->getSubItemtype();
1091+
if (!in_array($itemtype, $CFG_GLPI['asset_types'])) {
10841092
continue;
10851093
}
10861094

@@ -1095,14 +1103,14 @@ protected function setTargetAssociatedItem(array $data, PluginFormcreatorFormAns
10951103
}
10961104

10971105
// Skip if item doesn't exist in the DB (shouldn't happen)
1098-
$item = new $answer['values']();
1106+
$item = new $itemtype();
10991107
if (!$item->getFromDB($answer['answer'])) {
11001108
continue;
11011109
}
11021110

11031111
// Found a valid answer, stop here
11041112
$data['items_id'] = [
1105-
$answer['values'] => [$answer['answer'] => $answer['answer']]
1113+
$itemtype => [$answer['answer'] => $answer['answer']]
11061114
];
11071115
break;
11081116
}

install/upgrade_to_2.11.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,11 @@ public function migrateCheckboxesAndMultiselect() {
193193
foreach ($DB->request($request) as $row) {
194194
$newValues = $row['values'];
195195
if (json_decode($row['values']) === null) {
196-
// Seems already migrated, skipping
197196
$newValues = json_encode(explode("\r\n", $row['values']), JSON_OBJECT_AS_ARRAY+JSON_UNESCAPED_UNICODE);
198197
}
199198
$newValues = Toolbox::addslashes_deep($newValues);
200199
$newDefault = $row['default_values'];
201200
if (json_decode($row['default_values']) === null) {
202-
// Seems already migrated, skipping
203201
$newDefault = json_encode(explode("\r\n", $row['default_values']), JSON_OBJECT_AS_ARRAY+JSON_UNESCAPED_UNICODE);
204202
}
205203
$newDefault = Toolbox::addslashes_deep($newDefault);
@@ -224,7 +222,6 @@ public function migrateCheckboxesAndMultiselect() {
224222
foreach ($DB->request($request) as $row) {
225223
$newAnswer = $row['answer'];
226224
if (json_decode($row['answer']) === null) {
227-
// Seems already migrated, skipping
228225
$newAnswer = json_encode(explode("\r\n", $row['answer']), JSON_OBJECT_AS_ARRAY+JSON_UNESCAPED_UNICODE);
229226
$newAnswer = Toolbox::addslashes_deep($newAnswer);
230227
$DB->update($answerTable, ['answer' => $newAnswer], ['id' => $row['id']]);
@@ -250,7 +247,6 @@ public function migrateRadiosAndSelect() {
250247
foreach ($DB->request($request) as $row) {
251248
$newValues = $row['values'];
252249
if (json_decode($row['values']) === null) {
253-
// Seems already migrated, skipping
254250
$newValues = json_encode(explode("\r\n", $row['values']), JSON_OBJECT_AS_ARRAY+JSON_UNESCAPED_UNICODE);
255251
$newValues = Toolbox::addslashes_deep($newValues);
256252
$DB->update($questionTable, ['values' => $newValues], ['id' => $row['id']]);

install/upgrade_to_2.12.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,41 @@ public function upgrade(Migration $migration) {
4747
$table = 'glpi_plugin_formcreator_issues';
4848
$migration->changeField($table, 'date_creation', 'date_creation', 'timestamp');
4949
$migration->changeField($table, 'date_mod', 'date_mod', 'timestamp');
50+
51+
$this->changeDropdownTreeSettings();
52+
}
53+
54+
/**
55+
* Convert values field of wuestion from form
56+
* {"itemtype":"ITILCategory","show_ticket_categories_depth":"0","show_ticket_categories_root":"6354"}
57+
* to form
58+
* {"itemtype":"ITILCategory","show_tree_depth":-1,"show_tree_root":false}
59+
*
60+
* @return void
61+
*/
62+
public function changeDropdownTreeSettings() {
63+
global $DB;
64+
65+
$table = 'glpi_plugin_formcreator_questions';
66+
67+
$request = [
68+
'SELECT' => ['id', 'values'],
69+
'FROM' => $table,
70+
'WHERE' => ['fieldtype' => ['dropdown']],
71+
];
72+
foreach ($DB->request($request) as $row) {
73+
$newValues = $row['values'];
74+
$values = json_decode($row['values']);
75+
if ($values === null) {
76+
continue;
77+
}
78+
$newValues = $values;
79+
unset($newValues['show_ticket_categories_root']);
80+
unset($newValues['show_ticket_categories_depth']);
81+
$newValues['show_tree_root'] = $values['show_ticket_categories_root'] ?? '';
82+
$newValues['show_tree_depth'] = $values['show_ticket_categories_depth'] ?? '-1';
83+
$newValues = json_encode($newValues);
84+
$DB->update($table, ['values' => $newValues], ['id' => $row['id']]);
85+
}
5086
}
5187
}

js/scripts.js.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,22 @@ function plugin_formcreator_changeGlpiObjectItemType() {
13531353
}).done(function(response) {
13541354
$('#dropdown_default_value_field').html(response);
13551355
});
1356+
1357+
$.ajax({
1358+
url: formcreatorRootDoc + '/ajax/commontree.php',
1359+
type: 'GET',
1360+
data: {
1361+
itemtype: glpi_object,
1362+
root: $("#commonTreeDropdownRoot").val(),
1363+
maxDepth: $("#commonTreeDropdownMaxDepth").val(),
1364+
},
1365+
}).done(function(response) {
1366+
$('.plugin_formcreator_dropdown').html(response);
1367+
$('.plugin_formcreator_dropdown').toggle(true);
1368+
}).fail(function() {
1369+
$('.plugin_formcreator_dropdown').html("");
1370+
$('.plugin_formcreator_dropdown').toggle(false);
1371+
});
13561372
}
13571373

13581374
// === CONDITIONS ===

0 commit comments

Comments
 (0)