You are here

function crm_core_activity_update_7002 in CRM Core 7

Populate crm_core_activity.activity_type_id.

File

modules/crm_core_activity/crm_core_activity.install, line 249
Install, update, and uninstall functions for the CRM Core Activity module.

Code

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');
  }
}