app/Plugin/ContactManagement42/Event.php line 97

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