You are here

regcode.install in Registration codes 5.3

File

regcode.install
View source
<?php

/**
 * Implementation of hook_uninstall().
 */
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');
}

/**
 * Implementation of hook_install().
 */
function regcode_install() {
  regcode_create_table();
  watchdog('RegistrationCode', 'regcodes module installed');
}

/**
 * Implementation of hook_update().
 */
function regcode_update_5200() {
  $results[] = regcode_create_table();
  regcode_convert_codes();
  watchdog('RegistrationCode', 'regcodes module updated to v5.x-2.0');
  return $results;
}

/**
 * Helper function to create table for db storage
 */
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;
}

/**
 * Helper function to iterate through code-variables and initiating code conversion to database-storage
 */
function regcode_convert_codes() {

  // get codes and convert them...
  // - for role-specific 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}");
  }

  // - for role-independent codes:
  $count = regcode_convert_codes_query(variable_get("regcode_codes", ''), 0);
  watchdog('RegistrationCode', "converted {$count} role-independent codes");

  // delete old storage variables:
  // variable_del('regcode_codes');
  // foreach(user_roles() as $rid => $role_name) variable_del("regcode_codes_$rid");
}

/**
 * Helper function to actually convert existing codes from variable-storage to database-storage
 *
 * @param codes
 *  The string containing the codes to insert into the db (one code per line)
 * @param rid
 *  The role the given codes will grant
 * @return
 *  The rendered output for the requested admin page
 */
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;
}

Functions

Namesort descending Description
regcode_convert_codes Helper function to iterate through code-variables and initiating code conversion to database-storage
regcode_convert_codes_query Helper function to actually convert existing codes from variable-storage to database-storage
regcode_create_table Helper function to create table for db storage
regcode_install Implementation of hook_install().
regcode_uninstall Implementation of hook_uninstall().
regcode_update_5200 Implementation of hook_update().