Index: user_relationships_api.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/user_relationships/user_relationships_api.inc,v
retrieving revision 1.16
diff -u -r1.16 user_relationships_api.inc
--- user_relationships_api.inc	17 Oct 2007 01:46:39 -0000	1.16
+++ user_relationships_api.inc	19 Oct 2007 19:10:43 -0000
@@ -56,7 +56,7 @@
     $results = db_query("SELECT * FROM {user_relationship_types}");  
     while ($relationship = db_fetch_object($results)) {
       _user_relationships_invoke('load type', $relationship);
-      $relationship_types_list[] = $relationship;
+      $relationship_types_list[$relationship->rtid] = $relationship;
     }
   }
 
@@ -260,7 +260,7 @@
 /**
  * Public API for counting relationships that match specified criteria
  *
- * @param $array
+ * @param $criteria
  *    An associative array of attributes to search for. Keys in the array should match
  *    column names in the user_relationships table. However, as a convenience, you can
  *    also specify the following keys:
@@ -280,7 +280,57 @@
  *
  */
 function user_relationships_count_relationships($criteria = array()) {
-  $query = "SELECT COUNT(rid) FROM {user_relationships} WHERE 1 ";
+  return _user_relationships_select_relationships($criteria, TRUE);
+}
+
+/**
+ * Public API for loading relationships that match specified criteria
+ *
+ * @param $criteria
+ *    An associative array of attributes to search for. Keys in the array should match
+ *    See the comments for user_relationships_count_relationships() above for a description
+ *    of the criteria.
+ *
+ * @return
+ *    An array of the relationships that match the criteria
+ *
+ */
+function user_relationships_load_relationships($criteria = array()) {
+  return _user_relationships_select_relationships($criteria, FALSE);
+}
+
+/**
+ * Public API for loading relationships that match specified criteria, indexed by relatee
+ *
+ * @param $criteria
+ *    An associative array of attributes to search for. Keys in the array should match
+ *    See the comments for user_relationships_count_relationships() above for a description
+ *    of the criteria.
+ *
+ * @param $relater
+ *    The uid of the user whose relatees you want to identify
+ *
+ * @return
+ *    An array indexed by $relatee, where $relatee is whichever of (requester_id, requestee_id)
+ *    doesn't match $relater. Each element of the returned array is an array of relationships
+ *    between $relater and $relatee which match the criteria. If some relationships match the
+ *    criteria, but don't involve $relater, they're returned in the array indexed by $relatee = -1.
+ *
+ */
+function user_relationships_load_relationships_by_relatee($criteria = array(), $relater = -1) {
+  return _user_relationships_select_relationships($criteria, FALSE, $relater);
+}
+
+/**
+ * helper function that does the actual work of counting/loading relationships based on criteria
+ */
+function _user_relationships_select_relationships($criteria = array(), $count_only=FALSE, $relater=FALSE) {
+  if ($count_only) {
+    $query = "SELECT COUNT(rid) FROM {user_relationships} WHERE 1 ";
+  }
+  else {
+    $query = "SELECT * FROM {user_relationships} WHERE 1 ";
+  }
   $args  = array();
 
   foreach($criteria as $column => $value) {
@@ -328,10 +378,38 @@
       // invalid criteria
       return NULL;
       break;
+    } // switch ($column)
+  } // foreach($criteria as $column => $value)
+
+  // if all we want is a count, get it and return it
+  if ($count_only) {
+    return db_result(db_query($query, $args));
+  }
+
+  // load the relationships into an array
+  // if 'uid' was specified as a criteria, index the array by the user id of the user who relates to 'uid'
+  // otherwise, just index it sequentially
+  $relationships = array();
+  $results = db_query($query, $args);
+  while ($relationship = db_fetch_object($results)) {
+    if ($relater) {
+      if ($relater == $relationship->requester_id) {
+        $index = $relationship->requestee_id;
+      }
+      elseif ($relater == $relationship->requestee_id) {
+        $index = $relationship->requester_id;
+      }
+      else {
+        $index = -1; // don't conflict with any valid uid
+      }
+      $relationships[$index][] = $relationship;
+    }
+    else {
+      $relationships[] = $relationship;
     }
   }
-  return db_result(db_query($query, $args));
-}
+  return $relationships;
+} // function _user_relationships_select_relationships($criteria = array(), $count_only=FALSE)
 
 
 /**
