diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index 2efcbec3c2fca60c77f19d49c60a30c7557ee415..53403bb36fbeabf462e01a196c6e77252c3537d5 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 d65136a569aac97045ba5e578da18cc9ee883b1e..d364c211adfbc8eeed8b83e44fa8ab786288e13e 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 782bfebaf302069be61e8854e9af61b2775f731c..58ed9f1cd0a960d112fa03c5afe6d302d9d00bbd 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) {