You are here

function redhen_relation_relations in RedHen CRM 7

Return entities related to a given entity excluding self and duplicates.

Parameters

object $entity:

string $relation_types: Filter to given relation types if provided.

bool $active: Optionally limit relations to those that are active.

Return value

array Keyed by the relation id which contains an array of entities. Array ( 1 => array($entity1, $entity2), 3 => array($entity3, $entity4) )

5 calls to redhen_relation_relations()
redhen_contact_access in modules/redhen_contact/redhen_contact.module
Checks contact access for various operations.
redhen_org_group_contact_groups in modules/redhen_org_group/redhen_org_group.module
Return all redhen_org groups that a given user belongs to.
redhen_relation_connections_page in modules/redhen_relation/redhen_relation.module
Page callback for listing connections.
redhen_relation_get_available_entities in modules/redhen_relation/redhen_relation.module
Return a list of entities the given entity can be associated with.
redhen_relation_role_get_permissions in modules/redhen_relation/redhen_relation.module
Return relation role permission for a given account and permission.

File

modules/redhen_relation/redhen_relation.module, line 532
Redhen CRM Relation Module.

Code

function redhen_relation_relations($entity, $relation_types = array(), $active = FALSE) {

  // Backwards compatibility:
  if ($relation_types && !is_array($relation_types)) {
    $relation_types = array(
      $relation_types,
    );
  }
  $related_entities =& drupal_static(__FUNCTION__ . $entity
    ->entityType() . $entity
    ->internalIdentifier() . implode('-', $relation_types) . $active, array());
  if ($related_entities) {
    return $related_entities;
  }
  $query = relation_query($entity
    ->entityType(), $entity
    ->internalIdentifier());
  if ($relation_types) {
    $query
      ->propertyCondition('relation_type', $relation_types, 'IN');
  }

  // Optionally limit to only active relations.
  if ($active) {
    $query
      ->fieldCondition(REDHEN_RELATION_STATUS_FIELD, 'value', TRUE);
  }

  // This hideous hack is to avoid triggering the node access system which we
  // need to to in order to avoid infinite recursion in
  // redhen_org_group:node_grants().
  // time came from http://drupal.stackexchange.com/questions/3927/how-to-bypass-node-access-when-using-entityfieldquery
  // ticket pointing to issue http://drupal.org/node/1541236.
  $query
    ->addMetaData('account', user_load(1));
  $query
    ->addTag('redhen_relation');
  $results = $query
    ->execute();
  if ($results) {
    $relations = relation_load_multiple(array_keys($results));
    foreach ($relations as $relation) {
      $rid = entity_id('relation', $relation);
      $related_entities[$rid] = array();

      // To make sure duplicates of $entity get included in object list.
      $duplicate = FALSE;
      $endpoints = field_get_items('relation', $relation, 'endpoints');
      foreach ($endpoints as $endpoint) {

        // Add all entities that aren't this entity or duplicates, unless the
        // relation only has this entity as endpoint ($relation->arity = 1).
        if ($relation->arity > 1 && $endpoint['entity_type'] == $entity
          ->entityType() && $endpoint['entity_id'] == $entity
          ->internalIdentifier() && $duplicate == FALSE) {
          $duplicate = TRUE;
        }
        else {
          $object_entities = entity_load($endpoint['entity_type'], array(
            $endpoint['entity_id'],
          ));
          $related_entities[$rid][] = reset($object_entities);
        }
      }
    }
  }
  return $related_entities;
}