format excel headings properly
This commit is contained in:
@@ -540,8 +540,6 @@ function booking_varietysessions_csv_report($timeslot_id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//set options for the CSV file
|
//set options for the CSV file
|
||||||
$name = 'bookings-timeslot-' . $timeslot_id . '-variety-sessions-' . format_date(time(), 'custom', 'Y-m-d-His');
|
|
||||||
$filename = file_directory_temp() . '/' . $name;
|
|
||||||
$csv = '';
|
$csv = '';
|
||||||
$delimiter = ',';
|
$delimiter = ',';
|
||||||
$enclosure = '"';
|
$enclosure = '"';
|
||||||
@@ -566,7 +564,7 @@ function booking_varietysessions_csv_report($timeslot_id) {
|
|||||||
|
|
||||||
//watchdog('booking_debug', 'booking_varietysessions_csv_report session members: <pre>@info</pre>', array('@info' => print_r( $session_members, true)));
|
//watchdog('booking_debug', 'booking_varietysessions_csv_report session members: <pre>@info</pre>', array('@info' => print_r( $session_members, true)));
|
||||||
|
|
||||||
//generate the row data
|
// Organise people by session ID
|
||||||
foreach ($session_members as $member) {
|
foreach ($session_members as $member) {
|
||||||
$session_ids = drupal_json_decode($member->booking_variety_ids);
|
$session_ids = drupal_json_decode($member->booking_variety_ids);
|
||||||
//watchdog('booking_debug', 'booking_varietysessions_csv_report person session ids: <pre>@info</pre>', array('@info' => print_r( $session_ids, true)));
|
//watchdog('booking_debug', 'booking_varietysessions_csv_report person session ids: <pre>@info</pre>', array('@info' => print_r( $session_ids, true)));
|
||||||
@@ -585,7 +583,7 @@ function booking_varietysessions_csv_report($timeslot_id) {
|
|||||||
}
|
}
|
||||||
//watchdog('booking_debug', "<pre>Study Group CSV Report\n@info</pre>", array('@info' => print_r( $data_array, true)));
|
//watchdog('booking_debug', "<pre>Study Group CSV Report\n@info</pre>", array('@info' => print_r( $data_array, true)));
|
||||||
|
|
||||||
//calculate the CSV layout
|
// Calculate column headings and spreadsheet layout
|
||||||
$header_array = array_keys($data);
|
$header_array = array_keys($data);
|
||||||
$maximums = array();
|
$maximums = array();
|
||||||
$column_headings = array();
|
$column_headings = array();
|
||||||
@@ -595,19 +593,14 @@ function booking_varietysessions_csv_report($timeslot_id) {
|
|||||||
$heading = $variety_sessions[$column]->booking_variety_descrip;
|
$heading = $variety_sessions[$column]->booking_variety_descrip;
|
||||||
//enclose $heading if necessary
|
//enclose $heading if necessary
|
||||||
if ( $encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $heading ) ) {
|
if ( $encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $heading ) ) {
|
||||||
$column_headings[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $heading) . $enclosure;
|
$heading = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $heading) . $enclosure;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
$column_headings[] = $heading;
|
// Adjusted for excel, assume each column is just a string
|
||||||
}
|
$column_headings[] = array($heading => 'string');
|
||||||
//$column_headings[] = $variety_sessions[$column]->booking_variety_descrip;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//add the column headings to the CSV
|
// Calculate each row based on column headings
|
||||||
$header = implode($delimiter, $column_headings);
|
|
||||||
$csv .= $header . "\n";
|
|
||||||
|
|
||||||
//generate each row for the CSV
|
|
||||||
for ($i = 0; $i < max($maximums); $i++) {
|
for ($i = 0; $i < max($maximums); $i++) {
|
||||||
$output = array();
|
$output = array();
|
||||||
foreach ($header_array as $column) {
|
foreach ($header_array as $column) {
|
||||||
@@ -622,17 +615,13 @@ function booking_varietysessions_csv_report($timeslot_id) {
|
|||||||
}
|
}
|
||||||
} //loop through columns
|
} //loop through columns
|
||||||
$rows[] = $output;
|
$rows[] = $output;
|
||||||
$row = implode($delimiter, $output) . "\n";
|
//$row = implode($delimiter, $output) . "\n";
|
||||||
$csv .= $row;
|
//$csv .= $row;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//ini_set('display_errors', 0);
|
|
||||||
//ini_set('log_errors', 1);
|
|
||||||
//error_reporting(E_ALL & ~E_NOTICE);
|
|
||||||
|
|
||||||
// Create headers for Excel spreadsheet
|
// Create headers for Excel spreadsheet
|
||||||
$filename = $name . ".xlsx";
|
$filename = 'bookings-timeslot-' . $timeslot_id . '-variety-sessions-' . format_date(time(), 'custom', 'Y-m-d-His') . ".xlsx";
|
||||||
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
|
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
|
||||||
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||||
header('Content-Transfer-Encoding: binary');
|
header('Content-Transfer-Encoding: binary');
|
||||||
@@ -643,7 +632,7 @@ function booking_varietysessions_csv_report($timeslot_id) {
|
|||||||
$writer = new XLSXWriter();
|
$writer = new XLSXWriter();
|
||||||
$writer->setAuthor($event->booking_eventname);
|
$writer->setAuthor($event->booking_eventname);
|
||||||
//Add the header row
|
//Add the header row
|
||||||
$writer->writeSheetRow($sheetname, $column_headings);
|
$writer->writeSheetHeader($sheetname, $column_headings);
|
||||||
|
|
||||||
//Add the data
|
//Add the data
|
||||||
foreach($rows as $row) {
|
foreach($rows as $row) {
|
||||||
@@ -652,12 +641,6 @@ function booking_varietysessions_csv_report($timeslot_id) {
|
|||||||
|
|
||||||
$writer->writeToStdOut();
|
$writer->writeToStdOut();
|
||||||
exit(0);
|
exit(0);
|
||||||
|
|
||||||
//output the CSV to the browser
|
|
||||||
drupal_add_http_header("Content-type", "text/csv; charset=utf-8");
|
|
||||||
drupal_add_http_header("Content-Disposition", "attachment; filename=" . $name . ".csv");
|
|
||||||
print $csv;
|
|
||||||
exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user