Skip to content

Commit 626ee9a

Browse files
Helpdesk forms: add support for conditions on radio/checkbox/dropdown questions
1 parent 807cc84 commit 626ee9a

27 files changed

+985
-53
lines changed

js/modules/Forms/Condition/Engine.js

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class GlpiFormConditionEngine
6161
#getQuestionsData(container)
6262
{
6363
const questions_data = new Map();
64+
const array_values = new Map(); // Store array values temporarily
6465

6566
// Map questions that can be used as condition critera by others items.
6667
const questions_criteria_ids = [];
@@ -75,26 +76,57 @@ export class GlpiFormConditionEngine
7576
const data = new FormData(container);
7677
for (const entry of data.entries()) {
7778
const key = entry[0];
79+
const value = entry[1];
7880

7981
// Skip data unrelated to form answers
8082
if (key.indexOf('answers_') == -1) {
8183
continue;
8284
}
8385

84-
if (key.indexOf('[') == -1) {
85-
// Read simple text value
86-
const simple_key_regex = RegExp('answers_(.*)');
86+
if (key.includes('[')) {
87+
// Handle array values: answers_questionId[] or answers_questionId[key]
88+
const array_key_regex = /^answers_([^[]+)(\[\d*\]|\[[^\]]*\])$/;
89+
const match = array_key_regex.exec(key);
90+
91+
if (match && match[1]) {
92+
const question_id = match[1];
93+
94+
// Check if this question is a criteria
95+
if (questions_criteria_ids.indexOf(question_id) !== -1) {
96+
// Initialize array for this question if needed
97+
if (!array_values.has(question_id)) {
98+
array_values.set(question_id, []);
99+
}
100+
101+
// Add value to the array
102+
if (value !== '') {
103+
array_values.get(question_id).push(value);
104+
}
105+
}
106+
}
107+
} else {
108+
// Handle simple values: answers_questionId
109+
const simple_key_regex = /^answers_(.*)$/;
87110
const match = simple_key_regex.exec(key);
88111

89-
// Extra value if it is from a criteria
90-
if (questions_criteria_ids.indexOf(match[1]) != -1) {
91-
questions_data.set(match[1], entry[1]);
112+
if (match && match[1]) {
113+
const question_id = match[1];
114+
115+
// Extra value if it is from a criteria
116+
if (questions_criteria_ids.indexOf(question_id) !== -1) {
117+
questions_data.set(question_id, value);
118+
}
92119
}
93-
} else {
94-
// Value is an array, not yet supported (TODO)
95120
}
96121
};
97122

123+
// Add collected array values to questions_data
124+
for (const [question_id, values] of array_values.entries()) {
125+
if (values.length > 0) {
126+
questions_data.set(question_id, values);
127+
}
128+
}
129+
98130
return questions_data;
99131
}
100132

@@ -103,8 +135,14 @@ export class GlpiFormConditionEngine
103135
// Build POST data
104136
const form_data = new FormData();
105137
form_data.append('form_id', this.#form_id);
106-
for (const entry of data.answers.entries()) {
107-
form_data.append(`answers[${entry[0]}]`, entry[1]);
138+
for (const [key, value] of data.answers.entries()) {
139+
if (Array.isArray(value)) {
140+
value.forEach((v, i) => {
141+
form_data.append(`answers[${key}][${i}]`, v);
142+
});
143+
} else {
144+
form_data.append(`answers[${key}]`, value);
145+
}
108146
}
109147

110148
// Send request

js/modules/Forms/ConditionEditorController.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,13 @@ export class GlpiFormConditionEditorController
222222
const condition_value = $(condition).find(
223223
'[data-glpi-conditions-editor-value]'
224224
);
225-
if (condition_value.length > 0) {
225+
if (condition_value.length === 1) {
226226
condition_data.value = condition_value.val();
227+
} else if (condition_value.length > 1) {
228+
condition_data.value = [];
229+
condition_value.each((_index, element) => {
230+
condition_data.value.push(element.value);
231+
});
227232
}
228233

229234
conditions_data.push(condition_data);

js/modules/Forms/EditorController.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,28 @@ export class GlpiFormEditorController
12681268
name = input.name;
12691269
}
12701270

1271-
extra_data[name] = input.value;
1271+
const is_map = name.indexOf("[") !== -1
1272+
&& name.indexOf("]") !== -1
1273+
&& name.indexOf("[]") === -1
1274+
;
1275+
1276+
if (is_map) {
1277+
// Handle complex arrays with key and values
1278+
const matches = name.match(/^(.*)\[(.*)\]$/);
1279+
if (matches === null) {
1280+
throw new Error(`Unexpected input name: ${name}`);
1281+
}
1282+
if (extra_data[matches[1]] === undefined) {
1283+
extra_data[matches[1]] = {};
1284+
}
1285+
if (extra_data[matches[1]][matches[2]] === undefined) {
1286+
extra_data[matches[1]][matches[2]] = {};
1287+
}
1288+
extra_data[matches[1]][matches[2]] = input.value;
1289+
} else {
1290+
// Simple value
1291+
extra_data[name] = input.value;
1292+
}
12721293
}
12731294

12741295
return extra_data;

0 commit comments

Comments
 (0)