From 22c5c11aa37112264dc91d7ebb4729722781cc5b Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Tue, 28 Oct 2014 09:48:16 -0500 Subject: [PATCH] forms: Properly export choices lists Previously, this was not ported properly now that multiple selections are supported in the choice field and derivatives. --- include/class.dynamic_forms.php | 27 ++++++++++------------- include/class.forms.php | 38 +++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index 94b9b786e..8da97a335 100644 --- a/include/class.dynamic_forms.php +++ b/include/class.dynamic_forms.php @@ -1126,16 +1126,18 @@ class SelectionField extends FormField { $value = JsonDataParser::parse($value) ?: $value; if (!is_array($value)) { - $config = $this->getConfiguration(); - if (!$config['multiselect']) { - // CDATA may be built with comma-list - list($value,) = explode(',', $value, 2); - } + $values = array(); $choices = $this->getChoices(); - if (isset($choices[$value])) - $value = array($value => $choices[$value]); - elseif ($id && isset($choices[$id])) - $value = array($id => $choices[$id]); + foreach (explode(',', $value) as $V) { + if (isset($choices[$V])) + $values[$V] = $choices[$V]; + } + 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 // ID value. Instead, stick with the JSON value only. @@ -1253,13 +1255,6 @@ class SelectionField extends FormField { return $selection; } - function export($value) { - if ($value && is_numeric($value) - && ($item = DynamicListItem::lookup($value))) - return $item->toString(); - return $value; - } - function getFilterData() { $data = array(parent::getFilterData()); if (($v = $this->getClean()) instanceof DynamicListItem) { diff --git a/include/class.forms.php b/include/class.forms.php index c80c34ba1..1fc40ffeb 100644 --- a/include/class.forms.php +++ b/include/class.forms.php @@ -946,27 +946,33 @@ class ChoiceField extends FormField { function to_php($value) { if (is_string($value)) - $array = JsonDataParser::parse($value) ?: $value; - else - $array = $value; - $config = $this->getConfiguration(); - if (!$config['multiselect']) { - if (is_array($array) && count($array) < 2) { - reset($array); - return key($array); - } - if (is_string($array) && strpos($array, ',') !== false) { - list($array,) = explode(',', $array, 2); + $value = JsonDataParser::parse($value) ?: $value; + + // CDATA table may be built with comma-separated key,value,key,value + if (is_string($value)) { + $values = array(); + $choices = $this->getChoices(); + foreach (explode(',', $value) as $V) { + if (isset($choices[$V])) + $values[$V] = $choices[$V]; } + 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) { - $selection = $this->getChoice($value); - return is_array($selection) - ? (implode(', ', array_filter($selection)) ?: $value) - : (string) $selection; + if (!is_array($value)) + $value = $this->getChoice($value); + if (is_array($value)) + return implode(', ', $value); + return (string) $value; } function getChoice($value) { -- GitLab