From b109cb792f3e8f8197888a5d5cbca5ac26dad948 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Thu, 29 Jun 2017 14:36:45 +1000 Subject: [PATCH] fix --- booking.earlyaccess_admin.inc | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/booking.earlyaccess_admin.inc b/booking.earlyaccess_admin.inc index 6eafde9..1dc30df 100644 --- a/booking.earlyaccess_admin.inc +++ b/booking.earlyaccess_admin.inc @@ -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); +} } \ No newline at end of file