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\Paginator;
  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\Component\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")
  79.      * @Route("/%eccube_admin_route%/customer/page/{page_no}", requirements={"page_no" = "\d+"}, name="admin_customer_page")
  80.      * @Template("@admin/Customer/index.twig")
  81.      */
  82.     public function index(Request $request$page_no nullPaginator $paginator)
  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(EccubeEvents::ADMIN_CUSTOMER_INDEX_INITIALIZE$event);
  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(EccubeEvents::ADMIN_CUSTOMER_INDEX_SEARCH$event);
  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")
  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.         $activateUrl $this->generateUrl(
  175.             'entry_activate',
  176.             ['secret_key' => $Customer->getSecretKey()],
  177.             UrlGeneratorInterface::ABSOLUTE_URL
  178.         );
  179.         // メール送信
  180.         $this->mailService->sendAdminCustomerConfirmMail($Customer$activateUrl);
  181.         $event = new EventArgs(
  182.             [
  183.                 'Customer' => $Customer,
  184.                 'activateUrl' => $activateUrl,
  185.             ],
  186.             $request
  187.         );
  188.         $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_RESEND_COMPLETE$event);
  189.         $this->addSuccess('admin.common.send_complete''admin');
  190.         return $this->redirectToRoute('admin_customer');
  191.     }
  192.     /**
  193.      * @Route("/%eccube_admin_route%/customer/{id}/delete", requirements={"id" = "\d+"}, name="admin_customer_delete", methods={"DELETE"})
  194.      */
  195.     public function delete(Request $request$idTranslatorInterface $translator)
  196.     {
  197.         $this->isTokenValid();
  198.         log_info('会員削除開始', [$id]);
  199.         $page_no intval($this->session->get('eccube.admin.customer.search.page_no'));
  200.         $page_no $page_no $page_no Constant::ENABLED;
  201.         $Customer $this->customerRepository
  202.             ->find($id);
  203.         if (!$Customer) {
  204.             $this->deleteMessage();
  205.             return $this->redirect($this->generateUrl('admin_customer_page',
  206.                     ['page_no' => $page_no]).'?resume='.Constant::ENABLED);
  207.         }
  208.         try {
  209.             $this->entityManager->remove($Customer);
  210.             $this->entityManager->flush($Customer);
  211.             $this->addSuccess('admin.common.delete_complete''admin');
  212.         } catch (ForeignKeyConstraintViolationException $e) {
  213.             log_error('会員削除失敗', [$e], 'admin');
  214.             $message trans('admin.common.delete_error_foreign_key', ['%name%' => $Customer->getName01().' '.$Customer->getName02()]);
  215.             $this->addError($message'admin');
  216.         }
  217.         log_info('会員削除完了', [$id]);
  218.         $event = new EventArgs(
  219.             [
  220.                 'Customer' => $Customer,
  221.             ],
  222.             $request
  223.         );
  224.         $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELETE_COMPLETE$event);
  225.         return $this->redirect($this->generateUrl('admin_customer_page',
  226.                 ['page_no' => $page_no]).'?resume='.Constant::ENABLED);
  227.     }
  228.     /**
  229.      * 会員CSVの出力.
  230.      *
  231.      * @Route("/%eccube_admin_route%/customer/export", name="admin_customer_export")
  232.      *
  233.      * @param Request $request
  234.      *
  235.      * @return StreamedResponse
  236.      */
  237.     public function export(Request $request)
  238.     {
  239.         // タイムアウトを無効にする.
  240.         set_time_limit(0);
  241.         // sql loggerを無効にする.
  242.         $em $this->entityManager;
  243.         $em->getConfiguration()->setSQLLogger(null);
  244.         $response = new StreamedResponse();
  245.         $response->setCallback(function () use ($request) {
  246.             // CSV種別を元に初期化.
  247.             $this->csvExportService->initCsvType(CsvType::CSV_TYPE_CUSTOMER);
  248.             // ヘッダ行の出力.
  249.             $this->csvExportService->exportHeader();
  250.             // 会員データ検索用のクエリビルダを取得.
  251.             $qb $this->csvExportService
  252.                 ->getCustomerQueryBuilder($request);
  253.             // データ行の出力.
  254.             $this->csvExportService->setExportQueryBuilder($qb);
  255.             $this->csvExportService->exportData(function ($entity$csvService) use ($request) {
  256.                 $Csvs $csvService->getCsvs();
  257.                 /** @var $Customer \Eccube\Entity\Customer */
  258.                 $Customer $entity;
  259.                 $ExportCsvRow = new \Eccube\Entity\ExportCsvRow();
  260.                 // CSV出力項目と合致するデータを取得.
  261.                 foreach ($Csvs as $Csv) {
  262.                     // 会員データを検索.
  263.                     $ExportCsvRow->setData($csvService->getData($Csv$Customer));
  264.                     $event = new EventArgs(
  265.                         [
  266.                             'csvService' => $csvService,
  267.                             'Csv' => $Csv,
  268.                             'Customer' => $Customer,
  269.                             'ExportCsvRow' => $ExportCsvRow,
  270.                         ],
  271.                         $request
  272.                     );
  273.                     $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_CSV_EXPORT$event);
  274.                     $ExportCsvRow->pushData();
  275.                 }
  276.                 //$row[] = number_format(memory_get_usage(true));
  277.                 // 出力.
  278.                 $csvService->fputcsv($ExportCsvRow->getRow());
  279.             });
  280.         });
  281.         $now = new \DateTime();
  282.         $filename 'customer_'.$now->format('YmdHis').'.csv';
  283.         $response->headers->set('Content-Type''application/octet-stream');
  284.         $response->headers->set('Content-Disposition''attachment; filename='.$filename);
  285.         $response->send();
  286.         log_info('会員CSVファイル名', [$filename]);
  287.         return $response;
  288.     }
  289. }