You are here

class webform_exporter_delimited in Webform 7.3

Same name and namespace in other branches
  1. 5.2 webform_export.inc \webform_exporter_delimited
  2. 6.3 includes/webform.export.inc \webform_exporter_delimited
  3. 6.2 webform_export.inc \webform_exporter_delimited
  4. 7.4 includes/exporters/webform_exporter_delimited.inc \webform_exporter_delimited

Hierarchy

Expanded class hierarchy of webform_exporter_delimited

1 string reference to 'webform_exporter_delimited'
webform_webform_exporters in includes/webform.export.inc
Implements hook_webform_exporters().

File

includes/webform.export.inc, line 116
Provides several different handlers for exporting webform results.

View source
class webform_exporter_delimited extends webform_exporter {
  public $delimiter;

  /**
   * Constructor.
   *
   * @param array $options
   *   An associative array of options.
   */
  public function __construct($options) {
    $this->delimiter = isset($options['delimiter']) ? $options['delimiter'] : ',';

    // Convert tabs.
    if ($this->delimiter == '\\t') {
      $this->delimiter = "\t";
    }
  }

  /**
   * PHP 4 constructor compatibility shim.
   *
   * @deprecated in 7.x-3.30 and is removed from 7.x-4.0. Use PHP 5 class
   * instantiation instead.
   * @see https://www.drupal.org/project/webform/issues/3011149
   */
  public function webform_exporter_delimited($options) {
    self::__construct($options);
  }

  /**
   *
   */
  public function bof(&$file_handle) {
    $output = '';

    // Include at BOM at the beginning of the file for Little Endian.
    // This makes tab-separated imports work correctly in MS Excel.
    if (function_exists('mb_convert_encoding') && $this->delimiter == "\t") {
      $output = chr(255) . chr(254);
    }
    @fwrite($file_handle, $output);
  }

  /**
   *
   */
  public function add_row(&$file_handle, $data) {
    foreach ($data as $key => $value) {

      // Escape inner quotes and wrap all contents in new quotes.
      $data[$key] = '"' . str_replace('"', '""', $data[$key]) . '"';

      // Remove <script> tags, which mysteriously cause Excel not to import.
      $data[$key] = preg_replace('!<(/?script.*?)>!', '[$1]', $data[$key]);
    }
    $row = implode($this->delimiter, $data) . "\n";
    if (function_exists('mb_convert_encoding')) {
      $row = mb_convert_encoding($row, 'UTF-16LE', 'UTF-8');
    }
    @fwrite($file_handle, $row);
  }

  /**
   *
   */
  public function set_headers($filename) {
    parent::set_headers($filename);

    // Convert tabs.
    if ($this->delimiter == "\t") {
      $extension = 'tsv';
      $content_type = 'text/tab-separated-values';
    }
    else {
      $extension = 'csv';
      $content_type = 'text/csv';
    }
    drupal_add_http_header('Content-Type', $content_type);
    drupal_add_http_header('Content-Disposition', "attachment; filename={$filename}.{$extension}");
  }

}

Members