Skip to content

Commit

Permalink
Merge pull request EC-CUBE#1781 from nanasess/issues/1780
Browse files Browse the repository at this point in the history
Windows 版 PHP7 の環境で CSV アップロードに失敗するのを修正
  • Loading branch information
ryo-endo authored Sep 26, 2016
2 parents cfebf6a + 0aaf31b commit d8324b8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
3 changes: 1 addition & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ install:
#- cinst mysql
#- SET PATH=C:\tools\mysql\current\bin\;%PATH%
# Set PHP.
#- cinst php php7になってしまうので
- cinst php -version 5.6.17
- cinst php
- SET PATH=C:\tools\php\;%PATH%
- copy C:\tools\php\php.ini-production C:\tools\php\php.ini
- echo date.timezone="Asia/Tokyo" >> C:\tools\php\php.ini
Expand Down
18 changes: 14 additions & 4 deletions src/Eccube/Controller/Admin/Product/CsvImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,20 @@ protected function getImportData($app, $formFile)
$formFile->move($app['config']['csv_temp_realdir'], $this->fileName);

$file = file_get_contents($app['config']['csv_temp_realdir'] . '/' . $this->fileName);
// アップロードされたファイルがUTF-8以外は文字コード変換を行う
$encode = Str::characterEncoding(substr($file, 0, 6));
if ($encode != 'UTF-8') {
$file = mb_convert_encoding($file, 'UTF-8', $encode);

if ('\\' === DIRECTORY_SEPARATOR && PHP_VERSION_ID >= 70000) {
// Windows 環境の PHP7 の場合はファイルエンコーディングを CP932 に合わせる
// see https://github.com/EC-CUBE/ec-cube/issues/1780
setlocale(LC_ALL, ''); // 既定のロケールに設定
if (mb_detect_encoding($file) === 'UTF-8') { // UTF-8 を検出したら SJIS-win に変換
$file = mb_convert_encoding($file, 'SJIS-win', 'UTF-8');
}
} else {
// アップロードされたファイルがUTF-8以外は文字コード変換を行う
$encode = Str::characterEncoding(substr($file, 0, 6));
if ($encode != 'UTF-8') {
$file = mb_convert_encoding($file, 'UTF-8', $encode);
}
}
$file = Str::convertLineFeed($file);

Expand Down
22 changes: 20 additions & 2 deletions src/Eccube/Service/CsvImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ public function current()

// Since the CSV has column headers use them to construct an associative array for the columns in this line
if ($this->valid()) {
$line = $this->file->current();
$current = $this->file->current();
$current = $this->convertEncodingRows($current);

$line = $current;

// See if values for duplicate headers should be merged
if (self::DUPLICATE_HEADERS_MERGE === $this->duplicateHeadersFlag) {
Expand Down Expand Up @@ -181,6 +184,7 @@ public function getColumnHeaders()
*/
public function setColumnHeaders(array $columnHeaders)
{
$columnHeaders = $this->convertEncodingRows($columnHeaders);
$this->columnHeaders = array_count_values($columnHeaders);
$this->headersCount = count($columnHeaders);
}
Expand All @@ -204,7 +208,7 @@ public function setHeaderRowNumber($rowNumber, $duplicates = null)
$headers = $this->readHeaderRow($rowNumber);

if ($headers === false) {
return false;
return false;
}
$this->setColumnHeaders($headers);
return true;
Expand Down Expand Up @@ -400,4 +404,18 @@ protected function mergeDuplicates(array $line)
return $values;
}

/**
* 行の文字エンコーディングを変換する.
*
* Windows 版 PHP7 環境では、ファイルエンコーディングが CP932 になるため UTF-8 に変換する.
* それ以外の環境では何もしない。
*/
protected function convertEncodingRows($row) {
if ('\\' === DIRECTORY_SEPARATOR && PHP_VERSION_ID >= 70000) {
foreach ($row as &$col) {
$col = mb_convert_encoding($col , 'UTF-8', 'SJIS-win');
}
}
return $row;
}
}
12 changes: 6 additions & 6 deletions tests/Eccube/Tests/Web/Admin/Product/CsvImportControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public function createCsvAsArray($has_header = true)
$csv = array(
'商品ID' => null,
'公開ステータス(ID)' => 1,
'商品名' => $faker->word,
'ショップ用メモ欄' => $faker->paragraph,
'商品説明(一覧)' => $faker->paragraph,
'商品説明(詳細)' => $faker->text,
'検索ワード' => $faker->word,
'フリーエリア' => $faker->paragraph,
'商品名' => "商品名".$faker->word."商品名",
'ショップ用メモ欄' => "ショップ用メモ欄".$faker->paragraph."ショップ用メモ欄",
'商品説明(一覧)' => "商品説明(一覧)".$faker->paragraph."商品説明(一覧)",
'商品説明(詳細)' => "商品説明(詳細)".$faker->text."商品説明(詳細)",
'検索ワード' => "検索ワード".$faker->word."検索ワード",
'フリーエリア' => "フリーエリア".$faker->paragraph."フリーエリア",
'商品削除フラグ' => 0,
'商品画像' => $faker->word.'.jpg,'.$faker->word.'.jpg',
'商品カテゴリ(ID)' => '5,6',
Expand Down

0 comments on commit d8324b8

Please sign in to comment.