5 * Webform exporter for creating CSV/TSV delimited files.
8 class webform_exporter_delimited extends webform_exporter {
12 function __construct($options) {
13 $this->line_ending = webform_variable_get('webform_csv_line_ending');
14 $this->delimiter = isset($options['delimiter']) ? $options['delimiter'] : ',';
16 if ($this->delimiter == '\t') {
17 $this->delimiter = "\t";
19 $options['delimiter'] = $this->delimiter;
20 parent::__construct($options);
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]) . '"';
28 // Remove <script> tags, which mysteriously cause Excel not to import.
29 $data[$key] = preg_replace('!<(/?script.*?)>!', '[$1]', $data[$key]);
31 $row = implode($this->delimiter, $data) . $this->line_ending;
33 @fwrite($file_handle, $row);
36 function set_headers($filename) {
37 parent::set_headers($filename);
40 if ($this->delimiter == "\t") {
42 $content_type = 'text/tab-separated-values';
46 $content_type = 'text/csv';
49 drupal_add_http_header('Content-Type', $content_type);
50 drupal_add_http_header('Content-Disposition', "attachment; filename=$filename.$extension");