app/Plugin/ContactManagement42/Event.php line 120

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