Completed
Pull Request — 4.0 (#4440)
by
unknown
04:57
created

MailController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 234
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A index() 0 6 1
B delete() 0 50 5
C edit() 0 105 9
A preview() 0 20 2
A getHtmlFileName() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.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\CacheUtil;
23
use Eccube\Util\StringUtil;
24
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
25
use Symfony\Component\Filesystem\Filesystem;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\Routing\Annotation\Route;
28
use Twig\Environment;
29
30
/**
31
 * Class MailController
32
 */
33
class MailController extends AbstractController
34
{
35
    /**
36
     * @var MailTemplateRepository
37
     */
38
    protected $mailTemplateRepository;
39
40
    /**
41
     * MailController constructor.
42
     *
43
     * @param MailTemplateRepository $mailTemplateRepository
44 5
     */
45
    public function __construct(MailTemplateRepository $mailTemplateRepository)
46 5
    {
47
        $this->mailTemplateRepository = $mailTemplateRepository;
48
    }
49
50
    /**
51
     * @Route("/%eccube_admin_route%/setting/shop/mail", name="admin_setting_shop_mail")
52
     * @Template("@admin/Setting/Shop/mail.twig")
53
     */
54 5
    public function index()
55
    {
56 5
        $MailTemplates = $this->mailTemplateRepository->getList();
57 5
58
        return ['MailTemplates' => $MailTemplates];
59 5
    }
60
61 5
    /**
62 5
     * @Route("/%eccube_admin_route%/setting/shop/mail/{id}/delete", requirements={"id" = "\d+"}, name="admin_setting_shop_mail_delete", methods={"DELETE"})
63
     */
64 5
    public function delete(Request $request, $id = null, CacheUtil $cacheUtil)
65
    {
66 5
        $this->isTokenValid();
67
68 5
        /** @var MailTemplate $Mail */
69 5
        $Mail = $this->mailTemplateRepository
70
            ->findOneBy([
71
                'id' => $id,
72 5
            ]);
73
74 2
        if (!$Mail) {
75 2
            $this->deleteMessage();
76 2
77
            return $this->redirectToRoute('admin_setting_shop_mail');
78 2
        }
79
80
        // ユーザーが作ったページのみ削除する
81 5
        if ($Mail->getEditType() == MailTemplate::EDIT_TYPE_USER) {
82 3
83
            $templatePath = $this->getParameter('eccube_theme_front_dir');
84
            $file = $templatePath.'/'.$Mail->getFileName();
85 3
            $htmlFile = $this->getHtmlFileName($file);
86 2
87
            $fs = new Filesystem();
88 2
            if ($fs->exists($file)) {
89
                $fs->remove($file);
90
            }
91 1
            if ($fs->exists($htmlFile)) {
92 1
                $fs->remove($htmlFile);
93
            }
94
            $this->entityManager->remove($Mail);
95 1
            $this->entityManager->flush();
96 1
97
            $event = new EventArgs(
98 1
                [
99 1
                    'Mail' => $Mail,
100 1
                ],
101 1
                $request
102
            );
103 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_DELETE_COMPLETE, $event);
104
105 1
            $this->addSuccess('admin.common.delete_complete', 'admin');
106 1
107 1
            // キャッシュの削除
108 1
            $cacheUtil->clearTwigCache();
109
            $cacheUtil->clearDoctrineCache();
110 1
        }
111
112 1
        return $this->redirectToRoute('admin_setting_shop_mail');
113
    }
114 1
115
    /**
116 1
     * @Route("/%eccube_admin_route%/setting/shop/mail/edit/new", name="admin_setting_shop_mail_new")
117
     * @Route("/%eccube_admin_route%/setting/shop/mail/edit/{id}", requirements={"id" = "\d+"}, name="admin_setting_shop_mail_edit")
118
     * @Template("@admin/Setting/Shop/mail_edit.twig")
119
     */
120
    public function edit(Request $request, MailTemplate $Mail = null, Environment $twig)
121 2
    {
122 2
        $builder = $this->formFactory
123
            ->createBuilder(MailType::class, $Mail);
124
125
        $event = new EventArgs(
126
            [
127
                'builder' => $builder,
128
                'Mail' => $Mail,
129
            ],
130
            $request
131
        );
132
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_INITIALIZE, $event);
133
134
        $form = $builder->getForm();
135
136
        $isUserType = false;
137
138
        // 更新時
139
        if (!is_null($Mail)) {
140
141
            $htmlFileName = $this->getHtmlFileName($Mail->getFileName());
142
143
            // テンプレートファイルの取得
144
            $source = $twig->getLoader()
145
                ->getSourceContext($Mail->getFileName())
146
                ->getCode();
147
148
            $form->get('tpl_data')->setData($source);
149
            if ($twig->getLoader()->exists($htmlFileName)) {
150
                $source = $twig->getLoader()
151
                    ->getSourceContext($htmlFileName)
152
                    ->getCode();
153
154
                $form->get('html_tpl_data')->setData($source);
155
            }
156
        } else {
157
            $isUserType = true;
158
        }
159
160
        $form->handleRequest($request);
161
162
        if ($form->isSubmitted() && $form->isValid()) {
163
164
            if (is_null($Mail)) {
165
                $fileName = sprintf('Mail/%s.twig', $form->get('file_name')->getData());
166
            } else {
167
                $fileName = $Mail->getFileName();
168
            }
169
170
171
            // ファイル生成・更新
172
            $templatePath = $this->getParameter('eccube_theme_front_dir');
173
            $filePath = $templatePath.'/'.$fileName;
174
175
            $fs = new Filesystem();
176
            $mailData = $form->get('tpl_data')->getData();
177
            $mailData = StringUtil::convertLineFeed($mailData);
178
            $fs->dumpFile($filePath, $mailData);
179
180
            // HTMLファイル用
181
            $htmlMailData = $form->get('html_tpl_data')->getData();
182
            $htmlFile = $this->getHtmlFileName($filePath);
183
            if (!is_null($htmlMailData)) {
184
                $htmlMailData = StringUtil::convertLineFeed($htmlMailData);
185
                $fs->dumpFile($htmlFile, $htmlMailData);
186
            } else {
187
                $fs = new Filesystem();
188
                if ($fs->exists($htmlFile)) {
189
                    $fs->remove($htmlFile);
190
                }
191
            }
192
193
            /** @var MailTemplate $Mail */
194
            $Mail = $form->getData();
195
            if ($isUserType) {
196
                $Mail->setEditType(MailTemplate::EDIT_TYPE_USER);
197
            }
198
            $Mail->setFileName($fileName);
199
            $this->entityManager->persist($Mail);
200
            $this->entityManager->flush();
201
202
203
            $event = new EventArgs(
204
                [
205
                    'form' => $form,
206
                    'Mail' => $Mail,
207
                    'templatePath' => $templatePath,
208
                    'filePath' => $filePath,
209
                ],
210
                $request
211
            );
212
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_COMPLETE, $event);
213
214
            $this->addSuccess('admin.common.save_complete', 'admin');
215
216
            return $this->redirectToRoute('admin_setting_shop_mail_edit', ['id' => $Mail->getId()]);
217
        }
218
219
220
        return [
221
            'form' => $form->createView(),
222
            'Mail' => $Mail,
223
        ];
224
    }
225
226
    /**
227
     * @Route("/%eccube_admin_route%/setting/shop/mail/preview", name="admin_setting_shop_mail_preview")
228
     * @Template("@admin/Setting/Shop/mail_view.twig")
229
     */
230
    public function preview(Request $request)
231
    {
232
        if (!$request->isXmlHttpRequest()) {
233
            throw new BadRequestHttpException();
234
        }
235
236
        $html_body = $request->get('html_body');
237
238
        $event = new EventArgs(
239
            [
240
                'html_body' => $html_body,
241
            ],
242
            $request
243
        );
244
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_PREVIEW_COMPLETE, $event);
245
246
        return [
247
            'html_body' => $html_body,
248
        ];
249
    }
250
251
    /**
252
     * HTML用テンプレート名を取得する
253
     *
254
     * @param  string $fileName
255
     *
256
     * @return string
257
     */
258
    protected function getHtmlFileName($fileName)
259
    {
260
        // HTMLテンプレートファイルの取得
261
        $targetTemplate = explode('.', $fileName);
262
        $suffix = '.html';
263
264
        return $targetTemplate[0].$suffix.'.'.$targetTemplate[1];
265
    }
266
}
267