src/Eccube/Controller/Admin/AbstractCsvImportController.php line 24

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