function regcode_save_code in Registration codes 6
Same name and namespace in other branches
- 5.3 regcode_api.inc.php \regcode_save_code()
Save given code array to a record in the db
Parameters
$code: An array of code data fields (dates must be timestamps)
$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
2 calls to regcode_save_code()
- regcode_admin_create_submit in ./
regcode.admin.php - Submit handler for regcode_admin_create
- regcode_admin_import_submit in ./
regcode.admin.php - Handle the processing of a submitted import form
File
- ./
regcode.api.php, line 126 - A generic set of functions for interacting with and creating regcodes
Code
function regcode_save_code($code, $action = 'overwrite') {
// Sanity check
if (empty($code) || empty($code['code'])) {
return FALSE;
}
if ($action == 'overwrite') {
db_query("DELETE FROM {regcode} WHERE code = '%s'", $code['code']);
}
// Format data
$format = 'Y-m-d h:j:s';
$begins = empty($code['begins']) ? 'NULL' : '"' . date($format, $code['begins']) . '"';
$expires = empty($code['expires']) ? 'NULL' : '"' . date($format, $code['expires']) . '"';
$maxuses = empty($code['maxuses']) ? 'NULL' : intval($code['maxuses']);
// Insert
$sql = "INSERT INTO {regcode} (created,begins,expires,category,code,is_active,maxuses)\n VALUES (NOW(), {$begins}, {$expires}, '%s', '%s', %d, %d)";
$result = db_query($sql, empty($code['category']) ? 'default' : check_plain($code['category']), check_plain($code['code']), $code['is_active'], $maxuses);
return $result;
}