diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index 10419a2099c651e33265d443ccc4dc5ca788e292..d9d51ef8871661ecd702edc2a53e6fac238a2323 100644 --- a/include/class.dynamic_forms.php +++ b/include/class.dynamic_forms.php @@ -1519,6 +1519,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 d63b17129e439a20c114195f3fbc0e253ad6eb66..0733afd4ea00500d5dcf55ba64609c1ebd81bc10 100644 --- a/include/class.forms.php +++ b/include/class.forms.php @@ -1241,6 +1241,10 @@ class ChoiceField extends FormField { return $this->_choices; } + function lookupChoice($value) { + return null; + } + function getSearchMethods() { return array( 'set' => __('has a value'), @@ -2366,11 +2370,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; } @@ -2540,6 +2541,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 841ff3ea5960b4c2394b2fcd595d61026c2d697f..a228ab06f2bf04c0eb31f12dc827d37e5d585eaf 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) {