You are here

invite.install in Invite 5.2

Installation file for invite module.

File

invite.install
View source
<?php

/**
 * @file
 * Installation file for invite module.
 */

/**
 * Install the initial schema.
 */
function invite_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("\n        CREATE TABLE {invite} (\n          reg_code varchar(8) NOT NULL default '',\n          email varchar(100) NOT NULL default '',\n          uid int(10) NOT NULL default '0',\n          invitee int(10) NOT NULL default '0',\n          created int(10) NOT NULL default '0',\n          expiry int(10) NOT NULL default '0',\n          joined int(10) NOT NULL default '0',\n          canceled tinyint(1) NOT NULL default '0',\n          resent tinyint(3) NOT NULL default '0',\n          data text NOT NULL,\n          UNIQUE (reg_code),\n          KEY (email),\n          KEY (uid)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("\n        CREATE TABLE {invite_notifications} (\n          uid int(10) NOT NULL default '0',\n          invitee int(10) NOT NULL default '0',\n          UNIQUE (uid, invitee)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("\n        CREATE TABLE {invite} (\n          reg_code VARCHAR(8) NOT NULL DEFAULT '',\n          email VARCHAR(100) NOT NULL DEFAULT '',\n          uid INTEGER NOT NULL DEFAULT 0,\n          invitee INTEGER NOT NULL DEFAULT 0,\n          created INTEGER NOT NULL DEFAULT 0,\n          expiry INTEGER NOT NULL DEFAULT 0,\n          joined INTEGER NOT NULL DEFAULT 0,\n          canceled SMALLINT NOT NULL DEFAULT 0,\n          resent SMALLINT NOT NULL DEFAULT 0,\n          data TEXT NOT NULL DEFAULT '',\n          UNIQUE (reg_code)\n        )");
      db_query("CREATE INDEX {invite}_email_idx ON {invite}(email)");
      db_query("CREATE INDEX {invite}_uid_idx ON {invite}(uid)");
      db_query("\n        CREATE TABLE {invite_notifications} (\n          uid INTEGER NOT NULL DEFAULT 0,\n          invitee INTEGER NOT NULL DEFAULT 0,\n          UNIQUE (uid, invitee)\n        )");
      break;
  }
}

/**
 * Implementation of hook_uninstall().
 */
function invite_uninstall() {

  // Drop database table
  db_query('DROP TABLE {invite}');

  // Delete variables
  $sql = "DELETE from {variable} WHERE name LIKE '%s%%'";
  db_query($sql, 'invite_target_role_');
  db_query($sql, 'invite_maxnum_');
  db_query($sql, 'invite_maxmultiple_');
  variable_del('invite_target_role_default');
  variable_del('invite_expiry');
  variable_del('invite_allow_join_delete');
  variable_del('invite_subject');
  variable_del('invite_use_users_email');
  variable_del('invite_use_users_email_replyto');
  variable_del('invite_manual_from');
  variable_del('invite_manual_reply_to');
  variable_del('invite_page_title');
  variable_del('invite_default_mail_template');
  variable_del('invite_help_text');

  // invite_stats module
  variable_del('invite_num_ranks');
}

/**
 * Helper function to update tokens.
 */
function _invite_update_tokens($variables, $old, $new) {
  foreach ((array) $variables as $variable) {
    if ($value = variable_get($variable, NULL)) {
      $value = str_replace($old, $new, $value);
      variable_set($variable, $value);
    }
  }
}

/**
 * Helper function to add a permission to a role.
 */
function _invite_add_permission($rid, $permission) {
  if ($permission) {
    $current_perm = db_result(db_query("SELECT perm FROM {permission} WHERE rid = %d", $rid));
    if ($current_perm != '') {
      $current_perm .= ', ';
    }
    $current_perm .= $permission;
    db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", $current_perm, $rid);
  }
}
function invite_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {invite} CHANGE reg_code reg_code VARCHAR(64) UNIQUE NOT NULL");
      $ret[] = update_sql("ALTER TABLE {invite} ADD INDEX reg_code_idx (reg_code)");
      break;
  }
  return $ret;
}

/**
 * Drop duplicate index on reg_code. Add index for uid.
 */
function invite_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {invite} DROP INDEX reg_code_idx");
      $ret[] = update_sql("ALTER TABLE {invite} ADD INDEX (uid)");
      break;
    case 'pgsql':
      $ret[] = update_sql("DROP INDEX reg_code_idx");
      $ret[] = update_sql("CREATE INDEX {invite}_uid_idx ON {invite} (uid)");
      break;
  }
  return $ret;
}

/**
 * Clean up invites originating from deleted users.
 */
function invite_update_3() {
  $ret = array();
  $result = db_query("SELECT DISTINCT i.uid FROM {invite} i LEFT JOIN {users} u ON i.uid = u.uid WHERE u.uid IS NULL");
  $count = db_num_rows($result);
  $sql = "DELETE FROM {invite} WHERE uid = %d";
  if (!variable_get('invite_allow_join_delete', 0)) {
    $sql .= " AND timestamp != 0";
  }
  while ($inviter = db_fetch_object($result)) {
    db_query($sql, $inviter->uid);
  }
  $ret[] = array(
    'query' => strtr('%count orphaned invites have been deleted.', array(
      '%count' => $count,
    )),
    'success' => TRUE,
  );
  return $ret;
}

/**
 * Add notification after an invited user registers.
 */
function invite_update_4() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {invite} CHANGE received received tinyint(3) unsigned NOT NULL default '0'");
      break;
    case 'pgsql':
      db_change_column($ret, 'invite', 'received', 'received', 'SMALLINT', array(
        'not null' => TRUE,
      ));
      break;
  }

  // Prevent displaying a whole bunch of messages for old invites
  $ret[] = update_sql("UPDATE {invite} SET received = 1 WHERE mid <> 0");
  return $ret;
}

/**
 * Save invite message text.
 */
function invite_update_5() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {invite} ADD COLUMN message TEXT NOT NULL default ''");
      break;
    case 'pgsql':
      db_add_column($ret, 'invite', 'message', 'TEXT', array(
        'not null' => TRUE,
        'default' => "''",
      ));
      break;
  }
  return $ret;
}

/**
 * Clean up invitations of deleted users.
 */
function invite_update_6() {
  $ret = array();
  if (variable_get('invite_allow_join_delete', 0)) {
    $result = db_query("SELECT i.mid FROM {invite} i LEFT JOIN {users} u ON i.mid = u.uid WHERE u.uid IS NULL");
    $count = db_num_rows($result);
    while ($invitee = db_fetch_object($result)) {
      db_query("DELETE FROM {invite} WHERE mid = %d", $invitee->mid);
    }
    $ret[] = array(
      'query' => strtr('%count orphaned invites have been deleted.', array(
        '%count' => $count,
      )),
      'success' => TRUE,
    );
  }
  return $ret;
}

/**
 * Switch to token.module.
 */
function invite_update_7() {
  $ret = array();
  $old = array(
    '@site',
    '@join_link',
    '@homepage',
    '@message',
    '@inviter',
  );
  $new = array(
    '[site-name]',
    '[join-link]',
    '[site-url]',
    '[invite-message]',
    '[inviter]',
  );
  _invite_update_tokens('invite_default_mail_template', $old, $new);
  $ret[] = array(
    'query' => 'The message tokens for the invite module have been successfully updated.',
    'success' => TRUE,
  );
  drupal_set_message(strtr('Please note that invite now depends on the %token module.', array(
    '%token' => l('token', 'http://drupal.org/project/token', array(
      'target' => '_blank',
    ), NULL, NULL, TRUE),
  )), 'error');
  return $ret;
}

/**
 * Change message to a generic data column and convert existing messages.
 */
function invite_update_8() {
  $ret = array();

  // Add column data first
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {invite} ADD COLUMN data TEXT NOT NULL");
      break;
    case 'pgsql':
      db_add_column($ret, 'invite', 'data', 'TEXT', array(
        'not null' => TRUE,
        'default' => "''",
      ));
      break;
  }

  // Convert existing messages
  $result = db_query("SELECT reg_code, message FROM {invite} WHERE message <> ''");
  while ($row = db_fetch_object($result)) {
    if (drupal_substr($row->message, 0, 2) == 'a:') {

      // Already serialized
      continue;
    }
    $data = array(
      'subject' => NULL,
      'message' => $row->message,
    );
    db_query("UPDATE {invite} SET data = '%s' WHERE reg_code = '%s'", serialize($data), $row->reg_code);
  }

  // Finally drop column message
  $ret[] = update_sql("ALTER TABLE {invite} DROP message");
  return $ret;
}

/**
 * Update limit and move some settings to the premissions table.
 */
function invite_update_9() {
  $ret = array();
  foreach (user_roles(0, 'send invitations') as $rid => $role) {
    $role_no_space = str_replace(' ', '_', $role);

    // INVITE_UNLIMITED_INVITES changed from 0 to -1
    if (variable_get('invite_maxnum_' . $role_no_space, 0) == 0) {
      variable_set('invite_maxnum_' . $role_no_space, -1);
    }

    // Convert settings that have been moved to the permissions table
    $perms = array();
    if (variable_get('invite_maxmultiple_' . $role_no_space, 1) != 1) {
      $perms[] = 'send mass invitations';
    }
    if (variable_get('invite_allow_join_delete', 0)) {
      $perms[] = 'withdraw accepted invitations';
    }
    _invite_add_permission($rid, implode(', ', $perms));
  }
  $ret[] = update_sql("DELETE from {variable} WHERE name LIKE 'invite_maxmultiple_%%'");

  // Expiry periods changed
  $expiry = variable_get('invite_expiry', 30);
  switch ($expiry) {
    case 5:
      $expiry = 7;
      break;
    case 10:
      $expiry = 14;
      break;
    case 15:
      $expiry = 14;
      break;
    case 20:
      $expiry = 30;
      break;
    case 25:
      $expiry = 30;
      break;
    case 30:
      $expiry = 30;
      break;
    case 45:
      $expiry = 60;
      break;
    case 60:
      $expiry = 60;
      break;
  }
  variable_set('invite_expiry', $expiry);
  $ret[] = array(
    'query' => 'The access permissions have been updated by the invite module.',
    'success' => TRUE,
  );
  return $ret;
}

/**
 * Update tokens for security.
 */
function invite_update_10() {

  // E-mail template
  $old = array(
    '[invite-message]',
    '[inviter]',
  );
  $new = array(
    '[invite-message-raw]',
    '[inviter-raw]',
  );
  _invite_update_tokens('invite_default_mail_template', $old, $new);

  // E-mail name and subject
  _invite_update_tokens(array(
    'invite_email_name',
    'invite_subject',
  ), '[inviter]', '[inviter-raw]');
  $ret = array(
    array(
      'query' => 'The tokens for the invite module have been successfully updated.',
      'success' => TRUE,
    ),
  );
  return $ret;
}

/**
 * Add track permission to all roles that currently have send permission.
 */
function invite_update_11() {
  $ret = array();
  foreach (array_keys(user_roles(0, 'send invitations')) as $rid) {
    _invite_add_permission($rid, 'track invitations');
  }
  $ret[] = array(
    'query' => 'The access permissions have been updated by the invite module.',
    'success' => TRUE,
  );
  return $ret;
}

/**
 * Change user_register value for enhanced compatibility with LoginToboggan.
 */
function invite_update_12() {
  if (variable_get('user_register', 1) == 'inviteonly') {
    variable_set('user_register', '1-inviteonly');
  }
  return array();
}

/**
 * @{
 * Invite 2.x updates
 */

/**
 * 1. Allow multiple invitations for the same e-mail address.
 * 2. Changed some column names to be more descriptive.
 * 3. Added a column to flag canceled invites.
 * 4. Added resent column.
 */
function invite_update_200() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {invite} DROP PRIMARY KEY, ADD INDEX email (email)");
      $ret[] = update_sql("ALTER TABLE {invite} CHANGE mid invitee int(10) NOT NULL default '0'");
      $ret[] = update_sql("ALTER TABLE {invite} CHANGE timestamp joined int(10) NOT NULL default '0'");
      $ret[] = update_sql("ALTER TABLE {invite} ADD created int(10) NOT NULL default '0'");
      $ret[] = update_sql("ALTER TABLE {invite} ADD canceled tinyint(1) NOT NULL default '0'");
      $ret[] = update_sql("ALTER TABLE {invite} ADD resent tinyint(3) NOT NULL default '0'");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {invite} DROP CONSTRAINT {invite}_pkey");
      $ret[] = update_sql("CREATE INDEX {invite}_email_idx ON {invite}(email)");
      db_change_column($ret, 'invite', 'mid', 'invitee', 'INTEGER', array(
        'not null' => TRUE,
        'default' => 0,
      ));
      db_change_column($ret, 'invite', 'timestamp', 'joined', 'INTEGER', array(
        'not null' => TRUE,
        'default' => 0,
      ));
      db_add_column($ret, 'invite', 'created', 'INTEGER', array(
        'default' => 0,
        'not null' => TRUE,
      ));
      db_add_column($ret, 'invite', 'canceled', 'SMALLINT', array(
        'default' => 0,
        'not null' => TRUE,
      ));
      db_add_column($ret, 'invite', 'resent', 'SMALLINT', array(
        'default' => 0,
        'not null' => TRUE,
      ));
      break;
  }
  return $ret;
}

/**
 * Revamped notification system.
 */
function invite_update_201() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("\n        CREATE TABLE {invite_notifications} (\n          uid int(10) NOT NULL default '0',\n          invitee int(10) NOT NULL default '0',\n          KEY (uid)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      $ret[] = update_sql("\n        CREATE TABLE {invite_notifications} (\n          uid INTEGER NOT NULL DEFAULT 0,\n          invitee INTEGER NOT NULL DEFAULT 0\n        )");
      $ret[] = update_sql("CREATE INDEX {invite_notifications}_uid_idx ON {invite_notifications}(uid)");
      break;
  }

  // Convert old data
  $ret[] = update_sql("INSERT INTO {invite_notifications} (uid, invitee) SELECT uid, invitee FROM {invite} WHERE joined != 0 AND received = 0");

  // Drop old column
  $ret[] = update_sql("ALTER TABLE {invite} DROP received");
  return $ret;
}

/**
 * Optimize index of notification table.
 */
function invite_update_202() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {invite_notifications} DROP INDEX uid,  ADD UNIQUE uid_invitee (uid, invitee)");
      break;
    case 'pgsql':
      $ret[] = update_sql("DROP INDEX {invite_notifications}_uid_idx");
      $ret[] = update_sql("CREATE UNIQUE INDEX {invite_notifications}_uid_invitee_key ON {invite_notifications}(uid, invitee)");
      break;
  }
  return $ret;
}

Functions

Namesort descending Description
invite_install Install the initial schema.
invite_uninstall Implementation of hook_uninstall().
invite_update_1
invite_update_10 Update tokens for security.
invite_update_11 Add track permission to all roles that currently have send permission.
invite_update_12 Change user_register value for enhanced compatibility with LoginToboggan.
invite_update_2 Drop duplicate index on reg_code. Add index for uid.
invite_update_200 1. Allow multiple invitations for the same e-mail address. 2. Changed some column names to be more descriptive. 3. Added a column to flag canceled invites. 4. Added resent column.
invite_update_201 Revamped notification system.
invite_update_202 Optimize index of notification table.
invite_update_3 Clean up invites originating from deleted users.
invite_update_4 Add notification after an invited user registers.
invite_update_5 Save invite message text.
invite_update_6 Clean up invitations of deleted users.
invite_update_7 Switch to token.module.
invite_update_8 Change message to a generic data column and convert existing messages.
invite_update_9 Update limit and move some settings to the premissions table.
_invite_add_permission Helper function to add a permission to a role.
_invite_update_tokens Helper function to update tokens.