Skip to content

Commit 6d73872

Browse files
committed
fix(target): answer rendering issues
fix #877 #817
1 parent 253013e commit 6d73872

9 files changed

+135
-25
lines changed

inc/field.class.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ abstract class PluginFormcreatorField implements PluginFormcreatorFieldInterface
1212

1313
protected $fields = [];
1414

15+
/**
16+
* @param unknown $fields
17+
* @param array $data
18+
*/
1519
public function __construct($fields, $data = []) {
1620
$this->fields = $fields;
1721
$this->fields['answer'] = $data;
@@ -25,9 +29,22 @@ public function __construct($fields, $data = []) {
2529
* @return array input data to save as is
2630
*/
2731
public function prepareQuestionInputForSave($input) {
28-
return $input;
32+
return $input;
33+
}
34+
35+
/**
36+
* Prepares a answer value for output in a target object
37+
* @param string|array $input the answer to format for a target (ticket or change)
38+
* @return string
39+
*/
40+
public function prepareQuestionInputForTarget($input) {
41+
return addslashes($input);
2942
}
3043

44+
/**
45+
* Output HTML to display the field
46+
* @param boolean $canEdit is the field editable ?
47+
*/
3148
public function show($canEdit = true) {
3249
$required = ($canEdit && $this->fields['required']) ? ' required' : '';
3350

@@ -62,7 +79,6 @@ public function show($canEdit = true) {
6279

6380
/**
6481
* Outputs the HTML representing the field
65-
*
6682
* @param string $canEdit
6783
*/
6884
public function displayField($canEdit = true) {

inc/fieldinterface.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
die("Sorry. You can't access this file directly");
44
}
55

6-
interface PluginFormcreatorFieldInterface
7-
{
6+
interface PluginFormcreatorFieldInterface {
87
public static function getName();
98
public static function getPrefs();
109
public static function getJSFields();
1110
public function prepareQuestionInputForSave($input);
11+
public function prepareQuestionInputForTarget($input);
1212
}

inc/fields.class.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class PluginFormcreatorFields
88
{
99
/**
1010
* Retrive all field types and file path
11-
*
1211
* @return Array field_type => File_path
1312
*/
1413
public static function getTypes() {
@@ -29,7 +28,6 @@ public static function getTypes() {
2928

3029
/**
3130
* Get type and name of all field types
32-
*
3331
* @return Array field_type => Name
3432
*/
3533
public static function getNames() {
@@ -43,7 +41,7 @@ public static function getNames() {
4341

4442
// Get localized names of field types
4543
foreach (array_keys($tab_field_types) as $field_type) {
46-
$classname = 'PluginFormcreator' . ucfirst($field_type) . 'Field';
44+
$classname = 'PluginFormcreator' . ucfirst($field_type) . 'Field';
4745

4846
if ($classname == 'tagField' &&(!$plugin->isInstalled('tag') || !$plugin->isActivated('tag'))) {
4947
continue;
@@ -59,11 +57,11 @@ public static function getNames() {
5957

6058
/**
6159
* Get field value to display
62-
*
6360
* @param String $field Field object to display
6461
* @param String $value the value to display
65-
*
6662
* @return String
63+
* @deprecated 2.6.2 Only one caller, and being removed
64+
* @see PluginFormcreatorField::prepareQuestionInputForTarget
6765
*/
6866
public static function getValue($field, $value) {
6967
$class_file = dirname(__FILE__).'/fields/'.$field['fieldtype'].'field.class.php';
@@ -79,7 +77,6 @@ public static function getValue($field, $value) {
7977
return $value;
8078
}
8179

82-
8380
public static function printAllTabFieldsForJS() {
8481
$tabFieldsForJS = '';
8582
// Get field types and file path
@@ -97,7 +94,6 @@ public static function printAllTabFieldsForJS() {
9794
}
9895

9996
/**
100-
*
10197
* @param unknown $field
10298
* @param unknown $data
10399
* @param string $edit

inc/fields/checkboxesfield.class.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ public static function getName() {
9999
return __('Checkboxes', 'formcreator');
100100
}
101101

102+
public function getValue() {
103+
if (isset($this->fields['answer'])) {
104+
if (!is_array($this->fields['answer']) && is_array(json_decode($this->fields['answer']))) {
105+
return json_decode($this->fields['answer']);
106+
}
107+
return $this->fields['answer'];
108+
} else {
109+
return explode("\r\n", $this->fields['default_values']);
110+
}
111+
}
112+
102113
public function prepareQuestionInputForSave($input) {
103114
if (isset($input['values'])) {
104115
if (empty($input['values'])) {
@@ -117,6 +128,38 @@ public function prepareQuestionInputForSave($input) {
117128
return $input;
118129
}
119130

131+
public function prepareQuestionInputForTarget($input) {
132+
global $CFG_GLPI;
133+
134+
$value = [];
135+
$values = $this->getAvailableValues();
136+
137+
if (empty($input)) {
138+
return '';
139+
}
140+
141+
if (is_array($input)) {
142+
$tab_values = $input;
143+
} else if (is_array(json_decode($input))) {
144+
$tab_values = json_decode($input);
145+
} else {
146+
$tab_values = [$input];
147+
}
148+
149+
foreach ($tab_values as $input) {
150+
if (in_array($input, $values)) {
151+
$value[] = addslashes($input);
152+
}
153+
}
154+
155+
if ($CFG_GLPI['use_rich_text']) {
156+
$value = '<br />' . implode('<br />', $value);
157+
} else {
158+
$value = '\r\n' . implode('\r\n', $value);
159+
}
160+
return $value;
161+
}
162+
120163
public static function getPrefs() {
121164
return [
122165
'required' => 1,

inc/fields/dropdownfield.class.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,35 @@ public function displayField($canEdit = true) {
5858

5959
public function getAnswer() {
6060
$value = $this->getValue();
61+
$DbUtil = new DbUtils();
6162
if ($this->fields['values'] == 'User') {
62-
return getUserName($value);
63+
return $DbUtil->getUserName($value);
6364
} else {
6465
$decodedValues = json_decode($this->fields['values'], JSON_OBJECT_AS_ARRAY);
6566
if (!isset($decodedValues['itemtype'])) {
66-
return Dropdown::getDropdownName(getTableForItemType($this->fields['values']), $value);
67+
return Dropdown::getDropdownName($DbUtil->getTableForItemType($this->fields['values']), $value);
6768
} else {
68-
return Dropdown::getDropdownName(getTableForItemType($decodedValues['itemtype']), $value);
69+
return Dropdown::getDropdownName($DbUtil->getTableForItemType($decodedValues['itemtype']), $value);
6970
}
7071
}
7172
}
7273

74+
public function prepareQuestionInputForTarget($input) {
75+
$DbUtil = new DbUtils();
76+
if ($this->fields['values'] == User::class) {
77+
$value = $DbUtil->getUserName($input);
78+
} else {
79+
$decodedValues = json_decode($this->fields['values'], JSON_OBJECT_AS_ARRAY);
80+
if (!isset($decodedValues['itemtype'])) {
81+
$value = Dropdown::getDropdownName($DbUtil->getTableForItemType($this->fields['values']), $input);
82+
} else {
83+
$value = Dropdown::getDropdownName($DbUtil->getTableForItemType($decodedValues['itemtype']), $input);
84+
}
85+
}
86+
87+
return addslashes($value);
88+
}
89+
7390
public static function getName() {
7491
return _n('Dropdown', 'Dropdowns', 1);
7592
}

inc/fields/hiddenfield.class.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public static function getName() {
1616
return _n('Hidden field', 'Hidden fields', 1);
1717
}
1818

19+
public function prepareQuestionInputForTarget($input) {
20+
$input = str_replace("\n", '\r\n', addslashes($input));
21+
return $input;
22+
}
23+
1924
public static function getPrefs() {
2025
return [
2126
'required' => 0,

inc/fields/multiselectfield.class.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,37 @@ public function getAnswer() {
6767
return $return;
6868
}
6969

70+
public function prepareQuestionInputForTarget($input) {
71+
global $CFG_GLPI;
72+
73+
$value = [];
74+
$values = $this->getAvailableValues();
75+
76+
if (empty($input)) {
77+
return '';
78+
}
79+
80+
if (is_array($input)) {
81+
$tab_values = $input;
82+
} else if (is_array(json_decode($input))) {
83+
$tab_values = json_decode($input);
84+
} else {
85+
$tab_values = [$input];
86+
}
87+
88+
foreach ($tab_values as $input) {
89+
if (in_array($input, $values)) {
90+
$value[] = addslashes($input);
91+
}
92+
}
93+
if ($CFG_GLPI['use_rich_text']) {
94+
$value = '<br />' . implode('<br />', $value);
95+
} else {
96+
$value = '\r\n' . implode('\r\n', $value);
97+
}
98+
return $value;
99+
}
100+
70101
public static function getName() {
71102
return __('Multiselect', 'formcreator');
72103
}

inc/fields/textareafield.class.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public static function getName() {
2929
return __('Textarea', 'formcreator');
3030
}
3131

32+
public function prepareQuestionInputForTarget($input) {
33+
$input = str_replace("\r\n", '\r\n', addslashes($input));
34+
return $input;
35+
}
36+
3237
public static function getJSFields() {
3338
$prefs = self::getPrefs();
3439
return "tab_fields_fields['textarea'] = 'showFields(".implode(', ', $prefs).");';";

inc/targetbase.class.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -750,28 +750,25 @@ protected function parseTags($content, PluginFormcreatorForm_Answer $formanswer,
750750
ORDER BY `questions`.`order` ASC";
751751
$res_questions = $DB->query($query_questions);
752752
while ($question_line = $DB->fetch_assoc($res_questions)) {
753+
$classname = 'PluginFormcreator'.ucfirst($question_line['fieldtype']).'Field';
754+
if (class_exists($classname)) {
755+
$fieldObject = new $classname($question_line, $question_line['answer']);
756+
}
757+
753758
$id = $question_line['id'];
754759
if (!PluginFormcreatorFields::isVisible($question_line['id'], $answers_values)) {
755760
$name = '';
756761
$value = '';
757762
} else {
758763
$name = $question_line['name'];
759-
$value = PluginFormcreatorFields::getValue($question_line, $question_line['answer']);
760-
}
761-
if (is_array($value)) {
762-
if ($CFG_GLPI['use_rich_text']) {
763-
$value = '<br />' . implode('<br />', $value);
764-
} else {
765-
$value = "\r\n" . implode("\r\n", $value);
766-
}
764+
$value = $fieldObject->prepareQuestionInputForTarget($fieldObject->getValue());
767765
}
768-
769766
if ($question_line['fieldtype'] !== 'file') {
770767
$content = str_replace('##question_' . $id . '##', addslashes($name), $content);
771-
$content = str_replace('##answer_' . $id . '##', addslashes($value), $content);
768+
$content = str_replace('##answer_' . $id . '##', $value, $content);
772769
} else {
773770
if (strpos($content, '##answer_' . $id . '##') !== false) {
774-
$content = str_replace('##question_' . $id . '##', addslashes($name), $content);
771+
$content = str_replace('##question_' . $id . '##', $name, $content);
775772
if ($value !== '') {
776773
$content = str_replace('##answer_' . $id . '##', __('Attached document', 'formcreator'), $content);
777774

0 commit comments

Comments
 (0)