src/Eccube/Controller/Admin/Customer/CustomerController.php line 93

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\Customer;
  13. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  14. use Doctrine\ORM\QueryBuilder;
  15. use Eccube\Common\Constant;
  16. use Eccube\Controller\AbstractController;
  17. use Eccube\Entity\Master\CsvType;
  18. use Eccube\Event\EccubeEvents;
  19. use Eccube\Event\EventArgs;
  20. use Eccube\Form\Type\Admin\SearchCustomerType;
  21. use Eccube\Repository\CustomerRepository;
  22. use Eccube\Repository\Master\PageMaxRepository;
  23. use Eccube\Repository\Master\PrefRepository;
  24. use Eccube\Repository\Master\SexRepository;
  25. use Eccube\Service\CsvExportService;
  26. use Eccube\Service\MailService;
  27. use Eccube\Util\FormUtil;
  28. use Knp\Component\Pager\PaginatorInterface;
  29. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  30. use Symfony\Component\HttpFoundation\Request;
  31. use Symfony\Component\HttpFoundation\StreamedResponse;
  32. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  33. use Symfony\Component\Routing\Annotation\Route;
  34. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  35. use Symfony\Contracts\Translation\TranslatorInterface;
  36. class CustomerController extends AbstractController
  37. {
  38.     /**
  39.      * @var CsvExportService
  40.      */
  41.     protected $csvExportService;
  42.     /**
  43.      * @var MailService
  44.      */
  45.     protected $mailService;
  46.     /**
  47.      * @var PrefRepository
  48.      */
  49.     protected $prefRepository;
  50.     /**
  51.      * @var SexRepository
  52.      */
  53.     protected $sexRepository;
  54.     /**
  55.      * @var PageMaxRepository
  56.      */
  57.     protected $pageMaxRepository;
  58.     /**
  59.      * @var CustomerRepository
  60.      */
  61.     protected $customerRepository;
  62.     public function __construct(
  63.         PageMaxRepository $pageMaxRepository,
  64.         CustomerRepository $customerRepository,
  65.         SexRepository $sexRepository,
  66.         PrefRepository $prefRepository,
  67.         MailService $mailService,
  68.         CsvExportService $csvExportService
  69.     ) {
  70.         $this->pageMaxRepository $pageMaxRepository;
  71.         $this->customerRepository $customerRepository;
  72.         $this->sexRepository $sexRepository;
  73.         $this->prefRepository $prefRepository;
  74.         $this->mailService $mailService;
  75.         $this->csvExportService $csvExportService;
  76.     }
  77.     /**
  78.      * @Route("/%eccube_admin_route%/customer", name="admin_customer", methods={"GET", "POST"})
  79.      * @Route("/%eccube_admin_route%/customer/page/{page_no}", requirements={"page_no" = "\d+"}, name="admin_customer_page", methods={"GET", "POST"})
  80.      * @Template("@admin/Customer/index.twig")
  81.      */
  82.     public function index(Request $requestPaginatorInterface $paginator$page_no null)
  83.     {
  84.         $session $this->session;
  85.         $builder $this->formFactory->createBuilder(SearchCustomerType::class);
  86.         $event = new EventArgs(
  87.             [
  88.                 'builder' => $builder,
  89.             ],
  90.             $request
  91.         );
  92.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CUSTOMER_INDEX_INITIALIZE);
  93.         $searchForm $builder->getForm();
  94.         $pageMaxis $this->pageMaxRepository->findAll();
  95.         $pageCount $session->get('eccube.admin.customer.search.page_count'$this->eccubeConfig['eccube_default_page_count']);
  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.                     $session->set('eccube.admin.customer.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.                 $session->set('eccube.admin.customer.search'FormUtil::getViewData($searchForm));
  112.                 $session->set('eccube.admin.customer.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.                     'has_errors' => true,
  121.                 ];
  122.             }
  123.         } else {
  124.             if (null !== $page_no || $request->get('resume')) {
  125.                 if ($page_no) {
  126.                     $session->set('eccube.admin.customer.search.page_no', (int) $page_no);
  127.                 } else {
  128.                     $page_no $session->get('eccube.admin.customer.search.page_no'1);
  129.                 }
  130.                 $viewData $session->get('eccube.admin.customer.search', []);
  131.             } else {
  132.                 $page_no 1;
  133.                 $viewData FormUtil::getViewData($searchForm);
  134.                 $session->set('eccube.admin.customer.search'$viewData);
  135.                 $session->set('eccube.admin.customer.search.page_no'$page_no);
  136.             }
  137.             $searchData FormUtil::submitAndGetData($searchForm$viewData);
  138.         }
  139.         /** @var QueryBuilder $qb */
  140.         $qb $this->customerRepository->getQueryBuilderBySearchData($searchData);
  141.         $event = new EventArgs(
  142.             [
  143.                 'form' => $searchForm,
  144.                 'qb' => $qb,
  145.             ],
  146.             $request
  147.         );
  148.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CUSTOMER_INDEX_SEARCH);
  149.         $pagination $paginator->paginate(
  150.             $qb,
  151.             $page_no,
  152.             $pageCount
  153.         );
  154.         return [
  155.             'searchForm' => $searchForm->createView(),
  156.             'pagination' => $pagination,
  157.             'pageMaxis' => $pageMaxis,
  158.             'page_no' => $page_no,
  159.             'page_count' => $pageCount,
  160.             'has_errors' => false,
  161.         ];
  162.     }
  163.     /**
  164.      * @Route("/%eccube_admin_route%/customer/{id}/resend", requirements={"id" = "\d+"}, name="admin_customer_resend", methods={"GET"})
  165.      */
  166.     public function resend(Request $request$id)
  167.     {
  168.         $this->isTokenValid();
  169.         $Customer $this->customerRepository
  170.             ->find($id);
  171.         if (is_null($Customer)) {
  172.             throw new NotFoundHttpException();
  173.         }
  174.         $secretKey $this->customerRepository->getUniqueSecretKey();
  175.         $Customer->setSecretKey($secretKey);
  176.         $this->entityManager->persist($Customer);
  177.         $this->entityManager->flush();
  178.         $activateUrl $this->generateUrl(
  179.             'entry_activate',
  180.             ['secret_key' => $Customer->getSecretKey()],
  181.             UrlGeneratorInterface::ABSOLUTE_URL
  182.         );
  183.         // メール送信
  184.         $this->mailService->sendAdminCustomerConfirmMail($Customer$activateUrl);
  185.         $event = new EventArgs(
  186.             [
  187.                 'Customer' => $Customer,
  188.                 'activateUrl' => $activateUrl,
  189.             ],
  190.             $request
  191.         );
  192.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CUSTOMER_RESEND_COMPLETE);
  193.         $this->addSuccess('admin.common.send_complete''admin');
  194.         return $this->redirectToRoute('admin_customer');
  195.     }
  196.     /**
  197.      * @Route("/%eccube_admin_route%/customer/{id}/delete", requirements={"id" = "\d+"}, name="admin_customer_delete", methods={"DELETE"})
  198.      */
  199.     public function delete(Request $request$idTranslatorInterface $translator)
  200.     {
  201.         $this->isTokenValid();
  202.         log_info('会員削除開始', [$id]);
  203.         $page_no intval($this->session->get('eccube.admin.customer.search.page_no'));
  204.         $page_no $page_no $page_no Constant::ENABLED;
  205.         $Customer $this->customerRepository
  206.             ->find($id);
  207.         if (!$Customer) {
  208.             $this->deleteMessage();
  209.             return $this->redirect($this->generateUrl('admin_customer_page',
  210.                     ['page_no' => $page_no]).'?resume='.Constant::ENABLED);
  211.         }
  212.         try {
  213.             $this->entityManager->remove($Customer);
  214.             $this->entityManager->flush();
  215.             $this->addSuccess('admin.common.delete_complete''admin');
  216.         } catch (ForeignKeyConstraintViolationException $e) {
  217.             log_error('会員削除失敗', [$e]);
  218.             $message trans('admin.common.delete_error_foreign_key', ['%name%' => $Customer->getName01().' '.$Customer->getName02()]);
  219.             $this->addError($message'admin');
  220.         }
  221.         log_info('会員削除完了', [$id]);
  222.         $event = new EventArgs(
  223.             [
  224.                 'Customer' => $Customer,
  225.             ],
  226.             $request
  227.         );
  228.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CUSTOMER_DELETE_COMPLETE);
  229.         return $this->redirect($this->generateUrl('admin_customer_page',
  230.                 ['page_no' => $page_no]).'?resume='.Constant::ENABLED);
  231.     }
  232.     /**
  233.      * 会員CSVの出力.
  234.      *
  235.      * @Route("/%eccube_admin_route%/customer/export", name="admin_customer_export", methods={"GET"})
  236.      *
  237.      * @param Request $request
  238.      *
  239.      * @return StreamedResponse
  240.      */
  241.     public function export(Request $request)
  242.     {
  243.         // タイムアウトを無効にする.
  244.         set_time_limit(0);
  245.         // sql loggerを無効にする.
  246.         $em $this->entityManager;
  247.         $em->getConfiguration()->setSQLLogger(null);
  248.         $response = new StreamedResponse();
  249.         $response->setCallback(function () use ($request) {
  250.             // CSV種別を元に初期化.
  251.             $this->csvExportService->initCsvType(CsvType::CSV_TYPE_CUSTOMER);
  252.             // 会員データ検索用のクエリビルダを取得.
  253.             $qb $this->csvExportService
  254.                 ->getCustomerQueryBuilder($request);
  255.             // ヘッダ行の出力.
  256.             $this->csvExportService->exportHeader();
  257.             // データ行の出力.
  258.             $this->csvExportService->setExportQueryBuilder($qb);
  259.             $this->csvExportService->exportData(function ($entity$csvService) use ($request) {
  260.                 $Csvs $csvService->getCsvs();
  261.                 /** @var $Customer \Eccube\Entity\Customer */
  262.                 $Customer $entity;
  263.                 $ExportCsvRow = new \Eccube\Entity\ExportCsvRow();
  264.                 // CSV出力項目と合致するデータを取得.
  265.                 foreach ($Csvs as $Csv) {
  266.                     // 会員データを検索.
  267.                     $ExportCsvRow->setData($csvService->getData($Csv$Customer));
  268.                     $event = new EventArgs(
  269.                         [
  270.                             'csvService' => $csvService,
  271.                             'Csv' => $Csv,
  272.                             'Customer' => $Customer,
  273.                             'ExportCsvRow' => $ExportCsvRow,
  274.                         ],
  275.                         $request
  276.                     );
  277.                     $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CUSTOMER_CSV_EXPORT);
  278.                     $ExportCsvRow->pushData();
  279.                 }
  280.                 // $row[] = number_format(memory_get_usage(true));
  281.                 // 出力.
  282.                 $csvService->fputcsv($ExportCsvRow->getRow());
  283.             });
  284.         });
  285.         $now = new \DateTime();
  286.         $filename 'customer_'.$now->format('YmdHis').'.csv';
  287.         $response->headers->set('Content-Type''application/octet-stream');
  288.         $response->headers->set('Content-Disposition''attachment; filename='.$filename);
  289.         log_info('会員CSVファイル名', [$filename]);
  290.         return $response;
  291.     }
  292. }