233 lines
7.0 KiB
PHP
233 lines
7.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Admin pages for managing early access codes
|
|
*/
|
|
|
|
// @todo - list all early access codes for the current event, whether they're available, and who used them if not
|
|
|
|
|
|
/**
|
|
* Function to summarise information about early access codes
|
|
*/
|
|
function booking_earlyaccess_admin() {
|
|
global $event;
|
|
$output = "";
|
|
$rows = array();
|
|
$attributes = array('style' => 'max-width:60%', 'id' => 'sort-table');
|
|
$prefix = t("<h2>Early Access Codes</h2>");
|
|
|
|
$header = array(
|
|
'booking_earlyaccess_code' => array('data' => t('Code'), 'field' => 'c.booking_earlyaccess_code', 'sort' => 'asc'),
|
|
'booking_earlyaccess_code_avail' => array('data' => t('Available?'), 'field' => 'c.booking_earlyaccess_code_avail'),
|
|
'booking_firstname' => array('data' => t('First Name'), 'field' => 'p.booking_firstname'),
|
|
'booking_lastname' => array('data' => t('Last Name'), 'field' => 'p.booking_lastname'),
|
|
);
|
|
|
|
//get a list of all the early access codes for this event
|
|
$codelist_query = db_select('booking_earlyaccess_codes', 'c');
|
|
$codelist_query->leftJoin('booking_person', 'p', 'c.cid = p.booking_earlyaccess_code_id');
|
|
$codelist_query->condition('c.booking_eventid', $event->eid, '=')
|
|
->fields('c')
|
|
->fields('p')
|
|
->orderBy('cid');
|
|
$result = $codelist_query->execute();
|
|
|
|
//add results to array
|
|
foreach($result as $data) {
|
|
$rows[] = array(
|
|
'data' => array(
|
|
$data->booking_earlyaccess_code,
|
|
$data->booking_earlyaccess_code_avail == 'Y' ? 'Yes' : 'No',
|
|
$data->booking_firstname,
|
|
$data->booking_lastname,
|
|
),
|
|
);
|
|
}
|
|
|
|
//theme output
|
|
$output = array (
|
|
'first_para' => array (
|
|
'#type' => 'markup',
|
|
'#markup' => $prefix,
|
|
),
|
|
'table' => array (
|
|
'#theme' => 'table',
|
|
'#header' => $header,
|
|
'#rows' => $rows,
|
|
'#attributes' => $attributes,
|
|
//'#sticky' => FALSE,
|
|
)
|
|
);
|
|
|
|
//return markup array
|
|
return $output;
|
|
}
|
|
|
|
/**
|
|
* Function to generate early access codes for the current event
|
|
*/
|
|
function booking_earlyaccess_generate_codes() {
|
|
global $event;
|
|
$num_codes_to_add = 0;
|
|
$num_codes_available = 0;
|
|
$code_length = 7;
|
|
|
|
//determine number of currently available access codes for this event
|
|
$codecount_query = db_query("SELECT count(*) as num_codes FROM {booking_earlyaccess_codes} where booking_eventid = :eventid and booking_earlyaccess_code_avail = 'Y'",
|
|
array(':eventid' => $event->eid))
|
|
->fetchObject();
|
|
$num_codes_available = $codecount_query->num_codes;
|
|
//get the number of codes we need to have available from variable_get('booking_earlyaccess_codes_count')
|
|
$num_codes_to_add = variable_get('booking_earlyaccess_codes_count', 0) - $num_codes_available;
|
|
//add required number of codes to database table
|
|
watchdog('booking_debug', "Creating $num_codes_to_add early access codes.");
|
|
|
|
//use the multi-insert query type at https://drupal.org/node/310079
|
|
$insert_query = db_insert('booking_earlyaccess_codes')
|
|
->fields(array(
|
|
'booking_eventid',
|
|
'booking_earlyaccess_code',
|
|
'booking_earlyaccess_code_avail',
|
|
));
|
|
|
|
for ($i = 0; $i < $num_codes_to_add; $i++) {
|
|
$insert_query->values(array(
|
|
'booking_eventid' => $event->eid,
|
|
'booking_earlyaccess_code' => booking_getToken($code_length),
|
|
'booking_earlyaccess_code_avail' => 'Y',
|
|
));
|
|
}
|
|
$insert_query->execute();
|
|
return t("Created $num_codes_to_add early access codes.");
|
|
}
|
|
|
|
/**
|
|
* Function to generate csv of early access codes for the current event
|
|
*/
|
|
function booking_earlyaccess_csv_report() {
|
|
global $event;
|
|
$data = array();
|
|
$header_array = array();
|
|
//set options for the CSV file
|
|
$name = 'bookings-earlyaccesscodes-' . format_date(time(), 'custom', 'Y-m-d-His');
|
|
$filename = file_directory_temp() . '/' . $name;
|
|
$csv = '';
|
|
$delimiter = ',';
|
|
$enclosure = '"';
|
|
$encloseAll = true;
|
|
$nullToMysqlNull = true;
|
|
$delimiter_esc = preg_quote($delimiter, '/');
|
|
$enclosure_esc = preg_quote($enclosure, '/');
|
|
|
|
//get a list of all the early access codes for this event
|
|
$codelist_query = db_select('booking_earlyaccess_codes', 'c');
|
|
$codelist_query->condition('c.booking_eventid', $event->eid, '=')
|
|
->fields('c', array('booking_earlyaccess_code', 'booking_earlyaccess_code_avail'))
|
|
->orderBy('cid');
|
|
$result = $codelist_query->execute();
|
|
|
|
foreach ($result as $record) {
|
|
foreach ($record as $key => $value) {
|
|
$header_array[] = $key;
|
|
}
|
|
break;
|
|
}
|
|
|
|
//calculate the header row for CSV
|
|
$header = implode($delimiter, $header_array);
|
|
$csv .= $header . "\n";
|
|
|
|
//add the records
|
|
reset($result);
|
|
foreach ($result as $record) {
|
|
$output = array();
|
|
//each keypair in the record
|
|
foreach ($record as $key => $value) {
|
|
// Enclose fields containing $delimiter, $enclosure or whitespace
|
|
if ($encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $value ) ) {
|
|
$output[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $value) . $enclosure;
|
|
}
|
|
else {
|
|
$output[] = $field;
|
|
}
|
|
}
|
|
$row = implode($delimiter, $output) . "\n";
|
|
$csv .= $row;
|
|
}
|
|
|
|
//output the CSV to the browser
|
|
drupal_add_http_header("Content-type", "application/octet-stream; charset=utf-8");
|
|
drupal_add_http_header("Content-Disposition", "attachment; filename=" . $name . ".csv");
|
|
print $csv;
|
|
exit(0);
|
|
}
|
|
|
|
/**
|
|
* Function to calculate unique code
|
|
* @see https://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string
|
|
* Requires PHP7
|
|
*/
|
|
function booking_getToken($length) {
|
|
$token = "";
|
|
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
//$codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
|
|
$codeAlphabet.= "0123456789";
|
|
$max = strlen($codeAlphabet); // edited
|
|
|
|
if (!function_exists('random_int')) {
|
|
function random_int($min, $max) {
|
|
if (!function_exists('mcrypt_create_iv')) {
|
|
trigger_error(
|
|
'mcrypt must be loaded for random_int to work',
|
|
E_USER_WARNING
|
|
);
|
|
return null;
|
|
}
|
|
|
|
if (!is_int($min) || !is_int($max)) {
|
|
trigger_error('$min and $max must be integer values', E_USER_NOTICE);
|
|
$min = (int)$min;
|
|
$max = (int)$max;
|
|
}
|
|
|
|
if ($min > $max) {
|
|
trigger_error('$max can\'t be lesser than $min', E_USER_WARNING);
|
|
return null;
|
|
}
|
|
|
|
$range = $counter = $max - $min;
|
|
$bits = 1;
|
|
|
|
while ($counter >>= 1) {
|
|
++$bits;
|
|
}
|
|
|
|
$bytes = (int)max(ceil($bits/8), 1);
|
|
$bitmask = pow(2, $bits) - 1;
|
|
|
|
if ($bitmask >= PHP_INT_MAX) {
|
|
$bitmask = PHP_INT_MAX;
|
|
}
|
|
|
|
do {
|
|
$result = hexdec(
|
|
bin2hex(
|
|
mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM)
|
|
)
|
|
) & $bitmask;
|
|
} while ($result > $range);
|
|
|
|
return $result + $min;
|
|
}
|
|
}
|
|
|
|
for ($i=0; $i < $length; $i++) {
|
|
$token .= $codeAlphabet[random_int(0, $max-1)];
|
|
}
|
|
|
|
return $token;
|
|
}
|
|
|