app/Plugin/CustomerSupportPro42/Event/EstimateMailEvent.php line 140

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