Skip to content

Commit

Permalink
fix(target): answer rendering issues
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Feb 12, 2018
1 parent 33e66b6 commit 997779c
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 25 deletions.
20 changes: 18 additions & 2 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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' : '';

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

/**
* Outputs the HTML representing the field
*
* @param string $canEdit
*/
public function displayField($canEdit = true) {
Expand Down
4 changes: 2 additions & 2 deletions inc/fieldinterface.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
10 changes: 3 additions & 7 deletions inc/fields.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class PluginFormcreatorFields
{
/**
* Retrive all field types and file path
*
* @return Array field_type => File_path
*/
public static function getTypes() {
Expand All @@ -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() {
Expand All @@ -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;
Expand All @@ -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';
Expand All @@ -79,7 +77,6 @@ public static function getValue($field, $value) {
return $value;
}


public static function printAllTabFieldsForJS() {
$tabFieldsForJS = '';
// Get field types and file path
Expand All @@ -97,7 +94,6 @@ public static function printAllTabFieldsForJS() {
}

/**
*
* @param unknown $field
* @param unknown $data
* @param string $edit
Expand Down
43 changes: 43 additions & 0 deletions inc/fields/checkboxesfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand All @@ -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 = '<br />' . implode('<br />', $value);
} else {
$value = '\r\n' . implode('\r\n', $value);
}
return $value;
}

public static function getPrefs() {
return [
'required' => 1,
Expand Down
23 changes: 20 additions & 3 deletions inc/fields/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions inc/fields/hiddenfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions inc/fields/multiselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<br />' . implode('<br />', $value);
} else {
$value = '\r\n' . implode('\r\n', $value);
}
return $value;
}

public static function getName() {
return __('Multiselect', 'formcreator');
}
Expand Down
5 changes: 5 additions & 0 deletions inc/fields/textareafield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).");';";
Expand Down
19 changes: 8 additions & 11 deletions inc/targetbase.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<br />' . implode('<br />', $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);

Expand Down

0 comments on commit 997779c

Please sign in to comment.