You are here

function user_relationships_get_requestable_rtypes in User Relationships 7

Get relationship types that a user can request from another user.

Parameters

$requester: Requesting user object.

$requestee: The requestee user object.

$return: What should be returned, either name for the translated relationship name or full for the whole object. Defaults to name.

Return value

An array of relationship types a user can request from another, keyed by the relationship type id and either the name or full object as value, depending on the return argument.

2 calls to user_relationships_get_requestable_rtypes()
user_relationships_ui_actions_between in user_relationships_ui/user_relationships_ui.module
List of possible relationship actions with between two users.
user_relationships_ui_request_form in user_relationships_ui/user_relationships_ui.forms.inc
Request new user to user relationship

File

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

Code

function user_relationships_get_requestable_rtypes($requester, $requestee, $return = 'name') {
  $current_relationships = user_relationships_load(array(
    'between' => array(
      $requester->uid,
      $requestee->uid,
    ),
  ), array(
    'sort' => 'rtid',
  ));
  $relationship_types = user_relationships_types_load();
  $relationships = array();
  foreach ($relationship_types as $rtype) {

    // Exclude types that are not allowed.
    if (!user_relationships_can_request($requester, $rtype) || !user_relationships_can_receive($requestee, $rtype)) {
      continue;
    }

    // If there is a relationship o that type already, make further checks.
    if (isset($current_relationships[$rtype->rtid])) {
      $relationship = $current_relationships[$rtype->rtid];
      if (is_array($relationship) && count($relationship)) {
        $relationship = $relationship[0];
      }

      // Skip two-way relationships, one-way non-reciprocal relationships, or
      // reciprocal where this direction already exists.
      if (isset($current_relationships[$rtype->rtid]) && (!$rtype->is_oneway || !$rtype->is_reciprocal || isset($relationship) && $relationship->requester_id == $requester->uid)) {

        // If users can't have multiple relationships, we're done here.
        // Return an empty array.
        if (!variable_get('user_relationships_allow_multiple', TRUE)) {
          return array();
        }
        continue;
      }
    }
    if ($return == 'name') {
      $relationships[$rtype->rtid] = user_relationships_type_get_name($rtype);
    }
    else {
      $relationships[$rtype->rtid] = $rtype;
    }
  }
  return $relationships;
}