Skip to content

Commit

Permalink
Fix #272: UTF-8 encoding for HTML, CSV, TEXT formats
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Oct 11, 2018
1 parent 8a1e8b2 commit 249cb9f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Change Log: `yii2-export`

**Date:** _under development_

- (enh #272): UTF-8 encoding for HTML, CSV, TEXT formats.
- (enh #271): Locale specific validation messages and code enhancements.
- (enh #270): Add iframe as default target for export form download.
- (enh #248): Add direct download, supplement sheets, data validation features.
Expand Down
15 changes: 12 additions & 3 deletions src/ExportMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class ExportMenu extends GridView
{
use TranslationTrait;

/**
* @var string UTF-8 encoding
*/
const ENCODING_UTF8 = 'utf-8';
/**
* @var string HTML (Hyper Text Markup Language) export format
*/
Expand Down Expand Up @@ -373,9 +377,9 @@ class ExportMenu extends GridView
public $autoWidth = true;

/**
* @var string encoding for the downloaded file header. Defaults to 'utf-8'.
* @var string encoding for the downloaded file header. Defaults to [[ENCODING_UTF8]].
*/
public $encoding = 'utf-8';
public $encoding = self::ENCODING_UTF8;

/**
* @var string the exported output file name. Defaults to 'grid-export';
Expand Down Expand Up @@ -1117,10 +1121,15 @@ public function initPhpSpreadsheetWriter($type)
* @var WriterCsv $writer
*/
$writer = $this->_objWriter = IOFactory::createWriter($this->_objSpreadsheet, $type);
if ($this->_exportType === self::FORMAT_TEXT) {
$t = $this->_exportType;
if ($t === self::FORMAT_TEXT) {
$delimiter = $this->getSetting('delimiter', "\t");
$writer->setDelimiter($delimiter);
}
$needsEncoding = $t === self::FORMAT_HTML || $t === self::FORMAT_CSV || $t === self::FORMAT_TEXT;
if ($this->encoding === self::ENCODING_UTF8 && $needsEncoding) {
$writer->setUseBOM(true);
}
$this->raiseEvent('onInitWriter', [$this->_objWriter, $this]);
}

Expand Down

0 comments on commit 249cb9f

Please sign in to comment.