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 $delimiter;
|
10
|
|
11
|
function __construct($options) {
|
12
|
$this->delimiter = isset($options['delimiter']) ? $options['delimiter'] : ',';
|
13
|
// Convert tabs.
|
14
|
if ($this->delimiter == '\t') {
|
15
|
$this->delimiter = "\t";
|
16
|
}
|
17
|
$options['delimiter'] = $this->delimiter;
|
18
|
parent::__construct($options);
|
19
|
}
|
20
|
|
21
|
function add_row(&$file_handle, $data, $row_count) {
|
22
|
foreach ($data as $key => $value) {
|
23
|
// Escape inner quotes and wrap all contents in new quotes.
|
24
|
$data[$key] = '"' . str_replace('"', '""', $data[$key]) . '"';
|
25
|
|
26
|
// Remove <script> tags, which mysteriously cause Excel not to import.
|
27
|
$data[$key] = preg_replace('!<(/?script.*?)>!', '[$1]', $data[$key]);
|
28
|
}
|
29
|
$row = implode($this->delimiter, $data) . "\n";
|
30
|
|
31
|
@fwrite($file_handle, $row);
|
32
|
}
|
33
|
|
34
|
function set_headers($filename) {
|
35
|
parent::set_headers($filename);
|
36
|
|
37
|
// Convert tabs.
|
38
|
if ($this->delimiter == "\t") {
|
39
|
$extension = 'tsv';
|
40
|
$content_type = 'text/tab-separated-values';
|
41
|
}
|
42
|
else {
|
43
|
$extension = 'csv';
|
44
|
$content_type = 'text/csv';
|
45
|
}
|
46
|
|
47
|
drupal_add_http_header('Content-Type', $content_type);
|
48
|
drupal_add_http_header('Content-Disposition', "attachment; filename=$filename.$extension");
|
49
|
}
|
50
|
}
|