<?php

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

/**
 * Implements hook_schema().
 */
function simplenews_schema() {
  $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(
        '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,
      ),
    ),
    'primary key' => array('tid'),
  );

  $schema['simplenews_newsletter'] = array(
    'description' => 'Simplenews newsletter data.',
    'fields' => array(
      'nid' => array(
        'description' => '{node} that is used as newsletter.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'tid' => array(
        'description' => 'The newsletter category {simplenews_category}.tid this newsletter belongs to.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'status' => array(
        'description' => 'sent status of the newsletter issue (0 = not sent; 1 = pending; 2 = sent, 3 = send on publish).',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'sent_subscriber_count' => array(
        'description' => 'The count of subscribers to the newsletter when it was sent.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('nid'),
    'foreign keys' => array(
      'nid' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
    ),
  );

  $schema['simplenews_subscriber'] = array(
    'description' => 'Subscribers to {simplenews_category}. 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.',
      ),
      'timestamp' => array(
        'description' => 'UNIX timestamp of when the user is (un)subscribed.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      '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,
      ),
      'tid' => array(
        'description' => 'The category ({simplenews_category}.tid) 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', 'tid'),
    'foreign keys' => array(
      'snid' => array(
        'table' => 'simplenews_subscriber',
        'columns' => array('snid' => 'snid'),
      ),
      'tid' => array(
        'table' => 'simplenews_category',
        'columns' => array('tid' => 'tid'),
      )
    ),
    // @todo add foreign keys to other tables too?
  );

  $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' => '',
      ),
      'nid' => array(
        'description' => 'The {node}.nid of this newsletter.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'tid' => array(
        'description' => 'The {simplenews_category}.tid this newsletter 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(
      'tid' => array('tid'),
      'status' => array('status'),
      'snid_tid' => array('snid', 'tid'),
    ),
    'foreign keys' => array(
      'nid' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
      'tid' => array(
        'table' => 'simplenews_category',
        'columns' => array('tid'),
      ),
      'snid_tid' => array(
        'table' => 'simplenews_subscription',
        'columns' => array(
          'snid' => 'snid',
          'tid' => 'tid',
        ),
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function simplenews_install() {
  _simplenews_init_simplenews_vocabulary();
  _simplenews_init_simplenews_category();

  // 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('Simplenews newsletter'),
      '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);
  }
  if (!field_info_instance('node', 'field_simplenews_term', $type->type)) {
    simplenews_add_term_field($type);
  }
  variable_set('simplenews_content_type_' . $type->type, TRUE);
}

/**
 * Create simplenews vocabulary and initial term.
 */
function _simplenews_init_simplenews_vocabulary() {
  // Create the simplenews vocabulary. If it exists, set simplenews_vid variable.
  $vocabularies = taxonomy_vocabulary_load_multiple(array(), array('machine_name' => 'newsletter'));
  $vocabulary = reset($vocabularies);
  if (!$vocabulary) {
    $vocabulary = new stdClass();
    $vocabulary->name         = t('Newsletter');
    $vocabulary->machine_name = 'newsletter';
    $vocabulary->description  = t('Simplenews newsletter categories.');
    $vocabulary->weight       = '0';
    $vocabulary->hierarchy    = '0';
    $vocabulary->module       = 'simplenews';
  }
  taxonomy_vocabulary_save($vocabulary);
  variable_set('simplenews_vid', $vocabulary->vid);

  // Create a newsletter taxonomy term if none exists.
  $tree = taxonomy_get_tree($vocabulary->vid);
  if (count($tree) == 0) {
    $term = new stdClass();
    $term->name = t('@site_name newsletter', array('@site_name' => variable_get('site_name', 'Drupal')));
    $term->description = t('@site_name newsletter categories.', array('@site_name' => variable_get('site_name', 'Drupal')));
    $term->vid = $vocabulary->vid;
    taxonomy_term_save($term);
  }
}

/**
 * Create initial simplenews categories.
 */
function _simplenews_init_simplenews_category() {
  if ($tree = taxonomy_get_tree(variable_get('simplenews_vid', ''))) {
    $categories = simplenews_categories_load_multiple();
    $first = TRUE;
    foreach ($tree as $term) {
      // Create a newsletter category for each newsletter taxonomy term.
      if (!isset($categories[$term->tid])) {
        $category = new stdClass();
        $category->tid = $term->tid;
        // @todo use a function for category default values
        $category->from_name      = variable_get('site_name', 'Drupal');
        $category->email_subject  ='[[simplenews-category:name]] [node:title]';
        $category->from_address   = variable_get('site_mail', ini_get('sendmail_from'));
        $category->format         = 'plain';
        $category->priority       = SIMPLENEWS_PRIORITY_NONE;
        $category->receipt        = 0;
        $category->hyperlinks     = 1;
        $category->new_account    = 'none';
        $category->opt_inout      = 'double';
        $category->block          = 0;
        if ($first) {
          $category->block        = 1;
          $first = FALSE;
        }
        simplenews_category_save($category);
      }
    }
  }
}

/**
 * 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,
      ))
      // 2 equals SIMPLENEWS_STATUS_SEND_READY.
      ->condition('status', 2)
      ->condition('sent_subscriber_count', 0)
      ->condition('tid', $tid)
      ->execute();
  }
}