You are here

simplenews.install in Simplenews 7.2

Install, update and uninstall functions for the simplenews module

File

simplenews.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the simplenews module
 */

/**
 * Implements hook_schema().
 */
function simplenews_schema() {
  $schema['simplenews_newsletter'] = array(
    'description' => 'Simplenews newsletter categories.',
    'fields' => array(
      'newsletter_id' => array(
        'description' => '...',
        'type' => 'serial',
        'not null' => TRUE,
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The newsletter name.',
        'translatable' => TRUE,
      ),
      'description' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
        'description' => 'A description of the newsletter.',
        'translatable' => TRUE,
      ),
      'format' => array(
        'type' => 'varchar',
        'length' => 8,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Format of the newsletter email (plain, html).',
      ),
      'priority' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Email priority according to RFC 2156 and RFC 5231 (0 = none; 1 = highest; 2 = high; 3 = normal; 4 = low; 5 = lowest).',
      ),
      'receipt' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Boolean indicating request for email receipt confirmation according to RFC 2822.',
      ),
      'from_name' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Sender name for newsletter emails.',
      ),
      'email_subject' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Subject of newsletter email. May contain tokens.',
      ),
      'from_address' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Sender address for newsletter emails',
      ),
      'hyperlinks' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Flag indicating type of hyperlink conversion (1 = hyperlinks are in-line; 0 = hyperlinks are placed at email bottom).',
      ),
      'new_account' => array(
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
        'description' => 'How to treat subscription at account creation (none = None; on = Default on; off = Default off; silent = Invisible subscription).',
      ),
      'opt_inout' => array(
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
        'description' => 'How to treat subscription confirmation (hidden = Newsletter is hidden from the user; single = Single opt-in; double = Double opt-in).',
      ),
      'block' => array(
        'description' => 'For this category a subscription block is available.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The weight of this newsletter in relation to other newsletters.',
      ),
    ),
    'primary key' => array(
      'newsletter_id',
    ),
  );
  $schema['simplenews_subscriber'] = array(
    'description' => 'Newsletter subscribers. Many-to-many relation via {simplenews_subscription}',
    'fields' => array(
      'snid' => array(
        'description' => 'Primary key: Unique subscriber ID.',
        'type' => 'serial',
        'not null' => TRUE,
      ),
      'activated' => array(
        'description' => 'Boolean indicating the status of the subscription.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'mail' => array(
        'description' => "The subscriber's email address.",
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
      'uid' => array(
        'description' => 'The {users}.uid that has the same email address.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'language' => array(
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Subscriber preferred language.',
      ),
      'changes' => array(
        'description' => 'Contains the requested subscription changes',
        'type' => 'text',
        'serialize' => TRUE,
      ),
      'created' => array(
        'description' => 'UNIX timestamp of when the subscription record was added.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'snid',
    ),
    'indexes' => array(
      'mail' => array(
        'mail',
      ),
      'uid' => array(
        'uid',
      ),
    ),
    'foreign keys' => array(
      'uid' => array(
        'table' => 'users',
        'columns' => array(
          'uid' => 'uid',
        ),
      ),
    ),
  );
  $schema['simplenews_subscription'] = array(
    'description' => 'Newsletter subscription data. Which subscriber is subscribed to which mailing list.',
    'fields' => array(
      'snid' => array(
        'description' => 'The {simplenews_subscriber}.snid who is subscribed.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'newsletter_id' => array(
        'description' => 'The newsletter ({simplenews_newsletter}.newsletter_id) the subscriber is subscribed to.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'status' => array(
        'description' => 'A flag indicating whether the user is subscribed (1) or unsubscribed (0).',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 1,
      ),
      'timestamp' => array(
        'description' => 'UNIX timestamp of when the user is (un)subscribed.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'source' => array(
        'description' => 'The source via which the user is (un)subscription.',
        'type' => 'varchar',
        'length' => 24,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'snid',
      'newsletter_id',
    ),
    'foreign keys' => array(
      'snid' => array(
        'table' => 'simplenews_subscriber',
        'columns' => array(
          'snid' => 'snid',
        ),
      ),
      'newsletter' => array(
        'table' => 'simplenews_newsletter',
        'columns' => array(
          'newsletter_id' => 'newsletter_id',
        ),
      ),
    ),
  );
  $schema['simplenews_mail_spool'] = array(
    'description' => 'Spool for temporary storage of newsletter emails.',
    'fields' => array(
      'msid' => array(
        'description' => 'The primary identifier for a mail spool record.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'mail' => array(
        'description' => 'The formatted email address of mail message recipient.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'entity_type' => array(
        'description' => 'The entity type of this newsletter issue.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'entity_id' => array(
        'description' => 'The entity id of this newsletter issue.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'newsletter_id' => array(
        'description' => 'The {simplenews_newsletter}.newsletter_id this issue belongs to.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'status' => array(
        'description' => 'The sent status of the email (0 = hold, 1 = pending, 2 = done).',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'error' => array(
        'description' => 'A boolean indicating whether an error occured while sending the email.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'timestamp' => array(
        'description' => 'The time status was set or changed.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      '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.',
      ),
      'snid' => array(
        'description' => 'Foreign key for subscriber table ({simplenews_subscriptions}.snid)',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'msid',
    ),
    'indexes' => array(
      'newsletter_id' => array(
        'newsletter_id',
      ),
      'status' => array(
        'status',
      ),
      'snid_newsletter_id' => array(
        'snid',
        'newsletter_id',
      ),
    ),
    'foreign keys' => array(
      'newsletter_id' => array(
        'table' => 'simplenews_newsletter',
        'columns' => array(
          'newsletter_id',
        ),
      ),
      'snid_newsletter_id' => array(
        'table' => 'simplenews_subscription',
        'columns' => array(
          'snid' => 'snid',
          'newsletter_id' => 'newsletter_id',
        ),
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function simplenews_install() {
  _simplenews_init_default_newsletter();

  // add nodetype with newsletter vocabulary
  _simplenews_install_nodetype();
}

/**
 * Implements hook_uninstall().
 */
function simplenews_uninstall() {
  db_query("DELETE FROM {variable} WHERE name LIKE 'simplenews_%%'");
}

/**
 * Create simplenews node type.
 */
function _simplenews_install_nodetype() {

  // Create a newsletter type if needed.
  $type = node_type_get_type('simplenews');
  if (!$type) {
    $type = node_type_set_defaults(array(
      'type' => 'simplenews',
      'name' => t('Newsletter issue'),
      'base' => 'node_content',
      'description' => t('A newsletter issue to be sent to subscribed email addresses.'),
      'locked' => 0,
      'custom' => 1,
      'modified' => 1,
    ));
    node_type_save($type);
    node_add_body_field($type);
  }
  simplenews_issue_fields_add($type);
  variable_set('simplenews_content_type_' . $type->type, TRUE);
}
function _simplenews_init_default_newsletter() {
  $newsletter = simplenews_newsletter_get_all();
  if (is_array($newsletter)) {
    $newsletter = reset($newsletter);
  }
  if (!$newsletter) {
    $newsletter = entity_create('simplenews_newsletter', array(
      'name' => t('@site_name newsletter', array(
        '@site_name' => variable_get('site_name', 'Drupal'),
      )),
    ));
    $newsletter
      ->save();
  }
}

/**
 * Helper function to get all activated simplenews blocks
 *
 * @return Keyed array of simplenews blocks.
 */
function _simplenews_get_blocks() {
  $query = db_select('block', 'b');
  $result = $query
    ->fields('b', array(
    'delta',
  ))
    ->condition('b.status', 1)
    ->condition('b.module', 'simplenews')
    ->execute();
  return $result
    ->fetchAllAssoc('delta');
}

/**
 * Implements hook_update_last_removed().
 */
function simplenews_update_last_removed() {

  // Support upgrades from 6.x-1.x and 6.x-2.x.
  return 6101;
}

/**
 * Implements hook_update_dependencies().
 */
function simplenews_update_dependencies() {

  // Make sure that the taxonomy upgrade is run first.
  $dependencies['simplenews'][7000] = array(
    'taxonomy' => 7010,
  );
  return $dependencies;
}

/**
 * Helper function to convert tokens in variables to D7 format.
 */
function _simplenews_convert_tokens_in_variable($variables) {
  if (!is_array($variables)) {
    $variables = array(
      $variables,
    );
  }
  $old = array(
    '[site-name]',
    '[user-mail]',
    '[site-url]/user',
    '[site-url]',
    '[simplenews-subscribe-url]',
    '[simplenews-unsubscribe-url]',
    '[simplenews-newsletter-url]',
    '[simplenews-newsletters-name]',
    '[simplenews-newsletters-url]',
    '[simplenews-receiver-mail]',
  );
  $new = array(
    '[site:name]',
    '[user:mail]',
    '[site:login-url]',
    '[site:url]',
    '[simplenews-subscriber:subscribe-url]',
    '[simplenews-subscriber:unsubscribe-url]',
    '[simplenews-newsletter:url]',
    '[simplenews-list:name]',
    '[simplenews-list:url]',
    '[simplenews-subscriber:mail]',
  );
  foreach ($variables as $variable) {
    if ($text = variable_get($variable, FALSE)) {
      $text = str_replace($old, $new, $text);
      variable_set($variable, $text);
    }
  }
}

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

/**
 * Create key snid_tid on simplenews_mail_spool table.
 */
function simplenews_update_7001() {

  // Add the {simplenews_mail_spool}.snid field if it doesn't exist yet (added
  // in 6.x-2.x).
  if (!db_field_exists('simplenews_mail_spool', 'snid')) {
    db_add_field('simplenews_mail_spool', 'snid', array(
      'description' => 'Foreign key for subscriber table ({simplenews_subscriptions}.snid)',
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ));
  }
  if (!db_index_exists('simplenews_mail_spool', 'snid_tid')) {
    db_add_index('simplenews_mail_spool', 'snid_tid', array(
      'snid',
      'tid',
    ));
  }
}

/**
 * Drop support for node revisioning.
 */
function simplenews_update_7002() {
  if (db_field_exists('simplenews_newsletter', 'vid')) {
    db_drop_field('simplenews_newsletter', 'vid');
  }
  if (db_field_exists('simplenews_mail_spool', 'vid')) {
    db_drop_field('simplenews_mail_spool', 'vid');
  }
}

/**
 * [simplenews-newsletter] tokens have been removed in favor of [node] tokens.
 */
function simplenews_update_7003() {
  drupal_set_message(t('The [simplenews-newsletter] tokens have been removed in favor of [node] tokens. Existing newsletters might need to be updated accordingly.'), 'warning');
}

/**
 * Add the status field to {simplenews_subscription}.
 */
function simplenews_update_7004() {
  if (!db_field_exists('simplenews_subscription', 'status')) {
    db_add_field('simplenews_subscription', 'status', array(
      'description' => 'A flag indicating whether the user is subscribed (1) or unsubscribed (0).',
      'type' => 'int',
      'size' => 'small',
      'not null' => TRUE,
      'default' => 1,
    ));
  }
}

/**
 * Add support for combined confirmation mails.
 */
function simplenews_update_7005() {
  db_add_field('simplenews_subscriber', 'changes', array(
    'description' => 'Contains the requested subscription changes',
    'type' => 'text',
    'serialize' => TRUE,
  ));

  // To keep existing installations consistent, disable combined confirmation
  // mails.
  variable_set('simplenews_use_combined', 'never');
}

/**
 * Add support for subscriber create date
 */
function simplenews_update_7006() {
  db_add_field('simplenews_subscriber', 'created', array(
    'description' => 'UNIX timestamp of when the subscription record was added.',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
}

/**
 * Allow longer from_name field values.
 */
function simplenews_update_7007() {
  db_change_field('simplenews_category', 'from_name', 'from_name', array(
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
    'default' => '',
    'description' => 'Sender name for newsletter emails.',
  ));
}

/**
 * Add the timestamp and source columns to {simplenews_subscription} if missing.
 */
function simplenews_update_7008() {
  if (!db_field_exists('simplenews_subscription', 'timestamp')) {
    db_add_field('simplenews_subscription', 'timestamp', array(
      'description' => 'UNIX timestamp of when the user is (un)subscribed.',
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    ));
  }
  if (!db_field_exists('simplenews_subscription', 'source')) {
    db_add_field('simplenews_subscription', 'source', array(
      'description' => 'The source via which the user is (un)subscription.',
      'type' => 'varchar',
      'length' => 24,
      'not null' => TRUE,
      'default' => '',
    ));
  }
}

/**
 * Add the sent_subsriber_countcolumn to {simplenews_newsletter} if missing.
 */
function simplenews_update_7009() {
  if (!db_field_exists('simplenews_newsletter', 'sent_subscriber_count')) {
    db_add_field('simplenews_newsletter', 'sent_subscriber_count', array(
      'description' => 'The count of subscribers to the newsletter when it was sent.',
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ));
  }
}

/**
 * Update empty sent subscriber count column to current subscriber count.
 */
function simplenews_update_7010() {

  // Assume that already sent newsletters that have a sent subscriber count of
  // 0 have been sent to all subscribers. Do a update query per newsletter
  // category, to avoid having to re-execute the subquery on every row.
  $tids = db_query('SELECT tid FROM {simplenews_category}')
    ->fetchCol();
  foreach ($tids as $tid) {
    $count = db_query('SELECT COUNT(*) FROM {simplenews_subscription} ss WHERE status = 1 AND tid = :tid', array(
      ':tid' => $tid,
    ))
      ->fetchField();
    db_update('simplenews_newsletter')
      ->fields(array(
      'sent_subscriber_count' => $count,
    ))
      ->condition('status', 2)
      ->condition('sent_subscriber_count', 0)
      ->condition('tid', $tid)
      ->execute();
  }
}

/**
 * Replace {simplenews_newsletter}.
 */
function simplenews_update_7200() {

  // Rename {simplenews_newsletter}.
  db_rename_table('simplenews_newsletter', 'simplenews_newsletter_old');

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

  // Make sure that the required dependencies are enabled.
  module_enable(array(
    'simplenews',
    'entityreference',
    'ctools',
    'entity',
  ));
  _field_info_collate_types(TRUE);
  _registry_check_code(REGISTRY_RESET_LOOKUP_CACHE);
  drupal_flush_all_caches();

  // Add newsletter reference, status and sent count fields to newsletter nodes.
  $node_types = array();
  foreach (node_type_get_types() as $type) {
    if (variable_get('simplenews_content_type_' . $type->type, FALSE)) {
      simplenews_issue_newsletter_field_add($type);
      simplenews_issue_status_field_add($type);
      simplenews_issue_sent_count_field_add($type);
      $node_types[] = $type->type;
    }
  }

  // Fill new {simplenews_newsletter} table with existing data.
  $query = db_select('simplenews_category', 'c');
  $query
    ->innerJoin('taxonomy_term_data', 't', 't.tid = c.tid');
  $query
    ->addField('t', 'tid');
  $query
    ->addField('t', 'name');
  $query
    ->addField('t', 'description');
  $query
    ->addField('c', 'format');
  $query
    ->addField('c', 'priority');
  $query
    ->addField('c', 'receipt');
  $query
    ->addField('c', 'from_name');
  $query
    ->addField('c', 'email_subject');
  $query
    ->addField('c', 'from_address');
  $query
    ->addField('c', 'hyperlinks');
  $query
    ->addField('c', 'new_account');
  $query
    ->addField('c', 'opt_inout');
  $query
    ->addField('c', 'block');
  $query
    ->addField('t', 'weight');
  db_insert('simplenews_newsletter')
    ->fields(array(
    'newsletter_id',
    'name',
    'description',
    'format',
    'priority',
    'receipt',
    'from_name',
    'email_subject',
    'from_address',
    'hyperlinks',
    'new_account',
    'opt_inout',
    'block',
    'weight',
  ))
    ->from($query)
    ->execute();

  // Make sure that all instances have a default value if there is a newsletter.
  $newsletter = simplenews_newsletter_get_all();
  if ($newsletter) {
    $newsletter = reset($newsletter);
    $default_value = array(
      array(
        'target_id' => $newsletter->newsletter_id,
      ),
    );
    $field_name = variable_get('simplenews_newsletter_field', 'simplenews_newsletter');
    foreach ($node_types as $type) {
      $instance = field_info_instance('node', $field_name, $type);
      $instance['default_value'] = $default_value;
      field_update_instance($instance);
    }
  }
}

/**
 * Convert existing simplenews_newsletter table to fields.
 */
function simplenews_update_7201() {

  // Prepare a default query that can be reused for all three fields.
  $default_query = db_select('simplenews_newsletter_old', 's');
  $default_query
    ->innerJoin('node', 'n', 'n.nid = s.nid');
  $default_query
    ->addField('n', 'nid');
  $default_query
    ->addField('n', 'vid');
  $default_query
    ->addField('n', 'type');
  $default_query
    ->addExpression(':entity_type', 'entity_type', array(
    ':entity_type' => 'node',
  ));
  $default_query
    ->addExpression(':language', 'language', array(
    ':language' => LANGUAGE_NONE,
  ));
  $default_query
    ->addExpression(':delta', 'delta', array(
    ':delta' => 0,
  ));

  // First newsletter id/tid.
  $query = clone $default_query;
  $query
    ->addField('s', 'tid');
  $field_name = variable_get('simplenews_newsletter_field', 'simplenews_newsletter');
  db_insert('field_data_' . $field_name)
    ->fields(array(
    'entity_id',
    'revision_id',
    'bundle',
    $field_name . '_target_id',
    'entity_type',
    'language',
    'delta',
  ))
    ->from($query)
    ->execute();
  db_insert('field_revision_' . $field_name)
    ->fields(array(
    'entity_id',
    'revision_id',
    'bundle',
    $field_name . '_target_id',
    'entity_type',
    'language',
    'delta',
  ))
    ->from($query)
    ->execute();

  // Then the newsletter status.
  $query = clone $default_query;
  $query
    ->addField('s', 'status');
  $field_name = variable_get('simplenews_issue_status_field', 'simplenews_issue_status');
  db_insert('field_data_' . $field_name)
    ->fields(array(
    'entity_id',
    'revision_id',
    'bundle',
    $field_name . '_value',
    'entity_type',
    'language',
    'delta',
  ))
    ->from($query)
    ->execute();
  db_insert('field_revision_' . $field_name)
    ->fields(array(
    'entity_id',
    'revision_id',
    'bundle',
    $field_name . '_value',
    'entity_type',
    'language',
    'delta',
  ))
    ->from($query)
    ->execute();

  // And finaly the sent count.
  $query = clone $default_query;
  $query
    ->addField('s', 'sent_subscriber_count');
  $field_name = variable_get('simplenews_sent_count_field', 'simplenews_sent_count');
  db_insert('field_data_' . $field_name)
    ->fields(array(
    'entity_id',
    'revision_id',
    'bundle',
    $field_name . '_value',
    'entity_type',
    'language',
    'delta',
  ))
    ->from($query)
    ->execute();
  db_insert('field_revision_' . $field_name)
    ->fields(array(
    'entity_id',
    'revision_id',
    'bundle',
    $field_name . '_value',
    'entity_type',
    'language',
    'delta',
  ))
    ->from($query)
    ->execute();
}

/**
 * Rename columns, drop old database tables.
 */
function simplenews_update_7202() {
  db_change_field('simplenews_subscription', 'tid', 'newsletter_id', array(
    'description' => 'The newsletter ({simplenews_newsletter}.newsletter_id) the subscriber is subscribed to.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));
  db_change_field('simplenews_mail_spool', 'tid', 'newsletter_id', array(
    'description' => 'The {simplenews_newsletter}.newsletter_id this issue belongs to.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));
  db_drop_table('simplenews_newsletter_old');
  db_drop_table('simplenews_category');
  $field_name = variable_get('simplenews_category_field', 'field_simplenews_term');
  field_delete_field($field_name);
  variable_del('simplenews_category_field');
  taxonomy_vocabulary_delete(taxonomy_vocabulary_machine_name_load('newsletter')->vid);
}

/**
 * Update simplenews node type name.
 */
function simplenews_update_7203() {
  $type = node_type_get_type('simplenews');
  $type->name = t('Newsletter issue');
  node_type_save($type);
}

/**
 * Remove the timestamp column on the {simplenews_subscriber}.
 */
function simplenews_update_7204() {
  if (db_field_exists('simplenews_subscriber', 'timestamp')) {
    db_drop_field('simplenews_subscriber', 'timestamp');
  }
}

/**
 * Replace nid with entity_type and entity_id columns in
 * {simplenews_mail_spool}.
 * Set the entity_type column on existing rows to node.
 */
function simplenews_update_7205() {
  db_change_field('simplenews_mail_spool', 'nid', 'entity_id', array(
    'description' => 'The entity id of this newsletter issue.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));
  db_add_field('simplenews_mail_spool', 'entity_type', array(
    'description' => 'The entity type of this newsletter issue.',
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));

  // Set every entity type to 'node' at this point.
  db_update('simplenews_mail_spool')
    ->fields(array(
    'entity_type' => 'node',
  ))
    ->execute();
}

/**
 * Add recipient handler and handler settings field to issue content types.
 */
function simplenews_update_7206() {
  $node_types = array();
  foreach (node_type_get_types() as $type) {
    if (variable_get('simplenews_content_type_' . $type->type, FALSE)) {
      simplenews_issue_handler_field_add($type);
      simplenews_issue_handler_settings_field_add($type);
    }
  }
}

Functions

Namesort descending Description
simplenews_install Implements hook_install().
simplenews_schema Implements hook_schema().
simplenews_uninstall Implements hook_uninstall().
simplenews_update_7000 Create table {simplenews_category} to replace taxonomy terms. Migrate Newsletter taxonomy data to Newsletter categories.
simplenews_update_7001 Create key snid_tid on simplenews_mail_spool table.
simplenews_update_7002 Drop support for node revisioning.
simplenews_update_7003 [simplenews-newsletter] tokens have been removed in favor of [node] tokens.
simplenews_update_7004 Add the status field to {simplenews_subscription}.
simplenews_update_7005 Add support for combined confirmation mails.
simplenews_update_7006 Add support for subscriber create date
simplenews_update_7007 Allow longer from_name field values.
simplenews_update_7008 Add the timestamp and source columns to {simplenews_subscription} if missing.
simplenews_update_7009 Add the sent_subsriber_countcolumn to {simplenews_newsletter} if missing.
simplenews_update_7010 Update empty sent subscriber count column to current subscriber count.
simplenews_update_7200 Replace {simplenews_newsletter}.
simplenews_update_7201 Convert existing simplenews_newsletter table to fields.
simplenews_update_7202 Rename columns, drop old database tables.
simplenews_update_7203 Update simplenews node type name.
simplenews_update_7204 Remove the timestamp column on the {simplenews_subscriber}.
simplenews_update_7205 Replace nid with entity_type and entity_id columns in {simplenews_mail_spool}. Set the entity_type column on existing rows to node.
simplenews_update_7206 Add recipient handler and handler settings field to issue content types.
simplenews_update_dependencies Implements hook_update_dependencies().
simplenews_update_last_removed Implements hook_update_last_removed().
_simplenews_convert_tokens_in_variable Helper function to convert tokens in variables to D7 format.
_simplenews_get_blocks Helper function to get all activated simplenews blocks
_simplenews_init_default_newsletter
_simplenews_install_nodetype Create simplenews node type.