Completed
Pull Request — 4.0 (#4441)
by
unknown
05:21
created

MailController::delete()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 5

Importance

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