Project

General

Profile

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

root / drupal7 / sites / all / modules / webform / includes / exporters / webform_exporter_delimited.inc @ 7b2d1845

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
      $content_type = 'text/tab-separated-values';
49
    }
50
    else {
51
      $content_type = 'text/csv';
52
    }
53

    
54
    drupal_add_http_header('Content-Type', $content_type);
55
    drupal_add_http_header('Content-Disposition', 'attachment; filename=' . $this->get_filename($filename));
56
  }
57

    
58
  /**
59
   * {@inheritdoc}
60
   */
61
  public function get_filename($filename) {
62
    $extension = ($this->delimiter === "\t") ? 'tsv' : 'csv';
63
    return $filename . '.' . $extension;
64
  }
65

    
66
}