You are here

public function webform_exporter_excel_xlsx::post_process in Webform 7.4

Allow final processing of the results.

Parameters

$results: An array of result data, including:

  • node: The node whose results are being downloaded.
  • file_name: The full file path of the generated file.
  • row_count: The final number of rows in the generated file.

Overrides webform_exporter::post_process

File

includes/exporters/webform_exporter_excel_xlsx.inc, line 148

Class

webform_exporter_excel_xlsx
This exporter creates an XLSX file readable by newer versions of Excel.

Code

public function post_process(&$results) {

  // Our download file is currently a single XML sheet file. We need to add
  // the peripheral XML files to make this into a XLSX directory, then zip it.
  $file_uri = $results['file_name'];
  $zip_uri = _webform_export_tempname();

  // ZipArchive does not support stream wrappers, convert to filesystem path.
  $zip_filepath = drupal_realpath($zip_uri);
  $file_filepath = drupal_realpath($file_uri);
  $zip = new ZipArchive();
  if ($zip
    ->open($zip_filepath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {

    // Create a bare-bones Office Open XML format directory structure. This is
    // based on the sample simple XLSX file at
    // http://blogs.msdn.com/b/chrisrae/archive/2011/08/18/creating-a-simple-xlsx-from-scratch-using-the-open-xml-sdk.aspx
    $parts = $this
      ->xlsx_parts();
    foreach ($parts as $file_name => $file_contents) {
      if (empty($file_contents)) {
        $zip
          ->addEmptyDir($file_name);
      }
      else {
        $zip
          ->addFromString($file_name, $file_contents);
      }
    }

    // Add the actual export to the zip.
    $zip
      ->addEmptyDir('xl/worksheets');
    $zip
      ->addFile($file_filepath, 'xl/worksheets/sheet1.xml');
    $zip
      ->close();

    // Switch the results file name to the new zip (xlsx) file.
    unlink($file_uri);
    if (!@rename($zip_uri, $file_uri)) {

      // The file could not be renamed, probably due to different stream
      // wrappers during drush wfx execution.
      copy($zip_uri, $file_uri);
      unlink($zip_uri);
    }
  }
}