app/Plugin/ContactManagement4/Event.php line 82

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of ContactManagement4
  4.  *
  5.  * Copyright(c) U-Mebius Inc. All Rights Reserved.
  6.  *
  7.  * https://umebius.com/
  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\ContactManagement4;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Eccube\Event\EccubeEvents;
  15. use Eccube\Event\EventArgs;
  16. use Eccube\Event\TemplateEvent;
  17. use Plugin\ContactManagement4\Entity\Contact;
  18. use Plugin\ContactManagement4\Repository\ContactStatusRepository;
  19. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  22. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  23. class Event implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var AuthorizationCheckerInterface
  27.      */
  28.     private $authorizationChecker;
  29.     /**
  30.      * @var TokenStorageInterface
  31.      */
  32.     private $tokenStorage;
  33.     /**
  34.      * @var EntityManagerInterface
  35.      */
  36.     private $entityManager;
  37.     /**
  38.      * @var ContactStatusRepository
  39.      */
  40.     private $contactStatusRepository;
  41.     /**
  42.      * @var EventDispatcherInterface
  43.      */
  44.     private $eventDispatcher;
  45.     /**
  46.      * Event constructor.
  47.      */
  48.     public function __construct(
  49.         AuthorizationCheckerInterface $authorizationChecker,
  50.         TokenStorageInterface $tokenStorage,
  51.         EntityManagerInterface $entityManager,
  52.         ContactStatusRepository $contactStatusRepository,
  53.         EventDispatcherInterface $eventDispatcher
  54.     ) {
  55.         $this->authorizationChecker $authorizationChecker;
  56.         $this->tokenStorage $tokenStorage;
  57.         $this->entityManager $entityManager;
  58.         $this->contactStatusRepository $contactStatusRepository;
  59.         $this->eventDispatcher $eventDispatcher;
  60.     }
  61.     /**
  62.      * @return array
  63.      */
  64.     public static function getSubscribedEvents()
  65.     {
  66.         return [
  67.             EccubeEvents::FRONT_CONTACT_INDEX_COMPLETE => 'onContactComplete',
  68.             '@admin/Customer/edit.twig' => 'onAdminCustomerEditTwig',
  69.         ];
  70.     }
  71.     public function onContactComplete(EventArgs $event)
  72.     {
  73.         $data $event->getArgument('data');
  74.         $Contact = new Contact();
  75.         // エンティティを更新
  76.         $Contact
  77.             ->setCustomer($this->authorizationChecker->isGranted('ROLE_USER') ? $this->tokenStorage->getToken()->getUser() : null)
  78.             ->setContactDate(new \DateTime())
  79.             ->setName01($data['name01'])
  80.             ->setName02($data['name02'])
  81.             ->setKana01(@$data['kana01'])
  82.             ->setKana02(@$data['kana02'])
  83.             ->setPostalCode(@$data['postal_code'])
  84.             ->setPref(@$data['pref'])
  85.             ->setAddr01(@$data['addr01'])
  86.             ->setAddr02(@$data['addr02'])
  87.             ->setPhoneNumber($data['phone_number'])
  88.             ->setEmail($data['email'])
  89.             ->setContents($data['contents'])
  90.             ->setStatus($this->contactStatusRepository->findOneBy([], ['sort_no' => 'ASC']));
  91.         $event = new EventArgs(
  92.             [
  93.                 'form' => $event->getArgument('form'),
  94.                 'data' => $event->getArgument('data'),
  95.                 'Contact' => $Contact,
  96.             ],
  97.             $event->getRequest()
  98.         );
  99.         $this->eventDispatcher->dispatch('plugin.contact.index.complete'$event);
  100.         if ($event->hasArgument('extra_save_items')) {
  101.             $items $event->getArgument('extra_save_items');
  102.             if (!empty($items)) {
  103.                 $Contact->setExtra(json_encode($items));
  104.             }
  105.         }
  106.         // DB更新
  107.         $this->entityManager->persist($Contact);
  108.         $this->entityManager->flush($Contact);
  109.     }
  110.     public function onAdminCustomerEditTwig(TemplateEvent $templateEvent)
  111.     {
  112.         $templateEvent->addSnippet('@ContactManagement4/admin/Customer/edit_js.twig');
  113.     }
  114. }