You are here

forward.install in Forward 5

File

forward.install
View source
<?php

function forward_install() {
  drupal_set_message('Installing forward');
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      $query1 = db_query("create table {forward_log} (\n        path varchar(255) NOT NULL default '<front>',\n        type varchar(8) NOT NULL default '',\n        timestamp int(11) NOT NULL default '0',\n        KEY (path)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $query2 = db_query("CREATE TABLE {forward_statistics} (\n        nid int(10) unsigned NOT NULL,\n        last_forward_timestamp int(11) NOT NULL default '0',\n        forward_count int(10) unsigned NOT NULL default '0',\n        clickthrough_count int(10) unsigned NOT NULL default '0',\n        PRIMARY KEY (nid),\n        KEY (last_forward_timestamp)\n      );");
      break;
    case 'pgsql':
      $query1 = db_query("CREATE TABLE {forward_log} (\n        path varchar(255) NOT NULL default '<front>',\n        type varchar(8) NOT NULL default '',\n        timestamp integer NOT NULL default '0'\n      )");
      $query2 = db_query("CREATE TABLE {forward_statistics} (\n        nid integer NOT NULL,\n        last_forward_timestamp integer NOT NULL default '0',\n        forward_count integer NOT NULL default '0',\n        clickthrough_count integer NOT NULL default '0',\n        PRIMARY KEY (nid)\n      )");
      db_query("CREATE INDEX idx_forward_log_path ON {forward_log} (path);");
      db_query("CREATE INDEX {forward_statistics}_last_forward_timestamp_idx ON {forward_statistics} (last_forward_timestamp);");
      break;
    default:
      break;
  }

  // End case
  db_query("INSERT INTO {forward_statistics} (nid, last_forward_timestamp, forward_count, clickthrough_count) SELECT n.nid, 0, 0, 0 FROM {node} n");
  if ($query1 && $query2) {
    drupal_set_message(t('Forward module installed tables successfully.'));
  }
  else {
    drupal_set_message(t('Table installation for the Forward module was unsuccessful. The tables may need to be installed by hand. See forward.install file for a list of the installation queries.'), 'error');
  }
}

/**
 * Changed node_comment_statistics to use node->changed to avoid future
 * timestamps.
 */
function forward_update_1() {

  // initialize table
  $ret[] = update_sql("CREATE TABLE {forward_statistics} (\n    nid int(10) unsigned NOT NULL,\n    last_forward_timestamp int(11) NOT NULL default '0',\n    forward_count int(10) unsigned NOT NULL default '0',\n    clickthrough_count int(10) unsigned NOT NULL default '0',\n    PRIMARY KEY (nid),\n    KEY (last_forward_timestamp)\n  );");
  $ret[] = update_sql("INSERT INTO {forward_statistics} (nid, last_forward_timestamp, forward_count, clickthrough_count) values(0, 0, 0, 0)");
  $ret[] = update_sql("INSERT INTO {forward_statistics} (nid, last_forward_timestamp, forward_count, clickthrough_count) SELECT n.nid, 0, 0, 0 FROM {node} n");

  // fill table
  $forwards = db_query("SELECT f.nid, f.timestamp, COUNT(f.nid) as forward_count FROM {node} n LEFT JOIN {forward_log} f ON f.nid = n.nid WHERE f.type = 'SENT' GROUP BY f.nid, f.timestamp");
  while ($forward = db_fetch_object($forwards)) {
    $forward_count = db_result(db_query("SELECT COUNT(nid) FROM {forward_log} WHERE nid = %d AND type = '%s'", $forward->nid, 'SENT'));
    $clickthrough_count = db_result(db_query("SELECT COUNT(nid) FROM {forward_log} WHERE nid = %d AND type = '%s'", $forward->nid, 'REF'));
    db_query("UPDATE {forward_statistics} SET forward_count = %d, clickthrough_count = %d, last_forward_timestamp = %d WHERE nid = %d", $forward_count, $clickthrough_count, $forward->timestamp, $forward->nid);
  }
  return $ret;
}

/**
 * Changed node_comment_statistics to use node->changed to avoid future
 * timestamps.
 */
function forward_update_2() {

  //variable strings changed
  variable_set('forward_emailsubject', str_replace(array(
    '%name',
    '%site',
  ), array(
    '!name',
    '!site',
  ), variable_get('forward_emailsubject', '!name has forwarded a page to you from !site')));
  variable_set('forward_emailmessage', str_replace(array(
    '%name',
    '%site',
  ), array(
    '!name',
    '!site',
  ), variable_get('forward_emailmessage', '!name thought you would like to see this page from the !site web site.')));
  variable_set('forward_postcardsubject', str_replace(array(
    '%name',
    '%site',
  ), array(
    '!name',
    '!site',
  ), variable_get('forward_postcardsubject', '!name has sent you an e-postcard from !site')));
  variable_set('forward_postcardmessage', str_replace(array(
    '%name',
    '%site',
  ), array(
    '!name',
    '!site',
  ), variable_get('forward_postcardmessage', '!name has sent you an e-postcard from the !site web site.  Please take a moment to visit our web site.')));
  return array();
}

/**
 * Changed forward_statistics to handle non-node paths
 */
function forward_update_3() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {forward_log} ADD COLUMN path varchar(255) NOT NULL default '<front>' FIRST");
      break;
    case 'pgsql':
      db_add_column($ret, 'forward_log', 'path', 'varchar', array(
        'size' => 255,
        'default' => "'<front>'",
        'not null' => TRUE,
      ));
      break;
  }
  $ret[] = update_sql("UPDATE {forward_log} SET path = CONCAT('node/', nid) WHERE nid != 0");
  $ret[] = update_sql("UPDATE {forward_log} SET path = '<front>' WHERE nid = 0");
  $ret[] = update_sql("ALTER TABLE {forward_log} DROP COLUMN nid");
  return $ret;
}
function forward_uninstall() {
  db_query('DROP TABLE {forward_log}');
  db_query('DROP TABLE {forward_statistics}');
}

Functions

Namesort descending Description
forward_install
forward_uninstall
forward_update_1 Changed node_comment_statistics to use node->changed to avoid future timestamps.
forward_update_2 Changed node_comment_statistics to use node->changed to avoid future timestamps.
forward_update_3 Changed forward_statistics to handle non-node paths