1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of EC-CUBE |
5
|
|
|
* |
6
|
|
|
* Copyright(c) LOCKON CO.,LTD. All Rights Reserved. |
7
|
|
|
* |
8
|
|
|
* http://www.lockon.co.jp/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Eccube\Controller\Admin; |
15
|
|
|
|
16
|
|
|
use Eccube\Controller\AbstractController; |
17
|
|
|
use Eccube\Service\CsvImportService; |
18
|
|
|
use Eccube\Util\StringUtil; |
19
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
20
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
23
|
|
|
|
24
|
|
|
class AbstractCsvImportController extends AbstractController |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* アップロードされたCSVファイル名 |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $csvFileName; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* アップロードされたCSVファイルの行ごとの処理 |
35
|
|
|
* |
36
|
|
|
* @param UploadedFile $formFile |
37
|
|
|
* |
38
|
|
|
* @return CsvImportService|bool |
39
|
|
|
*/ |
40
|
17 |
|
protected function getImportData(UploadedFile $formFile) |
41
|
|
|
{ |
42
|
|
|
// アップロードされたCSVファイルを一時ディレクトリに保存 |
43
|
17 |
|
$this->csvFileName = 'upload_'.StringUtil::random().'.'.$formFile->getClientOriginalExtension(); |
44
|
17 |
|
$formFile->move($this->eccubeConfig['eccube_csv_temp_realdir'], $this->csvFileName); |
45
|
|
|
|
46
|
17 |
|
$file = file_get_contents($this->eccubeConfig['eccube_csv_temp_realdir'].'/'.$this->csvFileName); |
47
|
|
|
|
48
|
17 |
|
if ('\\' === DIRECTORY_SEPARATOR && PHP_VERSION_ID >= 70000) { |
49
|
|
|
// Windows 環境の PHP7 の場合はファイルエンコーディングを CP932 に合わせる |
50
|
|
|
// see https://github.com/EC-CUBE/ec-cube/issues/1780 |
51
|
|
|
setlocale(LC_ALL, ''); // 既定のロケールに設定 |
52
|
|
|
if (mb_detect_encoding($file) === 'UTF-8') { // UTF-8 を検出したら SJIS-win に変換 |
53
|
|
|
$file = mb_convert_encoding($file, 'SJIS-win', 'UTF-8'); |
54
|
|
|
} |
55
|
|
|
} else { |
56
|
|
|
// アップロードされたファイルがUTF-8以外は文字コード変換を行う |
57
|
17 |
|
$encode = StringUtil::characterEncoding(substr($file, 0, 6)); |
58
|
17 |
|
if ($encode != 'UTF-8') { |
59
|
2 |
|
$file = mb_convert_encoding($file, 'UTF-8', $encode); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
17 |
|
$file = StringUtil::convertLineFeed($file); |
64
|
|
|
|
65
|
17 |
|
$tmp = tmpfile(); |
66
|
17 |
|
fwrite($tmp, $file); |
67
|
17 |
|
rewind($tmp); |
68
|
17 |
|
$meta = stream_get_meta_data($tmp); |
69
|
17 |
|
$file = new \SplFileObject($meta['uri']); |
70
|
|
|
|
71
|
17 |
|
set_time_limit(0); |
72
|
|
|
|
73
|
|
|
// アップロードされたCSVファイルを行ごとに取得 |
74
|
17 |
|
$data = new CsvImportService($file, $this->eccubeConfig['eccube_csv_import_delimiter'], $this->eccubeConfig['eccube_csv_import_enclosure']); |
75
|
|
|
|
76
|
17 |
|
return $data->setHeaderRowNumber(0) ? $data : false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function sendTemplateResponse(Request $request, $columns, $filename) |
80
|
|
|
{ |
81
|
|
|
set_time_limit(0); |
82
|
|
|
|
83
|
|
|
$response = new StreamedResponse(); |
84
|
|
|
$response->setCallback(function () use ($request, $columns) { |
85
|
|
|
// ヘッダ行の出力 |
86
|
|
|
$row = []; |
87
|
|
|
foreach ($columns as $column) { |
88
|
|
|
$row[] = mb_convert_encoding($column, $this->eccubeConfig['eccube_csv_export_encoding'], 'UTF-8'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$fp = fopen('php://output', 'w'); |
92
|
|
|
fputcsv($fp, $row, $this->eccubeConfig['eccube_csv_export_separator']); |
93
|
|
|
fclose($fp); |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
$response->headers->set('Content-Type', 'application/octet-stream'); |
97
|
|
|
$response->headers->set('Content-Disposition', 'attachment; filename='.$filename); |
98
|
|
|
$response->send(); |
99
|
|
|
|
100
|
|
|
return $response; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* アップロードされたCSVファイルの削除 |
105
|
|
|
*/ |
106
|
17 |
|
protected function removeUploadedFile() |
107
|
|
|
{ |
108
|
17 |
|
if (!empty($this->csvFileName)) { |
109
|
|
|
try { |
110
|
17 |
|
$fs = new Filesystem(); |
111
|
17 |
|
$fs->remove($this->eccubeConfig['eccube_csv_temp_realdir'].'/'.$this->csvFileName); |
112
|
|
|
} catch (\Exception $e) { |
113
|
|
|
// エラーが発生しても無視する |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|