Completed
Push — 4.0 ( 9f3d01...5f3e20 )
by Ryo
06:29
created

MailController::preview()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 20
ccs 0
cts 0
cp 0
crap 6
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Controller\Admin\Setting\Shop;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Entity\MailTemplate;
18
use Eccube\Event\EccubeEvents;
19
use Eccube\Event\EventArgs;
20
use Eccube\Form\Type\Admin\MailType;
21
use Eccube\Repository\MailTemplateRepository;
22
use Eccube\Util\StringUtil;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
24
use Symfony\Component\Filesystem\Filesystem;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\Routing\Annotation\Route;
27
use Twig\Environment;
28
29
/**
30
 * Class MailController
31
 */
32
class MailController extends AbstractController
33
{
34
    /**
35
     * @var MailTemplateRepository
36
     */
37
    protected $mailTemplateRepository;
38
39
    /**
40
     * MailController constructor.
41
     *
42
     * @param MailTemplateRepository $mailTemplateRepository
43
     */
44 5
    public function __construct(MailTemplateRepository $mailTemplateRepository)
45
    {
46 5
        $this->mailTemplateRepository = $mailTemplateRepository;
47
    }
48
49
    /**
50
     * @Route("/%eccube_admin_route%/setting/shop/mail", name="admin_setting_shop_mail")
51
     * @Route("/%eccube_admin_route%/setting/shop/mail/{id}", requirements={"id" = "\d+"}, name="admin_setting_shop_mail_edit")
52
     * @Template("@admin/Setting/Shop/mail.twig")
53
     */
54 5
    public function index(Request $request, MailTemplate $Mail = null, Environment $twig)
55
    {
56 5
        $builder = $this->formFactory
57 5
            ->createBuilder(MailType::class, $Mail);
58
59 5
        $event = new EventArgs(
60
            [
61 5
                'builder' => $builder,
62 5
                'Mail' => $Mail,
63
            ],
64 5
            $request
65
        );
66 5
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_INITIALIZE, $event);
67
68 5
        $form = $builder->getForm();
69 5
        $form['template']->setData($Mail);
70
        $htmlFileName = $Mail ? $this->getHtmlFileName($Mail->getFileName()) : null;
71
72 5
        // 更新時
73
        if (!is_null($Mail)) {
74 2
            // テンプレートファイルの取得
75 2
            $source = $twig->getLoader()
76 2
                ->getSourceContext($Mail->getFileName())
77
                ->getCode();
78 2
79
            $form->get('tpl_data')->setData($source);
80
            if ($twig->getLoader()->exists($htmlFileName)) {
81 5
                $source = $twig->getLoader()
82 3
                    ->getSourceContext($htmlFileName)
83
                    ->getCode();
84
85 3
                $form->get('html_tpl_data')->setData($source);
86 2
            }
87
        }
88 2
89
        if ('POST' === $request->getMethod()) {
90
            $form->handleRequest($request);
91 1
92 1
            // 新規登録は現時点では未実装とする.
93
            if (is_null($Mail)) {
94
                $this->addError('admin.common.save_error', 'admin');
95 1
96 1
                return $this->redirectToRoute('admin_setting_shop_mail');
97
            }
98 1
99 1
            if ($form->isValid()) {
100 1
                $this->entityManager->flush();
101 1
102
                // ファイル生成・更新
103 1
                $templatePath = $this->getParameter('eccube_theme_front_dir');
104
                $filePath = $templatePath.'/'.$Mail->getFileName();
105 1
106 1
                $fs = new Filesystem();
107 1
                $mailData = $form->get('tpl_data')->getData();
108 1
                $mailData = StringUtil::convertLineFeed($mailData);
109
                $fs->dumpFile($filePath, $mailData);
110 1
111
                // HTMLファイル用
112 1
                $htmlMailData = $form->get('html_tpl_data')->getData();
113
                if (!is_null($htmlMailData)) {
114 1
                    $htmlMailData = StringUtil::convertLineFeed($htmlMailData);
115
                    $fs->dumpFile($templatePath.'/'.$htmlFileName, $htmlMailData);
116 1
                }
117
118
                $event = new EventArgs(
119
                    [
120
                        'form' => $form,
121 2
                        'Mail' => $Mail,
122 2
                        'templatePath' => $templatePath,
123
                        'filePath' => $filePath,
124
                    ],
125
                    $request
126
                );
127
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_COMPLETE, $event);
128
129
                $this->addSuccess('admin.common.save_complete', 'admin');
130
131
                return $this->redirectToRoute('admin_setting_shop_mail_edit', ['id' => $Mail->getId()]);
132
            }
133
        }
134
135
        return [
136
            'form' => $form->createView(),
137
            'id' => is_null($Mail) ? null : $Mail->getId(),
138
        ];
139
    }
140
141
    /**
142
     * @Route("/%eccube_admin_route%/setting/shop/mail/preview", name="admin_setting_shop_mail_preview")
143
     * @Template("@admin/Setting/Shop/mail_view.twig")
144
     */
145
    public function preview(Request $request)
146
    {
147
        if (!$request->isXmlHttpRequest()) {
148
            throw new BadRequestHttpException();
149
        }
150
151
        $html_body = $request->get('html_body');
152
153
        $event = new EventArgs(
154
            [
155
                'html_body' => $html_body,
156
            ],
157
            $request
158
        );
159
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_PREVIEW_COMPLETE, $event);
160
161
        return [
162
            'html_body' => $html_body,
163
        ];
164
    }
165
166
    /**
167
     * HTML用テンプレート名を取得する
168
     *
169
     * @param  string $fileName
170
     *
171
     * @return string
172
     */
173
    protected function getHtmlFileName($fileName)
174
    {
175
        // HTMLテンプレートファイルの取得
176
        $targetTemplate = explode('.', $fileName);
177
        $suffix = '.html';
178
179
        return $targetTemplate[0].$suffix.'.'.$targetTemplate[1];
180
    }
181
}
182