src/Eccube/Controller/Admin/Customer/CustomerEditController.php line 74

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 Eccube\Controller\AbstractController;
  14. use Eccube\Entity\Customer;
  15. use Eccube\Entity\Master\CustomerStatus;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Form\Type\Admin\CustomerType;
  19. use Eccube\Repository\CustomerRepository;
  20. use Eccube\Repository\Master\PageMaxRepository;
  21. use Eccube\Repository\OrderRepository;
  22. use Eccube\Util\StringUtil;
  23. use Knp\Component\Pager\PaginatorInterface;
  24. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  29. class CustomerEditController extends AbstractController
  30. {
  31.     /**
  32.      * @var CustomerRepository
  33.      */
  34.     protected $customerRepository;
  35.     /**
  36.      * @var EncoderFactoryInterface
  37.      */
  38.     protected $encoderFactory;
  39.     /**
  40.      * @var OrderRepository
  41.      */
  42.     protected $orderRepository;
  43.     /**
  44.      * @var PageMaxRepository
  45.      */
  46.     protected $pageMaxRepository;
  47.     public function __construct(
  48.         CustomerRepository $customerRepository,
  49.         EncoderFactoryInterface $encoderFactory,
  50.         OrderRepository $orderRepository,
  51.         PageMaxRepository $pageMaxRepository
  52.     ) {
  53.         $this->customerRepository $customerRepository;
  54.         $this->orderRepository $orderRepository;
  55.         $this->encoderFactory $encoderFactory;
  56.         $this->pageMaxRepository $pageMaxRepository;
  57.     }
  58.     /**
  59.      * @Route("/%eccube_admin_route%/customer/new", name="admin_customer_new", methods={"GET", "POST"})
  60.      * @Route("/%eccube_admin_route%/customer/{id}/edit", requirements={"id" = "\d+"}, name="admin_customer_edit", methods={"GET", "POST"})
  61.      * @Template("@admin/Customer/edit.twig")
  62.      */
  63.     public function index(Request $requestPaginatorInterface $paginator$id null)
  64.     {
  65.         $this->entityManager->getFilters()->enable('incomplete_order_status_hidden');
  66.         // 編集
  67.         if ($id) {
  68.             /** @var Customer $Customer */
  69.             $Customer $this->customerRepository
  70.                 ->find($id);
  71.             if (is_null($Customer)) {
  72.                 throw new NotFoundHttpException();
  73.             }
  74.             $oldStatusId $Customer->getStatus()->getId();
  75.             $Customer->setPlainPassword($this->eccubeConfig['eccube_default_password']);
  76.         // 新規登録
  77.         } else {
  78.             $Customer $this->customerRepository->newCustomer();
  79.             $oldStatusId null;
  80.         }
  81.         // 会員登録フォーム
  82.         $builder $this->formFactory
  83.             ->createBuilder(CustomerType::class, $Customer);
  84.         $event = new EventArgs(
  85.             [
  86.                 'builder' => $builder,
  87.                 'Customer' => $Customer,
  88.             ],
  89.             $request
  90.         );
  91.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_INITIALIZE);
  92.         $form $builder->getForm();
  93.         $form->handleRequest($request);
  94.         $page_count = (int) $this->session->get('eccube.admin.customer_edit.order.page_count',
  95.             $this->eccubeConfig->get('eccube_default_page_count'));
  96.         $page_count_param = (int) $request->get('page_count');
  97.         $pageMaxis $this->pageMaxRepository->findAll();
  98.         if ($page_count_param) {
  99.             foreach ($pageMaxis as $pageMax) {
  100.                 if ($page_count_param == $pageMax->getName()) {
  101.                     $page_count $pageMax->getName();
  102.                     $this->session->set('eccube.admin.customer_edit.order.page_count'$page_count);
  103.                     break;
  104.                 }
  105.             }
  106.         }
  107.         $page_no = (int) $request->get('page_no'1);
  108.         $qb $this->orderRepository->getQueryBuilderByCustomer($Customer);
  109.         $pagination = [];
  110.         if (!is_null($Customer->getId())) {
  111.             $pagination $paginator->paginate(
  112.                 $qb,
  113.                 $page_no $page_no 1,
  114.                 $page_count
  115.             );
  116.         }
  117.         if ($form->isSubmitted() && $form->isValid()) {
  118.             log_info('会員登録開始', [$Customer->getId()]);
  119.             $encoder $this->encoderFactory->getEncoder($Customer);
  120.             if ($Customer->getPlainPassword() !== $this->eccubeConfig['eccube_default_password']) {
  121.                 if ($Customer->getSalt() === null) {
  122.                     $Customer->setSalt($encoder->createSalt());
  123.                     $Customer->setSecretKey($this->customerRepository->getUniqueSecretKey());
  124.                 }
  125.                 $Customer->setPassword($encoder->encodePassword($Customer->getPlainPassword(), $Customer->getSalt()));
  126.             }
  127.             // 退会ステータスに更新の場合、ダミーのアドレスに更新
  128.             $newStatusId $Customer->getStatus()->getId();
  129.             if ($oldStatusId != $newStatusId && $newStatusId == CustomerStatus::WITHDRAWING) {
  130.                 $Customer->setEmail(StringUtil::random(60).'@dummy.dummy');
  131.             }
  132.             $this->entityManager->persist($Customer);
  133.             $this->entityManager->flush();
  134.             log_info('会員登録完了', [$Customer->getId()]);
  135.             $event = new EventArgs(
  136.                 [
  137.                     'form' => $form,
  138.                     'Customer' => $Customer,
  139.                 ],
  140.                 $request
  141.             );
  142.             $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE);
  143.             $this->addSuccess('admin.common.save_complete''admin');
  144.             return $this->redirectToRoute('admin_customer_edit', [
  145.                 'id' => $Customer->getId(),
  146.             ]);
  147.         }
  148.         return [
  149.             'form' => $form->createView(),
  150.             'Customer' => $Customer,
  151.             'pagination' => $pagination,
  152.             'pageMaxis' => $pageMaxis,
  153.             'page_no' => $page_no,
  154.             'page_count' => $page_count,
  155.         ];
  156.     }
  157. }