app/Customize/Event/CustomOrderMailEvent.php line 150

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 Customize\Event;
  11. use Customize\Service\StickerHelper;
  12. use Eccube\Event\EventArgs;
  13. use Eccube\Event\TemplateEvent;
  14. use Plugin\CustomerSupportPro42\Entity\Contact;
  15. use Plugin\CustomerSupportPro42\Entity\ContactComment;
  16. use Plugin\CustomerSupportPro42\Repository\Master\ContactStatusRepository;
  17. use Plugin\CustomerSupportPro42\Repository\ContactRepository;
  18. use Plugin\CustomerSupportPro42\Entity\Master\ContactStatus;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Plugin\CustomerSupportPro42\Service\ContactService;
  21. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  22. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  23. use Doctrine\ORM\EntityManagerInterface;
  24. use Eccube\Common\EccubeConfig;
  25. use Twig_Environment;
  26. use Eccube\Service\MailService;
  27. use Symfony\Component\DependencyInjection\ContainerInterface;
  28. class CustomOrderMailEvent implements EventSubscriberInterface
  29. {
  30.     /**
  31.      * @var ContactService
  32.      */
  33.     protected $contactService;
  34.     /**
  35.      * @var ContactStatusRepository
  36.      */
  37.     protected $contactStatusRepository;
  38.     /**
  39.      * @var ContactRepository
  40.      */
  41.     protected $contactRepository;
  42.     /**
  43.      * @var TokenStorageInterface
  44.      */
  45.     protected $tokenStorage;
  46.     /**
  47.      * @var EntityManagerInterface
  48.      */
  49.     private $entityManager;
  50.     /**
  51.      * @var EccubeConfig
  52.      */
  53.     protected $eccubeConfig;
  54.     /**
  55.      * @var Twig_Environment
  56.      */
  57.     protected $twig;
  58.     /**
  59.      * @var MailService
  60.      */
  61.     protected $mailService;
  62.     /**
  63.      * @var ContainerInterface
  64.      */
  65.     protected $container;
  66.     /**
  67.      * @var StickerHelper
  68.      */
  69.     protected $stickerHelper;
  70.     /**
  71.      * ContactEvent constructor.
  72.      *
  73.      * @param ContactService $contactService
  74.      * @param ContactStatusRepository $contactStatusRepository
  75.      * @param ContactRepository $contactRepository
  76.      * @param TokenStorageInterface $tokenStorage
  77.      * @param EntityManagerInterface $entityManager
  78.      * @param Twig_Environment $twig
  79.      * @param ContainerInterface $container
  80.      * @param StickerHelper $stickerHelper
  81.      * @param MailService $mailService
  82.      */
  83.     public function __construct(
  84.         ContactService $contactService,
  85.         ContactStatusRepository $contactStatusRepository,
  86.         ContactRepository $contactRepository,
  87.         TokenStorageInterface $tokenStorage,
  88.         EntityManagerInterface $entityManager,
  89.         Twig_Environment $twig,
  90.         ContainerInterface $container,
  91.         StickerHelper $stickerHelper,
  92.         MailService $mailService
  93.     ) {
  94.         $this->contactService $contactService;
  95.         $this->contactStatusRepository $contactStatusRepository;
  96.         $this->contactRepository $contactRepository;
  97.         $this->tokenStorage $tokenStorage;
  98.         $this->entityManager $entityManager;
  99.         $this->twig $twig;
  100.         $this->container $container;
  101.         $this->stickerHelper $stickerHelper;
  102.         $this->mailService $mailService;
  103.     }
  104.     /**
  105.      * @return array
  106.      */
  107.     public static function getSubscribedEvents()
  108.     {
  109.         return [
  110.             'custom.mail.admin.order' => 'onMailAdminOrder',
  111.             '@admin/Order/mail.twig' => 'onAdminOrderMailTwig',
  112.             '@admin/Order/mail_confirm.twig' => 'onAdminOrderMailConfirmTwig',
  113.         ];
  114.     }
  115.     /**
  116.      * 管理画面 -> 受注管理 -> メール通知(確認)
  117.      */
  118.     public function onAdminOrderMailTwig(TemplateEvent $event)
  119.     {
  120.         $event->addSnippet('@CustomerSupportPro42/admin/Order/mail.twig');
  121.     }
  122.     /**
  123.      * 管理画面 -> 受注管理 -> メール通知(確認)
  124.      */
  125.     public function onAdminOrderMailConfirmTwig(TemplateEvent $event)
  126.     {
  127.         $event->addSnippet('@CustomerSupportPro42/admin/Order/mail_confirm.twig');
  128.     }
  129.     /**
  130.      * 管理画面 -> 受注管理 -> メール送信
  131.      */
  132.     public function onMailAdminOrder(EventArgs $event)
  133.     {
  134.         $message $event->getArgument('message');
  135.         $Order $event->getArgument('Order');
  136.         $formData $event->getArgument('formData');
  137. //        受注情報を元にContactオブジェクトを作成
  138.         $Contact = new Contact();
  139.         $ContactStatus $this->contactStatusRepository->find(ContactStatus::DURING_CORRESPONDENCE);
  140.         if ($Order->getCustomer()) {
  141.             $Contact->setCustomer($Order->getCustomer());
  142.         }
  143.         $LoginedMember $this->tokenStorage->getToken()->getUser();
  144.         $Contact->setName01($Order->getName01());
  145.         $Contact->setName02($Order->getName02());
  146.         $Contact->setKana01($Order->getKana01());
  147.         $Contact->setKana02($Order->getKana02());
  148.         $Contact->setPostalCode($Order->getPostalCode());
  149.         $Contact->setPref($Order->getPref());
  150.         $Contact->setAddr01($Order->getAddr01());
  151.         $Contact->setAddr02($Order->getAddr02());
  152.         $Contact->setPhoneNumber($Order->getPhoneNumber());
  153.         $Contact->setEmail($Order->getEmail());
  154.         $Contact->setUrl($this->contactRepository->getUniqueUrl());
  155.         $Contact->setReplied(true);
  156.         $Contact->setContactStatus($ContactStatus);
  157.         $Contact->setChargeMember($LoginedMember);
  158.         $Contact->setUpdateMember($LoginedMember);
  159.         $baseUrl $this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL);
  160.         $replyUrl $Contact->getReplyUrl($baseUrl);
  161.         $twig = new \Twig_Environment(new \Twig_Loader_Array([]));
  162.         $template $twig->createTemplate($formData['tpl_data']);
  163.         $body $template->render(['replyUrl' => $replyUrl]);
  164.         $ContactComment = new ContactComment();
  165.         $ContactComment->setComment($body);
  166.         $ContactComment->setContact($Contact);
  167.         $ContactComment->setMember($LoginedMember);
  168.         $ContactComment->setSend(true);
  169.         $ContactComment->setMemo(false);
  170.         $Contact->addContactComment($ContactComment);
  171.         $this->entityManager->persist($Contact);
  172.         $this->entityManager->flush();
  173.         $message->subject($message->getSubject().'[お問い合わせID:'.$Contact->getId().']');
  174.         // HTMLテンプレートが存在する場合
  175.         $htmlFileName $this->mailService->getHtmlTemplate($formData['html_file_name']);
  176.         if (!is_null($htmlFileName)) {
  177.             $orderLink $this->container->get('router')->generate('design_confirm_after_order', ['order_id' => $Order->getId()], UrlGeneratorInterface::ABSOLUTE_URL);
  178.             $pass $this->stickerHelper->decrypt($Order->getPassword());
  179.             $htmlBody $this->twig->render($htmlFileName, [
  180.                 'Order' => $Order,
  181.                 'order_link' => $orderLink,
  182.                 'pass' => $pass,
  183.                 'replyUrl' => $replyUrl
  184.             ]);
  185.             $message
  186.                 ->text($body)
  187.                 ->html($htmlBody);
  188.         } else {
  189.             $message->text($body);
  190.         }
  191.     }
  192.     /**
  193.      * @see Symfony\Bundle\FrameworkBundle\Controller\AbstractController
  194.      */
  195.     private function generateUrl(string $route, array $parameters = [], int $referenceType UrlGeneratorInterface::ABSOLUTE_PATH): string
  196.     {
  197.         return $this->container->get('router')->generate($route$parameters$referenceType);
  198.     }
  199. }