<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\Noshiproduct\Controller;
use Eccube\Controller\AbstractController;
use Eccube\Repository\Master\PageMaxRepository;
use Eccube\Entity\OrderItem;
use Eccube\Repository\OrderItemRepository;
use Plugin\Noshiproduct\Entity\Noshiproduct;
use Plugin\Noshiproduct\Entity\NoshiproductConfig;
use Plugin\Noshiproduct\Form\Type\NoshiproductType;
use Plugin\Noshiproduct\Form\Type\Admin\NoshiproductSearchType;
use Plugin\Noshiproduct\Repository\NoshiproductRepository;
use Plugin\Noshiproduct\Repository\NoshiproductConfigRepository;
use Eccube\Util\CacheUtil;
use Eccube\Util\FormUtil;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Eccube\Entity\Master\CsvType;
use Eccube\Service\CsvExportService;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
/**
* Class NoshiproductController.
*/
class NoshiproductController extends AbstractController
{
/**
* @var CsvExportService
*/
protected $csvExportService;
/**
* @var PageMaxRepository
*/
protected $pageMaxRepository;
/**
* @var OrdeItemrRepository
*/
protected $orderItemRepository;
/**
* @var NoshiproductRepository
*/
private $noshiproductRepository;
/**
* @var NoshiproductConfigRepository
*/
private $noshiproductConfigRepository;
/**
* NoshiproductController constructor.
*
* @param NoshiproductRepository $noshiproductRepository
*/
public function __construct(CsvExportService $csvExportService, PageMaxRepository $pageMaxRepository, OrderItemRepository $orderItemRepository, NoshiproductRepository $noshiproductRepository, NoshiproductConfigRepository $noshiproductConfigRepository)
{
$this->csvExportService = $csvExportService;
$this->pageMaxRepository = $pageMaxRepository;
$this->orderItemRepository = $orderItemRepository;
$this->noshiproductRepository = $noshiproductRepository;
$this->noshiproductConfigRepository = $noshiproductConfigRepository;
}
/**
* 一覧を表示する。
*
* @param Request $request
*
* @return array
* @Route("/%eccube_admin_route%/order/noshiproduct", name="admin_order_noshiproduct")
* @Route("/%eccube_admin_route%/order/noshiproduct/page/{page_no}", requirements={"page_no" = "\d+"}, name="admin_order_noshiproduct_page")
* @Template("@Noshiproduct/admin/index.twig")
*/
public function index(Request $request, $page_no = 1, PaginatorInterface $paginator)
{
$CsvType = $this->noshiproductConfigRepository
->get()
->getCsvType();
$builder = $this->formFactory->createBuilder(NoshiproductSearchType::class);
$searchForm = $builder->getForm();
$pageMaxis = $this->pageMaxRepository->findAll();
$pageCount = $this->session->get(
'noshiproduct.admin.noshiproduct.search.page_count',
$this->eccubeConfig['eccube_default_page_count']
);
$pageCountParam = $request->get('page_count');
if ($pageCountParam && is_numeric($pageCountParam)) {
foreach ($pageMaxis as $pageMax) {
if ($pageCountParam == $pageMax->getName()) {
$pageCount = $pageMax->getName();
$this->session->set('noshiproduct.admin.noshiproduct.search.page_count', $pageCount);
break;
}
}
}
if ('POST' === $request->getMethod()) {
$searchForm->handleRequest($request);
if ($searchForm->isValid()) {
$searchData = $searchForm->getData();
$page_no = 1;
$this->session->set('noshiproduct.admin.noshiproduct.search', FormUtil::getViewData($searchForm));
$this->session->set('noshiproduct.admin.noshiproduct.search.page_no', $page_no);
} else {
return [
'searchForm' => $searchForm->createView(),
'pagination' => [],
'pageMaxis' => $pageMaxis,
'page_no' => $page_no,
'page_count' => $pageCount,
'CsvType' => $CsvType,
'has_errors' => true,
];
}
} else {
if (null !== $page_no || $request->get('resume')) {
if ($page_no) {
$this->session->set('noshiproduct.admin.noshiproduct.search.page_no', (int) $page_no);
} else {
$page_no = $this->session->get('noshiproduct.admin.noshiproduct.search.page_no', 1);
}
$viewData = $this->session->get('noshiproduct.admin.noshiproduct.search', []);
} else {
$page_no = 1;
$viewData = FormUtil::getViewData($searchForm);
$this->session->set('noshiproduct.admin.noshiproduct.search', $viewData);
$this->session->set('noshiproduct.admin.noshiproduct.search.page_no', $page_no);
}
$searchData = FormUtil::submitAndGetData($searchForm, $viewData);
}
$qb = $this->noshiproductRepository->getQueryBuilderBySearchData($searchData);
$pagination = $paginator->paginate(
$qb,
$page_no,
$pageCount
);
return [
'searchForm' => $searchForm->createView(),
'pagination' => $pagination,
'pageMaxis' => $pageMaxis,
'page_no' => $page_no,
'page_count' => $pageCount,
'CsvType' => $CsvType,
'has_errors' => false,
];
}
/**
* フロントページ 登録・編集する。
*
* @Route("/noshiproduct/new", name="noshiproduct_new")
* @Route("/noshiproduct/{fixed}/edit", requirements={"fixed" = "\d+"}, name="noshiproduct_edit")
*/
public function edit(Request $request, Noshiproduct $Noshiproduct = null, CacheUtil $cacheUtil)
{
// URLパラメータ取得
$order_id = $request->query->get('order');
if (is_null($Noshiproduct)) {
$Noshiproduct = $this->noshiproductRepository->findOneBy([], ['sort_no' => 'DESC']);
$sortNo = 1;
if ($Noshiproduct) {
$sortNo = $Noshiproduct->getSortNo() + 1;
}
$date = date("His").$order_id;
$Noshiproduct = new \Plugin\Noshiproduct\Entity\Noshiproduct();
$Noshiproduct
->setOrderId($order_id)
->setSortNo($sortNo)
->setFixed($date)
->setVisible(true);
}
$OrderItem = $this->orderItemRepository->findBy(['Order' => $order_id]);
$builder = $this->formFactory
->createBuilder(NoshiproductType::class, $Noshiproduct);
$form = $builder->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->noshiproductRepository->save($Noshiproduct);
// キャッシュの削除
$cacheUtil->clearDoctrineCache();
return $this->redirectToRoute('shopping', ['id' => $Noshiproduct->getId()]);
}
return $this->render('Noshiproduct/Resource/template/default/noshiproduct_edit.twig', [
'form' => $form->createView(),
'Noshiproduct' => $Noshiproduct,
'OrderItem' => $OrderItem,
]);
}
/**
* フロントページで、指定したデータを削除する。
*
* @param Request $request
* @param Noshiproduct $Noshiproduct
*
* @throws \Exception
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* @Route("/noshiproduct/{fixed}/delete", requirements={"fixed" = "\d+"}, name="noshiproduct_delete")
*/
public function delete(Request $request, Noshiproduct $Noshiproduct, CacheUtil $cacheUtil)
{
try {
$this->noshiproductRepository->delete($Noshiproduct);
// キャッシュの削除
$cacheUtil->clearDoctrineCache();
} catch (\Exception $e) {
}
$sortNo = 1;
$Noshiproducts = $this->noshiproductRepository
->findBy([], ['sort_no' => 'ASC']);
foreach ($Noshiproducts as $Noshiproduct) {
$Noshiproduct->setSortNo($sortNo);
$sortNo++;
}
$this->entityManager->flush();
return $this->redirectToRoute('shopping');
}
/**
* 管理画面編集 受注管理>のし希望一覧
*
* @Route("%eccube_admin_route%/order/noshiproduct/{id}/edit", requirements={"id" = "\d+"}, name="noshiproduct_admin_edit")
* @Template("@Noshiproduct/admin/edit.twig")
*/
public function editAdmin(Request $request, Noshiproduct $Noshiproduct = null, CacheUtil $cacheUtil)
{
$builder = $this->formFactory
->createBuilder(NoshiproductType::class, $Noshiproduct);
$form = $builder->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->noshiproductRepository->save($Noshiproduct);
// キャッシュの削除
$cacheUtil->clearDoctrineCache();
$this->addSuccess('admin.common.save_complete', 'admin');
return $this->redirectToRoute('noshiproduct_admin_edit', ['id' => $Noshiproduct->getId()]);
}
return [
'form' => $form->createView(),
'Noshiproduct' => $Noshiproduct,
];
}
/**
* 管理画面で、指定したデータを削除する。 受注管理>のし希望一覧
*
* @Method("DELETE")
* @Route("%eccube_admin_route%/order/noshiproduct/{id}/delete", name="admin_noshiproduct_delete")
*
* @param Request $request
* @param int $id
*
* @return RedirectResponse
*/
public function deleteAdmin(Noshiproduct $Noshiproduct)
{
$this->isTokenValid();
$this->entityManager->remove($Noshiproduct);
$this->entityManager->flush($Noshiproduct);
$this->addSuccess('admin.common.delete_complete', 'admin');
return $this->redirect($this->generateUrl('admin_order_noshiproduct'));
}
/**
* CSVの出力.
*
* @Route("%eccube_admin_route%/order/noshiproduct/export", name="admin_noshiproduct_export", methods={"GET"})
*
* @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) {
/** @var NoshiproductConfig $Config */
$Config = $this->noshiproductConfigRepository->get();
$csvType = $Config->getCsvType();
/* @var $csvService CsvExportService */
$csvService = $this->csvExportService;
/* @var $repo NoshiproductRepository */
$repo = $this->noshiproductRepository;
// CSV種別を元に初期化.
$csvService->initCsvType($csvType);
// ヘッダ行の出力.
$csvService->exportHeader();
$session = $request->getSession();
$searchForm = $this->createForm(NoshiproductSearchType::class);
$viewData = $session->get('eccube.admin.product.search', []);
$searchData = FormUtil::submitAndGetData($searchForm, $viewData);
$qb = $repo->getQueryBuilderBySearchData($searchData);
// データ行の出力.
$csvService->setExportQueryBuilder($qb);
$csvService->exportData(function ($entity, CsvExportService $csvService) {
$arrCsv = $csvService->getCsvs();
$row = [];
// CSV出力項目と合致するデータを取得.
foreach ($arrCsv as $csv) {
// 受注データを検索.
$data = $csvService->getData($csv, $entity);
$row[] = $data;
}
// 出力.
$csvService->fputcsv($row);
});
});
$now = new \DateTime();
$filename = 'noshiproduct_'.$now->format('YmdHis').'.csv';
$response->headers->set('Content-Type', 'application/octet-stream');
$response->headers->set('Content-Disposition', 'attachment; filename='.$filename);
log_info('のし希望CSV出力ファイル名', [$filename]);
return $response;
}
}