You are here

function user_relationships_request_relationship in User Relationships 7

Same name and namespace in other branches
  1. 5.3 user_relationships_api/user_relationships_api.api.inc \user_relationships_request_relationship()
  2. 5 user_relationships_api.inc \user_relationships_request_relationship()
  3. 5.2 user_relationships_api.inc \user_relationships_request_relationship()
  4. 6 user_relationships_api/user_relationships_api.api.inc \user_relationships_request_relationship()

Request a new user relationship

Parameters

$requester: object or UID of the requester

$requestee: object or UID of the requestee

$type: object or RTID of the relationship type

$approved: boolean status of the relationship

Return value

object of the newly created relationship

10 calls to user_relationships_request_relationship()
drush_user_relationships_generate_user_relationships in ./user_relationships.drush.inc
Generate relationships.
UserRelationshipsTestCase::testNoOnewayRelationships in ./user_relationships.test
Test the load functions if there are no one way relationship types.
UserRelationshipsTestCase::testRelationshipAPI in ./user_relationships.test
Test various relationship APi functions and params.
UserRelationshipsTestCase::testUserRelationshipsLoadDuplicates in ./user_relationships.test
user_relationships_rules_action_create_relationship in user_relationships_rules/user_relationships_rules.rules.inc
Action Implementation: Create relationship.

... See full list

File

./user_relationships.module, line 563
User Relationships API. Module shell.

Code

function user_relationships_request_relationship($requester, $requestee = NULL, $type = NULL, $approved = FALSE) {

  //#578372 check if this is a "new" type of invocation, where first argument is a relationship object
  if (is_object($requester) && isset($requester->requester) && isset($requester->requestee) && isset($requester->type)) {
    $relationship = $requester;
    $requester = $relationship->requester;
    $requestee = $relationship->requestee;
    $type = $relationship->type;
    if (isset($relationship->approved)) {
      $approved = $relationship->approved;
    }
  }
  else {
    $relationship = new stdClass();
  }

  // translate an ID into an object
  foreach (array(
    'requester' => $requester,
    'requestee' => $requestee,
    'type' => $type,
  ) as $key => $value) {
    if (!is_object($value)) {
      ${$key} = $key == 'type' ? user_relationships_type_load($value) : user_load($value);
    }
  }

  //check requester is allowed to create this rtype
  if (!user_relationships_can_request($requester, $type)) {
    watchdog('user_relationships', 'User %user has no suitable roles to request a %rtype relationship', array(
      '%user' => format_username($requester),
      '%rtype' => user_relationships_type_get_name($type),
    ), WATCHDOG_WARNING);

    //it would be good to return a reason why save is denied, but it's the API portion, it does not expose anything user-visible
    return FALSE;
  }

  //check requestee is allowed to receive this rtype
  if (!user_relationships_can_receive($requestee, $type)) {
    watchdog('user_relationships', 'User %user has no suitable roles to receive a %rtype relationship', array(
      '%user' => format_username($requestee),
      '%rtype' => user_relationships_type_get_name($type),
    ), WATCHDOG_WARNING);

    //it would be good to return a reason why save is denied, but it's the API portion, it does not expose anything user-visible
    return FALSE;
  }

  // check whether this relationship already exists
  if (!$type->is_oneway || !$type->is_reciprocal) {
    $existing = user_relationships_load(array(
      'between' => array(
        $requester->uid,
        $requestee->uid,
      ),
      'rtid' => $type->rtid,
    ), array(
      'count' => TRUE,
    ));
  }
  else {
    $existing = user_relationships_load(array(
      'requester_id' => $requester->uid,
      'requestee_id' => $requestee->uid,
      'rtid' => $type->rtid,
    ), array(
      'count' => TRUE,
    ));
  }
  if (!$existing) {
    $relationship->requester_id = $requester->uid;
    $relationship->requestee_id = $requestee->uid;
    $relationship->approved = $approved || !$type->requires_approval || !empty($requestee->data['user_relationships_ui_auto_approve']) && !empty($requestee->data['user_relationships_ui_auto_approve'][$type->rtid]);
    $relationship->rtid = $type->rtid;
    return user_relationships_save_relationship($relationship);
  }
}