| 1 |
<?php
|
| 2 |
// $Id: invite.install,v 1.13 2008/09/02 17:02:14 smk Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Installation file for invite module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Install the initial schema.
|
| 11 |
*/
|
| 12 |
function invite_install() {
|
| 13 |
drupal_install_schema('invite');
|
| 14 |
}
|
| 15 |
|
| 16 |
function invite_schema() {
|
| 17 |
$schema['invite'] = array(
|
| 18 |
'description' => t('The base table for invites.'),
|
| 19 |
'fields' => array(
|
| 20 |
'reg_code' => array(
|
| 21 |
'description' => t('Stores the issued registration code and acts as primary identifier for a invite.'),
|
| 22 |
'type' => 'varchar',
|
| 23 |
'length' => 8,
|
| 24 |
'not null' => TRUE,
|
| 25 |
'default' => '',
|
| 26 |
),
|
| 27 |
'email' => array(
|
| 28 |
'description' => t('Stores the e-mail the invite has been addressed to.'),
|
| 29 |
'type' => 'varchar',
|
| 30 |
'length' => 100,
|
| 31 |
'not null' => TRUE,
|
| 32 |
'default' => '',
|
| 33 |
),
|
| 34 |
'uid' => array(
|
| 35 |
'description' => t('Stores the user id of the inviter.'),
|
| 36 |
'type' => 'int',
|
| 37 |
'not null' => TRUE,
|
| 38 |
'default' => 0,
|
| 39 |
),
|
| 40 |
'invitee' => array(
|
| 41 |
'description' => t('Stores the user id of the invitee upon registration.'),
|
| 42 |
'type' => 'int',
|
| 43 |
'not null' => TRUE,
|
| 44 |
'default' => 0,
|
| 45 |
),
|
| 46 |
'created' => array(
|
| 47 |
'description' => t('Stores the creation time of the invite.'),
|
| 48 |
'type' => 'int',
|
| 49 |
'not null' => TRUE,
|
| 50 |
'default' => 0,
|
| 51 |
),
|
| 52 |
'expiry' => array(
|
| 53 |
'description' => t('Stores the expiry time of the invite.'),
|
| 54 |
'type' => 'int',
|
| 55 |
'not null' => TRUE,
|
| 56 |
'default' => 0,
|
| 57 |
),
|
| 58 |
'joined' => array(
|
| 59 |
'description' => t('Will be filled with the time the invite was accepted upon registration.'),
|
| 60 |
'type' => 'int',
|
| 61 |
'not null' => TRUE,
|
| 62 |
'default' => 0,
|
| 63 |
),
|
| 64 |
'canceled' => array(
|
| 65 |
'description' => t('Stores whether the invite has been withdrawn.'),
|
| 66 |
'type' => 'int',
|
| 67 |
'size' => 'tiny',
|
| 68 |
'not null' => TRUE,
|
| 69 |
'default' => 0,
|
| 70 |
),
|
| 71 |
'resent' => array(
|
| 72 |
'description' => t('Stores how many times the invite has been resent.'),
|
| 73 |
'type' => 'int',
|
| 74 |
'size' => 'tiny',
|
| 75 |
'not null' => TRUE,
|
| 76 |
'default' => 0,
|
| 77 |
),
|
| 78 |
'data' => array(
|
| 79 |
'description' => t('Stores auxiliary data.'),
|
| 80 |
'type' => 'text',
|
| 81 |
'not null' => TRUE,
|
| 82 |
),
|
| 83 |
),
|
| 84 |
'unique keys' => array(
|
| 85 |
'reg_code' => array('reg_code'),
|
| 86 |
),
|
| 87 |
'indexes' => array(
|
| 88 |
'email' => array('email'),
|
| 89 |
'uid' => array('uid'),
|
| 90 |
),
|
| 91 |
);
|
| 92 |
|
| 93 |
$schema['invite_notifications'] = array(
|
| 94 |
'description' => t('Stores notifications of inviters.'),
|
| 95 |
'fields' => array(
|
| 96 |
'uid' => array(
|
| 97 |
'description' => t('Stores the user id to be notified (inviter).'),
|
| 98 |
'type' => 'int',
|
| 99 |
'not null' => TRUE,
|
| 100 |
'default' => 0,
|
| 101 |
),
|
| 102 |
'invitee' => array(
|
| 103 |
'description' => t('Stores the user id of the invitee.'),
|
| 104 |
'type' => 'int',
|
| 105 |
'not null' => TRUE,
|
| 106 |
'default' => 0,
|
| 107 |
),
|
| 108 |
),
|
| 109 |
'unique keys' => array(
|
| 110 |
'uid_invitee' => array('uid', 'invitee'),
|
| 111 |
)
|
| 112 |
);
|
| 113 |
|
| 114 |
return $schema;
|
| 115 |
}
|
| 116 |
|
| 117 |
/**
|
| 118 |
* Implementation of hook_uninstall().
|
| 119 |
*/
|
| 120 |
function invite_uninstall() {
|
| 121 |
// Drop database schema.
|
| 122 |
drupal_uninstall_schema('invite');
|
| 123 |
|
| 124 |
// Delete variables
|
| 125 |
$sql = "DELETE from {variable} WHERE name LIKE '%s%%'";
|
| 126 |
db_query($sql, 'invite_target_role_');
|
| 127 |
db_query($sql, 'invite_maxnum_');
|
| 128 |
db_query($sql, 'invite_maxmultiple_');
|
| 129 |
|
| 130 |
variable_del('invite_target_role_default');
|
| 131 |
variable_del('invite_expiry');
|
| 132 |
variable_del('invite_allow_join_delete');
|
| 133 |
variable_del('invite_subject');
|
| 134 |
variable_del('invite_use_users_email');
|
| 135 |
variable_del('invite_use_users_email_replyto');
|
| 136 |
variable_del('invite_manual_from');
|
| 137 |
variable_del('invite_manual_reply_to');
|
| 138 |
variable_del('invite_page_title');
|
| 139 |
variable_del('invite_default_mail_template');
|
| 140 |
variable_del('invite_help_text');
|
| 141 |
|
| 142 |
// invite_stats module
|
| 143 |
variable_del('invite_num_ranks');
|
| 144 |
}
|
| 145 |
|
| 146 |
/**
|
| 147 |
* Helper function to update tokens.
|
| 148 |
*/
|
| 149 |
function _invite_update_tokens($variables, $old, $new) {
|
| 150 |
foreach ((array)$variables as $variable) {
|
| 151 |
if ($value = variable_get($variable, NULL)) {
|
| 152 |
$value = str_replace($old, $new, $value);
|
| 153 |
variable_set($variable, $value);
|
| 154 |
}
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
/**
|
| 159 |
* Helper function to add a permission to a role.
|
| 160 |
*/
|
| 161 |
function _invite_add_permission($rid, $permission) {
|
| 162 |
if ($permission) {
|
| 163 |
$current_perm = db_result(db_query("SELECT perm FROM {permission} WHERE rid = %d", $rid));
|
| 164 |
if ($current_perm != '') {
|
| 165 |
$current_perm .= ', ';
|
| 166 |
}
|
| 167 |
$current_perm .= $permission;
|
| 168 |
db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", $current_perm, $rid);
|
| 169 |
}
|
| 170 |
}
|
| 171 |
|
| 172 |
/**
|
| 173 |
* Helper function to update a variable name using role name to role id.
|
| 174 |
*/
|
| 175 |
function _invite_update_role_name_to_id($variable) {
|
| 176 |
$result = db_query("SELECT * FROM {role} ORDER BY name ASC");
|
| 177 |
while ($role = db_fetch_object($result)) {
|
| 178 |
// Look for both a translated (D6) and untranslated (D5) variables.
|
| 179 |
// A translated one is newer and has therefore precendence.
|
| 180 |
$translated_role = str_replace(' ', '_', t($role->name));
|
| 181 |
$value = variable_get($variable .'_'. $translated_role, NULL);
|
| 182 |
if (is_null($value)) {
|
| 183 |
$untranslated_role = str_replace(' ', '_', $role->name);
|
| 184 |
$value = variable_get($variable .'_'. $untranslated_role, NULL);
|
| 185 |
}
|
| 186 |
if (!is_null($value)) {
|
| 187 |
variable_set($variable .'_'. $role->rid, $value);
|
| 188 |
db_query("DELETE FROM {variable} WHERE name IN('%s', '%s')", $variable .'_'. $translated_role, $variable .'_'. $untranslated_role);
|
| 189 |
}
|
| 190 |
}
|
| 191 |
}
|
| 192 |
|
| 193 |
/**
|
| 194 |
* Switch to token.module.
|
| 195 |
*/
|
| 196 |
function invite_update_7() {
|
| 197 |
$ret = array();
|
| 198 |
$old = array('@site', '@join_link', '@homepage', '@message', '@inviter');
|
| 199 |
$new = array('[site-name]', '[join-link]', '[site-url]', '[invite-message]', '[inviter]');
|
| 200 |
_invite_update_tokens('invite_default_mail_template', $old, $new);
|
| 201 |
$ret[] = array(
|
| 202 |
'query' => 'The message tokens for the invite module have been successfully updated.',
|
| 203 |
'success' => TRUE
|
| 204 |
);
|
| 205 |
drupal_set_message(strtr('Please note that invite now depends on the %token module.', array('%token' => l('token', 'http://drupal.org/project/token', array('attributes' => array('target' => '_blank'), 'absolute' => TRUE)))), 'error');
|
| 206 |
return $ret;
|
| 207 |
}
|
| 208 |
|
| 209 |
/**
|
| 210 |
* Change message to a generic data column and convert existing messages.
|
| 211 |
*/
|
| 212 |
function invite_update_8() {
|
| 213 |
$ret = array();
|
| 214 |
db_change_field($ret, 'invite', 'message', 'data', array('type' => 'text', 'not null' => TRUE, 'default' => ''));
|
| 215 |
|
| 216 |
// Convert existing messages
|
| 217 |
$result = db_query("SELECT reg_code, data FROM {invite} WHERE data <> ''");
|
| 218 |
while ($row = db_fetch_object($result)) {
|
| 219 |
if (drupal_substr($row->message, 0, 2) == 'a:') {
|
| 220 |
// Already serialized
|
| 221 |
continue;
|
| 222 |
}
|
| 223 |
$data = array('subject' => NULL, 'message' => $row->data);
|
| 224 |
db_query("UPDATE {invite} SET data = '%s' WHERE reg_code = '%s'", serialize($data), $row->reg_code);
|
| 225 |
}
|
| 226 |
|
| 227 |
return $ret;
|
| 228 |
}
|
| 229 |
|
| 230 |
/**
|
| 231 |
* Update limit and move some settings to the premissions table.
|
| 232 |
*/
|
| 233 |
function invite_update_9() {
|
| 234 |
$ret = array();
|
| 235 |
foreach (user_roles(0, 'send invitations') as $rid => $role) {
|
| 236 |
$role_no_space = str_replace(' ', '_', $role);
|
| 237 |
|
| 238 |
// INVITE_UNLIMITED_INVITES changed from 0 to -1
|
| 239 |
if (variable_get('invite_maxnum_'. $role_no_space, 0) == 0) {
|
| 240 |
variable_set('invite_maxnum_'. $role_no_space, -1);
|
| 241 |
}
|
| 242 |
|
| 243 |
// Convert settings that have been moved to the permissions table
|
| 244 |
$perms = array();
|
| 245 |
if (variable_get('invite_maxmultiple_'. $role_no_space, 1) != 1) {
|
| 246 |
$perms[] = 'send mass invitations';
|
| 247 |
}
|
| 248 |
if (variable_get('invite_allow_join_delete', 0)) {
|
| 249 |
$perms[] = 'withdraw accepted invitations';
|
| 250 |
}
|
| 251 |
_invite_add_permission($rid, implode(', ', $perms));
|
| 252 |
}
|
| 253 |
$ret[] = update_sql("DELETE from {variable} WHERE name LIKE 'invite_maxmultiple_%%'");
|
| 254 |
|
| 255 |
// Expiry periods changed
|
| 256 |
$expiry = variable_get('invite_expiry', 30);
|
| 257 |
switch ($expiry) {
|
| 258 |
case 5: $expiry = 7; break;
|
| 259 |
case 10: $expiry = 14; break;
|
| 260 |
case 15: $expiry = 14; break;
|
| 261 |
case 20: $expiry = 30; break;
|
| 262 |
case 25: $expiry = 30; break;
|
| 263 |
case 30: $expiry = 30; break;
|
| 264 |
case 45: $expiry = 60; break;
|
| 265 |
case 60: $expiry = 60; break;
|
| 266 |
}
|
| 267 |
variable_set('invite_expiry', $expiry);
|
| 268 |
|
| 269 |
$ret[] = array(
|
| 270 |
'query' => 'The access permissions have been updated by the invite module.',
|
| 271 |
'success' => TRUE
|
| 272 |
);
|
| 273 |
return $ret;
|
| 274 |
}
|
| 275 |
|
| 276 |
/**
|
| 277 |
* Update tokens for security.
|
| 278 |
*/
|
| 279 |
function invite_update_10() {
|
| 280 |
// E-mail template
|
| 281 |
$old = array('[invite-message]', '[inviter]');
|
| 282 |
$new = array('[invite-message-raw]', '[inviter-raw]');
|
| 283 |
_invite_update_tokens('invite_default_mail_template', $old, $new);
|
| 284 |
// E-mail name and subject
|
| 285 |
_invite_update_tokens(array('invite_email_name', 'invite_subject'), '[inviter]', '[inviter-raw]');
|
| 286 |
$ret = array(array(
|
| 287 |
'query' => 'The tokens for the invite module have been successfully updated.',
|
| 288 |
'success' => TRUE
|
| 289 |
));
|
| 290 |
return $ret;
|
| 291 |
}
|
| 292 |
|
| 293 |
/**
|
| 294 |
* Add track permission to all roles that currently have send permission.
|
| 295 |
*/
|
| 296 |
function invite_update_11() {
|
| 297 |
$ret = array();
|
| 298 |
foreach (array_keys(user_roles(0, 'send invitations')) as $rid) {
|
| 299 |
_invite_add_permission($rid, 'track invitations');
|
| 300 |
}
|
| 301 |
$ret[] = array(
|
| 302 |
'query' => 'The access permissions have been updated by the invite module.',
|
| 303 |
'success' => TRUE
|
| 304 |
);
|
| 305 |
return $ret;
|
| 306 |
}
|
| 307 |
|
| 308 |
/**
|
| 309 |
* Change user_register value for enhanced compatibility with LoginToboggan.
|
| 310 |
*/
|
| 311 |
function invite_update_12() {
|
| 312 |
if (variable_get('user_register', 1) == 'inviteonly') {
|
| 313 |
variable_set('user_register', '1-inviteonly');
|
| 314 |
}
|
| 315 |
return array();
|
| 316 |
}
|
| 317 |
|
| 318 |
/**
|
| 319 |
* @{
|
| 320 |
* Invite 2.x updates
|
| 321 |
*/
|
| 322 |
|
| 323 |
/**
|
| 324 |
* 1. Allow multiple invitations for the same e-mail address.
|
| 325 |
* 2. Changed some column names to be more descriptive.
|
| 326 |
* 3. Added a column to flag canceled invites.
|
| 327 |
* 4. Added resent column.
|
| 328 |
*/
|
| 329 |
function invite_update_200() {
|
| 330 |
$ret = array();
|
| 331 |
db_drop_primary_key($ret, 'invite');
|
| 332 |
db_add_index($ret, 'invite', 'email', array('email'));
|
| 333 |
db_change_field($ret, 'invite', 'mid', 'invitee', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
|
| 334 |
db_change_field($ret, 'invite', 'timestamp', 'joined', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
|
| 335 |
db_add_field($ret, 'invite', 'created', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
|
| 336 |
db_add_field($ret, 'invite', 'canceled', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
|
| 337 |
db_add_field($ret, 'invite', 'resent', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
|
| 338 |
return $ret;
|
| 339 |
}
|
| 340 |
|
| 341 |
/**
|
| 342 |
* Revamped notification system.
|
| 343 |
*/
|
| 344 |
function invite_update_201() {
|
| 345 |
$ret = array();
|
| 346 |
db_create_table($ret, 'invite_notifications', array(
|
| 347 |
'description' => t('Stores notifications of inviters.'),
|
| 348 |
'fields' => array(
|
| 349 |
'uid' => array(
|
| 350 |
'description' => t('Stores the user id to be notified (inviter).'),
|
| 351 |
'type' => 'int',
|
| 352 |
'unsigned' => TRUE,
|
| 353 |
'not null' => TRUE,
|
| 354 |
'default' => 0,
|
| 355 |
),
|
| 356 |
'invitee' => array(
|
| 357 |
'description' => t('Stores the user id of the invitee.'),
|
| 358 |
'type' => 'int',
|
| 359 |
'unsigned' => TRUE,
|
| 360 |
'not null' => TRUE,
|
| 361 |
'default' => 0,
|
| 362 |
),
|
| 363 |
),
|
| 364 |
'indexes' => array(
|
| 365 |
'uid' => array('uid'),
|
| 366 |
)
|
| 367 |
));
|
| 368 |
// Convert old data
|
| 369 |
$ret[] = update_sql("INSERT INTO {invite_notifications} (uid, invitee) SELECT uid, invitee FROM {invite} WHERE joined <> 0 AND received = 0");
|
| 370 |
// Drop old column
|
| 371 |
db_drop_field($ret, 'invite', 'received');
|
| 372 |
return $ret;
|
| 373 |
}
|
| 374 |
|
| 375 |
/**
|
| 376 |
* Optimize index of notification table.
|
| 377 |
*/
|
| 378 |
function invite_update_202() {
|
| 379 |
$ret = array();
|
| 380 |
db_drop_index($ret, 'invite_notifications', 'uid');
|
| 381 |
db_add_unique_key($ret, 'invite_notifications', 'uid_invitee', array('uid', 'invitee'));
|
| 382 |
return $ret;
|
| 383 |
}
|
| 384 |
|
| 385 |
/**
|
| 386 |
* Update variable names to use role id instead of translated role name.
|
| 387 |
* @see #322748
|
| 388 |
*/
|
| 389 |
function invite_update_203() {
|
| 390 |
$ret = array();
|
| 391 |
_invite_update_role_name_to_id('invite_maxnum');
|
| 392 |
_invite_update_role_name_to_id('invite_target_role');
|
| 393 |
return $ret;
|
| 394 |
}
|
| 395 |
|