View source
<?php
function webform_webform_exporters() {
return array(
'delimited' => array(
'title' => t('Delimited text'),
'description' => t('A plain text file delimited by commas, tabs, or other characters.'),
'handler' => 'webform_exporter_delimited',
),
'excel' => array(
'title' => t('Microsoft Excel'),
'description' => t('A file readable by Microsoft Excel.'),
'handler' => 'webform_exporter_excel',
),
);
}
function webform_export_list() {
$exporters = webform_export_fetch_definition();
$list = array();
foreach ($exporters as $name => $exporter) {
$list[$name] = $exporter['title'];
}
return $list;
}
function webform_export_fetch_definition($format = NULL) {
static $cache;
if (!isset($cache)) {
$cache = module_invoke_all('webform_exporters');
}
if (isset($format)) {
if (isset($cache[$format])) {
return $cache[$format];
}
}
else {
return $cache;
}
}
function webform_export_create_handler($format, $options) {
$definition = webform_export_fetch_definition($format);
if (isset($definition) && class_exists($definition['handler'])) {
$handler = new $definition['handler']($options);
}
else {
$handler = new webform_exporter_broken($options);
}
return $handler;
}
class webform_exporter {
function add_row(&$file_handle, $data) {
}
function set_headers($filename) {
drupal_set_header('Content-Type: application/force-download');
drupal_set_header('Pragma: public');
drupal_set_header('Cache-Control: max-age=0');
}
function bof(&$file_handle) {
}
function eof(&$file_handle) {
}
}
class webform_exporter_delimited extends webform_exporter {
var $delimiter;
function webform_exporter_delimited($options) {
$this->delimiter = isset($options['delimiter']) ? $options['delimiter'] : ',';
if ($this->delimiter == '\\t') {
$this->delimiter = "\t";
}
}
function bof(&$file_handle) {
$output = '';
if (function_exists('mb_convert_encoding') && $this->delimiter == '\\t') {
$output = chr(255) . chr(254);
}
@fwrite($file_handle, $output);
}
function add_row(&$file_handle, $data) {
foreach ($data as $key => $value) {
$data[$key] = '"' . str_replace('"', '""', $value) . '"';
}
$row = implode($this->delimiter, $data) . "\n";
if (function_exists('mb_convert_encoding')) {
$row = mb_convert_encoding($row, 'UTF-16LE', 'UTF-8');
}
@fwrite($file_handle, $row);
}
function set_headers($filename) {
parent::set_headers($filename);
if ($this->delimiter == "\t") {
$extension = 'tsv';
$content_type = 'text/tab-separated-values';
}
else {
$extension = 'csv';
$content_type = 'text/csv';
}
drupal_set_header("Content-Type: {$content_type}");
drupal_set_header("Content-Disposition: attachment; filename={$filename}.{$extension}");
}
}
class webform_exporter_excel extends webform_exporter_delimited {
var $delimiter;
function webform_exporter_excel($options) {
$options['delimiter'] = '\\t';
parent::webform_exporter_delimited($options);
}
function set_headers($filename) {
drupal_set_header('Content-Type: application/x-msexcel');
drupal_set_header("Content-Disposition: attachment; filename={$filename}.xls");
drupal_set_header('Pragma: public');
drupal_set_header('Cache-Control: max-age=0');
}
}