You are here

function user_relationship_service_request in User Relationships 7

Same name and namespace in other branches
  1. 5.2 plugins/user_relationship_service/user_relationship_service.module \user_relationship_service_request()
  2. 6 user_relationship_service/user_relationship_service.inc \user_relationship_service_request()

Request a relationship.

Parameters

$uid: User id of person to request relationship with

$type_name: Name of type of relationship to request. See user_relationships_types_load() or [GET] {endpoint}/relationships/types

Return value

The relationship object of the newly created relationship.

See also

user_relationships_request_relationship()

1 string reference to 'user_relationship_service_request'
_user_relationship_service_definition in user_relationship_service/user_relationship_service.module

File

user_relationship_service/user_relationship_service.inc, line 165
Link general user relationship functionalities to services module.

Code

function user_relationship_service_request($uid, $type_name) {
  try {
    $type = user_relationships_type_load(array(
      'name' => $type_name,
    ));
    if (!$type) {
      throw new Exception(t('No such relationship type'));
    }
    global $user;
    $existing_relationship = user_relationships_load(array(
      'rtid' => $type->rtid,
      'between' => array(
        $user->uid,
        $uid,
      ),
    ));

    // If there is already an existing relationship, return it.
    if (!empty($existing_relationship)) {
      return current($existing_relationship);
    }
    $ret = user_relationships_request_relationship($user, $uid, $type);
    if (!$ret) {
      throw new Exception(t('Unknown failure or permission denied'));
    }
    elseif (!is_object($ret)) {
      throw new Exception($ret);
    }
    return $ret;
  } catch (Exception $ex) {
    return services_error(t('Error requesting relationship: @msg', array(
      '@msg' => $ex
        ->getMessage(),
    )), 406);
  }
}