You are here

crm_core_contact.install in CRM Core 7

Install, update and uninstall functions for the CRM Core Contact module.

File

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

/**
 * @file
 * Install, update and uninstall functions for the CRM Core Contact module.
 */

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

  // Add default contact type for an Individual person.
  $individual = crm_core_contact_type_new('individual');
  $individual->name = $t('Individual');
  $individual->description = $t('A single individual.');
  $individual->is_new = TRUE;
  crm_core_contact_type_save($individual);

  // Add default contact type for an organization.
  $organization = crm_core_contact_type_new('organization');
  $organization->name = $t('Organization');
  $organization->description = $t('A collection of individuals or other organizations that has additional contact data.');
  crm_core_contact_type_save($organization);

  // Add default contact type for a household.
  $household = crm_core_contact_type_new('household');
  $household->name = $t('Household');
  $household->description = $t('A collection of individuals generally located at the same residence.');
  crm_core_contact_type_save($household);
  crm_core_contact_default_label_format_install();
}

/**
 * Create custom name format to be used as contact label.
 */
function crm_core_contact_default_label_format_install() {
  $t = get_t();
  db_insert('name_custom_format')
    ->fields(array(
    'name' => $t('CRM Core Contact label format'),
    'machine_name' => 'crm_core_contact_label_format',
    'format' => '((((t+ig)+im)+if)+is)+jc',
  ))
    ->execute();
}

/**
 * Implements hook_uninstall().
 */
function crm_core_contact_uninstall() {

  // Instance field information field_info_field is not available
  // because we have already unloaded our module.
  $additional_params = array(
    'include_inactive' => TRUE,
  );
  $instances = field_read_instances(array(
    'entity_type' => 'crm_core_contact',
  ), $additional_params);
  foreach ($instances as $instance) {
    field_delete_instance($instance);
  }
}

/**
 * Implements hook_enable().
 */
function crm_core_contact_enable() {

  // Clear the cache to display in Feeds as available plugin.
  cache_clear_all('plugins:feeds:plugins', 'cache');
}

/**
 * Implements hook_schema().
 */
function crm_core_contact_schema() {
  $schema['crm_core_contact'] = array(
    'description' => 'Stores contact data.',
    'fields' => array(
      'contact_id' => array(
        'description' => 'The primary identifer for a contact.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
        'description' => 'The current {crm_core_contact_revision}.vid of this contact.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'type' => array(
        'description' => 'The {crm_core_contact_type}.type of contact.',
        'type' => 'varchar',
        'length' => 40,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Timestamp for when contact was created.',
      ),
      'changed' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Timestamp for when contact was last changed',
      ),
      'uid' => array(
        'description' => 'The {users}.uid that owns this contact; initially, this is the user that created it.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'name' => array(
        'description' => 'Plain text contact name used in DB queries. Updated on contact update.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'indexes' => array(
      'created' => array(
        'created',
      ),
      'changed' => array(
        'changed',
      ),
      'contact_name' => array(
        'name',
      ),
    ),
    'foreign keys' => array(
      'contact_revision' => array(
        'table' => 'crm_core_contact_revision',
        'columns' => array(
          'vid' => 'vid',
        ),
      ),
    ),
    'primary key' => array(
      'contact_id',
    ),
  );
  $schema['crm_core_contact_revision'] = array(
    'description' => 'Saves information about each saved revision of a {crm_core_contact}',
    'fields' => array(
      'vid' => array(
        'description' => 'The primary identifier of this {crm_core_contact_revision}.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'contact_id' => array(
        'description' => 'The {crm_core_contact}.contact_id for this revision.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'log' => array(
        'description' => 'The log entry explaining the changes in this version.',
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
      ),
      'created' => array(
        'description' => 'The unix timestamp for when this revision was created.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'changed' => array(
        'description' => 'The unix timestamp for when this revision was last changed.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'The {users}.uid that created this version.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'foreign keys' => array(
      'versioned_contact' => array(
        'table' => 'crm_core_contact',
        'columns' => array(
          'contact_id' => 'contact_id',
        ),
      ),
      'version_creator' => array(
        'table' => 'user',
        'columns' => array(
          'uid' => 'uid',
        ),
      ),
    ),
    'primary key' => array(
      'vid',
    ),
  );
  $schema['crm_core_contact_type'] = array(
    'description' => 'Stores information about all defined contact types.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique contact type ID.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'The human-readable name of this type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'translatable' => TRUE,
      ),
      'description' => array(
        'description' => 'A brief description of this type.',
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'medium',
        'translatable' => TRUE,
      ),
      'custom' => array(
        'description' => 'A boolean indicating whether this type is defined by a module (FALSE) or by a user via Add content type (TRUE).',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'disabled' => array(
        'description' => 'A boolean indicating whether this type is disabled or not, disabled contact type contacts will not show up in the list of contacts',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'locked' => array(
        'description' => 'A boolean indicating whether this type is locked or not, locked contact type cannot be edited or disabled/deleted',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'primary_fields' => array(
        'description' => 'Serialized array of key-value pairs, where key is the primary field type and value is real field name used for this type.',
        'type' => 'blob',
        'serialize' => TRUE,
      ),
    ) + entity_exportable_schema_fields(),
    'primary key' => array(
      'id',
    ),
    'unique keys' => array(
      'type' => array(
        'type',
      ),
    ),
  );
  if (module_exists('uuid')) {
    $field = uuid_schema_field_definition();
    $schema['crm_core_contact']['fields']['uuid'] = $field;
    $schema['crm_core_contact']['indexes']['uuid'] = array(
      'uuid',
    );
    $schema['crm_core_contact_revision']['fields']['vuuid'] = $field;
    $schema['crm_core_contact_revision']['indexes']['vuuid'] = array(
      'vuuid',
    );
  }
  return $schema;
}

/**
 * Add database table fields 'module' and 'status' for exportability.
 */
function crm_core_contact_update_7001() {
  $table = 'crm_core_contact_type';

  // Remove old primary key.
  db_drop_primary_key($table);
  db_add_field($table, 'id', array(
    'type' => 'serial',
    'not null' => TRUE,
    'description' => 'Primary Key: Unique contact type ID.',
  ), array(
    'primary key' => array(
      'id',
    ),
  ));
  foreach (entity_exportable_schema_fields() as $field => $spec) {
    db_add_field($table, $field, $spec);
  }

  // Set unique key.
  db_add_unique_key($table, 'type', array(
    'type',
  ));
}

/**
 * Add field to contact type to store primary fields settings.
 */
function crm_core_contact_update_7002() {
  db_add_field('crm_core_contact_type', 'primary_fields', array(
    'description' => 'Serialized array of key-value pairs, where key is the primary field type and value is real field name used for this type.',
    'type' => 'blob',
    'serialize' => TRUE,
  ));
}

/**
 * Add missing uid to contact base table. See http://drupal.org/node/1914326.
 */
function crm_core_contact_update_7003() {
  $table = 'crm_core_contact';
  db_add_field($table, 'uid', array(
    'description' => 'The {users}.uid that owns this contact; initially, this is the user that created it.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));

  // Restore missing data from the revisions table.
  db_query("UPDATE {crm_core_contact}\n    INNER JOIN (\n      SELECT MAX({crm_core_contact_revision}.vid), uid, contact_id\n      FROM {crm_core_contact_revision}\n      WHERE {crm_core_contact_revision}.uid > 0\n      GROUP BY {crm_core_contact_revision}.contact_id\n    ) contact_revisions ON ({crm_core_contact}.contact_id = contact_revisions.contact_id)\n    SET {crm_core_contact}.uid = contact_revisions.uid");
}

/**
 * Unlocking 'contact_name' field to permit change instance settings.
 */
function crm_core_contact_update_7004() {
  $field = field_info_field('contact_name');
  $field['locked'] = FALSE;
  field_update_field($field);
}

/**
 * UUID integration.
 */
function crm_core_contact_update_7005() {
  if (module_exists('uuid')) {
    _crm_core_contact_check_uuid();
    uuid_sync_all();
  }
}

/**
 * Add "name" field to "crm_core_contact" table.
 */
function crm_core_contact_update_7006(&$sandbox) {
  $t = get_t();
  $msgs = array();
  if (!db_field_exists('crm_core_contact', 'name')) {
    $field_schema = array(
      'description' => 'Plain text contact name used in DB queries. Updated on contact update.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
    );
    $new_keys = array(
      'indexes' => array(
        'contact_name' => array(
          'name',
        ),
      ),
    );
    db_add_field('crm_core_contact', 'name', $field_schema, $new_keys);
    $msgs[] = $t('Field "name" was successfully added to "crm_core_contact" table.');
  }
  crm_core_contact_default_label_format_install();

  // Processing existing contacts.
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current'] = 0;
    $sandbox['total'] = db_query('SELECT COUNT(contact_id) FROM {crm_core_contact}')
      ->fetchField();
    if (empty($sandbox['total'])) {
      $msgs[] = $t('No contact records to update.');
      return implode(' ', $msgs);
    }
  }
  $contact_ids = db_select('crm_core_contact', 'c')
    ->fields('c', array(
    'contact_id',
  ))
    ->condition('contact_id', $sandbox['current'], '>')
    ->range(0, 10)
    ->orderBy('contact_id', 'ASC')
    ->execute()
    ->fetchCol();
  foreach ($contact_ids as $contact_id) {
    $contact = crm_core_contact_load($contact_id);
    $contact
      ->save();
    $sandbox['progress']++;
    $sandbox['current'] = $contact
      ->identifier();
  }
  $msgs[] = $t('Updated @count contact records.', array(
    '@count' => count($contact_ids),
  ));
  $sandbox['#finished'] = empty($sandbox['total']) ? 1 : $sandbox['progress'] / $sandbox['total'];
  return implode(' ', $msgs);
}

Functions

Namesort descending Description
crm_core_contact_default_label_format_install Create custom name format to be used as contact label.
crm_core_contact_enable Implements hook_enable().
crm_core_contact_install Implements hook_install().
crm_core_contact_schema Implements hook_schema().
crm_core_contact_uninstall Implements hook_uninstall().
crm_core_contact_update_7001 Add database table fields 'module' and 'status' for exportability.
crm_core_contact_update_7002 Add field to contact type to store primary fields settings.
crm_core_contact_update_7003 Add missing uid to contact base table. See http://drupal.org/node/1914326.
crm_core_contact_update_7004 Unlocking 'contact_name' field to permit change instance settings.
crm_core_contact_update_7005 UUID integration.
crm_core_contact_update_7006 Add "name" field to "crm_core_contact" table.