You are here

simplenews.install in Simplenews 5

File

simplenews.install
View source
<?php

function simplenews_install() {
  $result = array();
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      $result[] = db_query("CREATE TABLE {simplenews_subscriptions} (\n        snid int(10) NOT NULL auto_increment,\n        a_status int(2) NOT NULL default '0',\n        s_status int(2) NOT NULL default '0',\n        mail varchar(64) NOT NULL default '',\n        uid int(10) NOT NULL default '0',\n        PRIMARY KEY  (snid),\n        KEY mail (mail)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $result[] = db_query("CREATE TABLE {simplenews_newsletters} (\n        nid int(10) NOT NULL default '0',\n        vid int(10) NOT NULL default '0',\n        tid int(10) NOT NULL default '0',\n        s_status int(2) NOT NULL default '0',\n        s_format varchar(8) NOT NULL default '',\n        priority int(2) NOT NULL default '0',\n        receipt int(2) NOT NULL default '0',\n        PRIMARY KEY  (nid)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $result[] = db_query("CREATE TABLE {simplenews_snid_tid} (\n        snid int(10) NOT NULL default '0',\n        tid int(10) NOT NULL default '0',\n        PRIMARY KEY  (snid,tid)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      $result[] = db_query("CREATE TABLE {simplenews_subscriptions} (\n        snid SERIAL,\n        a_status smallint NOT NULL default '0',\n        s_status smallint NOT NULL default '0',\n        mail varchar(64) NOT NULL default '',\n        uid integer NOT NULL default '0',\n        PRIMARY KEY  (snid)\n        );");
      $result[] = db_query('CREATE INDEX {simplenews_subscriptions}_mail_idx ON {simplenews_subscriptions} (mail)');
      $result[] = db_query("CREATE TABLE {simplenews_newsletters} (\n        nid integer NOT NULL default '0',\n        vid integer NOT NULL default '0',\n        tid integer NOT NULL default '0',\n        s_status smallint NOT NULL default '0',\n        s_format varchar(8) NOT NULL default '',\n        priority smallint NOT NULL default '0',\n        receipt smallint NOT NULL default '0',\n        PRIMARY KEY  (nid)\n        );");
      $result[] = db_query("CREATE TABLE {simplenews_snid_tid} (\n        snid integer NOT NULL default '0',\n        tid integer NOT NULL default '0',\n        PRIMARY KEY  (snid,tid)\n        );");
      break;
    case 'mssql':
      $result[] = db_query("CREATE TABLE {simplenews_subscriptions} (\n        snid int NOT NULL IDENTITY(1,1),\n        a_status int NOT NULL default '0',\n        s_status int NOT NULL default '0',\n        mail varchar(64) NOT NULL default '',\n        uid int NOT NULL default '0',\n        PRIMARY KEY (snid)\n        );");
      $result[] = db_query("CREATE TABLE {simplenews_newsletters} (\n        nid int NOT NULL default '0',\n        tid int NOT NULL default '0',\n        s_status int NOT NULL default '0',\n        s_format varchar(8) NOT NULL default '',\n        priority int NOT NULL default '0',\n        receipt int NOT NULL default '0',\n        PRIMARY KEY (nid)\n        );");
      $result[] = db_query("CREATE TABLE {simplenews_snid_tid} (\n        snid int NOT NULL default '0',\n        tid int NOT NULL default '0',\n        PRIMARY KEY  (snid,tid)\n        );");
      break;
  }
  if (count($result) != count(array_filter($result))) {
    drupal_set_message(t('The installation of the Simplenews module was unsuccessful.'), 'error');
  }
}

/**
 * Implementation of hook_enable().
 *
 * If required create vocabulary for simplenews newsletter and one newsletter term
 */
function simplenews_enable() {
  if ($vocabulary = taxonomy_get_vocabulary(variable_get('simplenews_vid', ''))) {

    // Existing install. Add back newsletter node type, if the forums
    // vocabulary still exists. Leave all other node types unchanged.
    $vocabulary = (array) $vocabulary;
    $vocabulary['nodes']['simplenews'] = 1;

    // If it exists we remove the 0 key. It is the indication that no node types are selected.
    unset($vocabulary['nodes'][0]);
    taxonomy_save_vocabulary($vocabulary);
  }
  else {

    // Create the simplenews vocabulary if it does not exist.
    $vocabulary = array(
      'name' => t('Newsletter'),
      'multiple' => '0',
      'required' => '0',
      'hierarchy' => '0',
      'relations' => '0',
      'module' => 'simplenews',
      'nodes' => array(
        'simplenews' => 1,
      ),
    );
    taxonomy_save_vocabulary($vocabulary);
    variable_set('simplenews_vid', $vocabulary['vid']);
  }

  // Check to see if at least 1 term exists, else create one
  $tid = db_result(db_query('SELECT tid FROM {term_data} WHERE vid = %d', $vocabulary['vid']));
  if (!$tid) {
    $form_values = array(
      'name' => variable_get('site_name', 'Drupal') . ' ' . t('newsletter'),
      'vid' => $vocabulary['vid'],
      'weight' => 0,
    );
    switch (taxonomy_save_term($form_values)) {
      case SAVED_UPDATED:
        drupal_set_message(t('Updated term %name.', array(
          '%name' => $form_values['name'],
        )));
        break;
      case SAVED_DELETED:
        drupal_set_message(t('Deleted term %name.', array(
          '%name' => $form_values['name'],
        )));
        break;
    }
  }
}

/**
 * Implementation of hook_uninstall().
 */
function simplenews_uninstall() {
  db_query('DROP TABLE {simplenews_snid_tid}');
  db_query('DROP TABLE {simplenews_newsletters}');
  db_query('DROP TABLE {simplenews_subscriptions}');
  db_query("DELETE FROM {variable} WHERE name LIKE 'simplenews_%%'");
}
function simplenews_update_1() {
  return _system_update_utf8(array(
    'sn_snid_tid',
    'sn_newsletters',
    'sn_subscriptions',
  ));
}

/**
 * Rename sn_* tables to simplenews_* to avoid namespace conflicts.
 */
function simplenews_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
    case 'pgsql':
      $ret[] = update_sql('ALTER TABLE {sn_snid_tid} RENAME TO {simplenews_snid_tid}');
      $ret[] = update_sql('ALTER TABLE {sn_newsletters} RENAME TO {simplenews_newsletters}');
      $ret[] = update_sql('ALTER TABLE {sn_subscriptions} RENAME TO {simplenews_subscriptions}');
      break;
  }
  return $ret;
}

/**
 * Add index to simplenews_subscriptions
 */
function simplenews_update_5000() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {simplenews_subscriptions} ADD INDEX mail (mail);");
      break;
    case 'pgsql':
      $ret[] = update_sql('CREATE INDEX {simplenews_subscriptions}_mail_idx ON {simplenews_subscriptions} (mail)');
      break;
  }
  return $ret;
}

/**
 * Add index to simplenews_subscriptions
 */
function simplenews_update_5001() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {simplenews_newsletters} ADD vid int(10) NOT NULL default '0'");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {simplenews_newsletters} ADD vid int NOT NULL default '0'");
      break;
  }
  return $ret;
}

Functions

Namesort descending Description
simplenews_enable Implementation of hook_enable().
simplenews_install
simplenews_uninstall Implementation of hook_uninstall().
simplenews_update_1
simplenews_update_2 Rename sn_* tables to simplenews_* to avoid namespace conflicts.
simplenews_update_5000 Add index to simplenews_subscriptions
simplenews_update_5001 Add index to simplenews_subscriptions