From 573a7b99dedcfc7de647b1c1a5cbcbe9750fc575 Mon Sep 17 00:00:00 2001
From: Sascha Grossenbacher <saschagros@gmail.com>
Date: Sat, 2 Apr 2011 10:21:23 +0200
Subject: [PATCH] Issue #1092754 by Berdir: Fixed relationship type filter.

---
 .../user_relationship_views.module                 |   17 +++++++++--------
 ...iews_handler_filter_user_relationships_type.inc |   18 ++++++++----------
 2 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/user_relationship_views/user_relationship_views.module b/user_relationship_views/user_relationship_views.module
index 2b13e05..70e4ee2 100644
--- a/user_relationship_views/user_relationship_views.module
+++ b/user_relationship_views/user_relationship_views.module
@@ -45,17 +45,18 @@ function user_relationship_views_ajax_autocomplete_relationships_type($string =
   if ($last_string != '') {
     $prefix = count($array) ? implode(', ', $array) .', ' : '';
 
-    $result = db_query_range("SELECT rtid, name FROM {user_relationship_types} WHERE LOWER(name) LIKE LOWER(:string)", 0, 10, array(':string' => $last_string));
 
-    foreach ($result as $rtype) {
-      $n = $rtype->name;
-      // Commas and quotes in terms are special cases, so encode 'em.
-      if (strpos($rtype->name, ',') !== FALSE || strpos($rtype->name, '"') !== FALSE) {
-        $n = '"'. str_replace('"', '""', $rtype->name) .'"';
+    foreach (user_relationships_types_load() as $rtype) {
+      if (stripos($rtype->name, $string) === 0) {
+        $n = $rtype->name;
+        // Commas and quotes in terms are special cases, so encode 'em.
+        if (strpos($rtype->name, ',') !== FALSE || strpos($rtype->name, '"') !== FALSE) {
+          $n = '"'. str_replace('"', '""', $rtype->name) .'"';
+        }
+        $matches[$prefix . $n] = check_plain(ur_tt("user_relationships:rtid:$rtype->rtid:name", $rtype->name));
       }
-      $matches[$prefix . $n] = check_plain(ur_tt("user_relationships:rtid:$rtype->rtid:name", $rtype->name));
     }
   }
 
-  drupal_json($matches);
+  drupal_json_output($matches);
 }
diff --git a/user_relationship_views/views_handler_filter_user_relationships_type.inc b/user_relationship_views/views_handler_filter_user_relationships_type.inc
index 0acf52c..5e68279 100644
--- a/user_relationship_views/views_handler_filter_user_relationships_type.inc
+++ b/user_relationship_views/views_handler_filter_user_relationships_type.inc
@@ -75,23 +75,21 @@ class views_handler_filter_user_relationships_type extends views_handler_filter_
    */
   function validate_type_strings(&$form, $values) {
     $rtids = array();
-    $placeholders = array();
-    $args = array();
-    $results = array();
+    $names = array();
     foreach ($values as $value) {
       $missing[strtolower($value)] = TRUE;
-      $args[] = $value;
-      $placeholders[] = "'%s'";
+      $names[] = $value;
     }
 
-    if (!$args) {
+    if (!$names) {
       return $rtids;
     }
 
-    $result = db_query("SELECT * FROM {user_relationship_types} WHERE name IN (:placeholders)", array(':placeholders' => $args));
-    foreach ($result as $rtid) {
-      unset($missing[strtolower($rtype->name)]);
-      $rtids[] = $rtype->rtid;
+    foreach ($names as $name) {
+      if ($rtype = user_relationships_type_load(array('name' => $name))) {
+        unset($missing[strtolower($rtype->name)]);
+        $rtids[] = $rtype->rtid;
+      }
     }
 
     if ($missing) {
-- 
1.7.4.1

