Projet

Général

Profil

Paste
Télécharger (1,47 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / includes / exporters / webform_exporter_delimited.inc @ 8c72e82a

1
<?php
2

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

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

    
22
  function add_row(&$file_handle, $data, $row_count) {
23
    foreach ($data as $key => $value) {
24
      // Escape inner quotes and wrap all contents in new quotes.
25
      $data[$key] = '"' . str_replace('"', '""', $data[$key]) . '"';
26

    
27
      // Remove <script> tags, which mysteriously cause Excel not to import.
28
      $data[$key] = preg_replace('!<(/?script.*?)>!', '[$1]', $data[$key]);
29
    }
30
    $row = implode($this->delimiter, $data) . $this->line_ending;
31

    
32
    @fwrite($file_handle, $row);
33
  }
34

    
35
  function set_headers($filename) {
36
    parent::set_headers($filename);
37

    
38
    // Convert tabs.
39
    if ($this->delimiter == "\t") {
40
      $extension = 'tsv';
41
      $content_type = 'text/tab-separated-values';
42
    }
43
    else {
44
      $extension = 'csv';
45
      $content_type = 'text/csv';
46
    }
47

    
48
    drupal_add_http_header('Content-Type', $content_type);
49
    drupal_add_http_header('Content-Disposition', "attachment; filename=$filename.$extension");
50
  }
51
}