This commit is contained in:
2017-06-29 14:36:45 +10:00
parent 381d61b85d
commit b109cb792f

View File

@@ -127,5 +127,52 @@ function booking_getToken($length) {
* Function to generate csv of early access codes for the current event
*/
function booking_earlyaccess_csv_report() {
global $event;
$data = 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_regn_earlyaccess_codes', 'c');
$codelist_query->condition('c.booking_eventid', $event->eid, '=')
->fields('p')
->orderBy('cid');
$result = $codelist_query->execute();
//calculate the header row for CSV
$header = implode( $delimiter, array_keys($data));
$csv .= $header . "\n";
//add the records
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);
}
}