ExportToCsvUtf8

The Contact Form DB ExportToCsvUtf8 class.

Defined (1)

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

/ExportToCsvUtf8.php  
  1. class ExportToCsvUtf8 extends ExportBase implements CFDBExport { 
  2.  
  3. var $useBom = false; 
  4. var $bak = false; 
  5.  
  6. // For Japanese 
  7. var $useShiftJIS = false; 
  8. // The code number of Japanese two-byte character "*" is separated by Japanese encoding types. 
  9. // Hyphen, Centered dot 
  10. var $utf_escape_patterns_search = array('/\xE2\x80\x93/', '/\xE2\x80\xA2/'); 
  11. var $utf_escape_patterns_replace = array("\xE2\x88\x92", "\xE3\x83\xBB"); 
  12.  
  13. public function setUseBom($use) { 
  14. $this->useBom = $use; 
  15.  
  16. public function setUseShiftJIS($use) { 
  17. // If mb_convert_encoding function is not enabled (mb_string module is not installed),  
  18. // then converting cannot be done. 
  19. if ($use && !function_exists('mb_convert_encoding')) { 
  20. $this->useShiftJIS = false; 
  21. else { 
  22. $this->useShiftJIS = $use; 
  23.  
  24. public function export($formName, $options = null) { 
  25.  
  26. if (isset($options['bak']) && $options['bak'] == 'true') { 
  27. $this->bak = true; 
  28. $options['hide'] = 'Submitted'; 
  29. $options['show'] = 'submit_time, /.*/'; 
  30. $options['unbuffered'] = 'true'; 
  31.  
  32. $this->setOptions($options); 
  33. $this->setCommonOptions(); 
  34.  
  35. // Security Check 
  36. if (!$this->isAuthorized()) { 
  37. $this->assertSecurityErrorMessage(); 
  38. return; 
  39.  
  40. if ($this->options && !$this->bak && is_array($this->options)) { 
  41. if (isset($this->options['bom'])) { 
  42. $this->useBom = $this->options['bom'] == 'true'; 
  43.  
  44. // Headers 
  45. $charSet = 'UTF-8'; 
  46. if ($this->useShiftJIS) { 
  47. $charSet = 'Shift_JIS'; 
  48. $this->echoHeaders( 
  49. array("Content-Type: text/csv; charset=$charSet",  
  50. "Content-Disposition: attachment; filename=\"$formName.csv\"")); 
  51.  
  52. $this->echoCsv($formName); 
  53.  
  54. /** 
  55. * Convert Shift-JIS (Standard Encoding for Japanese Applications) to UTF-8. 
  56. * @param $str string 
  57. * @return string 
  58. */ 
  59. public function japanese_convert_utf8_to_sjis($str) { 
  60. return preg_replace( 
  61. $this->utf_escape_patterns_search,  
  62. $this->utf_escape_patterns_replace,  
  63. $str); 
  64.  
  65.  
  66. public function echoCsv($formName) { 
  67. if ($this->useBom) { 
  68. // File encoding UTF-8 Byte Order Mark (BOM) http://wiki.sdn.sap.com/wiki/display/ABAP/Excel+files+-+CSV+format 
  69. echo chr(239) . chr(187) . chr(191); 
  70.  
  71. $eol = "\n"; 
  72.  
  73. // Query DB for the data for that form 
  74. $submitTimeKeyName = 'Submit_Time_Key'; 
  75. $this->setDataIterator($formName, $submitTimeKeyName); 
  76.  
  77.  
  78. // Column Headers 
  79. if (isset($this->options['header']) && $this->options['header'] != 'true') { 
  80. // do not output column headers 
  81. else { 
  82. foreach ($this->dataIterator->getDisplayColumns() as $aCol) { 
  83. $colDisplayValue = $aCol; 
  84. if ($this->headers && isset($this->headers[$aCol])) { 
  85. $colDisplayValue = $this->headers[$aCol]; 
  86. printf('"%s", ', str_replace('"', '""', $colDisplayValue)); 
  87. echo $eol; 
  88.  
  89. // Rows 
  90. $showFileUrlsInExport = $this->plugin->getOption('ShowFileUrlsInExport') == 'true'; 
  91. while ($this->dataIterator->nextRow()) { 
  92. $fields_with_file = null; 
  93. if ($showFileUrlsInExport && 
  94. isset($this->dataIterator->row['fields_with_file']) && 
  95. $this->dataIterator->row['fields_with_file'] != null) { 
  96. $fields_with_file = explode(', ', $this->dataIterator->row['fields_with_file']); 
  97. foreach ($this->dataIterator->getDisplayColumns() as $aCol) { 
  98. $cell = isset($this->dataIterator->row[$aCol]) ? $this->dataIterator->row[$aCol] : ''; 
  99. if ($showFileUrlsInExport && 
  100. $fields_with_file && 
  101. $cell && 
  102. in_array($aCol, $fields_with_file)) { 
  103. $cell = $this->plugin->getFileUrl($this->dataIterator->row[$submitTimeKeyName], $formName, $aCol); 
  104. if ($this->useShiftJIS) { 
  105. printf('"%s", ', str_replace('"', '""', mb_convert_encoding($this->japanese_convert_utf8_to_sjis($cell), "SJIS-win", "utf-8"))); 
  106. else { 
  107. printf('"%s", ', str_replace('"', '""', $cell)); 
  108. echo $eol; 
  109.  
  110.