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