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 @ e9f59ce0

1
<?php
2

    
3
/**
4
 * @file
5
 * Webform exporter for creating CSV/TSV delimited files.
6
 */
7

    
8
class webform_exporter_delimited extends webform_exporter {
9
  public $line_ending;
10
  public $delimiter;
11

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

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

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

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

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

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

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