You are here

crm_core_activity.install in CRM Core 7

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

File

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

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

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

  // Create a default meeting activity type.
  $meeting = entity_create('crm_core_activity_type', array(
    'type' => 'meeting',
    'label' => $t('Meeting'),
    'description' => $t('A meeting between 2 or more contacts'),
  ));
  crm_core_activity_type_save($meeting);

  // Create a phone call activity type.
  $phone_call = entity_create('crm_core_activity_type', array(
    'type' => 'phone_call',
    'label' => $t('Phone call'),
    'description' => $t('A phone call between 2 or more contacts'),
  ));
  crm_core_activity_type_save($phone_call);
}

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

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

/**
 * Implements hook_uninstall().
 */
function crm_core_activity_uninstall() {
  $additional_params = array(
    'include_inactive' => TRUE,
  );
  $instances = field_read_instances(array(
    'entity_type' => 'crm_core_activity',
  ), $additional_params);
  foreach ($instances as $instance) {
    field_delete_instance($instance);
  }
}

/**
 * Implements hook_schema().
 */
function crm_core_activity_schema() {
  $schema = array();
  $schema['crm_core_activity'] = array(
    'description' => 'The base table for activities.',
    'fields' => array(
      'activity_id' => array(
        'description' => 'The primary identifier for an activity.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'revision_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'default' => NULL,
        'description' => 'Primary identifier of this {crm_core_activity_revision}.',
      ),
      'uid' => array(
        'description' => 'The {users}.uid that owns this activity; initially, this is the user that created it.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'The type (bundle) of this activity.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'title' => array(
        'description' => 'The title of the activity.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the activity was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the activity was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'created' => array(
        'created',
      ),
      'changed' => array(
        'changed',
      ),
    ),
    'foreign keys' => array(
      'activity_type' => array(
        'table' => 'crm_core_activity_type',
        'columns' => array(
          'type' => 'type',
        ),
      ),
      'activity_revision' => array(
        'table' => 'crm_core_activity_revision',
        'columns' => array(
          'revision_id' => 'revision_id',
        ),
      ),
    ),
    'primary key' => array(
      'activity_id',
    ),
  );
  $schema['crm_core_activity_revision'] = array(
    'description' => 'Stores information about each saved revision of a {crm_core_activity}',
    'fields' => array(
      'revision_id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary identifier of this {crm_core_activity_revision}.',
      ),
      'activity_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'default' => NULL,
        'description' => 'The {crm_core_activity}.activity_id for this revision.',
      ),
      'uid' => array(
        'description' => 'The {users}.uid that created this version.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'title' => array(
        'description' => 'The title of the activity.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the activity was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the activity was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'foreign keys' => array(
      'versioned_activity' => array(
        'table' => 'crm_core_activity',
        'columns' => array(
          'activity_id' => 'activity_id',
        ),
      ),
      'version_creator' => array(
        'table' => 'user',
        'columns' => array(
          'uid' => 'uid',
        ),
      ),
    ),
    'primary key' => array(
      'revision_id',
    ),
  );
  $schema['crm_core_activity_type'] = array(
    'description' => 'Stores information about all defined activity types.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique activity type ID.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => 'The human-readable name of this type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'A brief description of this type.',
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'medium',
        'translatable' => TRUE,
      ),
      'activity_string' => array(
        'description' => 'Text describing the relationship between the contact and this activity.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'translatable' => 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_activity']['fields']['uuid'] = $field;
    $schema['crm_core_activity']['indexes']['uuid'] = array(
      'uuid',
    );
    $schema['crm_core_activity_revision']['fields']['vuuid'] = $field;
    $schema['crm_core_activity_revision']['indexes']['vuuid'] = array(
      'vuuid',
    );
  }
  return $schema;
}

/**
 * Update crm_core_activity schema.
 */
function crm_core_activity_update_7001(&$sandbox) {
  if (!db_field_exists('crm_core_activity', 'activity_type_id')) {
    $spec = array(
      'description' => 'The current {crm_core_activity_type}.id of this activity.',
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    );
    db_add_field('crm_core_activity', 'activity_type_id', $spec);
  }
}

/**
 * Populate crm_core_activity.activity_type_id.
 */
function crm_core_activity_update_7002(&$sandbox) {

  // Update 10 activities at a time to change {crm_core_activity}.type to
  // int value of {crm_core_activity_type}.id
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_activity_id'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT activity_id) FROM {crm_core_activity}')
      ->fetchField();
  }
  $activities = db_select('crm_core_activity', 'a')
    ->fields('a')
    ->condition('activity_id', $sandbox['current_activity_id'], '>')
    ->range(0, 10)
    ->orderBy('activity_id', 'ASC')
    ->execute();
  foreach ($activities as $activity) {

    // Get the matching id from {crm_core_activity_type}.
    $id = db_select('crm_core_activity_type', 'at')
      ->fields('at', array(
      'id',
    ))
      ->condition('type', $activity->type, '=')
      ->execute()
      ->fetchField();
    if (is_numeric($id)) {
      db_update('crm_core_activity')
        ->fields(array(
        'activity_type_id' => $id,
      ))
        ->condition('activity_id', $activity->activity_id)
        ->execute();
    }
    $sandbox['progress']++;
    $sandbox['current_activity_id'] = $activity->activity_id;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  if ($sandbox['#finished']) {
    drupal_flush_all_caches();
    $t = get_t();
    return $t('Added and populated field values for new {crm_core_activity}.activity_type_id field');
  }
}

/**
 * Enabling revisions for activities.
 */
function crm_core_activity_update_7003() {
  db_add_field('crm_core_activity', 'revision_id', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => FALSE,
    'default' => NULL,
    'description' => 'Primary identifier of this {crm_core_activity_revision}.',
  ));
  db_add_index('crm_core_activity', 'created', array(
    'created',
  ));
  db_add_index('crm_core_activity', 'changed', array(
    'changed',
  ));

  // Initialize the revision_id to be the same as the activity_id.
  db_update('crm_core_activity')
    ->expression('revision_id', 'activity_id')
    ->execute();
  $revision_schema = array(
    'description' => 'Stores information about each saved revision of a {crm_core_activity}',
    'fields' => array(
      'revision_id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary identifier of this {crm_core_activity_revision}.',
      ),
      'activity_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'default' => NULL,
        'description' => 'The {crm_core_activity}.activity_id for this revision.',
      ),
      'uid' => array(
        'description' => 'The {users}.uid that created this version.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'title' => array(
        'description' => 'The title of the activity.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the activity was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the activity was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'foreign keys' => array(
      'versioned_activity' => array(
        'table' => 'crm_core_activity',
        'columns' => array(
          'activity_id' => 'activity_id',
        ),
      ),
      'version_creator' => array(
        'table' => 'user',
        'columns' => array(
          'uid' => 'uid',
        ),
      ),
    ),
    'primary key' => array(
      'revision_id',
    ),
  );
  db_create_table('crm_core_activity_revision', $revision_schema);
  $activities = db_select('crm_core_activity', 'activity')
    ->fields('activity')
    ->execute();
  foreach ($activities as $activity) {

    // Update crm_core_activity_revision table.
    db_insert('crm_core_activity_revision')
      ->fields(array(
      'revision_id' => $activity->activity_id,
      'activity_id' => $activity->activity_id,
      'uid' => $activity->uid,
      'title' => $activity->title,
      'created' => $activity->created,
      'changed' => $activity->changed,
    ))
      ->execute();
  }
}

/**
 * UUID integration.
 */
function crm_core_activity_update_7004() {
  if (module_exists('uuid')) {
    _crm_core_activity_check_uuid();
    uuid_sync_all();
  }
  db_drop_field('crm_core_activity', 'activity_type_id');
}

/**
 * Adding 'activity string' as property of activity type.
 */
function crm_core_activity_update_7005() {
  db_add_field('crm_core_activity_type', 'activity_string', array(
    'description' => 'Text describing the relationship between the contact and this activity.',
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));
}

Functions

Namesort descending Description
crm_core_activity_enable Implements hook_enable().
crm_core_activity_install Implements hook_install().
crm_core_activity_schema Implements hook_schema().
crm_core_activity_uninstall Implements hook_uninstall().
crm_core_activity_update_7001 Update crm_core_activity schema.
crm_core_activity_update_7002 Populate crm_core_activity.activity_type_id.
crm_core_activity_update_7003 Enabling revisions for activities.
crm_core_activity_update_7004 UUID integration.
crm_core_activity_update_7005 Adding 'activity string' as property of activity type.