You are here

function simplenews_update_7201 in Simplenews 7.2

Convert existing simplenews_newsletter table to fields.

File

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

Code

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