Skip to content
Snippets Groups Projects
Commit 9c9bfae1 authored by Peter Rotich's avatar Peter Rotich
Browse files

Merge pull request #1436 from greezybacon/issue/choices-export


forms: Properly export choices lists

Reviewed-By: default avatarPeter Rotich <peter@osticket.com>
parents 55de7378 22c5c11a
Branches
Tags
No related merge requests found
...@@ -1126,16 +1126,18 @@ class SelectionField extends FormField { ...@@ -1126,16 +1126,18 @@ class SelectionField extends FormField {
$value = JsonDataParser::parse($value) ?: $value; $value = JsonDataParser::parse($value) ?: $value;
if (!is_array($value)) { if (!is_array($value)) {
$config = $this->getConfiguration(); $values = array();
if (!$config['multiselect']) {
// CDATA may be built with comma-list
list($value,) = explode(',', $value, 2);
}
$choices = $this->getChoices(); $choices = $this->getChoices();
if (isset($choices[$value])) foreach (explode(',', $value) as $V) {
$value = array($value => $choices[$value]); if (isset($choices[$V]))
elseif ($id && isset($choices[$id])) $values[$V] = $choices[$V];
$value = array($id => $choices[$id]); }
if ($id && isset($choices[$id]))
$values[$id] = $choices[$id];
if ($values)
return $values;
// else return $value unchanged
} }
// Don't set the ID here as multiselect prevents using exactly one // Don't set the ID here as multiselect prevents using exactly one
// ID value. Instead, stick with the JSON value only. // ID value. Instead, stick with the JSON value only.
...@@ -1253,13 +1255,6 @@ class SelectionField extends FormField { ...@@ -1253,13 +1255,6 @@ class SelectionField extends FormField {
return $selection; return $selection;
} }
function export($value) {
if ($value && is_numeric($value)
&& ($item = DynamicListItem::lookup($value)))
return $item->toString();
return $value;
}
function getFilterData() { function getFilterData() {
$data = array(parent::getFilterData()); $data = array(parent::getFilterData());
if (($v = $this->getClean()) instanceof DynamicListItem) { if (($v = $this->getClean()) instanceof DynamicListItem) {
......
...@@ -946,27 +946,33 @@ class ChoiceField extends FormField { ...@@ -946,27 +946,33 @@ class ChoiceField extends FormField {
function to_php($value) { function to_php($value) {
if (is_string($value)) if (is_string($value))
$array = JsonDataParser::parse($value) ?: $value; $value = JsonDataParser::parse($value) ?: $value;
else
$array = $value; // CDATA table may be built with comma-separated key,value,key,value
$config = $this->getConfiguration(); if (is_string($value)) {
if (!$config['multiselect']) { $values = array();
if (is_array($array) && count($array) < 2) { $choices = $this->getChoices();
reset($array); foreach (explode(',', $value) as $V) {
return key($array); if (isset($choices[$V]))
} $values[$V] = $choices[$V];
if (is_string($array) && strpos($array, ',') !== false) {
list($array,) = explode(',', $array, 2);
} }
if (array_filter($values))
$value = $values;
} }
return $array; $config = $this->getConfiguration();
if (!$config['multiselect'] && is_array($value) && count($value) < 2) {
reset($value);
return key($value);
}
return $value;
} }
function toString($value) { function toString($value) {
$selection = $this->getChoice($value); if (!is_array($value))
return is_array($selection) $value = $this->getChoice($value);
? (implode(', ', array_filter($selection)) ?: $value) if (is_array($value))
: (string) $selection; return implode(', ', $value);
return (string) $value;
} }
function getChoice($value) { function getChoice($value) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment