From 2ebdcb5cbf2e6e8b7170d6ff68caf46fae429018 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 27 Oct 2014 16:07:44 -0500
Subject: [PATCH] Fix display of subject field for drop-down lists

There was a slight issue when transitioning from a short answer field to a
drop-down field, and when the CDATA table is dropped and rebuilt.
---
 include/class.dynamic_forms.php | 10 ++++++++--
 include/class.forms.php         | 14 ++++++++++----
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index 3a4457177..94b9b786e 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -1122,9 +1122,15 @@ class SelectionField extends FormField {
     }
 
     function to_php($value, $id=false) {
-        $value = ($value && !is_array($value))
-            ? JsonDataParser::parse($value) : $value;
+        if (is_string($value))
+            $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);
+            }
             $choices = $this->getChoices();
             if (isset($choices[$value]))
                 $value = array($value => $choices[$value]);
diff --git a/include/class.forms.php b/include/class.forms.php
index 6e2ddffc8..c80c34ba1 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -950,16 +950,22 @@ class ChoiceField extends FormField {
         else
             $array = $value;
         $config = $this->getConfiguration();
-        if (is_array($array) && !$config['multiselect'] && count($array) < 2) {
-            reset($array);
-            return key($array);
+        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);
+            }
         }
         return $array;
     }
 
     function toString($value) {
         $selection = $this->getChoice($value);
-        return is_array($selection) ? implode(', ', array_filter($selection))
+        return is_array($selection)
+            ? (implode(', ', array_filter($selection)) ?: $value)
             : (string) $selection;
     }
 
-- 
GitLab