You are here

function redhen_relation_connections_page in RedHen CRM 7

Page callback for listing connections.

Parameters

RedhenContact|RedhenOrg $entity: Entity.

Return value

array|null|string Connections list or message.

1 string reference to 'redhen_relation_connections_page'
redhen_relation_menu in modules/redhen_relation/redhen_relation.module
Implements hook_menu().

File

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

Code

function redhen_relation_connections_page($entity) {
  $related = redhen_relation_relations($entity);
  if (!empty($related)) {
    $header = array(
      'connection' => t('Connection'),
      'type' => t('Type'),
      'name' => t('Name'),
      'role' => t('Roles'),
      'status' => t('Status'),
      'created' => t('Created'),
      'author' => t('Author'),
    );
    if ($entity
      ->entityType() == 'redhen_org') {
      $header['primary'] = t('Primary contact');
    }
    $header['operations'] = array(
      'data' => t('Operations'),
    );
    $destination = drupal_get_destination();
    foreach ($related as $relation_id => $related_entities) {
      $relation = relation_load($relation_id);
      $relation_wrapper = entity_metadata_wrapper('relation', $relation);
      $relation_uri = entity_uri('relation', $relation);
      $reversed = FALSE;
      $endpoints = field_get_items('relation', $relation, 'endpoints');
      foreach ($endpoints as $endpoint) {
        if ($endpoint['entity_type'] == $entity
          ->entityType() && $endpoint['r_index']) {
          $reversed = TRUE;
          break;
        }
      }
      foreach ($related_entities as $related_entity) {
        $object_label = entity_label($related_entity
          ->entityType(), $related_entity);
        $object_uri = entity_uri($related_entity
          ->entityType(), $related_entity);
        $author = user_load($relation->uid);
        if (strpos($relation->relation_type, 'redhen_') === FALSE) {
          $active = t('NA', array(), array(
            'context' => 'redhen_relation',
          ));
        }
        else {
          $items = field_get_items('relation', $relation, REDHEN_RELATION_STATUS_FIELD);
          $active = $items[0]['value'] ? t('Active', array(), array(
            'context' => 'redhen_relation',
          )) : t('Inactive', array(), array(
            'context' => 'redhen_relation',
          ));
        }
        $related_entity_info = entity_get_info($related_entity
          ->entityType());

        // Get roles of the relation.
        $relation_role_names = array();
        if (isset($relation_wrapper->{REDHEN_RELATION_ROLES_FIELD})) {
          $relation_roles = $relation_wrapper->{REDHEN_RELATION_ROLES_FIELD}
            ->value();
          foreach ($relation_roles as $relation_role) {
            if (!empty($relation_role)) {
              $relation_role_names[] = $relation_role
                ->label();
            }
          }
          sort($relation_role_names);
        }
        $relation_role_names = implode(', ', $relation_role_names);
        $data = array(
          'connection' => array(
            'data' => array(
              '#markup' => relation_get_type_label($relation, $reversed),
            ),
          ),
          'type' => $related_entity_info['label'],
          'name' => array(
            'data' => array(
              '#type' => 'link',
              '#title' => $object_label,
              '#href' => $object_uri['path'],
            ),
          ),
          'role' => $relation_role_names,
          'active' => $active,
          'created' => date('m/d/Y', $relation->created),
          'author' => $author->name,
        );
        $row_classes = array();

        // Add primary contact handling for redhen_orgs and contacts.
        if ($entity
          ->entityType() == 'redhen_org' && $related_entity
          ->entityType() == 'redhen_contact') {
          if ($entity->primary_contact_id !== $related_entity->contact_id) {
            $entity_uri = entity_uri('redhen_org', $entity);
            $data['primary'] = array(
              'data' => array(
                '#type' => 'link',
                '#title' => t('set as primary'),
                '#href' => $entity_uri['path'] . '/primary/' . $related_entity->contact_id,
                '#options' => array(
                  'query' => array(
                    $destination,
                  ),
                ),
              ),
            );
          }
          else {
            $data['primary'] = t('Primary');
            $row_classes[] = 'primary_contact';
          }
        }
        elseif ($entity
          ->entityType() == 'redhen_org') {
          $data[] = t('NA', array(), array(
            'context' => 'redhen_relation',
          ));
        }

        // Build a list of all the accessible operations for the current relation.
        $ops = array();
        if (redhen_relation_entity_access('view', $relation)) {
          $ops['view'] = array(
            'title' => t('view'),
            'href' => $relation_uri['path'],
          );
        }
        if (redhen_relation_entity_access('update', $relation)) {
          $ops['edit'] = array(
            'title' => t('edit'),
            'href' => $relation_uri['path'] . '/edit',
            'query' => $destination,
          );
        }
        if (redhen_relation_entity_access('delete', $relation)) {
          $ops['delete'] = array(
            'title' => t('delete'),
            'href' => $relation_uri['path'] . '/delete',
            'query' => $destination,
          );
        }
        if (count($ops) > 1) {

          // Render an unordered list of operations links.
          $data['operations'] = array(
            'data' => array(
              '#theme' => 'links__node_operations',
              '#links' => $ops,
              '#attributes' => array(
                'class' => array(
                  'links',
                  'inline',
                ),
              ),
            ),
          );
        }
        elseif (!empty($ops)) {

          // Render the first and only operation as a link.
          $link = reset($ops);
          $data['operations'] = array(
            'data' => array(
              '#type' => 'link',
              '#title' => $link['title'],
              '#href' => $link['href'],
              '#options' => isset($link['query']) ? array(
                'query' => $link['query'],
              ) : array(),
            ),
          );
        }
        else {
          unset($header['operations']);
        }
        $rows[$relation_id] = array(
          'data' => $data,
          'class' => $row_classes,
        );
      }
    }
    return array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
    );
  }
  else {
    return t('%name has no connections.', array(
      '%name' => $entity
        ->label(),
    ));
  }
}