You are here

function birthdays_update_7001 in Birthdays 7

Upgrade the field data to Drupal 7.

File

./birthdays.install, line 182
Installation file for the Birthdays module.

Code

function birthdays_update_7001(&$sandbox) {
  if (!db_table_exists('dob')) {
    return;
  }
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['last_uid_processed'] = -1;
    $sandbox['max'] = db_query('SELECT COUNT(*) FROM {dob}')
      ->fetchField();
  }
  $instance = field_info_instance('user', 'birthdays', 'user');
  $query = db_query_range('SELECT uid, DAY(birthday) AS day, MONTH(birthday) AS month, YEAR(birthday) AS year FROM {dob} WHERE uid > :lastuid ORDER BY uid', 0, 10, array(
    ':lastuid' => $sandbox['last_uid_processed'],
  ));
  $i = 0;
  foreach ($query as $row) {
    $data = array(
      'entity_type' => 'user',
      'bundle' => 'user',
      'deleted' => 0,
      'entity_id' => $row->uid,
      'revision_id' => NULL,
      'language' => LANGUAGE_NONE,
      'delta' => 0,
      'birthdays_day' => $row->day,
      'birthdays_month' => $row->month,
      'birthdays_year' => $row->year,
      'birthdays_triggers' => 1,
    );
    $account = user_load($row->uid);
    if ($instance['settings']['triggers']['user']) {
      $data['birthdays_triggers'] = !empty($account->birthdays_user_send_mail);
    }
    unset($account->birthdays_user_send_mail);
    if ($instance['settings']['hide_year'] == BIRTHDAYS_HIDE_YEAR_USER) {
      if ($account->birthdays_user_hide_year) {
        $data['birthdays_year'] = 0;
      }
    }
    unset($account->birthdays_user_hide_year);
    user_save($account);
    db_insert('field_data_birthdays')
      ->fields($data)
      ->execute();
    $i++;
    $sandbox['progress']++;
    $sandbox['last_uid_processed'] = $row->uid;
  }
  if (empty($sandbox['max']) || $i < 10) {
    db_drop_table('dob');
    return t('Birthdays field data has been migrated.');
  }
  else {
    $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  }
}