webform.export.inc 3.2 KB

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