1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Provides several different handlers for exporting webform results.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_webform_exporters().
|
10
|
*
|
11
|
* Defines the exporters this module implements.
|
12
|
*
|
13
|
* @return array
|
14
|
* An "array of arrays", keyed by content-types. The 'handler' slot
|
15
|
* should point to the PHP class implementing this flag.
|
16
|
*/
|
17
|
function webform_webform_exporters() {
|
18
|
$exporters = array(
|
19
|
'delimited' => array(
|
20
|
'title' => t('Delimited text'),
|
21
|
'description' => t('A plain text file delimited by commas, tabs, or other characters.'),
|
22
|
'handler' => 'webform_exporter_delimited',
|
23
|
'file' => drupal_get_path('module', 'webform') . '/includes/exporters/webform_exporter_delimited.inc',
|
24
|
'weight' => 10,
|
25
|
),
|
26
|
'excel' => array(
|
27
|
'title' => t('Microsoft Excel'),
|
28
|
'description' => t('A file readable by Microsoft Excel.'),
|
29
|
'handler' => 'webform_exporter_excel_xlsx',
|
30
|
'file' => drupal_get_path('module', 'webform') . '/includes/exporters/webform_exporter_excel_xlsx.inc',
|
31
|
'weight' => -1,
|
32
|
// Tells the options to use consistent dates instead of user-defined
|
33
|
// formats.
|
34
|
'options' => array(
|
35
|
'iso8601_time' => TRUE,
|
36
|
'iso8601_date' => TRUE,
|
37
|
),
|
38
|
),
|
39
|
'excel_legacy' => array(
|
40
|
'title' => t('Microsoft Excel (older versions)'),
|
41
|
'description' => t('A file readable by older versions of Microsoft Excel.'),
|
42
|
'handler' => 'webform_exporter_excel_delimited',
|
43
|
'file' => drupal_get_path('module', 'webform') . '/includes/exporters/webform_exporter_excel_delimited.inc',
|
44
|
'weight' => 0,
|
45
|
),
|
46
|
);
|
47
|
|
48
|
// The new Excel exporter is only available if ZipArchive is available.
|
49
|
if (!class_exists('ZipArchive')) {
|
50
|
drupal_set_message(t('@format downloads are not available because this install of PHP lacks Zip archive support.', array('@format' => $exporters['excel']['title'])), 'warning');
|
51
|
unset($exporters['excel']);
|
52
|
}
|
53
|
// By default the legacy Excel exporter is disabled.
|
54
|
if (!webform_variable_get('webform_excel_legacy_exporter')) {
|
55
|
unset($exporters['excel_legacy']);
|
56
|
}
|
57
|
|
58
|
return $exporters;
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* Return a list of exporters suitable for display in a select list.
|
63
|
*/
|
64
|
function webform_export_list() {
|
65
|
$exporters = webform_export_fetch_definition();
|
66
|
$list = array();
|
67
|
foreach ($exporters as $name => $exporter) {
|
68
|
$list[$name] = $exporter['title'];
|
69
|
}
|
70
|
return $list;
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* Returns a Webform exporter definition.
|
75
|
*/
|
76
|
function webform_export_fetch_definition($format = NULL) {
|
77
|
static $cache;
|
78
|
if (!isset($cache)) {
|
79
|
$cache = module_invoke_all('webform_exporters');
|
80
|
drupal_alter('webform_exporters', $cache);
|
81
|
foreach ($cache as $key => $exporter) {
|
82
|
$cache[$key] += array('weight' => 0);
|
83
|
// Used in sorting.
|
84
|
$cache[$key]['name'] = $key;
|
85
|
}
|
86
|
uasort($cache, '_webform_components_sort');
|
87
|
}
|
88
|
|
89
|
if (isset($format)) {
|
90
|
if (isset($cache[$format])) {
|
91
|
return $cache[$format];
|
92
|
}
|
93
|
}
|
94
|
else {
|
95
|
return $cache;
|
96
|
}
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* Instantiates a new Webform handler based on the format.
|
101
|
*/
|
102
|
function webform_export_create_handler($format, $options) {
|
103
|
$definition = webform_export_fetch_definition($format);
|
104
|
if (isset($definition['file'])) {
|
105
|
include_once $definition['file'];
|
106
|
}
|
107
|
if (isset($definition)) {
|
108
|
$handler = new $definition['handler']($options);
|
109
|
}
|
110
|
else {
|
111
|
// TODO: Create a default broken exporter.
|
112
|
$handler = new webform_exporter_broken($options);
|
113
|
}
|
114
|
|
115
|
return $handler;
|
116
|
}
|