Project

General

Profile

Paste
Download (1.58 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / webform / includes / exporters / webform_exporter_delimited.inc @ 01f36513

1
<?php
2

    
3
/**
4
 * Webform exporter for creating CSV/TSV delimited files.
5
 */
6
class webform_exporter_delimited extends webform_exporter {
7
  public $line_ending;
8
  public $delimiter;
9

    
10
  /**
11
   * {@inheritdoc}
12
   */
13
  public function __construct($options) {
14
    $this->line_ending = webform_variable_get('webform_csv_line_ending');
15
    $this->delimiter = isset($options['delimiter']) ? $options['delimiter'] : ',';
16
    // Convert tabs.
17
    if ($this->delimiter == '\t') {
18
      $this->delimiter = "\t";
19
    }
20
    $options['delimiter'] = $this->delimiter;
21
    parent::__construct($options);
22
  }
23

    
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function add_row(&$file_handle, array $data, $row_count) {
28
    foreach ($data as $key => $value) {
29
      // Escape inner quotes and wrap all contents in new quotes.
30
      $data[$key] = '"' . str_replace('"', '""', $data[$key]) . '"';
31

    
32
      // Remove <script> tags, which mysteriously cause Excel not to import.
33
      $data[$key] = preg_replace('!<(/?script.*?)>!', '[$1]', $data[$key]);
34
    }
35
    $row = implode($this->delimiter, $data) . $this->line_ending;
36

    
37
    @fwrite($file_handle, $row);
38
  }
39

    
40
  /**
41
   * {@inheritdoc}
42
   */
43
  public function set_headers($filename) {
44
    parent::set_headers($filename);
45

    
46
    // Convert tabs.
47
    if ($this->delimiter == "\t") {
48
      $extension = 'tsv';
49
      $content_type = 'text/tab-separated-values';
50
    }
51
    else {
52
      $extension = 'csv';
53
      $content_type = 'text/csv';
54
    }
55

    
56
    drupal_add_http_header('Content-Type', $content_type);
57
    drupal_add_http_header('Content-Disposition', "attachment; filename=$filename.$extension");
58
  }
59

    
60
}