1 |
a45e4bc1
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* Webform exporter for creating CSV/TSV delimited files.
|
5 |
|
|
*/
|
6 |
|
|
class webform_exporter_delimited extends webform_exporter {
|
7 |
01d522a6
|
Assos Assos
|
public $line_ending;
|
8 |
a45e4bc1
|
Assos Assos
|
public $delimiter;
|
9 |
|
|
|
10 |
feca1e4a
|
Assos Assos
|
/**
|
11 |
01f36513
|
Assos Assos
|
* {@inheritdoc}
|
12 |
feca1e4a
|
Assos Assos
|
*/
|
13 |
|
|
public function __construct($options) {
|
14 |
01d522a6
|
Assos Assos
|
$this->line_ending = webform_variable_get('webform_csv_line_ending');
|
15 |
a45e4bc1
|
Assos Assos
|
$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 |
feca1e4a
|
Assos Assos
|
/**
|
25 |
01f36513
|
Assos Assos
|
* {@inheritdoc}
|
26 |
feca1e4a
|
Assos Assos
|
*/
|
27 |
01f36513
|
Assos Assos
|
public function add_row(&$file_handle, array $data, $row_count) {
|
28 |
a45e4bc1
|
Assos Assos
|
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 |
01d522a6
|
Assos Assos
|
$row = implode($this->delimiter, $data) . $this->line_ending;
|
36 |
a45e4bc1
|
Assos Assos
|
|
37 |
|
|
@fwrite($file_handle, $row);
|
38 |
|
|
}
|
39 |
|
|
|
40 |
feca1e4a
|
Assos Assos
|
/**
|
41 |
01f36513
|
Assos Assos
|
* {@inheritdoc}
|
42 |
feca1e4a
|
Assos Assos
|
*/
|
43 |
|
|
public function set_headers($filename) {
|
44 |
a45e4bc1
|
Assos Assos
|
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 |
7b2d1845
|
Assos Assos
|
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 |
a45e4bc1
|
Assos Assos
|
}
|
65 |
feca1e4a
|
Assos Assos
|
|
66 |
a45e4bc1
|
Assos Assos
|
} |