diff --git a/inc/field.class.php b/inc/field.class.php index fb388231a..920bc0a65 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -12,6 +12,10 @@ abstract class PluginFormcreatorField implements PluginFormcreatorFieldInterface protected $fields = []; + /** + * @param unknown $fields + * @param array $data + */ public function __construct($fields, $data = []) { $this->fields = $fields; $this->fields['answer'] = $data; @@ -25,9 +29,22 @@ public function __construct($fields, $data = []) { * @return array input data to save as is */ public function prepareQuestionInputForSave($input) { - return $input; + return $input; + } + + /** + * Prepares a answer value for output in a target object + * @param string|array $input the answer to format for a target (ticket or change) + * @return string + */ + public function prepareQuestionInputForTarget($input) { + return addslashes($input); } + /** + * Output HTML to display the field + * @param boolean $canEdit is the field editable ? + */ public function show($canEdit = true) { $required = ($canEdit && $this->fields['required']) ? ' required' : ''; @@ -62,7 +79,6 @@ public function show($canEdit = true) { /** * Outputs the HTML representing the field - * * @param string $canEdit */ public function displayField($canEdit = true) { diff --git a/inc/fieldinterface.class.php b/inc/fieldinterface.class.php index ba9612913..5db374af1 100644 --- a/inc/fieldinterface.class.php +++ b/inc/fieldinterface.class.php @@ -3,10 +3,10 @@ die("Sorry. You can't access this file directly"); } -interface PluginFormcreatorFieldInterface -{ +interface PluginFormcreatorFieldInterface { public static function getName(); public static function getPrefs(); public static function getJSFields(); public function prepareQuestionInputForSave($input); + public function prepareQuestionInputForTarget($input); } diff --git a/inc/fields.class.php b/inc/fields.class.php index 44b05705d..9398fb063 100644 --- a/inc/fields.class.php +++ b/inc/fields.class.php @@ -8,7 +8,6 @@ class PluginFormcreatorFields { /** * Retrive all field types and file path - * * @return Array field_type => File_path */ public static function getTypes() { @@ -29,7 +28,6 @@ public static function getTypes() { /** * Get type and name of all field types - * * @return Array field_type => Name */ public static function getNames() { @@ -43,7 +41,7 @@ public static function getNames() { // Get localized names of field types foreach (array_keys($tab_field_types) as $field_type) { - $classname = 'PluginFormcreator' . ucfirst($field_type) . 'Field'; + $classname = 'PluginFormcreator' . ucfirst($field_type) . 'Field'; if ($classname == 'tagField' &&(!$plugin->isInstalled('tag') || !$plugin->isActivated('tag'))) { continue; @@ -59,11 +57,11 @@ public static function getNames() { /** * Get field value to display - * * @param String $field Field object to display * @param String $value the value to display - * * @return String + * @deprecated 2.6.2 Only one caller, and being removed + * @see PluginFormcreatorField::prepareQuestionInputForTarget */ public static function getValue($field, $value) { $class_file = dirname(__FILE__).'/fields/'.$field['fieldtype'].'field.class.php'; @@ -79,7 +77,6 @@ public static function getValue($field, $value) { return $value; } - public static function printAllTabFieldsForJS() { $tabFieldsForJS = ''; // Get field types and file path @@ -97,7 +94,6 @@ public static function printAllTabFieldsForJS() { } /** - * * @param unknown $field * @param unknown $data * @param string $edit diff --git a/inc/fields/checkboxesfield.class.php b/inc/fields/checkboxesfield.class.php index 67fa0a3bf..d14a57435 100644 --- a/inc/fields/checkboxesfield.class.php +++ b/inc/fields/checkboxesfield.class.php @@ -99,6 +99,17 @@ public static function getName() { return __('Checkboxes', 'formcreator'); } + public function getValue() { + if (isset($this->fields['answer'])) { + if (!is_array($this->fields['answer']) && is_array(json_decode($this->fields['answer']))) { + return json_decode($this->fields['answer']); + } + return $this->fields['answer']; + } else { + return explode("\r\n", $this->fields['default_values']); + } + } + public function prepareQuestionInputForSave($input) { if (isset($input['values'])) { if (empty($input['values'])) { @@ -117,6 +128,38 @@ public function prepareQuestionInputForSave($input) { return $input; } + public function prepareQuestionInputForTarget($input) { + global $CFG_GLPI; + + $value = []; + $values = $this->getAvailableValues(); + + if (empty($input)) { + return ''; + } + + if (is_array($input)) { + $tab_values = $input; + } else if (is_array(json_decode($input))) { + $tab_values = json_decode($input); + } else { + $tab_values = [$input]; + } + + foreach ($tab_values as $input) { + if (in_array($input, $values)) { + $value[] = addslashes($input); + } + } + + if ($CFG_GLPI['use_rich_text']) { + $value = '
' . implode('
', $value); + } else { + $value = '\r\n' . implode('\r\n', $value); + } + return $value; + } + public static function getPrefs() { return [ 'required' => 1, diff --git a/inc/fields/dropdownfield.class.php b/inc/fields/dropdownfield.class.php index 2933178cf..0be6a8136 100644 --- a/inc/fields/dropdownfield.class.php +++ b/inc/fields/dropdownfield.class.php @@ -58,18 +58,35 @@ public function displayField($canEdit = true) { public function getAnswer() { $value = $this->getValue(); + $DbUtil = new DbUtils(); if ($this->fields['values'] == 'User') { - return getUserName($value); + return $DbUtil->getUserName($value); } else { $decodedValues = json_decode($this->fields['values'], JSON_OBJECT_AS_ARRAY); if (!isset($decodedValues['itemtype'])) { - return Dropdown::getDropdownName(getTableForItemType($this->fields['values']), $value); + return Dropdown::getDropdownName($DbUtil->getTableForItemType($this->fields['values']), $value); } else { - return Dropdown::getDropdownName(getTableForItemType($decodedValues['itemtype']), $value); + return Dropdown::getDropdownName($DbUtil->getTableForItemType($decodedValues['itemtype']), $value); } } } + public function prepareQuestionInputForTarget($input) { + $DbUtil = new DbUtils(); + if ($this->fields['values'] == User::class) { + $value = $DbUtil->getUserName($input); + } else { + $decodedValues = json_decode($this->fields['values'], JSON_OBJECT_AS_ARRAY); + if (!isset($decodedValues['itemtype'])) { + $value = Dropdown::getDropdownName($DbUtil->getTableForItemType($this->fields['values']), $input); + } else { + $value = Dropdown::getDropdownName($DbUtil->getTableForItemType($decodedValues['itemtype']), $input); + } + } + + return addslashes($value); + } + public static function getName() { return _n('Dropdown', 'Dropdowns', 1); } diff --git a/inc/fields/hiddenfield.class.php b/inc/fields/hiddenfield.class.php index db26eca7d..219f950b7 100644 --- a/inc/fields/hiddenfield.class.php +++ b/inc/fields/hiddenfield.class.php @@ -16,6 +16,11 @@ public static function getName() { return _n('Hidden field', 'Hidden fields', 1); } + public function prepareQuestionInputForTarget($input) { + $input = str_replace("\n", '\r\n', addslashes($input)); + return $input; + } + public static function getPrefs() { return [ 'required' => 0, diff --git a/inc/fields/multiselectfield.class.php b/inc/fields/multiselectfield.class.php index 70427ad38..6e085d511 100644 --- a/inc/fields/multiselectfield.class.php +++ b/inc/fields/multiselectfield.class.php @@ -67,6 +67,37 @@ public function getAnswer() { return $return; } + public function prepareQuestionInputForTarget($input) { + global $CFG_GLPI; + + $value = []; + $values = $this->getAvailableValues(); + + if (empty($input)) { + return ''; + } + + if (is_array($input)) { + $tab_values = $input; + } else if (is_array(json_decode($input))) { + $tab_values = json_decode($input); + } else { + $tab_values = [$input]; + } + + foreach ($tab_values as $input) { + if (in_array($input, $values)) { + $value[] = addslashes($input); + } + } + if ($CFG_GLPI['use_rich_text']) { + $value = '
' . implode('
', $value); + } else { + $value = '\r\n' . implode('\r\n', $value); + } + return $value; + } + public static function getName() { return __('Multiselect', 'formcreator'); } diff --git a/inc/fields/textareafield.class.php b/inc/fields/textareafield.class.php index ae4aae23c..765034383 100644 --- a/inc/fields/textareafield.class.php +++ b/inc/fields/textareafield.class.php @@ -29,6 +29,11 @@ public static function getName() { return __('Textarea', 'formcreator'); } + public function prepareQuestionInputForTarget($input) { + $input = str_replace("\r\n", '\r\n', addslashes($input)); + return $input; + } + public static function getJSFields() { $prefs = self::getPrefs(); return "tab_fields_fields['textarea'] = 'showFields(".implode(', ', $prefs).");';"; diff --git a/inc/targetbase.class.php b/inc/targetbase.class.php index 239b2fdf0..e3bf753a2 100644 --- a/inc/targetbase.class.php +++ b/inc/targetbase.class.php @@ -750,28 +750,25 @@ protected function parseTags($content, PluginFormcreatorForm_Answer $formanswer, ORDER BY `questions`.`order` ASC"; $res_questions = $DB->query($query_questions); while ($question_line = $DB->fetch_assoc($res_questions)) { + $classname = 'PluginFormcreator'.ucfirst($question_line['fieldtype']).'Field'; + if (class_exists($classname)) { + $fieldObject = new $classname($question_line, $question_line['answer']); + } + $id = $question_line['id']; if (!PluginFormcreatorFields::isVisible($question_line['id'], $answers_values)) { $name = ''; $value = ''; } else { $name = $question_line['name']; - $value = PluginFormcreatorFields::getValue($question_line, $question_line['answer']); - } - if (is_array($value)) { - if ($CFG_GLPI['use_rich_text']) { - $value = '
' . implode('
', $value); - } else { - $value = "\r\n" . implode("\r\n", $value); - } + $value = $fieldObject->prepareQuestionInputForTarget($fieldObject->getValue()); } - if ($question_line['fieldtype'] !== 'file') { $content = str_replace('##question_' . $id . '##', addslashes($name), $content); - $content = str_replace('##answer_' . $id . '##', addslashes($value), $content); + $content = str_replace('##answer_' . $id . '##', $value, $content); } else { if (strpos($content, '##answer_' . $id . '##') !== false) { - $content = str_replace('##question_' . $id . '##', addslashes($name), $content); + $content = str_replace('##question_' . $id . '##', $name, $content); if ($value !== '') { $content = str_replace('##answer_' . $id . '##', __('Attached document', 'formcreator'), $content);