Skip to content

Commit

Permalink
規格・規格分類:「CSV登録」作成, 「CSVダウンロード・CSV出力項目設定」追加
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshiro.sawamura authored and carkn committed Jun 3, 2022
1 parent 9b2f5f5 commit d75fe7d
Show file tree
Hide file tree
Showing 15 changed files with 897 additions and 3 deletions.
86 changes: 85 additions & 1 deletion src/Eccube/Controller/Admin/Product/ClassCategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
namespace Eccube\Controller\Admin\Product;

use Eccube\Controller\AbstractController;
use Eccube\Entity\Master\CsvType;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Form\Type\Admin\ClassCategoryType;
use Eccube\Repository\ClassCategoryRepository;
use Eccube\Repository\ClassNameRepository;
use Eccube\Repository\ProductClassRepository;
use Eccube\Service\CsvExportService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
Expand All @@ -44,21 +47,29 @@ class ClassCategoryController extends AbstractController
*/
protected $classNameRepository;

/**
* @var CsvExportService
*/
protected $csvExportService;

/**
* ClassCategoryController constructor.
*
* @param ProductClassRepository $productClassRepository
* @param ClassCategoryRepository $classCategoryRepository
* @param ClassNameRepository $classNameRepository
* @param CsvExportService $csvExportService
*/
public function __construct(
ProductClassRepository $productClassRepository,
ClassCategoryRepository $classCategoryRepository,
ClassNameRepository $classNameRepository
ClassNameRepository $classNameRepository,
CsvExportService $csvExportService
) {
$this->productClassRepository = $productClassRepository;
$this->classCategoryRepository = $classCategoryRepository;
$this->classNameRepository = $classNameRepository;
$this->csvExportService = $csvExportService;
}

/**
Expand Down Expand Up @@ -265,4 +276,77 @@ public function moveSortNo(Request $request)
return new Response('Successful');
}
}

/**
* 規格分類CSVの出力.
*
* @Route("/%eccube_admin_route%/product/class_category/export/{class_name_id}", name="admin_product_class_category_export")
*
* @param Request $request
*
* @return StreamedResponse
*/
public function export(Request $request, $class_name_id)
{
// タイムアウトを無効にする.
set_time_limit(0);

// sql loggerを無効にする.
$em = $this->entityManager;
$em->getConfiguration()->setSQLLogger(null);

$response = new StreamedResponse();
$response->setCallback(function () use ($request, $class_name_id) {
// CSV種別を元に初期化.
$this->csvExportService->initCsvType(CsvType::CSV_TYPE_CLASS_CATEGORY);
// ヘッダ行の出力.
$this->csvExportService->exportHeader();

$qb = $this->classCategoryRepository
->createQueryBuilder('cc')
->where('cc.ClassName = :ClassName')
->setParameter('ClassName', $class_name_id)
->orderBy('cc.sort_no', 'DESC');

// データ行の出力.
$this->csvExportService->setExportQueryBuilder($qb);
$this->csvExportService->exportData(function ($entity, $csvService) use ($request) {
$Csvs = $csvService->getCsvs();

/** @var $ClassCategory \Eccube\Entity\ClassCategory */
$ClassCategory = $entity;

// CSV出力項目と合致するデータを取得.
$ExportCsvRow = new \Eccube\Entity\ExportCsvRow();
foreach ($Csvs as $Csv) {
$ExportCsvRow->setData($csvService->getData($Csv, $ClassCategory));

$event = new EventArgs(
[
'csvService' => $csvService,
'Csv' => $Csv,
'ClassCategory' => $ClassCategory,
'ExportCsvRow' => $ExportCsvRow,
],
$request
);
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_CSV_EXPORT, $event);

$ExportCsvRow->pushData();
}
//$row[] = number_format(memory_get_usage(true));
// 出力.
$csvService->fputcsv($ExportCsvRow->getRow());
});
});
$now = new \DateTime();
$filename = 'class_category_'.$now->format('YmdHis').'.csv';
$response->headers->set('Content-Type', 'application/octet-stream');
$response->headers->set('Content-Disposition', 'attachment; filename='.$filename);
$response->send();

log_info('規格分類CSV出力ファイル名', [$filename]);

return $response;
}
}
89 changes: 87 additions & 2 deletions src/Eccube/Controller/Admin/Product/ClassNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@

use Eccube\Controller\AbstractController;
use Eccube\Entity\ClassName;
use Eccube\Entity\Master\CsvType;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Form\Type\Admin\ClassNameType;
use Eccube\Repository\ClassNameRepository;
use Eccube\Service\CsvExportService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
Expand All @@ -33,14 +36,23 @@ class ClassNameController extends AbstractController
*/
protected $classNameRepository;

/**
* @var CsvExportService
*/
protected $csvExportService;

/**
* ClassNameController constructor.
*
* @param ClassNameRepository $classNameRepository
* @param CsvExportService $csvExportService
*/
public function __construct(ClassNameRepository $classNameRepository)
{
public function __construct(
ClassNameRepository $classNameRepository,
CsvExportService $csvExportService
) {
$this->classNameRepository = $classNameRepository;
$this->csvExportService = $csvExportService;
}

/**
Expand Down Expand Up @@ -184,4 +196,77 @@ public function moveSortNo(Request $request)
return new Response();
}
}

/**
* 規格CSVの出力.
*
* @Route("/%eccube_admin_route%/product/class_name/export", name="admin_product_class_name_export")
*
* @param Request $request
*
* @return StreamedResponse
*/
public function export(Request $request)
{
// タイムアウトを無効にする.
set_time_limit(0);

// sql loggerを無効にする.
$em = $this->entityManager;
$em->getConfiguration()->setSQLLogger(null);

$response = new StreamedResponse();
$response->setCallback(function () use ($request) {
// CSV種別を元に初期化.
$this->csvExportService->initCsvType(CsvType::CSV_TYPE_CLASS_NAME);

// ヘッダ行の出力.
$this->csvExportService->exportHeader();

$qb = $this->classNameRepository
->createQueryBuilder('cn')
->orderBy('cn.sort_no', 'DESC');

// データ行の出力.
$this->csvExportService->setExportQueryBuilder($qb);
$this->csvExportService->exportData(function ($entity, $csvService) use ($request) {
$Csvs = $csvService->getCsvs();

/** @var $ClassName \Eccube\Entity\ClassName */
$ClassName = $entity;

// CSV出力項目と合致するデータを取得.
$ExportCsvRow = new \Eccube\Entity\ExportCsvRow();
foreach ($Csvs as $Csv) {
$ExportCsvRow->setData($csvService->getData($Csv, $ClassName));

$event = new EventArgs(
[
'csvService' => $csvService,
'Csv' => $Csv,
'ClassName' => $ClassName,
'ExportCsvRow' => $ExportCsvRow,
],
$request
);
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_NAME_CSV_EXPORT, $event);

$ExportCsvRow->pushData();
}
//$row[] = number_format(memory_get_usage(true));
// 出力.
$csvService->fputcsv($ExportCsvRow->getRow());
});
});

$now = new \DateTime();
$filename = 'class_name_'.$now->format('YmdHis').'.csv';
$response->headers->set('Content-Type', 'application/octet-stream');
$response->headers->set('Content-Disposition', 'attachment; filename='.$filename);
$response->send();

log_info('規格CSV出力ファイル名', [$filename]);

return $response;
}
}
Loading

0 comments on commit d75fe7d

Please sign in to comment.