From 0311f2f961dc38c0097ce136f39cec6131ab0c89 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@enhancesoft.com>
Date: Fri, 27 Mar 2015 15:23:14 +0000
Subject: [PATCH] forms: Add choice lookup support

Support using value or abbrv. for choice/selection input fields.
---
 include/class.dynamic_forms.php | 15 +++++++++++++++
 include/class.forms.php         | 13 ++++++++-----
 include/class.list.php          | 10 ++++++----
 3 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index 2efcbec3c..53403bb36 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -1508,6 +1508,21 @@ class SelectionField extends FormField {
         return $selection;
     }
 
+    function lookupChoice($value) {
+
+        // See if it's in the choices.
+        $choices = $this->getChoices();
+        if ($choices && ($i=array_search($value, $choices)))
+            return array($i=>$choices[$i]);
+
+        // Query the store by value or extra (abbrv.)
+        if (($list=$this->getList()) && ($i=$list->getItem($value)))
+            return array($i->getId() => $i->getValue());
+
+        return null;
+    }
+
+
     function getFilterData() {
         // Start with the filter data for the list item as the [0] index
         $data = array(parent::getFilterData());
diff --git a/include/class.forms.php b/include/class.forms.php
index d65136a56..d364c211a 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -1219,6 +1219,10 @@ class ChoiceField extends FormField {
         return $this->_choices;
     }
 
+    function lookupChoice($value) {
+        return null;
+    }
+
     function getSearchMethods() {
         return array(
             'set' =>        __('has a value'),
@@ -2335,11 +2339,8 @@ class TextboxSelectionWidget extends TextboxWidget {
     function getValue() {
 
         $value = parent::getValue();
-        if (($i=$this->field->getList()->getItem((string) $value)))
-            $value = array($i->getId() => $i->getValue());
-        elseif (($choices=$this->field->getChoices())
-                && ($k=array_search($value, $choices)))
-            $value = array($k => $choices[$k]);
+        if ($value && ($item=$this->field->lookupChoice((string) $value)))
+            $value = $item;
 
         return $value;
     }
@@ -2509,6 +2510,8 @@ class ChoicesWidget extends Widget {
             foreach($value as $k => $v) {
                 if (isset($choices[$v]))
                     $values[$v] = $choices[$v];
+                elseif (($i=$this->field->lookupChoice($v)))
+                    $values += $i;
             }
         }
 
diff --git a/include/class.list.php b/include/class.list.php
index 782bfebaf..58ed9f1cd 100644
--- a/include/class.list.php
+++ b/include/class.list.php
@@ -231,13 +231,15 @@ class DynamicList extends VerySimpleModel implements CustomList {
 
     function getItem($val) {
 
-        $criteria = array('list_id' => $this->getId());
+        $items = DynamicListItem::objects()->filter(
+                array('list_id' => $this->getId()));
+
         if (is_int($val))
-            $criteria['id'] = $val;
+            $items->filter(array('id' => $val));
         else
-            $criteria['value'] = $val;
+            $items->filter(Q::any(array('value'=>$val, 'extra' => $val)));
 
-         return DynamicListItem::lookup($criteria);
+        return $items->first();
     }
 
     function addItem($vars, &$errors) {
-- 
GitLab