You are here

invite.install in Invite 5

File

invite.install
View source
<?php

/**
 * Install the initial schema.
 */
function invite_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("\n        CREATE TABLE {invite} (\n          email varchar(64) NOT NULL default '',\n          reg_code varchar(64) NOT NULL default '',\n          uid int(10) unsigned NOT NULL default '0',\n          mid int(11) unsigned NOT NULL default '0',\n          expiry int(11) NOT NULL default '0',\n          timestamp int(11) NOT NULL default '0',\n          received tinyint(3) unsigned NOT NULL default '0',\n          data text NOT NULL,\n          PRIMARY KEY  (email),\n          UNIQUE (reg_code),\n          KEY (uid)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("\n        CREATE TABLE {invite} (\n          email VARCHAR(64) NOT NULL DEFAULT '',\n          reg_code VARCHAR(64) NOT NULL DEFAULT '',\n          uid INTEGER NOT NULL DEFAULT 0,\n          mid INTEGER NOT NULL DEFAULT 0,\n          expiry INTEGER NOT NULL DEFAULT 0,\n          timestamp INTEGER NOT NULL DEFAULT 0,\n          received SMALLINT NOT NULL DEFAULT 0,\n          data TEXT NOT NULL DEFAULT '',\n          PRIMARY KEY (email),\n          UNIQUE (reg_code)\n        );");
      db_query("CREATE INDEX {invite}_uid_idx ON {invite} (uid)");
      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 (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));
  }
  db_query("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;
}

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_2 Drop duplicate index on reg_code. Add index for uid.
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.