src/Eccube/Controller/Admin/Order/MailController.php line 195

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Controller\Admin\Order;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Entity\MailHistory;
  15. use Eccube\Entity\Order;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Form\Type\Admin\OrderMailType;
  19. use Eccube\Repository\MailHistoryRepository;
  20. use Eccube\Repository\OrderRepository;
  21. use Eccube\Service\MailService;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Twig\Environment;
  26. class MailController extends AbstractController
  27. {
  28.     /**
  29.      * @var MailService
  30.      */
  31.     protected $mailService;
  32.     /**
  33.      * @var MailHistoryRepository
  34.      */
  35.     protected $mailHistoryRepository;
  36.     /**
  37.      * @var OrderRepository
  38.      */
  39.     protected $orderRepository;
  40.     /**
  41.      * @var Environment
  42.      */
  43.     protected $twig;
  44.     /**
  45.      * MailController constructor.
  46.      *
  47.      * @param MailService $mailService
  48.      * @param MailHistoryRepository $mailHistoryRepository
  49.      * @param OrderRepository $orderRepository
  50.      * @param twig $twig
  51.      */
  52.     public function __construct(
  53.         MailService $mailService,
  54.         MailHistoryRepository $mailHistoryRepository,
  55.         OrderRepository $orderRepository,
  56.         Environment $twig
  57.     ) {
  58.         $this->mailService $mailService;
  59.         $this->mailHistoryRepository $mailHistoryRepository;
  60.         $this->orderRepository $orderRepository;
  61.         $this->twig $twig;
  62.     }
  63.     /**
  64.      * @Route("/%eccube_admin_route%/order/{id}/mail", requirements={"id" = "\d+"}, name="admin_order_mail", methods={"GET", "POST"})
  65.      * @Template("@admin/Order/mail.twig")
  66.      */
  67.     public function index(Request $requestOrder $Order)
  68.     {
  69.         $MailHistories $this->mailHistoryRepository->findBy(['Order' => $Order]);
  70.         $builder $this->formFactory->createBuilder(OrderMailType::class);
  71.         $event = new EventArgs(
  72.             [
  73.                 'builder' => $builder,
  74.                 'Order' => $Order,
  75.                 'MailHistories' => $MailHistories,
  76.             ],
  77.             $request
  78.         );
  79.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_ORDER_MAIL_INDEX_INITIALIZE);
  80.         $form $builder->getForm();
  81.         if ('POST' === $request->getMethod()) {
  82.             $form->handleRequest($request);
  83.             $mode $request->get('mode');
  84.             $body null;
  85.             // テンプレート変更の場合は. バリデーション前に内容差し替え.
  86.             switch ($mode) {
  87.                 case 'change':
  88.                     if ($form->get('template')->isValid()) {
  89.                         /** @var $data \Eccube\Entity\MailTemplate */
  90.                         $MailTemplate $form->get('template')->getData();
  91.                         if ($MailTemplate) {
  92.                             $twig $MailTemplate->getFileName();
  93.                             if (!$twig) {
  94.                                 $twig 'Mail/order.twig';
  95.                             }
  96.                             // 本文確認用
  97.                             $body $this->createBody($Order$twig);
  98.                         }
  99.                         $form $builder->getForm();
  100.                         $event = new EventArgs(
  101.                             [
  102.                                 'form' => $form,
  103.                                 'Order' => $Order,
  104.                                 'MailTemplate' => $MailTemplate,
  105.                             ],
  106.                             $request
  107.                         );
  108.                         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_ORDER_MAIL_INDEX_CHANGE);
  109.                         $form->get('template')->setData($MailTemplate);
  110.                         if ($MailTemplate) {
  111.                             $form->get('mail_subject')->setData($MailTemplate->getMailSubject());
  112.                         }
  113.                         $form->get('tpl_data')->setData($body);
  114.                     }
  115.                     break;
  116.                 case 'confirm':
  117.                     if ($form->isSubmitted() && $form->isValid()) {
  118.                         $builder->setAttribute('freeze'true);
  119.                         $builder->setAttribute('freeze_display_text'false);
  120.                         $form $builder->getForm();
  121.                         $form->handleRequest($request);
  122.                         return $this->render('@admin/Order/mail_confirm.twig', [
  123.                             'form' => $form->createView(),
  124.                             'Order' => $Order,
  125.                             'MailHistories' => $MailHistories,
  126.                         ]);
  127.                     }
  128.                     break;
  129.                 case 'complete':
  130.                     if ($form->isSubmitted() && $form->isValid()) {
  131.                         $data $form->getData();
  132.                         $data['tpl_data'] = $form->get('tpl_data')->getData();
  133.                         // メール送信
  134.                         $message $this->mailService->sendAdminOrderMail($Order$data);
  135.                         // 送信履歴を保存.
  136.                         $MailTemplate $form->get('template')->getData();
  137.                         $MailHistory = new MailHistory();
  138.                         $MailHistory
  139.                             ->setMailSubject($message->getSubject())
  140.                             ->setMailBody($message->getTextBody())
  141.                             ->setSendDate(new \DateTime())
  142.                             ->setOrder($Order);
  143.                         $this->entityManager->persist($MailHistory);
  144.                         $this->entityManager->flush();
  145.                         $event = new EventArgs(
  146.                             [
  147.                                 'form' => $form,
  148.                                 'Order' => $Order,
  149.                                 'MailTemplate' => $MailTemplate,
  150.                                 'MailHistory' => $MailHistory,
  151.                             ],
  152.                             $request
  153.                         );
  154.                         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_ORDER_MAIL_INDEX_COMPLETE);
  155.                         $this->addSuccess('admin.order.mail_send_complete''admin');
  156.                         return $this->redirectToRoute('admin_order_edit', ['id' => $Order->getId()]);
  157.                     }
  158.                     break;
  159.                 default:
  160.                     break;
  161.             }
  162.         }
  163.         return [
  164.             'form' => $form->createView(),
  165.             'Order' => $Order,
  166.             'MailHistories' => $MailHistories,
  167.         ];
  168.     }
  169.     private function createBody($Order$twig 'Mail/order.twig')
  170.     {
  171.         return $this->renderView($twig, [
  172.             'Order' => $Order,
  173.         ]);
  174.     }
  175. }