app/Plugin/CustomerSupportPro42/Event/OrderMailEvent.php line 139

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the ContactManagement Plugin
  4.  *
  5.  * Copyright (C) 2020 Diezon.
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Plugin\CustomerSupportPro42\Event;
  11. use Eccube\Event\EventArgs;
  12. use Eccube\Event\TemplateEvent;
  13. use Plugin\CustomerSupportPro42\Entity\Contact;
  14. use Plugin\CustomerSupportPro42\Entity\ContactComment;
  15. use Plugin\CustomerSupportPro42\Repository\Master\ContactStatusRepository;
  16. use Plugin\CustomerSupportPro42\Repository\ContactRepository;
  17. use Plugin\CustomerSupportPro42\Entity\Master\ContactStatus;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Plugin\CustomerSupportPro42\Service\ContactService;
  20. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  21. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  22. use Doctrine\ORM\EntityManagerInterface;
  23. use Eccube\Common\EccubeConfig;
  24. use Twig_Environment;
  25. use Eccube\Service\MailService;
  26. use Symfony\Component\DependencyInjection\ContainerInterface;
  27. class OrderMailEvent implements EventSubscriberInterface
  28. {
  29.     /**
  30.      * @var ContactService
  31.      */
  32.     protected $contactService;
  33.     /**
  34.      * @var ContactStatusRepository
  35.      */
  36.     protected $contactStatusRepository;
  37.     /**
  38.      * @var ContactRepository
  39.      */
  40.     protected $contactRepository;
  41.     /**
  42.      * @var TokenStorageInterface
  43.      */
  44.     protected $tokenStorage;
  45.     /**
  46.      * @var EntityManagerInterface
  47.      */
  48.     private $entityManager;
  49.     /**
  50.      * @var EccubeConfig
  51.      */
  52.     protected $eccubeConfig;
  53.     /**
  54.      * @var Twig_Environment
  55.      */
  56.     protected $twig;
  57.     /**
  58.      * @var MailService
  59.      */
  60.     protected $mailService;
  61.     /**
  62.      * @var ContainerInterface
  63.      */
  64.     protected $container;
  65.     /**
  66.      * ContactEvent constructor.
  67.      *
  68.      * @param ContactService $contactService
  69.      * @param ContactStatusRepository $contactStatusRepository
  70.      * @param ContactRepository $contactRepository
  71.      * @param TokenStorageInterface $tokenStorage
  72.      * @param EntityManagerInterface $entityManager
  73.      * @param Twig_Environment $twig
  74.      * @param ContainerInterface $container
  75.      */
  76.     public function __construct(
  77.         ContactService $contactService,
  78.         ContactStatusRepository $contactStatusRepository,
  79.         ContactRepository $contactRepository,
  80.         TokenStorageInterface $tokenStorage,
  81.         EntityManagerInterface $entityManager,
  82.         Twig_Environment $twig,
  83.         ContainerInterface $container
  84.     ) {
  85.         $this->contactService $contactService;
  86.         $this->contactStatusRepository $contactStatusRepository;
  87.         $this->contactRepository $contactRepository;
  88.         $this->tokenStorage $tokenStorage;
  89.         $this->entityManager $entityManager;
  90.         $this->twig $twig;
  91.         $this->container $container;
  92.     }
  93.     /**
  94.      * @return array
  95.      */
  96.     public static function getSubscribedEvents()
  97.     {
  98.         return [
  99.             'mail.admin.order' => 'onMailAdminOrder',
  100.             '@admin/Order/mail.twig' => 'onAdminOrderMailTwig',
  101.             '@admin/Order/mail_confirm.twig' => 'onAdminOrderMailConfirmTwig',
  102.         ];
  103.     }
  104.     /**
  105.      * 管理画面 -> 受注管理 -> メール通知(確認)
  106.      */
  107.     public function onAdminOrderMailTwig(TemplateEvent $event)
  108.     {
  109.         $event->addSnippet('@CustomerSupportPro42/admin/Order/mail.twig');
  110.     }
  111.     /**
  112.      * 管理画面 -> 受注管理 -> メール通知(確認)
  113.      */
  114.     public function onAdminOrderMailConfirmTwig(TemplateEvent $event)
  115.     {
  116.         $event->addSnippet('@CustomerSupportPro42/admin/Order/mail_confirm.twig');
  117.     }
  118.     /**
  119.      * 管理画面 -> 受注管理 -> メール送信
  120.      */
  121.     public function onMailAdminOrder(EventArgs $event)
  122.     {
  123.         $message $event->getArgument('message');
  124.         $Order $event->getArgument('Order');
  125.         $formData $event->getArgument('formData');
  126. //        受注情報を元にContactオブジェクトを作成
  127.         $Contact = new Contact();
  128.         $ContactStatus $this->contactStatusRepository->find(ContactStatus::DURING_CORRESPONDENCE);
  129.         if ($Order->getCustomer()) {
  130.             $Contact->setCustomer($Order->getCustomer());
  131.         }
  132.         $LoginedMember $this->tokenStorage->getToken()->getUser();
  133.         $Contact->setName01($Order->getName01());
  134.         $Contact->setName02($Order->getName02());
  135.         $Contact->setKana01($Order->getKana01());
  136.         $Contact->setKana02($Order->getKana02());
  137.         $Contact->setPostalCode($Order->getPostalCode());
  138.         $Contact->setPref($Order->getPref());
  139.         $Contact->setAddr01($Order->getAddr01());
  140.         $Contact->setAddr02($Order->getAddr02());
  141.         $Contact->setPhoneNumber($Order->getPhoneNumber());
  142.         $Contact->setEmail($Order->getEmail());
  143.         $Contact->setUrl($this->contactRepository->getUniqueUrl());
  144.         $Contact->setReplied(true);
  145.         $Contact->setContactStatus($ContactStatus);
  146.         $Contact->setChargeMember($LoginedMember);
  147.         $Contact->setUpdateMember($LoginedMember);
  148.         $baseUrl $this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL);
  149.         $replyUrl $Contact->getReplyUrl($baseUrl);
  150.         $twig = new \Twig_Environment(new \Twig_Loader_Array([]));
  151.         $template $twig->createTemplate($formData['tpl_data']);
  152.         $body $template->render(['replyUrl' => $replyUrl]);
  153.         $ContactComment = new ContactComment();
  154.         $ContactComment->setComment($body);
  155.         $ContactComment->setContact($Contact);
  156.         $ContactComment->setMember($LoginedMember);
  157.         $ContactComment->setSend(true);
  158.         $ContactComment->setMemo(false);
  159.         $Contact->addContactComment($ContactComment);
  160.         $this->entityManager->persist($Contact);
  161.         $this->entityManager->flush();
  162.         $message->subject($message->getSubject().'[お問い合わせID:'.$Contact->getId().']');
  163.         $message->text($body);
  164.     }
  165.     /**
  166.      * @see Symfony\Bundle\FrameworkBundle\Controller\AbstractController
  167.      */
  168.     private function generateUrl(string $route, array $parameters = [], int $referenceType UrlGeneratorInterface::ABSOLUTE_PATH): string
  169.     {
  170.         return $this->container->get('router')->generate($route$parameters$referenceType);
  171.     }
  172. }