app/Plugin/Noshiproduct/Controller/NoshiproductController.php line 94

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 Plugin\Noshiproduct\Controller;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Repository\Master\PageMaxRepository;
  15. use Eccube\Entity\OrderItem;
  16. use Eccube\Repository\OrderItemRepository;
  17. use Plugin\Noshiproduct\Entity\Noshiproduct;
  18. use Plugin\Noshiproduct\Entity\NoshiproductConfig;
  19. use Plugin\Noshiproduct\Form\Type\NoshiproductType;
  20. use Plugin\Noshiproduct\Form\Type\Admin\NoshiproductSearchType;
  21. use Plugin\Noshiproduct\Repository\NoshiproductRepository;
  22. use Plugin\Noshiproduct\Repository\NoshiproductConfigRepository;
  23. use Eccube\Util\CacheUtil;
  24. use Eccube\Util\FormUtil;
  25. use Knp\Component\Pager\PaginatorInterface;
  26. use Symfony\Component\Form\Form;
  27. use Symfony\Component\HttpFoundation\RedirectResponse;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\HttpFoundation\StreamedResponse;
  31. use Eccube\Entity\Master\CsvType;
  32. use Eccube\Service\CsvExportService;
  33. use Symfony\Component\Routing\Annotation\Route;
  34. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  35. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  36. /**
  37.  * Class NoshiproductController.
  38.  */
  39. class NoshiproductController extends AbstractController
  40. {
  41.     /**
  42.      * @var CsvExportService
  43.      */
  44.     protected $csvExportService;
  45.     /**
  46.      * @var PageMaxRepository
  47.      */
  48.     protected $pageMaxRepository;
  49.     /**
  50.      * @var OrdeItemrRepository
  51.      */
  52.     protected $orderItemRepository;
  53.     /**
  54.      * @var NoshiproductRepository
  55.      */
  56.     private $noshiproductRepository;
  57.     /**
  58.      * @var NoshiproductConfigRepository
  59.      */
  60.     private $noshiproductConfigRepository;
  61.     /**
  62.      * NoshiproductController constructor.
  63.      *
  64.      * @param NoshiproductRepository $noshiproductRepository
  65.      */
  66.     public function __construct(CsvExportService $csvExportServicePageMaxRepository $pageMaxRepositoryOrderItemRepository $orderItemRepositoryNoshiproductRepository $noshiproductRepositoryNoshiproductConfigRepository $noshiproductConfigRepository)
  67.     {
  68.         $this->csvExportService $csvExportService;
  69.         $this->pageMaxRepository $pageMaxRepository;
  70.         $this->orderItemRepository $orderItemRepository;
  71.         $this->noshiproductRepository $noshiproductRepository;
  72.         $this->noshiproductConfigRepository $noshiproductConfigRepository;
  73.     }
  74.     /**
  75.      * 一覧を表示する。
  76.      *
  77.      * @param Request     $request
  78.      *
  79.      * @return array
  80.      * @Route("/%eccube_admin_route%/order/noshiproduct", name="admin_order_noshiproduct")
  81.      * @Route("/%eccube_admin_route%/order/noshiproduct/page/{page_no}", requirements={"page_no" = "\d+"}, name="admin_order_noshiproduct_page")
  82.      * @Template("@Noshiproduct/admin/index.twig")
  83.      */
  84.     public function index(Request $request$page_no 1PaginatorInterface $paginator)
  85.     {
  86.         $CsvType $this->noshiproductConfigRepository
  87.             ->get()
  88.             ->getCsvType();
  89.         $builder $this->formFactory->createBuilder(NoshiproductSearchType::class);
  90.         $searchForm $builder->getForm();
  91.         $pageMaxis $this->pageMaxRepository->findAll();
  92.         $pageCount $this->session->get(
  93.             'noshiproduct.admin.noshiproduct.search.page_count',
  94.             $this->eccubeConfig['eccube_default_page_count']
  95.         );
  96.         $pageCountParam $request->get('page_count');
  97.         if ($pageCountParam && is_numeric($pageCountParam)) {
  98.             foreach ($pageMaxis as $pageMax) {
  99.                 if ($pageCountParam == $pageMax->getName()) {
  100.                     $pageCount $pageMax->getName();
  101.                     $this->session->set('noshiproduct.admin.noshiproduct.search.page_count'$pageCount);
  102.                     break;
  103.                 }
  104.             }
  105.         }
  106.         if ('POST' === $request->getMethod()) {
  107.             $searchForm->handleRequest($request);
  108.             if ($searchForm->isValid()) {
  109.                 $searchData $searchForm->getData();
  110.                 $page_no 1;
  111.                 $this->session->set('noshiproduct.admin.noshiproduct.search'FormUtil::getViewData($searchForm));
  112.                 $this->session->set('noshiproduct.admin.noshiproduct.search.page_no'$page_no);
  113.             } else {
  114.                 return [
  115.                     'searchForm' => $searchForm->createView(),
  116.                     'pagination' => [],
  117.                     'pageMaxis' => $pageMaxis,
  118.                     'page_no' => $page_no,
  119.                     'page_count' => $pageCount,
  120.                     'CsvType' => $CsvType,
  121.                     'has_errors' => true,
  122.                 ];
  123.             }
  124.         } else {
  125.             if (null !== $page_no || $request->get('resume')) {
  126.                 if ($page_no) {
  127.                     $this->session->set('noshiproduct.admin.noshiproduct.search.page_no', (int) $page_no);
  128.                 } else {
  129.                     $page_no $this->session->get('noshiproduct.admin.noshiproduct.search.page_no'1);
  130.                 }
  131.                 $viewData $this->session->get('noshiproduct.admin.noshiproduct.search', []);
  132.             } else {
  133.                 $page_no 1;
  134.                 $viewData FormUtil::getViewData($searchForm);
  135.                 $this->session->set('noshiproduct.admin.noshiproduct.search'$viewData);
  136.                 $this->session->set('noshiproduct.admin.noshiproduct.search.page_no'$page_no);
  137.             }
  138.             $searchData FormUtil::submitAndGetData($searchForm$viewData);
  139.         }
  140.         $qb $this->noshiproductRepository->getQueryBuilderBySearchData($searchData);
  141.         $pagination $paginator->paginate(
  142.             $qb,
  143.             $page_no,
  144.             $pageCount
  145.         );
  146.         return [
  147.             'searchForm' => $searchForm->createView(),
  148.             'pagination' => $pagination,
  149.             'pageMaxis' => $pageMaxis,
  150.             'page_no' => $page_no,
  151.             'page_count' => $pageCount,
  152.             'CsvType' => $CsvType,
  153.             'has_errors' => false,
  154.         ];
  155.     }
  156.     /**
  157.      * フロントページ 登録・編集する。
  158.      *
  159.      * @Route("/noshiproduct/new", name="noshiproduct_new")
  160.      * @Route("/noshiproduct/{fixed}/edit", requirements={"fixed" = "\d+"}, name="noshiproduct_edit")
  161.      */
  162.     public function edit(Request $requestNoshiproduct $Noshiproduct nullCacheUtil $cacheUtil)
  163.     {
  164.         // URLパラメータ取得
  165.         $order_id $request->query->get('order');
  166.         if (is_null($Noshiproduct)) {
  167.             $Noshiproduct $this->noshiproductRepository->findOneBy([], ['sort_no' => 'DESC']);
  168.             $sortNo 1;
  169.             if ($Noshiproduct) {
  170.                 $sortNo $Noshiproduct->getSortNo() + 1;
  171.             }
  172.             
  173.             $date date("His").$order_id;
  174.             
  175.             $Noshiproduct = new \Plugin\Noshiproduct\Entity\Noshiproduct();
  176.             $Noshiproduct
  177.                 ->setOrderId($order_id)
  178.                 ->setSortNo($sortNo)
  179.                 ->setFixed($date)
  180.                 ->setVisible(true);
  181.         }
  182.         
  183.         $OrderItem $this->orderItemRepository->findBy(['Order' => $order_id]);
  184.         $builder $this->formFactory
  185.             ->createBuilder(NoshiproductType::class, $Noshiproduct);
  186.         $form $builder->getForm();
  187.         
  188.         $form->handleRequest($request);
  189.         if ($form->isSubmitted() && $form->isValid()) {
  190.             $this->noshiproductRepository->save($Noshiproduct);
  191.             // キャッシュの削除
  192.             $cacheUtil->clearDoctrineCache();
  193.             return $this->redirectToRoute('shopping', ['id' => $Noshiproduct->getId()]);
  194.         }
  195.         return $this->render('Noshiproduct/Resource/template/default/noshiproduct_edit.twig', [
  196.             'form' => $form->createView(),
  197.             'Noshiproduct' => $Noshiproduct,
  198.             'OrderItem' => $OrderItem,
  199.         ]);
  200.     }
  201.     /**
  202.      * フロントページで、指定したデータを削除する。
  203.      *
  204.      * @param Request     $request
  205.      * @param Noshiproduct $Noshiproduct
  206.      *
  207.      * @throws \Exception
  208.      *
  209.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  210.      * @Route("/noshiproduct/{fixed}/delete", requirements={"fixed" = "\d+"}, name="noshiproduct_delete")
  211.      */
  212.     public function delete(Request $requestNoshiproduct $NoshiproductCacheUtil $cacheUtil)
  213.     {
  214.         try {
  215.             $this->noshiproductRepository->delete($Noshiproduct);
  216.             // キャッシュの削除
  217.             $cacheUtil->clearDoctrineCache();
  218.         } catch (\Exception $e) {
  219.         }
  220.         $sortNo 1;
  221.         $Noshiproducts $this->noshiproductRepository
  222.             ->findBy([], ['sort_no' => 'ASC']);
  223.         foreach ($Noshiproducts as $Noshiproduct) {
  224.                 $Noshiproduct->setSortNo($sortNo);
  225.                 $sortNo++;
  226.         }
  227.         $this->entityManager->flush();
  228.         return $this->redirectToRoute('shopping');
  229.     }
  230.     /**
  231.      * 管理画面編集 受注管理>のし希望一覧
  232.      *
  233.      * @Route("%eccube_admin_route%/order/noshiproduct/{id}/edit", requirements={"id" = "\d+"}, name="noshiproduct_admin_edit")
  234.      * @Template("@Noshiproduct/admin/edit.twig")
  235.      */
  236.     public function editAdmin(Request $requestNoshiproduct $Noshiproduct nullCacheUtil $cacheUtil)
  237.     {
  238.         
  239.         $builder $this->formFactory
  240.             ->createBuilder(NoshiproductType::class, $Noshiproduct);
  241.         $form $builder->getForm();
  242.         
  243.         $form->handleRequest($request);
  244.         if ($form->isSubmitted() && $form->isValid()) {
  245.             $this->noshiproductRepository->save($Noshiproduct);
  246.             // キャッシュの削除
  247.             $cacheUtil->clearDoctrineCache();
  248.             $this->addSuccess('admin.common.save_complete''admin');
  249.             return $this->redirectToRoute('noshiproduct_admin_edit', ['id' => $Noshiproduct->getId()]);
  250.         }
  251.         return [
  252.             'form' => $form->createView(),
  253.             'Noshiproduct' => $Noshiproduct,
  254.         ];
  255.     }
  256.     /**
  257.      * 管理画面で、指定したデータを削除する。 受注管理>のし希望一覧
  258.      *
  259.      * @Method("DELETE")
  260.      * @Route("%eccube_admin_route%/order/noshiproduct/{id}/delete", name="admin_noshiproduct_delete")
  261.      *
  262.      * @param Request $request
  263.      * @param int $id
  264.      *
  265.      * @return RedirectResponse
  266.      */
  267.     public function deleteAdmin(Noshiproduct $Noshiproduct)
  268.     {
  269.         $this->isTokenValid();
  270.         $this->entityManager->remove($Noshiproduct);
  271.         $this->entityManager->flush($Noshiproduct);
  272.         $this->addSuccess('admin.common.delete_complete''admin');
  273.         return $this->redirect($this->generateUrl('admin_order_noshiproduct'));
  274.     }
  275.     /**
  276.      * CSVの出力.
  277.      *
  278.      * @Route("%eccube_admin_route%/order/noshiproduct/export", name="admin_noshiproduct_export", methods={"GET"})
  279.      *
  280.      * @param Request $request
  281.      *
  282.      * @return StreamedResponse
  283.      */
  284.     public function export(Request $request)
  285.     {
  286.         // タイムアウトを無効にする.
  287.         set_time_limit(0);
  288.         // sql loggerを無効にする.
  289.         $em $this->entityManager;
  290.         $em->getConfiguration()->setSQLLogger(null);
  291.         
  292.         $response = new StreamedResponse();
  293.         $response->setCallback(function () use ($request) {
  294.             /** @var NoshiproductConfig $Config */
  295.             $Config $this->noshiproductConfigRepository->get();
  296.             $csvType $Config->getCsvType();
  297.             /* @var $csvService CsvExportService */
  298.             $csvService $this->csvExportService;
  299.             /* @var $repo NoshiproductRepository */
  300.             $repo $this->noshiproductRepository;
  301.             // CSV種別を元に初期化.
  302.             $csvService->initCsvType($csvType);
  303.             // ヘッダ行の出力.
  304.             $csvService->exportHeader();
  305.             $session $request->getSession();
  306.             $searchForm $this->createForm(NoshiproductSearchType::class);
  307.             $viewData $session->get('eccube.admin.product.search', []);
  308.             $searchData FormUtil::submitAndGetData($searchForm$viewData);
  309.             $qb $repo->getQueryBuilderBySearchData($searchData);
  310.             // データ行の出力.
  311.             $csvService->setExportQueryBuilder($qb);
  312.             $csvService->exportData(function ($entityCsvExportService $csvService) {
  313.                 $arrCsv $csvService->getCsvs();
  314.                 $row = [];
  315.                 // CSV出力項目と合致するデータを取得.
  316.                 foreach ($arrCsv as $csv) {
  317.                     // 受注データを検索.
  318.                     $data $csvService->getData($csv$entity);
  319.                     $row[] = $data;
  320.                 }
  321.                 // 出力.
  322.                 $csvService->fputcsv($row);
  323.             });
  324.         });
  325.         $now = new \DateTime();
  326.         $filename 'noshiproduct_'.$now->format('YmdHis').'.csv';
  327.         $response->headers->set('Content-Type''application/octet-stream');
  328.         $response->headers->set('Content-Disposition''attachment; filename='.$filename);
  329.         log_info('のし希望CSV出力ファイル名', [$filename]);
  330.         return $response;
  331.     }
  332. }