app/Plugin/CustomerSupportPro42/Event/ContactEvent.php line 198

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\Repository\Master\ContactStatusRepository;
  14. use Plugin\CustomerSupportPro42\Entity\Master\ContactStatus;
  15. use Plugin\CustomerSupportPro42\Controller\ContactReplyController;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Plugin\CustomerSupportPro42\Service\ContactService;
  18. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use Eccube\Repository\MailTemplateRepository;
  21. use Eccube\Common\EccubeConfig;
  22. use Plugin\CustomerSupportPro42\Repository\ContactCommentRepository;
  23. use Twig_Environment;
  24. use Eccube\Service\MailService;
  25. use Plugin\CustomerSupportPro42\Repository\ContactRepository;
  26. use Eccube\Entity\Customer;
  27. class ContactEvent implements EventSubscriberInterface
  28. {
  29.     /**
  30.      * @var ContactService
  31.      */
  32.     protected $contactService;
  33.     /**
  34.      * @var TokenStorageInterface
  35.      */
  36.     protected $tokenStorage;
  37.     /**
  38.      * @var ContactReplyController
  39.      */
  40.     protected $contactReplyController;
  41.     /**
  42.      * @var EntityManagerInterface
  43.      */
  44.     protected $entityManager;
  45.     /**
  46.      * @var MailTemplateRepository
  47.      */
  48.     protected $mailTemplateRepository;
  49.     /**
  50.      * @var EccubeConfig
  51.      */
  52.     protected $eccubeConfig;
  53.     /**
  54.      * @var ContactCommentRepository
  55.      */
  56.     protected $contactCommentRepository;
  57.     /**
  58.      * @var Twig_Environment
  59.      */
  60.     protected $twig;
  61.     /**
  62.      * @var MailService
  63.      */
  64.     protected $mailService;
  65.     /**
  66.      * @var ContactRepository
  67.      */
  68.     protected $contactRepository;
  69.     /**
  70.      * ContactEvent constructor.
  71.      *
  72.      * @param ContactService $contactService
  73.      * @param ContactStatusRepository $contactStatusRepository
  74.      * @param TokenStorageInterface $tokenStorage
  75.      * @param ContactReplyController $contactReplyController
  76.      * @param EntityManagerInterface $entityManager
  77.      * @param MailTemplateRepository $mailTemplateRepository
  78.      * @param EccubeConfig $eccubeConfig
  79.      * @param ContactCommentRepository $contactCommentRepository
  80.      * @param Twig_Environment $twig
  81.      * @param MailService $mailService
  82.      * @param ContactRepository $contactRepository
  83.      */
  84.     public function __construct(
  85.         ContactService $contactService,
  86.         ContactStatusRepository $contactStatusRepository,
  87.         TokenStorageInterface $tokenStorage,
  88.         ContactReplyController $contactReplyController,
  89.         EntityManagerInterface $entityManager,
  90.         MailTemplateRepository $mailTemplateRepository,
  91.         EccubeConfig $eccubeConfig,
  92.         ContactCommentRepository $contactCommentRepository,
  93.         Twig_Environment $twig,
  94.         MailService $mailService,
  95.         ContactRepository $contactRepository
  96.     ) {
  97.         $this->contactService $contactService;
  98.         $this->contactStatusRepository $contactStatusRepository;
  99.         $this->tokenStorage $tokenStorage;
  100.         $this->contactReplyController $contactReplyController;
  101.         $this->entityManager $entityManager;
  102.         $this->mailTemplateRepository $mailTemplateRepository;
  103.         $this->eccubeConfig $eccubeConfig;
  104.         $this->contactCommentRepository $contactCommentRepository;
  105.         $this->twig $twig;
  106.         $this->mailService $mailService;
  107.         $this->contactRepository $contactRepository;
  108.     }
  109.     /**
  110.      * @return array
  111.      */
  112.     public static function getSubscribedEvents()
  113.     {
  114.         return [
  115.             'front.contact.index.initialize' => 'onFrontContactIndexInitialize',
  116.             'Contact/index.twig' => 'onDefaultContactIndexTwig',
  117.             'Contact/confirm.twig' => 'onDefaultContactConfirmTwig',
  118.             'front.contact.index.complete' => 'onFrontContactIndexComplete',
  119.             'mail.contact' => 'onMailContact',
  120.         ];
  121.     }
  122.     /**
  123.      * フロント画面 -> 問い合わせ(入力)
  124.      */
  125.     public function onFrontContactIndexInitialize(EventArgs $event)
  126.     {
  127.         $builder $event->getArgument('builder');
  128.         $builder->setData(null);
  129.         $builder->get('ContactComment')->remove('comment');
  130.     }
  131.     /**
  132.      * フロント画面 -> 問い合わせ(入力)
  133.      */
  134.     public function onDefaultContactIndexTwig(TemplateEvent $event)
  135.     {
  136.         $event->addSnippet('@CustomerSupportPro42/default/Contact/index.twig');
  137.     }
  138.     /**
  139.      * フロント画面 -> 問い合わせ(入力)
  140.      */
  141.     public function onDefaultContactConfirmTwig(TemplateEvent $event)
  142.     {
  143.         $event->addSnippet('@CustomerSupportPro42/default/Contact/confirm.twig');
  144.     }
  145.     /**
  146.      * フロント画面 -> 問い合わせ(確認)
  147.      */
  148.     public function onFrontContactIndexComplete(EventArgs $event)
  149.     {
  150.         $form $event->getArgument('form');
  151.         $Contact $event->getArgument('data');
  152.         $ContactStatus $this->contactStatusRepository->find(ContactStatus::NEW_RECEPTION);
  153.         $token $this->tokenStorage->getToken();
  154.         $Customer $token $token->getUser() : null;
  155.         if ($Customer instanceof Customer && $Customer->getId()) {
  156.             $Contact->setCustomer($Customer);
  157.         }
  158.         $Contact->setUrl($this->contactRepository->getUniqueUrl());
  159.         $Contact->setReplied(false);
  160.         $Contact->setContactStatus($ContactStatus);
  161.         $ContactComment $form['ContactComment']->getData();
  162.         $ContactComment->setContact($Contact);
  163.         $ContactComment->setComment($form->get('contents')->getData());
  164.         $ContactComment->setSend(true);
  165.         $ContactComment->setMemo(false);
  166.         $Contact->addContactComment($ContactComment);
  167.         $this->contactReplyController->registerContactCommentImage($form['ContactComment'], $ContactComment);
  168.         $this->entityManager->persist($Contact);
  169.         $this->entityManager->flush();
  170.     }
  171.     /**
  172.      * フロント画面 -> 問い合わせ(確認)
  173.      */
  174.     public function onMailContact(EventArgs $event)
  175.     {
  176.         $message $event->getArgument('message');
  177.         $Contact $event->getArgument('formData');
  178.         $BaseInfo $event->getArgument('BaseInfo');
  179.         //  問い合わせ管理プラグイン用テンプレートへ差し替え
  180.         $MailTemplate $this->mailTemplateRepository->findOneBy(['name' => $this->eccubeConfig['eccube_contact_management_mail_template_name']]);
  181.         $ContactComment $this->contactCommentRepository->findOneBy(['Contact' => $Contact], ['create_date' => 'DESC']);
  182.         $body $this->twig->render($MailTemplate->getFileName(), [
  183.             'Contact' => $Contact,
  184.             'BaseInfo' => $BaseInfo,
  185.             'ContactComment' => $ContactComment->getComment(),
  186.         ]);
  187.         // HTMLテンプレートが存在する場合
  188.         $htmlFileName $this->mailService->getHtmlTemplate($MailTemplate->getFileName());
  189.         if (!is_null($htmlFileName)) {
  190.             $htmlBody $this->twig->render($htmlFileName, [
  191.                 'Contact' => $Contact,
  192.                 'BaseInfo' => $BaseInfo,
  193.                 'ContactComment' => $ContactComment->getComment(),
  194.             ]);
  195.             $message
  196.                 ->text($body)
  197.                 ->html($htmlBody);
  198.         } else {
  199.             $message->text($body);
  200.         }
  201.         $message->subject($message->getSubject().'[お問い合わせID:'.$Contact->getId().']');
  202.         $ContactCommentImages $ContactComment->getContactCommentImages();
  203.         if ($ContactCommentImages) {
  204.             foreach ($ContactCommentImages as $ContactCommentImage) {
  205.                 $message->attachFromPath($this->eccubeConfig['eccube_image_contact_comment_dir'].'/'.$ContactCommentImage->getFileName(), $ContactCommentImage->getFileName());
  206.             }
  207.         }
  208.     }
  209. }