You are here

function user_relationships_save_relationship in User Relationships 5.3

Same name and namespace in other branches
  1. 6 user_relationships_api/user_relationships_api.api.inc \user_relationships_save_relationship()
  2. 7 user_relationships.module \user_relationships_save_relationship()

Create or update a user relationship.

Parameters

$relationship: object of the current relationship

$op: the reason for the update

Return value

object of the updated relationship or false if the relationship wasn't able to save

2 calls to user_relationships_save_relationship()
user_relationships_request_relationship in user_relationships_api/user_relationships_api.api.inc
Public API for creating a relationship.
user_relationships_ui_pending_requested_submit in user_relationships_ui/user_relationships_ui.actions.inc
Approve, Disapprove, or Cancel a relationship request

File

user_relationships_api/user_relationships_api.api.inc, line 191

Code

function user_relationships_save_relationship(&$relationship, $op = 'request') {

  //set basic info if it doesn't already exist
  !isset($relationship->flags) ? $relationship->flags = UR_OK : NULL;
  !isset($relationship->created_at) ? $relationship->created_at = time() : NULL;
  $relationship->updated_at = time();
  _user_relationships_invoke('presave', $relationship);
  if ($relationship->rid) {
    db_query("DELETE FROM {user_relationships} WHERE rid = %d", $relationship->rid);

    // Delete possible requests coming the other direction
    $relationship_type = user_relationships_type_load($relationship->rtid);
    if (!$relationship_type->is_oneway) {
      db_query('DELETE FROM {user_relationships} WHERE rtid = %d AND requester_id = %d AND requestee_id = %d', $relationship->rtid, $relationship->requestee_id, $relationship->requester_id);
    }
  }
  else {
    $relationship->rid = db_next_id('{user_relationships}_id');
  }
  $queries = array();
  $relationship_type = user_relationships_type_load($relationship->rtid);
  $query = 'INSERT INTO {user_relationships} (rid, requester_id, requestee_id, rtid, approved, created_at, updated_at, flags) VALUES';
  $insertions = '(%d, %d, %d, %d, %d, %d, %d, %d)';
  $arguments = array(
    $relationship->rid,
    $relationship->requester_id,
    $relationship->requestee_id,
    $relationship->rtid,
    $relationship->approved,
    $relationship->created_at,
    time(),
    $relationship->flags,
  );
  $queries["{$query} {$insertions}"] = $arguments;

  // Relationship is two-way so we need to do a double entry
  // This keeps db SELECTs fast
  if ($relationship->approved && !$relationship_type->is_oneway) {

    // flip-flop the requester/requestee
    $query = str_replace('requester_id, requestee_id', 'requestee_id, requester_id', $query);
    $queries["{$query} {$insertions}"] = $arguments;
  }
  $valid = TRUE;
  foreach ($queries as $query => $arguments) {
    if (db_query($query, $arguments) === FALSE) {
      $valid = FALSE;
      break;
    }
  }
  if (!$valid) {

    // hopefully we never get here cause it means big problems
    // but let's be gracefull about the failure
    db_query('DELETE FROM {user_relationships} WHERE rid = %d', $relationship->rid);
    unset($relationship->rid);
    return FALSE;
  }
  else {
    _user_relationships_invoke($op, $relationship);
    return $relationship;
  }
}