You are here

function simplenews_update_7200 in Simplenews 7.2

Replace {simplenews_newsletter}.

File

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

Code

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);
    }
  }
}