You are here

crm_core_relationship.install in CRM Core 7

Install, update and uninstall functions for the relationship module.

File

modules/crm_core_relationship/crm_core_relationship.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the relationship module.
 */

/**
 * Implements hook_install().
 */
function crm_core_relationship_install() {
  $t = get_t();

  // Add default relationships
  $relation_types_info = array(
    array(
      'relation_type' => 'crm_friend',
      'label' => $t('is friends with'),
      'source_bundles' => array(
        'crm_core_contact:individual',
      ),
      'r_unique' => TRUE,
    ),
    array(
      'relation_type' => 'crm_employee',
      'label' => $t('works for'),
      'reverse_label' => $t('employs'),
      'directional' => TRUE,
      'r_unique' => TRUE,
      'source_bundles' => array(
        'crm_core_contact:individual',
      ),
      'target_bundles' => array(
        'crm_core_contact:organization',
      ),
    ),
    array(
      'relation_type' => 'crm_member',
      'label' => $t('is a member of'),
      'reverse_label' => $t('has member'),
      'directional' => TRUE,
      'r_unique' => TRUE,
      'source_bundles' => array(
        'crm_core_contact:individual',
      ),
      'target_bundles' => array(
        'crm_core_contact:household',
      ),
    ),
  );
  foreach ($relation_types_info as $relation_type_info) {
    $relation_type = relation_type_create($relation_type_info);
    relation_type_save($relation_type);
  }
}

/**
 * Implements hook_uninstall()
 */
function crm_core_relationship_uninstall() {

  // get all the relationship_types (bundles), find all fields
  // delete them
  $query = db_select('relation_bundles', 'rb')
    ->fields('rb', array(
    'relation_type',
  ))
    ->condition('rb.entity_type', 'crm_core_contact')
    ->distinct()
    ->execute();
  while ($record = $query
    ->fetchAssoc()) {
    $relationship_type[] = $record['relation_type'];
  }
  foreach ($relationship_type as $type) {

    // look into the database for each type
    $relationship_query = db_select('relation', 'r')
      ->fields('r', array(
      'rid',
    ))
      ->condition('r.relation_type', $type)
      ->execute();
    while ($result = $relationship_query
      ->fetchAssoc()) {

      // delete all the relationships of that type
      relation_delete($result['rid']);
    }

    // finally delete the relationshp_type
    relation_type_delete($type);
  }
}

/**
 * Adding "status" field to crm_core relationships.
 */
function crm_core_relationship_update_7000() {
  watchdog('update', 'Starting ' . __FUNCTION__ . ' update.');

  // Attach "status" field to existing crm_core relationships.
  foreach (crm_core_relationship_get_relationships() as $crm_relation) {
    crm_core_relationship_field_attach_create_bundle('relation', $crm_relation->relation_type);
  }
  $t = get_t();

  // Rename "Friend" relationship to "Friend of", if not renamed yet.
  $crm_friend = relation_type_load('crm_friend');
  if ($crm_friend->label == 'Friend') {
    $crm_friend->label = $t('Friend of');
    $crm_friend->reverse_label = $t('Friend of');
    relation_type_save($crm_friend, array(
      'relation_type',
    ));
  }

  // Fill in "status" field for existing relationships.
  $rids = db_select('relation', 'r')
    ->fields('r', array(
    'rid',
  ))
    ->execute()
    ->fetchCol();
  $relations = relation_load_multiple($rids);
  foreach ($relations as $relationship) {
    if (crm_core_relationship_is_relationship_type($relationship->relation_type)) {
      $relationship->crm_core_relationship_status[LANGUAGE_NONE][0]['value'] = 1;
      field_attach_update('relation', $relationship);
    }
  }
  watchdog('update', 'Finished ' . __FUNCTION__ . ' update.');
}

Functions

Namesort descending Description
crm_core_relationship_install Implements hook_install().
crm_core_relationship_uninstall Implements hook_uninstall()
crm_core_relationship_update_7000 Adding "status" field to crm_core relationships.