View source
<?php
function regcode_uninstall() {
variable_del('regcode_optional');
variable_del('regcode_fieldtitle');
variable_del('regcode_fielddescription');
variable_del('regcode_codes');
foreach (user_roles() as $rid => $role_name) {
variable_del("regcode_codes_{$rid}");
}
db_query("DROP TABLE IF EXISTS {regcode}");
watchdog('RegistrationCode', 'regcodes module uninstalled');
}
function regcode_install() {
regcode_create_table();
watchdog('RegistrationCode', 'regcodes module installed');
}
function regcode_update_5200() {
$results[] = regcode_create_table();
regcode_convert_codes();
watchdog('RegistrationCode', 'regcodes module updated to v5.x-2.0');
return $results;
}
function regcode_create_table() {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$result = db_query("\n CREATE TABLE IF NOT EXISTS {regcode} (\n `code` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,\n `available` INT NOT NULL DEFAULT '1',\n `reuse` INT NOT NULL DEFAULT '0',\n `used_count` INT NOT NULL DEFAULT '0',\n `uid` INT( 10 ) UNSIGNED NULL ,\n `rid` INT( 10 ) UNSIGNED NULL ,\n `created` INT( 11 ) NOT NULL DEFAULT '0',\n `used` INT( 11 ) NOT NULL DEFAULT '0',\n `expire` INT( 11 ) NOT NULL DEFAULT '0',\n `revoke` INT( 11 ) NOT NULL DEFAULT '0',\n `info` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,\n PRIMARY KEY ( `code` ),\n INDEX ( `uid` ),\n INDEX ( `rid` , `expire` )\n )\n ");
watchdog('RegistrationCode', 'regcodes table created: ' . ($result['success'] ? 'successful' : 'FAILED'));
break;
}
return $result;
}
function regcode_convert_codes() {
foreach (user_roles() as $rid => $role_name) {
$count = regcode_convert_codes_query(variable_get("regcode_codes_{$rid}", ''), $rid);
watchdog('RegistrationCode', "converted {$count} codes for role: {$role_name}");
}
$count = regcode_convert_codes_query(variable_get("regcode_codes", ''), 0);
watchdog('RegistrationCode', "converted {$count} role-independent codes");
}
function regcode_convert_codes_query($codes, $rid) {
if (empty($codes)) {
return 0;
}
$codes = preg_split('/[\\n\\r]+/', $codes, $max_lines = 100000);
$rid = intval($rid);
$time = time();
$count = 0;
foreach ($codes as $code) {
$count++;
db_query("\n INSERT DELAYED INTO {regcode}\n (`code`, `available`, `uid`, `rid`, `created`, `expire`, `revoke`, `info`)\n VALUES ('%s', '1', NULL, " . ($rid ? "%d" : "NULL") . ", UNIX_TIMESTAMP(), '0', '0', 'converted');\n ", $code, $rid);
}
return $count;
}