From 939f336489ae796a81e97fddc417ab243918d3f2 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Mon, 16 Jul 2018 17:22:42 +1000 Subject: [PATCH] test main csv output as excel --- booking.reports.inc | 123 +++++++++++++++++++++++++++----------------- 1 file changed, 77 insertions(+), 46 deletions(-) diff --git a/booking.reports.inc b/booking.reports.inc index 023a691..ef1eb40 100644 --- a/booking.reports.inc +++ b/booking.reports.inc @@ -701,33 +701,40 @@ function ucname($string) { function booking_csv_report() { global $event; - $name = 'bookings-' . format_date(time(), 'custom', 'Y-m-d-His'); - $filename = file_directory_temp() . '/' . $name; + module_load_include('php', 'booking', 'libraries/xlsxwriter.class'); + + $filename = 'bookings-' . format_date(time(), 'custom', 'Y-m-d-His') . '.xlsx'; + // List of style options at https://github.com/mk-j/PHP_XLSXWriter/issues/198 + $header_style = array( 'border'=>'bottom','border-style'=>'thin', 'border-color'=>'#000000', + 'valign'=>'top', 'font-style' => 'bold','font' => 'Calibri'); + $row_style = array('font' => 'Calibri'); + + //$filename = file_directory_temp() . '/' . $name; $csv = ''; $delimiter = ','; $enclosure = '"'; - $encloseAll = true; + $encloseAll = false; $nullToMysqlNull = true; $delimiter_esc = preg_quote($delimiter, '/'); $enclosure_esc = preg_quote($enclosure, '/'); $readinggroup_id = ""; - module_load_include('php', 'booking', 'libraries/xlsxwriter.class'); - - //$readinggroup = "session" . variable_get('booking_readinggroup_id','7'); - + $studygroup_descriptions = array(); + $header_array = array(); + $rows = array(); + //calculate fields to ignore in the output csv file $builtin_fields_to_skip = array('booking_eventid'); $custom_fields_to_skip = explode(";", variable_get('booking_csv_exclude_fields', '')); $fields_to_skip = array_merge($builtin_fields_to_skip, $custom_fields_to_skip); //keep a list of any fields that we need to handle as dates + $date_fields = array('booking_dob'); $datetime_fields = array('booking_outflight_origin_ts', 'booking_outflight_destination_ts', 'booking_rtrnflight_origin_ts', 'booking_rtrnflight_destination_ts', 'booking_timestamp', 'booking_flight_datetime_inbound', 'booking_flight_datetime_outbound'); $number_only_fields = array('booking_postcode', 'booking_mobile', 'booking_phone', 'booking_guardian_phone', 'booking_guardian_phone_alt'); //look up the titles of the study groups and add to array for updating in the header - $studygroup_descriptions = array(); $studygroups_query = db_query("SELECT * FROM {booking_studygroup_list} WHERE booking_eventid = :eid", array(':eid' => $event->eid)); $studygroups = $studygroups_query->fetchAllAssoc('sid'); @@ -753,35 +760,50 @@ function booking_csv_report() { //watchdog('booking', "CSV raw data: @info", array('@info' => var_export($result, TRUE))); //write the header based on the first result - $header_array = array(); foreach (reset($result) as $key => $value) { if (in_array($key, $fields_to_skip)) { continue; } - //Replace headings for study group sessions and roles with the name of the study group rather than just sessionN and sessionN_role etc + // Replace headings for study group sessions and roles with the name of the study group rather than just sessionN and sessionN_role etc if (variable_get('booking_friendly_csv_groupnames','0') == 1 && array_key_exists($key, $studygroup_descriptions)) { - $header_array[] = $studygroup_descriptions[$key]; + //$header_array[] = $studygroup_descriptions[$key]; + $heading = $studygroup_descriptions[$key]; + $header_array[$heading] = 'string'; continue; } - $header_array[] = $key; - //add in a special column for a processed version of the date of birth - if (strcmp($key,"booking_dob") == 0) { - $header_array[] = "booking_dob_processed"; + // Apply data formatting as per https://github.com/mk-j/PHP_XLSXWriter + if (in_array($key, $datetime_fields)) { + $header_array[$key] = 'datetime'; + } + elseif (in_array($key, $date_fields)) { + $header_array[$key] = 'date'; + } + elseif (in_array($key, $number_only_fields)) { + $header_array[$key] = 'integer'; + } + else { + $header_array[$key] = 'string'; } + //add in a special column for a processed version of the date of birth + if (strcmp($key,"booking_dob") == 0) { + $header_array["booking_dob_processed"] = 'string'; + } //add in a calculated field for amount owing if (strcmp($key, "nid") == 0) { - $header_array[] = "booking_amount_owing_gross"; - $header_array[] = "booking_amount_owing_net"; + $header_array["booking_amount_owing_gross"] = 'dollar'; + $header_array["booking_amount_owing_net"] = 'dollar'; } } + + watchdog('booking_debug', "
CSV report headers\n@info
", array('@info' => print_r($header_array, true))); - $header = implode( $delimiter, $header_array ); + //$header = implode( $delimiter, $header_array ); + //$csv .= $header . "\n"; //watchdog('booking', "CSV header: @info", array('@info' => var_export($header_array, TRUE))); - //@fwrite($handle, $header . "\n"); - $csv .= $header . "\n"; + //each record foreach ($result as $record) { //watchdog('booking', "CSV raw data entry: @info", array('@info' => var_export($record, TRUE))); @@ -873,38 +895,47 @@ function booking_csv_report() { } // 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 { + //if ( $encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $value ) ) { + // $output[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $value) . $enclosure; + //} + //else { $output[] = $field; - } + //} + } // End of record processing + $rows[] = $output; - } - - $row = implode($delimiter, $output) . "\n"; - - //@fwrite($handle, $row); - $csv .= $row; - //$index++; + //$row = implode($delimiter, $output) . "\n"; + //$csv .= $row; } - //@fclose($handle); + watchdog('booking_debug', "
CSV report spreadsheet rows\n@info
", array('@info' => print_r( $rows, true))); + + // Create headers for Excel spreadsheet + header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"'); + header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + header('Content-Transfer-Encoding: binary'); + header('Cache-Control: must-revalidate'); + header('Pragma: public'); + + $sheetname = $event->booking_eventname; + $writer = new XLSXWriter(); + $writer->setAuthor($event->booking_eventname); + //Add the header row + $writer->writeSheetHeader($sheetname, $column_headings, $header_style); + + //Add the data + foreach($rows as $row) { + $writer->writeSheetRow($sheetname, $row, $row_style); + } + + $writer->writeToStdOut(); + exit(0); //see http://stackoverflow.com/questions/4348802/how-can-i-output-a-utf-8-csv-in-php-that-excel-will-read-properly // but none of these options seem to work - drupal_add_http_header("Content-type", "application/octet-stream; charset=utf-8"); - //drupal_add_http_header("Content-type", "application/octet-stream; charset=UTF-16LE"); - //drupal_add_http_header("Content-Type: application/vnd.ms-excel"); - drupal_add_http_header("Content-Disposition", "attachment; filename=" . $name . ".csv"); - - // @readfile($filename); - //print chr(255) . chr(254); - //print 'sep=,' . "\n"; - print $csv; - //print mb_convert_encoding($csv, 'UTF-16LE', 'UTF-8'); - - //@unlink($filename); - exit(0); + //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); }