ExportToCsvUtf16le

The Contact Form DB ExportToCsvUtf16le class.

Defined (1)

The class is defined in the following location(s).

/ExportToCsvUtf16le.php  
  1. class ExportToCsvUtf16le extends ExportBase implements CFDBExport { 
  2.  
  3. public function export($formName, $options = null) { 
  4. $this->setOptions($options); 
  5. $this->setCommonOptions(); 
  6.  
  7. // Security Check 
  8. if (!$this->isAuthorized()) { 
  9. $this->assertSecurityErrorMessage(); 
  10. return; 
  11.  
  12. // Headers 
  13. $this->echoHeaders( 
  14. array('Content-Type: text/csv; charset=UTF-16LE',  
  15. "Content-Disposition: attachment; filename=\"$formName.csv\"")); 
  16. // todo: make this work 
  17. // $fileName = $formName . '.csv'; 
  18. // $fileName = $this->encodeWordRfc2231($formName) . '.csv'; 
  19. // header("Content-Disposition: attachment; filename*=UTF-8''$fileName"); 
  20.  
  21. //Bytes FF FE (UTF-16LE BOM) 
  22. echo chr(255) . chr(254); 
  23. $eol = $this->encode(utf8_encode("\n")); 
  24. $delimiter = $this->encode(utf8_encode("\t")); 
  25.  
  26. // Query DB for the data for that form 
  27. $submitTimeKeyName = 'Submit_Time_Key'; 
  28. $this->setDataIterator($formName, $submitTimeKeyName); 
  29.  
  30. // Column Headers 
  31. if (isset($this->options['header']) && $this->options['header'] != 'true') { 
  32. // do not output column headers 
  33. else { 
  34. foreach ($this->dataIterator->getDisplayColumns() as $aCol) { 
  35. $colDisplayValue = $aCol; 
  36. if ($this->headers && isset($this->headers[$aCol])) { 
  37. $colDisplayValue = $this->headers[$aCol]; 
  38. echo $this->prepareCsvValue($colDisplayValue); 
  39. echo $delimiter; 
  40. echo $eol; 
  41.  
  42. // Rows 
  43. $showFileUrlsInExport = $this->plugin->getOption('ShowFileUrlsInExport') == 'true'; 
  44. while ($this->dataIterator->nextRow()) { 
  45. $fields_with_file = null; 
  46. if ($showFileUrlsInExport && 
  47. isset($this->dataIterator->row['fields_with_file']) && 
  48. $this->dataIterator->row['fields_with_file'] != null) { 
  49. $fields_with_file = explode(', ', $this->dataIterator->row['fields_with_file']); 
  50. foreach ($this->dataIterator->getDisplayColumns() as $aCol) { 
  51. $cell = isset($this->dataIterator->row[$aCol]) ? $this->dataIterator->row[$aCol] : ''; 
  52. if ($showFileUrlsInExport && 
  53. $fields_with_file && 
  54. $cell && 
  55. in_array($aCol, $fields_with_file)) { 
  56. $cell = $this->plugin->getFileUrl($this->dataIterator->row[$submitTimeKeyName], $formName, $aCol); 
  57. echo $this->prepareCsvValue($cell); 
  58. echo $delimiter; 
  59. echo $eol; 
  60.  
  61.  
  62. protected function &prepareCsvValue($text) { 
  63. // Excel does not like \n characters in UTF-16LE, so we replace with a space 
  64. $text = str_replace("\n", ' ', $text); 
  65.  
  66. // In CSV, escape double-quotes by putting two double quotes together 
  67. $text = str_replace('"', '""', $text); 
  68.  
  69. // Quote it to escape delimiters 
  70. $text = '"' . $text . '"'; 
  71.  
  72. // Encode UTF-16LE 
  73. $text = $this->encode($text); 
  74.  
  75. return $text; 
  76.  
  77. protected function encode($text) { 
  78. return mb_convert_encoding($text, 'UTF-16LE', 'UTF-8'); 
  79.  
  80. protected function &encodeWordRfc2231($word) { 
  81. $binArray = unpack("C*", $word); 
  82. $hex = ''; 
  83. foreach ($binArray as $chr) { 
  84. $hex .= '%' . sprintf("%02X", base_convert($chr, 2, 16)); 
  85. return $hex; 
  86.