src/Eccube/Controller/Admin/Setting/Shop/MailController.php line 56

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\Setting\Shop;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Entity\MailTemplate;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Event\EventArgs;
  17. use Eccube\Form\Type\Admin\MailType;
  18. use Eccube\Repository\MailTemplateRepository;
  19. use Eccube\Util\CacheUtil;
  20. use Eccube\Util\StringUtil;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  22. use Symfony\Component\Filesystem\Filesystem;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Twig\Environment;
  27. /**
  28.  * Class MailController
  29.  */
  30. class MailController extends AbstractController
  31. {
  32.     /**
  33.      * @var MailTemplateRepository
  34.      */
  35.     protected $mailTemplateRepository;
  36.     /**
  37.      * MailController constructor.
  38.      *
  39.      * @param MailTemplateRepository $mailTemplateRepository
  40.      */
  41.     public function __construct(MailTemplateRepository $mailTemplateRepository)
  42.     {
  43.         $this->mailTemplateRepository $mailTemplateRepository;
  44.     }
  45.     /**
  46.      * @Route("/%eccube_admin_route%/setting/shop/mail", name="admin_setting_shop_mail", methods={"GET", "POST"})
  47.      * @Route("/%eccube_admin_route%/setting/shop/mail/{id}", requirements={"id" = "\d+"}, name="admin_setting_shop_mail_edit", methods={"GET", "POST"})
  48.      * @Template("@admin/Setting/Shop/mail.twig")
  49.      */
  50.     public function index(Request $requestEnvironment $twigCacheUtil $cacheUtilMailTemplate $Mail null)
  51.     {
  52.         $builder $this->formFactory
  53.             ->createBuilder(MailType::class, $Mail);
  54.         $event = new EventArgs(
  55.             [
  56.                 'builder' => $builder,
  57.                 'Mail' => $Mail,
  58.             ],
  59.             $request
  60.         );
  61.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_INITIALIZE);
  62.         $form $builder->getForm();
  63.         $form['template']->setData($Mail);
  64.         $htmlFileName $Mail $this->getHtmlFileName($Mail->getFileName()) : null;
  65.         // 更新時
  66.         if (!is_null($Mail)) {
  67.             // テンプレートファイルの取得
  68.             $source $twig->getLoader()
  69.                 ->getSourceContext($Mail->getFileName())
  70.                 ->getCode();
  71.             $form->get('tpl_data')->setData($source);
  72.             if ($twig->getLoader()->exists($htmlFileName)) {
  73.                 $source $twig->getLoader()
  74.                     ->getSourceContext($htmlFileName)
  75.                     ->getCode();
  76.                 $form->get('html_tpl_data')->setData($source);
  77.             }
  78.         }
  79.         if ('POST' === $request->getMethod()) {
  80.             $form->handleRequest($request);
  81.             // 新規登録は現時点では未実装とする.
  82.             if (is_null($Mail)) {
  83.                 $this->addError('admin.common.save_error''admin');
  84.                 return $this->redirectToRoute('admin_setting_shop_mail');
  85.             }
  86.             if ($form->isValid()) {
  87.                 $this->entityManager->flush();
  88.                 // ファイル生成・更新
  89.                 $templatePath $this->getParameter('eccube_theme_front_dir');
  90.                 $filePath $templatePath.'/'.$Mail->getFileName();
  91.                 $fs = new Filesystem();
  92.                 $mailData $form->get('tpl_data')->getData();
  93.                 $mailData StringUtil::convertLineFeed($mailData);
  94.                 $fs->dumpFile($filePath$mailData);
  95.                 // HTMLファイル用
  96.                 $htmlMailData $form->get('html_tpl_data')->getData();
  97.                 if (!is_null($htmlMailData)) {
  98.                     $htmlMailData StringUtil::convertLineFeed($htmlMailData);
  99.                     $fs->dumpFile($templatePath.'/'.$htmlFileName$htmlMailData);
  100.                 }
  101.                 $event = new EventArgs(
  102.                     [
  103.                         'form' => $form,
  104.                         'Mail' => $Mail,
  105.                         'templatePath' => $templatePath,
  106.                         'filePath' => $filePath,
  107.                     ],
  108.                     $request
  109.                 );
  110.                 $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_COMPLETE);
  111.                 $this->addSuccess('admin.common.save_complete''admin');
  112.                 // キャッシュの削除
  113.                 $cacheUtil->clearTwigCache();
  114.                 return $this->redirectToRoute('admin_setting_shop_mail_edit', ['id' => $Mail->getId()]);
  115.             }
  116.         }
  117.         return [
  118.             'form' => $form->createView(),
  119.             'id' => is_null($Mail) ? null $Mail->getId(),
  120.         ];
  121.     }
  122.     /**
  123.      * @Route("/%eccube_admin_route%/setting/shop/mail/preview", name="admin_setting_shop_mail_preview", methods={"POST"})
  124.      * @Template("@admin/Setting/Shop/mail_view.twig")
  125.      */
  126.     public function preview(Request $request)
  127.     {
  128.         if (!$request->isXmlHttpRequest() && $this->isTokenValid()) {
  129.             throw new BadRequestHttpException();
  130.         }
  131.         $html_body $request->get('html_body');
  132.         $event = new EventArgs(
  133.             [
  134.                 'html_body' => $html_body,
  135.             ],
  136.             $request
  137.         );
  138.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_SETTING_SHOP_MAIL_PREVIEW_COMPLETE);
  139.         return [
  140.             'html_body' => $html_body,
  141.         ];
  142.     }
  143.     /**
  144.      * HTML用テンプレート名を取得する
  145.      *
  146.      * @param  string $fileName
  147.      *
  148.      * @return string
  149.      */
  150.     protected function getHtmlFileName($fileName)
  151.     {
  152.         // HTMLテンプレートファイルの取得
  153.         $targetTemplate pathinfo($fileName);
  154.         $suffix '.html';
  155.         return $targetTemplate['dirname'].DIRECTORY_SEPARATOR.$targetTemplate['filename'].$suffix.'.'.$targetTemplate['extension'];
  156.     }
  157. }