From 0a6ad37a2f8b7d059f7c11e82302e4ec2d158450 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@enhancesoft.com>
Date: Mon, 6 Apr 2015 15:27:25 +0000
Subject: [PATCH] temp:  Support abbrev. on selection fields

This is a temporary patch for v1.9.7 -- develop-next already has a better
implementation.
---
 include/class.dynamic_forms.php | 10 +++++++++-
 include/class.list.php          |  9 +++++----
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index 776f1b3e4..d97e4b348 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -1326,12 +1326,20 @@ class SelectionField extends FormField {
         $config = $this->getConfiguration();
         $choices = $this->getChoices();
         $selection = array();
+
+        if ($value && !is_array($value))
+            $value = array($value);
+
         if ($value && is_array($value)) {
             foreach ($value as $k=>$v) {
-                if (($i=$list->getItem((int) $k)))
+                if ($k && ($i=$list->getItem((int) $k)))
                     $selection[$i->getId()] = $i->getValue();
                 elseif (isset($choices[$k]))
                     $selection[$k] = $choices[$k];
+                elseif (isset($choices[$v]))
+                    $selection[$v] = $choices[$v];
+                elseif (($i=$list->getItem($v, true)))
+                    $selection[$i->getId()] = $i->getValue();
             }
         } elseif($value) {
             //Assume invalid textbox input to be validated
diff --git a/include/class.list.php b/include/class.list.php
index 843eee446..12d2a8556 100644
--- a/include/class.list.php
+++ b/include/class.list.php
@@ -227,17 +227,18 @@ class DynamicList extends VerySimpleModel implements CustomList {
         return $this->_items;
     }
 
-
-
-    function getItem($val) {
+    function getItem($val, $extra=false) {
 
         $items = DynamicListItem::objects()->filter(
                 array('list_id' => $this->getId()));
 
         if (is_int($val))
             $items->filter(array('id' => $val));
+        elseif ($extra)
+            $items->filter(array('extra' => $val));
         else
-            $items->filter(Q::any(array('value'=>$val, 'extra' => $val)));
+            $items->filter(array('value' => $val));
+
 
         return $items->first();
     }
-- 
GitLab