You are here

function _user_relationships_select_relationships in User Relationships 5

helper function that does the actual work of counting/loading relationships based on criteria

3 calls to _user_relationships_select_relationships()
user_relationships_count_relationships in ./user_relationships_api.inc
Public API for counting relationships that match specified criteria
user_relationships_load_relationships in ./user_relationships_api.inc
Public API for loading relationships that match specified criteria
user_relationships_load_relationships_by_relatee in ./user_relationships_api.inc
Public API for loading relationships that match specified criteria, indexed by relatee

File

./user_relationships_api.inc, line 343

Code

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) {
    $column = strtolower($column);
    switch ($column) {
      case 'uid':
      case 'uid1':
      case 'uid2':
        if (is_array($value)) {
          $query .= "AND (requester_id IN (%s) OR requestee_id IN (%s)) ";
          $args[] = implode(', ', $value);
          $args[] = implode(', ', $value);
        }
        else {
          $query .= "AND (requester_id=%d OR requestee_id=%d) ";
          $args[] = (int) $value;
          $args[] = (int) $value;
        }
        break;
      case 'typename':
        if ($type = user_relationships_relationship_type_load(array(
          'name' => $value,
        ))) {
          $query .= "AND rtid=%d ";
          $args[] = (int) $type->rtid;
        }
        else {
          return NULL;

          // if an invalid typename was specified, no relationships can match it
        }
        break;
      case 'rid':
      case 'requester_id':
      case 'requestee_id':
      case 'rtid':
        if (is_array($value)) {
          $query .= "AND {$column} IN (%s) ";
          $args[] = implode(', ', $value);
        }
        else {
          $query .= "AND {$column}=%d ";
          $args[] = (int) $value;
        }
        break;
      case 'approved':
        $query .= "AND {$column}=%d ";
        $args[] = (int) $value;
        break;
      case 'created_at':
      case 'updated_at':

        // for the dates, the first character of $value should be the operator, i.e. '<', '=' or '>'
        // the remainder should be the value: either a (numeric) Unix timestamp, or a string formatted
        //   to match MySQL's date/timestamp format
        //   e.g. 'created_at' => '>2006-12-31 23:59:59' to find relationships added in 2007 or later
        $operator = substr($value, 0, 1);
        $value = substr($value, 1);
        if (is_numeric($value)) {
          $value = date('Y-m-d H:i:s', (int) $value);
        }
        $query .= "AND {$column}{$operator}'%s' ";
        $args[] = $value;
        break;
      default:

        // 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 $relater was not specified, load the relationships into a flat array with an arbitrary index
  // if $relater was specified, index the array by the user id of the user who relates to $relater: each
  //    element of the returned array is an array of relationships between $relater and $relatee; if
  //    a relationship matches the criteria, but doesn't involve $relater, use -1 as the $relatee
  $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 $relationships;
}