Skip to content

Commit f6e09f0

Browse files
committed
fix(targetticket): last valid category ignored visibility state
Signed-off-by: Thierry Bugier <[email protected]>
1 parent 24dacd2 commit f6e09f0

File tree

5 files changed

+225
-124
lines changed

5 files changed

+225
-124
lines changed

inc/fields.class.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ public static function getNames() {
112112
}
113113

114114
/**
115-
* Reset the cache of visibility of hide-able items
115+
* Reset cache of evaluated visibility
116+
* used for unit tests
116117
*
117118
* @return void
118119
*/

inc/fields/dropdownfield.class.php

+6
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,15 @@ public function prepareQuestionInputForSave($input) {
385385
if (is_a($input['dropdown_values'], "CommonTreeDropdown", true)) {
386386
// Specific param for ITILCategory
387387
if ($input['dropdown_values'] == ITILCategory::class) {
388+
if (!isset($input['show_ticket_categories'])) {
389+
$input['show_ticket_categories'] = 'all';
390+
}
388391
$input['values']['show_ticket_categories'] = $input['show_ticket_categories'];
389392
}
390393

394+
if (!isset($input['show_ticket_categories_depth'])) {
395+
$input['show_ticket_categories_depth'] = 0;
396+
}
391397
if ($input['show_ticket_categories_depth'] != (int) $input['show_ticket_categories_depth']) {
392398
$input['values']['show_ticket_categories_depth'] = 0;
393399
} else {

inc/form.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,7 @@ public function getFromDBByQuestion(PluginFormcreatorQuestion $question) {
21712171
/**
21722172
* Get an array of instances of all fields for the form
21732173
*
2174-
* @return array
2174+
* @return PluginFormcreatorFields[]
21752175
*/
21762176
public function getFields() {
21772177
$fields = [];

inc/targetbase.class.php

+33-34
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,10 @@ protected function setTargetEntity($data, PluginFormcreatorFormAnswer $formanswe
377377
return $data;
378378
}
379379

380-
protected function setTargetCategory($data, $formanswer) {
380+
protected function setTargetCategory($data, PluginFormcreatorFormAnswer $formanswer) {
381381
global $DB;
382382

383-
$category = null;
383+
$category = 0;
384384

385385
switch ($this->fields['category_rule']) {
386386
case self::CATEGORY_RULE_ANSWER:
@@ -398,54 +398,53 @@ protected function setTargetCategory($data, $formanswer) {
398398
$category = $this->fields['category_question'];
399399
break;
400400
case self::CATEGORY_RULE_LAST_ANSWER:
401-
$form_id = $formanswer->fields['id'];
402-
403-
// Get all answers for dropdown questions of this form, ordered
404-
// from last to first displayed
405-
$answers = $DB->request([
406-
'SELECT' => ['answer.answer', 'question.values'],
407-
'FROM' => PluginFormcreatorAnswer::getTable() . ' AS answer',
408-
'JOIN' => [
409-
PluginFormcreatorQuestion::getTable() . ' AS question' => [
410-
'ON' => [
411-
'answer' => 'plugin_formcreator_questions_id',
412-
'question' => 'id',
413-
]
414-
]
415-
],
416-
'WHERE' => [
417-
'answer.plugin_formcreator_formanswers_id' => $form_id,
418-
'question.fieldtype' => "dropdown",
419-
],
420-
'ORDER' => [
421-
'row DESC',
422-
'col DESC',
423-
]
424-
]);
401+
$questionFields = $formanswer->getForm()->getFields();
402+
$answers_values = $formanswer->getAnswers($formanswer->getID());
403+
foreach ($questionFields as $id => $question) {
404+
$questionFields[$id]->deserializeValue($answers_values['formcreator_field_' . $id]);
405+
}
406+
407+
// filter questions; keep DropdownFields only
408+
$filteredFields = array_filter($questionFields, function($item) {
409+
return get_class($item) === PluginFormcreatorDropdownField::class;
410+
});
411+
412+
// Sort question in reverse order
413+
uasort($filteredFields, function($a, $b) {
414+
$orderA = $a->getQuestion()->fields['order'];
415+
$orderB = $b->getQuestion()->fields['order'];
416+
if ($orderA == $orderB) {
417+
return 0;
418+
}
419+
return ($orderA > $orderB) ? -1 : 1;
420+
});
425421

426-
foreach ($answers as $answer) {
422+
foreach ($filteredFields as $questionField) {
427423
// Decode dropdown settings
428-
$itemtype = \PluginFormcreatorDropdownField::getSubItemtypeForValues($answer['values']);
424+
$itemtype = \PluginFormcreatorDropdownField::getSubItemtypeForValues($questionField->getQuestion()->fields['values']);
429425

430426
// Skip if not a dropdown on categories
431-
if ($itemtype !== "ITILCategory") {
427+
if ($itemtype !== ITILCategory::class) {
428+
continue;
429+
}
430+
431+
// Skip if question is invisible
432+
if (!PluginFormcreatorFields::isVisible($questionField->getQuestion(), $questionFields)) {
432433
continue;
433434
}
434435

435436
// Skip if question was not answered
436-
if (empty($answer['answer'])) {
437+
if (empty($questionField->getValueForDesign())) {
437438
continue;
438439
}
439440

440441
// Found a valid answer, stop here
441-
$category = $answer['answer'];
442+
$category = $questionField->getValueForDesign();
442443
break;
443444
}
444445
break;
445446
}
446-
if ($category !== null) {
447-
$data['itilcategories_id'] = $category;
448-
}
447+
$data['itilcategories_id'] = $category;
449448

450449
return $data;
451450
}

0 commit comments

Comments
 (0)