You are here

function simplenews_update_7000 in Simplenews 7

Same name and namespace in other branches
  1. 7.2 simplenews.install \simplenews_update_7000()

Create table {simplenews_category} to replace taxonomy terms. Migrate Newsletter taxonomy data to Newsletter categories.

Rename table simplenews_subscriptions to simplenews_subscriber. Rename table simplenews_newsletters to simplenews_newsletter. Drop fields {simplenews_newsletter}.s_format, .priority and .receipt.

Rename table simplenews_snid_tid to simplenews_subscription.

Delete deprecated simplenews variables.

File

./simplenews.install, line 516
Install, update and uninstall functions for the simplenews module

Code

function simplenews_update_7000() {

  // Convert tokens in variables to D7 format.
  $variables = array(
    'simplenews_confirm_subscribe_subject',
    'simplenews_confirm_subscribe_unsubscribed',
    'simplenews_confirm_subscribe_subscribed',
    'simplenews_confirm_unsubscribe_subscribed',
    'simplenews_confirm_unsubscribe_unsubscribed',
  );
  _simplenews_convert_tokens_in_variable($variables);

  // Create table 'simplenews_category'.
  $schema['simplenews_category'] = array(
    'description' => 'Simplenews newsletter categories.',
    'fields' => array(
      'tid' => array(
        'description' => '{taxonomy_term_data}.tid used as newsletter category.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'format' => array(
        'description' => 'Format of the newsletter email (plain, html).',
        'type' => 'varchar',
        'length' => 8,
        'not null' => TRUE,
        'default' => '',
      ),
      'priority' => array(
        'description' => 'Email priority according to RFC 2156 and RFC 5231 (0 = none; 1 = highest; 2 = high; 3 = normal; 4 = low; 5 = lowest).',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'receipt' => array(
        'description' => 'Boolean indicating request for email receipt confirmation according to RFC 2822.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'from_name' => array(
        'description' => 'Sender name for newsletter emails.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'email_subject' => array(
        'description' => 'Subject of newsletter email. May contain tokens.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'from_address' => array(
        'description' => 'Sender address for newsletter emails',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
      'hyperlinks' => array(
        'description' => 'Flag indicating type of hyperlink conversion (1 = hyperlinks are in-line; 0 = hyperlinks are placed at email bottom).',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'new_account' => array(
        'description' => 'How to treat subscription at account creation (none = None; on = Default on; off = Default off; silent = Invisible subscription).',
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
      'opt_inout' => array(
        'description' => 'How to treat subscription confirmation (hidden = Newsletter is hidden from the user; single = Single opt-in; double = Double opt-in).',
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
      'block' => array(
        'description' => 'For this category a subscription block is available.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'tid',
    ),
  );
  db_create_table('simplenews_category', $schema['simplenews_category']);

  // Migrate Newsletter taxonomy data to Newsletter categories.
  // Query the database directly, to avoid triggering our own hooks.
  $tids = db_query('SELECT tid FROM {taxonomy_term_data} where vid = :vid', array(
    ':vid' => variable_get('simplenews_vid', ''),
  ))
    ->fetchCol();

  // @todo Check if simplenews blocks are still active after core update.
  //      If not, there is no purpose in migrating the block status ('block' => 0).
  $blocks = _simplenews_get_blocks();
  foreach ($tids as $tid) {
    _simplenews_convert_tokens_in_variable('simplenews_email_subject_' . $tid);

    // check string lengths: variables can be an arbitrary length but the new table has limits
    $from_name = variable_get('simplenews_from_name_' . $tid, variable_get('simplenews_from_name', variable_get('site_name', 'Drupal')));
    $email_subject = variable_get('simplenews_email_subject_' . $tid, '[[simplenews-newsletters-name]] [title-raw]');
    $from_address = variable_get('simplenews_from_address_' . $tid, variable_get('simplenews_from_address', variable_get('site_mail', ini_get('sendmail_from'))));
    if (strlen($from_name) > 128) {
      drupal_set_message(t('The from_name field for simplenews list @tid (@value) was too long and has been truncated to 128 characters.', array(
        '@tid' => $tid,
        '@value' => $from_name,
      )), 'warning');
      $from_name = substr($from_name, 0, 128);
    }
    if (strlen($email_subject) > 255) {
      drupal_set_message(t('The email_subject field for simplenews list @tid (@value) was too long and has been truncated to 255 characters.', array(
        '@tid' => $tid,
        '@value' => $email_subject,
      )), 'warning');
      $from_name = substr($email_subject, 0, 255);
    }
    if (strlen($from_address) > 64) {
      drupal_set_message(t('The from_address field for simplenews list @tid (@value) was too long and has been truncated to 64 characters.', array(
        '@tid' => $tid,
        '@value' => $from_address,
      )), 'warning');
      $from_name = substr($from_address, 0, 64);
    }
    db_insert('simplenews_category')
      ->fields(array(
      'tid' => $tid,
      'format' => 'plain',
      'priority' => '0',
      'receipt' => '0',
      'from_name' => $from_name,
      'email_subject' => $email_subject,
      'from_address' => $from_address,
      'hyperlinks' => variable_get('simplenews_hyperlinks_' . $tid, 1),
      'new_account' => variable_get('simplenews_new_account_' . $tid, 'none'),
      'opt_inout' => variable_get('simplenews_opt_inout_' . $tid, 'double'),
      'block' => isset($blocks[$tid]) ? 1 : 0,
    ))
      ->execute();
  }

  // Change table simplenews_subscriptions to simplenews_subscriber.
  db_rename_table('simplenews_subscriptions', 'simplenews_subscriber');

  // Change table simplenews_newsletters to simplenews_newsletter.
  // Drop fields: s_format, priority, receipt (moved to simplenews_category).
  db_rename_table('simplenews_newsletters', 'simplenews_newsletter');
  db_change_field('simplenews_newsletter', 'tid', 'tid', array(
    'description' => 'The newsletter category {simplenews_category}.tid this newsletter belongs to.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));
  db_drop_field('simplenews_newsletter', 's_format');
  db_drop_field('simplenews_newsletter', 'priority');
  db_drop_field('simplenews_newsletter', 'receipt');

  // Change table simplenews_snid_tid to simplenews_subscription.
  // Change field {simplenews_subscription}.tid description
  db_drop_primary_key('simplenews_snid_tid');
  db_rename_table('simplenews_snid_tid', 'simplenews_subscription');

  // Add {simplenews_mail_spool}.data to store subscriber data.
  db_add_field('simplenews_mail_spool', 'data', array(
    'type' => 'text',
    'not null' => FALSE,
    'size' => 'big',
    'serialize' => TRUE,
    'description' => 'A serialized array of name value pairs that are related to the email address.',
  ));

  // Rename field {simplenews_mail_spool}.s_status to "status".
  db_change_field('simplenews_newsletter', 's_status', 'status', array(
    'description' => 'sent status of the newsletter issue (0 = not sent; 1 = pending; 2 = sent). ',
    'type' => 'int',
    'size' => 'tiny',
    'not null' => TRUE,
    'default' => 0,
  ));

  // Delete deprecated variables.
  foreach ($tids as $tid) {
    variable_del('simplenews_from_name_' . $tid);
    variable_del('simplenews_email_subject_' . $tid);
    variable_del('simplenews_from_address_' . $tid);
    variable_del('simplenews_hyperlinks_' . $tid);
    variable_del('simplenews_new_account_' . $tid);
    variable_del('simplenews_opt_inout_' . $tid);
  }

  // @todo Add return text about checking of Newsletter Category settings.
  // @todo Add return text about Block checkboxes
  // Convert old content type settings.
  module_load_include('module', 'node');
  module_load_include('module', 'simplenews');

  // Update the machine name of the simplenews vocabulary.
  if ($vid = variable_get('simplenews_vid', '')) {
    db_update('taxonomy_vocabulary')
      ->fields(array(
      'machine_name' => 'newsletter',
    ))
      ->condition('vid', $vid)
      ->execute();
    $field_name = 'taxonomy_vocabulary_' . $vid;
    variable_set('simplenews_category_field', $field_name);
    $field = field_info_field($field_name);
    $field['settings']['allowed_values'][0]['vocabulary'] = 'newsletter';
    field_update_field($field);
  }
  variable_del('simplenews_vid');
  $content_types = variable_get('simplenews_content_types');
  if (!empty($content_types)) {
    foreach ($content_types as $simplenews_content_type) {
      variable_set('simplenews_content_type_' . $simplenews_content_type, TRUE);
    }
  }
  variable_del('simplenews_content_types');
}