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

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