You are here

function birthdays_update_7000 in Birthdays 7

Upgrade the data structure to Drupal 7, creating actions, triggers and fields.

File

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

Code

function birthdays_update_7000() {

  // Delete the D7 default action.
  if ($default_action = actions_load(variable_get('birthdays_defaults_action', 0))) {
    actions_delete($default_action->aid);
  }

  // Create a new email action, based on the old settings.
  module_load_include('install', 'birthdays', 'defaults/birthdays_defaults');
  $params = array();
  if ($user_message = variable_get('birthdays_send_user_message')) {
    $params['message'] = _birthdays_upgrade_tokens($user_message);
  }
  if ($user_subject = variable_get('birthdays_send_user_subject')) {
    $params['subject'] = _birthdays_upgrade_tokens($user_subject);
  }
  $aid = actions_save('system_send_email_action', 'system', $params + _birthdays_defaults_action_parameters(), t('Happy birthday mail'));
  variable_set('birthdays_defaults_action', $aid);
  variable_del('birthdays_send_user_message');
  variable_del('birthdays_send_user_subject');

  // Create a birthdays field on the user entity type.
  if (!field_info_field('birthdays')) {
    $field = array(
      'field_name' => 'birthdays',
      'type' => 'birthdays_date',
      'module' => 'birthdays',
      'cardinality' => 1,
      'translatable' => FALSE,
      'entity_types' => array(
        'user',
      ),
    );
    _update_7000_field_create_field($field);
  }

  // The field instance settings with admin_mail and hide_year setting.
  $instance = array(
    'field_name' => 'birthdays',
    'entity_type' => 'user',
    'bundle' => 'user',
    'label' => t('Birthday'),
    'settings' => array(
      'admin_mail' => variable_get('birthdays_remind', BIRTHDAYS_ADMIN_MAIL_DISABLED),
      'hide_year' => variable_get('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_NO),
    ),
  );
  variable_del('birthdays_remind');
  variable_del('birthdays_hide_year');

  // Upgrade user mail settings.
  // 0 was BIRTHDAYS_USER_MAIL_NO.
  // 1 was BIRTHDAYS_USER_MAIL_YES
  // 2 was BIRTHDAYS_USER_MAIL_USER.
  if (variable_get('birthdays_send_user', 0) == 2) {

    // Use the same texts that were in the user form.
    $instance['settings']['triggers'] = array(
      'user' => TRUE,
      'title' => t('Do not send birthday mail'),
      'description' => t('Do not send me an e-mail or e-card when it\'s my birthday.'),
    );
  }
  if (variable_get('birthdays_send_user', 0) != 0) {
    if (db_table_exists('trigger_assignments')) {
      db_insert('trigger_assignments')
        ->fields(array(
        'hook' => _birthdays_instance_hook($instance),
        'aid' => variable_get('birthdays_defaults_action'),
        'weight' => 0,
      ))
        ->execute();
    }
  }
  variable_del('birthdays_send_user');

  // Save the field instance settings.
  if (field_info_instance('user', 'birthdays', 'user')) {
    field_update_instance($instance);
  }
  else {
    field_create_instance($instance);
  }

  // The data should be synchronized for the next step, so we don't need the
  // field id.
  variable_del('birthdays_field_id');

  // Block, page and viewmode aren't upgraded automatically. Delete the
  // remaining variables and leave a message.
  variable_del('birthdays_show_starsign');
  variable_del('birthdays_page_settings');
  variable_del('birthdays_page_list_number');
  variable_del('birthdays_page_show_filters');
  variable_del('birthdays_block_number_by_days');
  variable_del('birthdays_block_number_by_birthdays');
  return t('Birthday block and page are now a view provided by the Birthdays defaults module and birthdays are in their own field type. Check you view and view mode settings, to make sure they are correct, because they are not upgraded automatically.');
}