You are here

function regcode_save_code in Registration codes 5.3

Same name and namespace in other branches
  1. 6 regcode.api.php \regcode_save_code()

Save given code array to a record in the db

Parameters

$code: An array of code data fields, containing the fields: code - code identifier string (unique) available - is code available for user registration? ('1' = available) reuse - is code re-usable for many user registrations? ('1' = re-use) uid - user id to assign this code to a particular user (and thus mark is as "taken" by that user) rid - role id of the role this code will grant to the user that registers with it created - unix timestamp of the date this code has been created/added used - unix timestamp of the date this code has been used for registration revoke - unix timestamp of the date this code and its granted role will be revoked from the user that used it (not implemented) expire - unix timestamp of the date this code's availability for registration will expire (not touching already used codes) info - string (up to 255 characters) for any arbitrary information data

$action: Action to perform when saving the code: overwrite - overwrite any existing code with same code identifier skip - skip and do not save code if there is any existing code with same code identifier

  • - "

Return value

An array list of code data arrays

1 call to regcode_save_code()
regcode_import_code in ./regcode_api.inc.php
Import a single codes from a single ine string (plain one-field or CSV)

File

./regcode_api.inc.php, line 173
regcode_api.inc.php contains general low-level functions for the registration-code module, for tasks like

Code

function regcode_save_code($code, $action = 'overwrite') {
  if (empty($code)) {
    return FALSE;
  }
  if (empty($code['code'])) {
    return FALSE;
  }

  // empty template
  $row = array(
    'code' => '',
    'available' => 1,
    'reuse' => 0,
    'uid' => 'NULL',
    'rid' => 'NULL',
    'created' => $_SERVER['REQUEST_TIME'],
    'used' => 0,
    'revoke' => 0,
    'expire' => 0,
    'info' => 'NULL',
  );

  // fill row template with filtered and escaped input for fields which actually exist
  foreach ($code as $key => $value) {
    if (isset($row[$key])) {
      $row[$key] = "'" . db_escape_string(check_plain($value)) . "'";
    }
  }

  // optionally delete old record
  if ($action == 'overwrite') {
    db_query("DELETE FROM {regcode} WHERE code = '%s' LIMIT 1", $code['code']);
  }

  // insert record into db
  $sql = "INSERT INTO {regcode} (`" . implode('`, `', array_keys($row)) . "`) VALUES (" . implode(", ", $row) . ")";

  // implode(',', $placeholders)
  $result = db_query($sql);

  // , $row);
  return $result;
}