| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: webform.export.inc,v 1.4.2.2 2010/04/03 04:06:28 quicksketch Exp $ 3 4 /** 5 * @file 6 * Provides several different handlers for exporting webform results. 7 */ 8 9 /** 10 * Implementation of hook_webform_exporters(). 11 * 12 * Defines the exporters this module implements. 13 * 14 * @return 15 * An "array of arrays", keyed by content-types. The 'handler' slot 16 * should point to the PHP class implementing this flag. 17 */ 18 function webform_webform_exporters() { 19 return array( 20 'delimited' => array( 21 'title' => t('Delimited text'), 22 'description' => t('A plain text file delimited by commas, tabs, or other characters.'), 23 'handler' => 'webform_exporter_delimited', 24 ), 25 'excel' => array( 26 'title' => t('Microsoft Excel'), 27 'description' => t('A file readable by Microsoft Excel.'), 28 'handler' => 'webform_exporter_excel', 29 ), 30 ); 31 } 32 33 /** 34 * Return a list of exporters suitable for display in a select list. 35 */ 36 function webform_export_list() { 37 $exporters = webform_export_fetch_definition(); 38 $list = array(); 39 foreach ($exporters as $name => $exporter) { 40 $list[$name] = $exporter['title']; 41 } 42 return $list; 43 } 44 45 /** 46 * Returns a Webform exporter definition. 47 */ 48 function webform_export_fetch_definition($format = NULL) { 49 static $cache; 50 if (!isset($cache)) { 51 $cache = module_invoke_all('webform_exporters'); 52 } 53 54 if (isset($format)) { 55 if (isset($cache[$format])) { 56 return $cache[$format]; 57 } 58 } 59 else { 60 return $cache; 61 } 62 } 63 64 /** 65 * Instantiates a new Webform handler based on the format. 66 */ 67 function webform_export_create_handler($format, $options) { 68 $definition = webform_export_fetch_definition($format); 69 if (isset($definition) && class_exists($definition['handler'])) { 70 $handler = new $definition['handler']($options); 71 } 72 else { 73 // TODO: Create a default broken exporter. 74 $handler = new webform_exporter_broken($options); 75 } 76 77 return $handler; 78 } 79 80 class webform_exporter { 81 function add_row(&$file_handle, $data) { 82 } 83 84 function set_headers($filename) { 85 drupal_set_header('Content-Type: application/force-download'); 86 drupal_set_header('Pragma: public'); 87 drupal_set_header('Cache-Control: max-age=0'); 88 } 89 90 function bof(&$file_handle) { 91 } 92 93 function eof(&$file_handle) { 94 } 95 } 96 97 class webform_exporter_delimited extends webform_exporter { 98 var $delimiter; 99 100 function webform_exporter_delimited($options) { 101 $this->delimiter = isset($options['delimiter']) ? $options['delimiter'] : ','; 102 // Convert tabs. 103 if ($this->delimiter == '\t') { 104 $this->delimiter = "\t"; 105 } 106 } 107 108 function bof(&$file_handle) { 109 $output = ''; 110 111 // Include at BOM at the beginning of the file for Little Endian. 112 // This makes tab-separated imports work correctly in MS Excel. 113 if (function_exists('mb_convert_encoding') && $this->delimiter == "\t") { 114 $output = chr(255) . chr(254); 115 } 116 @fwrite($file_handle, $output); 117 } 118 119 function add_row(&$file_handle, $data) { 120 foreach ($data as $key => $value) { 121 // Escape inner quotes and wrap all contents in new quotes. 122 $data[$key] = '"' . str_replace('"', '""', $data[$key]) . '"'; 123 124 // Remove <script> tags, which mysteriously cause Excel not to import. 125 $data[$key] = preg_replace('!<(/?script.*?)>!', '[$1]', $data[$key]); 126 } 127 $row = implode($this->delimiter, $data) . "\n"; 128 129 if (function_exists('mb_convert_encoding')) { 130 $row = mb_convert_encoding($row, 'UTF-16LE', 'UTF-8'); 131 } 132 133 @fwrite($file_handle, $row); 134 } 135 136 function set_headers($filename) { 137 parent::set_headers($filename); 138 139 // Convert tabs. 140 if ($this->delimiter == "\t") { 141 $extension = 'tsv'; 142 $content_type = 'text/tab-separated-values'; 143 } 144 else { 145 $extension = 'csv'; 146 $content_type = 'text/csv'; 147 } 148 149 drupal_set_header("Content-Type: $content_type"); 150 drupal_set_header("Content-Disposition: attachment; filename=$filename.$extension"); 151 } 152 } 153 154 /** 155 * The Excel exporter currently is just a tab-delimited export. 156 */ 157 class webform_exporter_excel extends webform_exporter_delimited { 158 var $delimiter; 159 160 function webform_exporter_excel($options) { 161 $options['delimiter'] = '\t'; 162 parent::webform_exporter_delimited($options); 163 } 164 165 function set_headers($filename) { 166 drupal_set_header('Content-Type: application/x-msexcel'); 167 drupal_set_header("Content-Disposition: attachment; filename=$filename.xls"); 168 drupal_set_header('Pragma: public'); 169 drupal_set_header('Cache-Control: max-age=0'); 170 } 171 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |