View source
<?php
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;
}
}
function invite_uninstall() {
db_query('DROP TABLE {invite}');
$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');
variable_del('invite_num_ranks');
}
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);
}
}
}
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;
}
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;
}
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;
}
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;
}
$ret[] = update_sql("UPDATE {invite} SET received = 1 WHERE mid != 0");
return $ret;
}
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;
}
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;
}
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;
}
function invite_update_8() {
$ret = array();
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;
}
$result = db_query("SELECT reg_code, message FROM {invite} WHERE message != ''");
while ($row = db_fetch_object($result)) {
if (substr($row->message, 0, 2) == 'a:') {
continue;
}
$data = array(
'subject' => NULL,
'message' => $row->message,
);
db_query("UPDATE {invite} SET data = '%s' WHERE reg_code = '%s'", serialize($data), $row->reg_code);
}
$ret[] = update_sql("ALTER TABLE {invite} DROP message");
return $ret;
}
function invite_update_9() {
$ret = array();
foreach (user_roles(0, 'send invitations') as $rid => $role) {
$role_no_space = str_replace(' ', '_', $role);
if (variable_get('invite_maxnum_' . $role_no_space, 0) == 0) {
variable_set('invite_maxnum_' . $role_no_space, -1);
}
$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 = 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;
}
function invite_update_10() {
$old = array(
'[invite-message]',
'[inviter]',
);
$new = array(
'[invite-message-raw]',
'[inviter-raw]',
);
_invite_update_tokens('invite_default_mail_template', $old, $new);
_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;
}
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;
}