From bc94d5c2869453fc6cd577a5f7dc0ffe40b4465c Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Mon, 14 Jul 2014 22:36:21 -0500 Subject: [PATCH] Allow ExtraFieldValue::get_item_id_from_field_variable_and_field_value() to return the last value instead of the first - refs BT#8392 --- main/inc/lib/extra_field_value.lib.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/main/inc/lib/extra_field_value.lib.php b/main/inc/lib/extra_field_value.lib.php index 13e53bb88b..1e449d2797 100755 --- a/main/inc/lib/extra_field_value.lib.php +++ b/main/inc/lib/extra_field_value.lib.php @@ -499,10 +499,12 @@ class ExtraFieldValue extends Model * the given field is defined with the given value * @param string Field (type of data) we want to check * @param string Data we are looking for in the given field + * @param bool Whether to transform the result to a human readable strings + * @param bool Whether to return the last element or simply the first one we get * @return mixed Give the ID if found, or false on failure or not found * @assert (-1,-1) === false */ - public function get_item_id_from_field_variable_and_field_value($field_variable, $field_value, $transform = false) + public function get_item_id_from_field_variable_and_field_value($field_variable, $field_value, $transform = false, $last = false) { $field_value = Database::escape_string($field_value); $field_variable = Database::escape_string($field_variable); @@ -513,8 +515,16 @@ class ExtraFieldValue extends Model WHERE field_value = '$field_value' AND field_variable = '".$field_variable."' + ORDER BY {$this->handler_id} "; + if ($last) { + // If we want the last element instead of the first + // This is useful in special cases where there might + // (erroneously) be more than one row for an item + $sql .= ' DESC'; + } + $result = Database::query($sql); if ($result !== false && Database::num_rows($result)) { $result = Database::fetch_array($result, 'ASSOC');