Skip to content
crm_core_relationship_ui.module 14.5 KiB
Newer Older
 */
function crm_core_relationship_ui_menu() {
  $items = array();
  /*
   * CRM Contact Relationships
   */
    'no_contact' => 'crm-core/contact/relationship-add',
    'contact' => 'crm-core/contact/%crm_core_contact/relationships/add',
  foreach ($relation_paths as $key => $path) {
    $items[$path] = array(
      'title' => 'Add a relationship',
      'description' => 'Add a relationship to this contact',
      'page callback' => 'crm_core_relationship_ui_add_relationship',
      'page arguments' => array(($key == 'contact' ? 2 : NULL)),
      'access callback' => TRUE,
      'type' => MENU_LOCAL_ACTION,
      'file' => 'crm_core_relationship_ui.pages.inc',

    foreach (crm_core_relationship_get_relationships() as $type => $info) {
      $items[$path . '/' . $type . '/%'] = array(
        'title' => 'Add ' . $info->label,
        'description' => 'Add a relationship to this contact',
        'page callback' => 'crm_core_relation_entity_ui_get_form_wrapper',
        'page arguments' => ($key == 'contact' ? array( 2, 5, 6) : array(NULL, 3, 4)),
        'access callback' => 'crm_core_relationship_access_permissions',
        'access arguments' => array(NULL, 'create_view'),
        'type' => MENU_NORMAL_ITEM,
        'file' => 'crm_core_relationship_ui.pages.inc',
      );
    }
  $items['crm-core/contact/%crm_core_contact/relationships/add/%relation_type/%/autocomplete'] = array(
    'page callback' => 'crm_core_relationship_ui_add_relationship_autocomplete',
    'page arguments' => array(2, 5, 6),
    'access callback' => 'crm_core_relationship_access_permissions',
    'access arguments' => array(5, 'create'),
    'type' => MENU_CALLBACK,
    'file' => 'crm_core_relationship_ui.pages.inc',
  );

  $items['crm-core/relationships/%relation_type/%/autocomplete'] = array(
    'page callback' => 'crm_core_relationship_ui_add_relationship_autocomplete',
    'page arguments' => array(NULL, 2, 3),
    'access callback' => 'crm_core_relationship_access_permissions',
    'access arguments' => array(2, 'create'),
    'type' => MENU_CALLBACK,
    'file' => 'crm_core_relationship_ui.pages.inc',
  );

  /*
   * CRM Relationship types
   */
  $items['admin/structure/crm-core/relationship-types'] = array(
    'title' => 'CRM Core Relationship Types',
    'description' => 'Manage relationship types.',
    'page callback' => 'crm_core_relationship_ui_types_overview',
    'access callback' => 'crm_core_relationship_access_permissions',
    'access arguments' => array(NULL, 'admin'),
    'file' => 'crm_core_relationship_ui.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );

  $items['admin/structure/crm-core/relationship-types/add'] = array(
    'access callback' => 'crm_core_relationship_access_permissions',
    'access arguments' => array(NULL, 'admin'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('crm_core_relationship_ui_type_form'),
    'type' => MENU_LOCAL_ACTION,
    'file' => 'crm_core_relationship_ui.admin.inc',
  $items['admin/structure/crm-core/relationship-types/manage/%crm_core_relationship_ui_relationship_type'] = array(
    'access callback' => 'crm_core_relationship_access_permissions',
    'access arguments' => array(NULL, 'admin'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('crm_core_relationship_ui_type_form', 5),
    'file' => 'crm_core_relationship_ui.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['admin/structure/crm-core/relationship-types/manage/%crm_core_relationship_ui_relationship_type/delete'] = array(
    'access callback' => 'crm_core_relationship_access_permissions',
    'access arguments' => array(NULL, 'admin'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'crm_core_relationship_ui_type_delete_confirm',
      5
    ),
    'file' => 'crm_core_relationship_ui.admin.inc',
    'type' => MENU_CALLBACK,
  $items['crm-core/contact/%/relationships/%/active'] = array(
    'title' => 'Toggle relationship status',
    'access arguments' => array('edit relations'),
    'page callback' => 'crm_core_relationship_ui_toggle_relationship_status',
    'page arguments' => array(4, TRUE),
    'file' => 'crm_core_relationship_ui.pages.inc',
  );
  $items['crm-core/contact/%/relationships/%/inactive'] = array(
    'title' => 'Toggle relationship status',
    'access arguments' => array('edit relations'),
    'page callback' => 'crm_core_relationship_ui_toggle_relationship_status',
    'page arguments' => array(4, FALSE),
    'file' => 'crm_core_relationship_ui.pages.inc',
  );
  /**
   * Relationship Edit/Delete section.
   */
  $items['crm-core/contact/%/relationships/%relation/edit'] = array(
    'title' => 'Edit',
    'access arguments' => array('edit relations'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('relation_edit_form', 4),
    'type' => MENU_LOCAL_ACTION,
  );
  $items['crm-core/contact/%/relationships/%relation/delete'] = array(
    'title' => 'Delete',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('relation_delete_confirm', 4),
    'access arguments' => array('delete relations'),
    'weight' => 10,
    'type' => MENU_LOCAL_ACTION,
  );
/**
 * Implements hook_menu_alter
 */
function crm_core_relationship_ui_menu_alter(&$items) {
  // we are simply hiding crm relationships from other types of relations
  $items['admin/structure/relation']['page callback'] = 'crm_core_relationship_ui_types_overview';
  $items['admin/structure/relation']['file path'] = drupal_get_path('module', 'crm_core_relationship_ui');
  $items['admin/structure/relation']['file'] = 'crm_core_relationship_ui.admin.inc';
  $items['admin/structure/relation']['page arguments'] = array(1);
/**
 * Implements hook_form_alter
 */
function crm_core_relationship_ui_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'field_ui_field_overview_form' || $form_id == 'field_ui_display_overview_form') {
    // we only care about crm contact types (bundles)
    // setting title to the fields form
    if (crm_core_relationship_is_relationship_type($form['#bundle'])) {
      drupal_set_title($form['#bundle']);
/**
 * Implements hook_entity_info_alter().
 */
function crm_core_relationship_entity_info_alter(&$entity_info) {
  // we are only altering the admin path for CRM relationship types
  foreach (relation_get_types() as $type => $info) {
    if (crm_core_relationship_is_relationship_type($type)) {
      $entity_info['relation']['bundles'][$type]['label'] = $type;
      $entity_info['relation']['bundles'][$type]['admin'] = array(
        'path' => 'admin/structure/crm-core/relationship-types/manage/%relation_type',
        'real path' => 'admin/structure/crm-core/relationship-types/manage/' . $type,
        'access callback' => 'crm_core_relationship_access_permissions',
        'access arguments' => array(NULL, 'admin'),
/**
 * Argument loading function
 *
 * @param string $relationship_type
 * machine readable name of the relationship type
 *
 * @return object $relation_type
 * A loaded relation type object
 */
function crm_core_relationship_ui_relationship_type_load($relationship_type) {
  return relation_type_load($relationship_type);
}

/**
 * Implements hook_field_formatter_info_alter
 */
function crm_core_relationship_ui_field_formatter_info_alter(&$info) {
  // we will add our own field formatter
  // for display relationships
  $info['contact_relationship'] = array(
    'label' => t('Related Contact'),
    'field types' => array('relation_endpoint'),
    'module' => 'crm_core_relationship_ui',
  );
  $info['contact_relationship_human'] = array(
    'label' => t('Related Contact(Human readable)'),
    'field types' => array('relation_endpoint'),
    'module' => 'crm_core_relationship_ui',
  );
  $info['contact_relationship_view'] = array(
    'label' => t('Related Contact(Table)'),
    'field types' => array('relation_endpoint'),
    'module' => 'crm_core_relationship_ui',
  );
  $info['contact_relationship_current'] = array(
    'label' => t('Current Contact'),
    'field types' => array('relation_endpoint'),
    'module' => 'crm_core_relationship_ui',
  );
}

/**
 * Implements hook_field_formatter_view
 */
function crm_core_relationship_ui_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
      // getting the contact id from the url
      $current_contact_id = $display['views_view']->args[0];
      $related_contact_id = ($items[1]['entity_id'] == $current_contact_id) ? $items[0]['entity_id'] : $items[1]['entity_id'];
      $related_contact = crm_core_contact_load($related_contact_id);
      $contact_label = entity_label('crm_core_contact', $related_contact);
      $relationship_type = ($items[1]['entity_id'] == $current_contact_id) ? relation_get_type_label($entity, TRUE) : relation_get_type_label($entity);
      $created = date('Y-m-d H:i:s', $entity->created);
      $related_contact_uri = entity_uri('crm_core_contact', $related_contact);

      $rows[] = array(
        l($contact_label, $related_contact_uri['path'], $related_contact_uri['options']),
        $relationship_type,
        $created,
      $headers = array('Related Contact', 'Relationship Type', 'Created');
      return array(
        '#theme' => 'table',
        '#header' => $headers,
        '#rows' => $rows,
      );
      // getting the contact id from the url
      $current_contact_id = $display['views_view']->args[0];
      if (isset($items[0]['entity_id']) && isset($items[1]['entity_id'])) {
        $related_contact_id = ($items[1]['entity_id'] == $current_contact_id) ? $items[0]['entity_id'] : $items[1]['entity_id'];
        $related_contact = crm_core_contact_load($related_contact_id);
        $contact_label = entity_label('crm_core_contact', $related_contact);
        $related_contact_uri = entity_uri('crm_core_contact', $related_contact);
        return array(array('#markup' => l($contact_label, $related_contact_uri['path'])));
      return array(array('#markup' => ''));
    case 'contact_relationship_human':
      $current_contact_id = $display['views_view']->args[0];
      $current_contact = crm_core_contact_load($current_contact_id);
      $current_contact_label = entity_label('crm_core_contact', $current_contact);
      $current_contact_uri = entity_uri('crm_core_contact', $current_contact);
      $current_contact_link = l($current_contact_label, $current_contact_uri['path']);
      $related_contact_id = ($items[1]['entity_id'] == $current_contact_id) ? $items[0]['entity_id'] : $items[1]['entity_id'];
      $related_contact = crm_core_contact_load($related_contact_id);
      $related_contact_label = entity_label('crm_core_contact', $related_contact);
      $related_contact_uri = entity_uri('crm_core_contact', $related_contact);
      $related_contact_link = l($related_contact_label, $related_contact_uri['path']);
      $relationship_type = relation_type_load($entity->relation_type);
      // Check if we need a reverse label.
      $relation_label = '';
      if ($relationship_type->directional && $items[1]['entity_id'] == $current_contact_id) {
        $relation_label = strtolower(relation_get_type_label($entity, TRUE));
      }
      else {
        $relation_label = strtolower(relation_get_type_label($entity));
      }

      return array(
        array(
          '#markup' => t('!current_contact @relation !related_contact', array(
            '!current_contact' => $current_contact_link,
            '@relation' => $relation_label,
            '!related_contact' => $related_contact_link,
          ))
        )
      );
      break;
    case 'contact_relationship_current':
      $current_contact_id = $display['views_view']->args[0];
      $current_contact = crm_core_contact_load($current_contact_id);
      $current_contact_label = entity_label('crm_core_contact', $current_contact);
      return array(
        array(
          '#markup' => t('@current_contact', array(
            '@current_contact' => $current_contact_label,
          ))
        )
      );
      break;
  }
}

/**
 * Implements hook_views_api().
 */
function crm_core_relationship_ui_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'crm_core_relationship_ui'),
  );
}

/**
 * Alter query of view "crm_relationships_by_contact".
 */
function crm_core_relationship_ui_query_views_crm_relationships_by_contact_alter($query) {
  $where =& $query->conditions();
Yuriy Gerasimov's avatar
Yuriy Gerasimov committed
  if (!isset($where[0]['field'])) {
    return;
  }
  $field_conditions =& $where[0]['field']->conditions();
  $field_field_conditions =& $field_conditions[0]['field']->conditions();

  // If condition is to find active relationships we also include NULL values.
  if ($field_field_conditions[0]['operator'] == 'not in') {
    unset($field_field_conditions[0]);
    $or = db_or();
    $or->condition('field_data_field_relationship_active.field_relationship_active_value', NULL, 'IS');
    $or->condition('field_data_field_relationship_active.field_relationship_active_value', 'active', '<>');
    $query->condition($or);
/**
 * Implements hook_views_pre_render().
 */
function crm_core_relationship_ui_views_pre_render(&$view) {
  if ($view->name == 'crm_relationships_by_contact') {
    // Grabbing 1st view argument to avoid preview problems.
    $contact_id = $view->args[0];
    $contact = crm_core_contact_load($contact_id);
    _crm_core_relationships_ui_breadcrmub($contact);
    $view->build_info['title'] = t('Relationships for @contact_name', array('@contact_name' => $contact->label()));

/**
 * Helper function sets breadcrumb on contact pages.
 *
 * @param $contact object
 *  Entity object of crm_core_contact type.
 */
function _crm_core_relationships_ui_breadcrmub($contact) {
  $contact_uri = $contact->uri();
  $breadcrumb = array(
    l(t('Home'), '<front>'),
    l(t('CRM Core'), 'crm-core'),
    l(t('Contacts'), 'crm-core/contact'),
    l(t($contact->label()), $contact_uri['path']),
    l(t('Relationships'), $contact_uri['path'] . '/relationships'),
  );
  drupal_set_breadcrumb($breadcrumb);
}

/**
 * Implements hook_preprocess_page().
 */
function crm_core_relationship_ui_preprocess_page(&$vars) {
  //TODO: Figure out better way to get rid of tabs at "crm-core/contact/%/relationships/add" page.
  if (arg(0) == 'crm-core' && arg(1) == 'contact' && arg(3) == 'relationships' && arg(4) == 'add') {
    $vars['tabs']['#primary'] = array();
  }
}