Projet

Général

Profil

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

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

1
<?php
2

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

    
8
/**
9
 *
10
 */
11
class webform_exporter_delimited extends webform_exporter {
12
  public $line_ending;
13
  public $delimiter;
14

    
15
  /**
16
   *
17
   */
18
  public function __construct($options) {
19
    $this->line_ending = webform_variable_get('webform_csv_line_ending');
20
    $this->delimiter = isset($options['delimiter']) ? $options['delimiter'] : ',';
21
    // Convert tabs.
22
    if ($this->delimiter == '\t') {
23
      $this->delimiter = "\t";
24
    }
25
    $options['delimiter'] = $this->delimiter;
26
    parent::__construct($options);
27
  }
28

    
29
  /**
30
   *
31
   */
32
  public function add_row(&$file_handle, $data, $row_count) {
33
    foreach ($data as $key => $value) {
34
      // Escape inner quotes and wrap all contents in new quotes.
35
      $data[$key] = '"' . str_replace('"', '""', $data[$key]) . '"';
36

    
37
      // Remove <script> tags, which mysteriously cause Excel not to import.
38
      $data[$key] = preg_replace('!<(/?script.*?)>!', '[$1]', $data[$key]);
39
    }
40
    $row = implode($this->delimiter, $data) . $this->line_ending;
41

    
42
    @fwrite($file_handle, $row);
43
  }
44

    
45
  /**
46
   *
47
   */
48
  public function set_headers($filename) {
49
    parent::set_headers($filename);
50

    
51
    // Convert tabs.
52
    if ($this->delimiter == "\t") {
53
      $extension = 'tsv';
54
      $content_type = 'text/tab-separated-values';
55
    }
56
    else {
57
      $extension = 'csv';
58
      $content_type = 'text/csv';
59
    }
60

    
61
    drupal_add_http_header('Content-Type', $content_type);
62
    drupal_add_http_header('Content-Disposition', "attachment; filename=$filename.$extension");
63
  }
64

    
65
}