Skip to content

Commit

Permalink
feat: backport of PR 1681 for 2.10
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Apr 14, 2020
1 parent 03a2693 commit 7e79785
Show file tree
Hide file tree
Showing 4 changed files with 323 additions and 20 deletions.
22 changes: 17 additions & 5 deletions inc/fields/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,23 @@ public function getHtmlIcon() {
*
* @return string
*/
protected function getSubItemtype() {
$decodedValues = json_decode($this->question->fields['values'], JSON_OBJECT_AS_ARRAY);
protected function getSubItemtype() {
return self::getSubItemtypeForValues($this->question->fields['values']);
}

/**
* Get the itemtype of the item to show for the given values
*
* @param string $values json or raw string
*
* @return string
*/
public static function getSubItemtypeForValues($values) {
$decodedValues = json_decode($values, JSON_OBJECT_AS_ARRAY);
if ($decodedValues === null) {
return $this->question->fields['values'];
return $values;
}

return $decodedValues['itemtype'];
}
return $decodedValues['itemtype'];
}
}
57 changes: 52 additions & 5 deletions inc/targetbase.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ abstract protected function getTaggableFields();
const CATEGORY_RULE_NONE = 1;
const CATEGORY_RULE_SPECIFIC = 2;
const CATEGORY_RULE_ANSWER = 3;
const CATEGORY_RULE_LAST_ANSWER = 4;

const LOCATION_RULE_NONE = 1;
const LOCATION_RULE_SPECIFIC = 2;
Expand Down Expand Up @@ -218,9 +219,10 @@ public static function getEnumUrgencyRule() {

public static function getEnumCategoryRule() {
return [
self::CATEGORY_RULE_NONE => __('Category from template or none', 'formcreator'),
self::CATEGORY_RULE_SPECIFIC => __('Specific category', 'formcreator'),
self::CATEGORY_RULE_ANSWER => __('Equals to the answer to the question', 'formcreator'),
self::CATEGORY_RULE_NONE => __('Category from template or none', 'formcreator'),
self::CATEGORY_RULE_SPECIFIC => __('Specific category', 'formcreator'),
self::CATEGORY_RULE_ANSWER => __('Equals to the answer to the question', 'formcreator'),
self::CATEGORY_RULE_LAST_ANSWER => __('Last valid answer', 'formcreator'),
];
}

Expand Down Expand Up @@ -373,6 +375,8 @@ protected function setTargetEntity($data, PluginFormcreatorFormAnswer $formanswe
protected function setTargetCategory($data, $formanswer) {
global $DB;

$category = null;

switch ($this->fields['category_rule']) {
case self::CATEGORY_RULE_ANSWER:
$category = $DB->request([
Expand All @@ -388,8 +392,51 @@ protected function setTargetCategory($data, $formanswer) {
case self::CATEGORY_RULE_SPECIFIC:
$category = $this->fields['category_question'];
break;
default:
$category = null;
case self::CATEGORY_RULE_LAST_ANSWER:
$form_id = $formanswer->fields['id'];

// Get all answers for dropdown questions of this form, ordered
// from last to first displayed
$answers = $DB->request([
'SELECT' => ['answer.answer', 'question.values'],
'FROM' => PluginFormcreatorAnswer::getTable() . ' AS answer',
'JOIN' => [
PluginFormcreatorQuestion::getTable() . ' AS question' => [
'ON' => [
'answer' => 'plugin_formcreator_questions_id',
'question' => 'id',
]
]
],
'WHERE' => [
'answer.plugin_formcreator_formanswers_id' => $form_id,
'question.fieldtype' => "dropdown",
],
'ORDER' => [
'row DESC',
'col DESC',
]
]);

foreach ($answers as $answer) {
// Decode dropdown settings
$itemtype = \PluginFormcreatorDropdownField::getSubItemtypeForValues($answer['values']);

// Skip if not a dropdown on categories
if ($itemtype !== "ITILCategory") {
continue;
}

// Skip if question was not answered
if (empty($answer['answer'])) {
continue;
}

// Found a valid answer, stop here
$category = $answer['answer'];
break;
}
break;
}
if ($category !== null) {
$data['itilcategories_id'] = $category;
Expand Down
63 changes: 59 additions & 4 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class PluginFormcreatorTargetTicket extends PluginFormcreatorTargetBase
const ASSOCIATE_RULE_NONE = 1;
const ASSOCIATE_RULE_SPECIFIC = 2;
const ASSOCIATE_RULE_ANSWER = 3;
const ASSOCIATE_RULE_LAST_ANSWER = 4;

const REQUESTTYPE_NONE = 0;
const REQUESTTYPE_SPECIFIC = 1;
Expand Down Expand Up @@ -85,9 +86,10 @@ protected function getCategoryFilter() {

public static function getEnumAssociateRule() {
return [
self::ASSOCIATE_RULE_NONE => __('None', 'formcreator'),
self::ASSOCIATE_RULE_SPECIFIC => __('Specific asset', 'formcreator'),
self::ASSOCIATE_RULE_ANSWER => __('Equals to the answer to the question', 'formcreator'),
self::ASSOCIATE_RULE_NONE => __('None', 'formcreator'),
self::ASSOCIATE_RULE_SPECIFIC => __('Specific asset', 'formcreator'),
self::ASSOCIATE_RULE_ANSWER => __('Equals to the answer to the question', 'formcreator'),
self::ASSOCIATE_RULE_LAST_ANSWER => __('Last valid answer', 'formcreator'),
];
}

Expand Down Expand Up @@ -920,7 +922,7 @@ protected function showAssociateSettings($rand) {
}

protected function setTargetAssociatedItem($data, $formanswer) {
global $DB;
global $DB, $CFG_GLPI;

switch ($this->fields['associate_rule']) {
case self::ASSOCIATE_RULE_ANSWER:
Expand Down Expand Up @@ -963,6 +965,58 @@ protected function setTargetAssociatedItem($data, $formanswer) {
$data['items_id'] = [$row['itemtype'] => [$row['items_id'] => $row['items_id']]];
}
break;

case self::ASSOCIATE_RULE_LAST_ANSWER:
$form_id = $formanswer->fields['id'];

// Get all answers for glpiselect questions of this form, ordered
// from last to first displayed
$answers = $DB->request([
'SELECT' => ['answer.answer', 'question.values'],
'FROM' => PluginFormcreatorAnswer::getTable() . ' AS answer',
'JOIN' => [
PluginFormcreatorQuestion::getTable() . ' AS question' => [
'ON' => [
'answer' => 'plugin_formcreator_questions_id',
'question' => 'id',
]
]
],
'WHERE' => [
'answer.plugin_formcreator_formanswers_id' => $form_id,
'question.fieldtype' => "glpiselect",
],
'ORDER' => [
'row DESC',
'col DESC',
]
]);

foreach ($answers as $answer) {
// Skip if the object type is not valid asset type
if (!in_array($answer['values'], $CFG_GLPI["asset_types"])) {
continue;
}

// Skip if question was not answered
if (empty($answer['answer'])) {
continue;
}

// Skip if item doesn't exist in the DB (shouldn't happen)
$item = new $answer['values']();
if (!$item->getFromDB($answer['answer'])) {
continue;
}

// Found a valid answer, stop here
$data['items_id'] = [
$answer['values'] => [$answer['answer']]
];
break;
}

break;
}

return $data;
Expand Down Expand Up @@ -1155,6 +1209,7 @@ public function export($remove_uuid = false) {
private function saveAssociatedItems($input) {
switch ($input['associate_rule']) {
case self::ASSOCIATE_RULE_ANSWER:
case self::ASSOCIATE_RULE_LAST_ANSWER:
$input['associate_question'] = $input['_associate_question'];
break;

Expand Down
Loading

0 comments on commit 7e79785

Please sign in to comment.