You are here

function user_relationships_save_relationship in User Relationships 6

Same name and namespace in other branches
  1. 5.3 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

6 calls to user_relationships_save_relationship()
UserRelationshipsApiSocnetTestCase::testIsPending in user_relationships_api/tests/user_relationships_api.socnet.test
user_relationships_request_relationship in user_relationships_api/user_relationships_api.api.inc
Request a new user relationship
user_relationships_rules_action_create_relationship in user_relationships_rules/user_relationships_rules.rules.inc
Action Implementation: Create relationship.
user_relationships_ui_pending_requested_submit in user_relationships_ui/user_relationships_ui.actions.inc
Approve, Disapprove, or Cancel a relationship request
user_relationship_implications_user_relationships in user_relationship_implications/user_relationship_implications.module
hook_user_relationships() Add or remove implied relationships as directed by configuration

... See full list

File

user_relationships_api/user_relationships_api.api.inc, line 232
User Relationships API. Data abstraction layer @author Jeff Smick (creator) @author Alex Karshakevich (maintainer) http://drupal.org/user/183217

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();
  if ($op == 'approve') {
    $relationship->approved = 1;
  }
  _user_relationships_invoke('presave', $relationship);
  if (isset($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);
    }
  }

  //#454680 make sure that an update is performed if this is an existing relationship
  if ($valid = drupal_write_record('user_relationships', $relationship, isset($relationship->rid) ? array(
    'rid',
  ) : array())) {

    // if the relationship has been approved and is two-way we need
    // to do a double entry for DB efficiency
    $relationship_type = user_relationships_type_load($relationship->rtid);
    if ($relationship->approved && !$relationship_type->is_oneway) {

      //#365623 drupal_write_record will not work because rid is a serial field, and we need to use the same rid
      $valid = db_query('INSERT INTO {user_relationships} (rid, requester_id, requestee_id, rtid, approved, created_at, updated_at, flags) SELECT rid, requestee_id, requester_id, rtid, approved, created_at, updated_at, flags from {user_relationships} where rid = %d', $relationship->rid);
    }

    // second entry didn't go well. revert the first
    if (!$valid) {
      db_query('DELETE FROM {user_relationships} WHERE rid = %d', $relationship->rid);
      unset($relationship->rid);
    }
  }
  if ($valid) {
    _user_relationships_invoke($op, $relationship);
    return $relationship;
  }
}