Completed
Push — sf/composer-install-update ( a1e834 )
by Kiyotaka
10:44
created

MailController::getHtmlFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
rs 10
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
    public function __construct(MailTemplateRepository $mailTemplateRepository)
45
    {
46
        $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
    public function index(Request $request, MailTemplate $Mail = null, Environment $twig)
55
    {
56
        $builder = $this->formFactory
57
            ->createBuilder(MailType::class, $Mail);
58
59
        $event = new EventArgs(
60
            [
61
                'builder' => $builder,
62
                'Mail' => $Mail,
63
            ],
64
            $request
65
        );
66
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_INITIALIZE, $event);
67
68
        $form = $builder->getForm();
69
        $form['template']->setData($Mail);
70
        $htmlFileName = $Mail ? $this->getHtmlFileName($Mail->getFileName()) : null;
71
72
        // 更新時
73
        if (!is_null($Mail)) {
74
            // テンプレートファイルの取得
75
            $source = $twig->getLoader()
76
                ->getSourceContext($Mail->getFileName())
77
                ->getCode();
78
79
            $form->get('tpl_data')->setData($source);
80
            if ($twig->getLoader()->exists($htmlFileName)) {
81
                $source = $twig->getLoader()
82
                    ->getSourceContext($htmlFileName)
83
                    ->getCode();
84
85
                $form->get('html_tpl_data')->setData($source);
86
            }
87
        }
88
89
        if ('POST' === $request->getMethod()) {
90
            $form->handleRequest($request);
91
92
            // 新規登録は現時点では未実装とする.
93
            if (is_null($Mail)) {
94
                $this->addError('admin.common.save_error', 'admin');
95
96
                return $this->redirectToRoute('admin_setting_shop_mail');
97
            }
98
99
            if ($form->isValid()) {
100
                $this->entityManager->flush();
101
102
                // ファイル生成・更新
103
                $templatePath = $this->getParameter('eccube_theme_front_dir');
104
                $filePath = $templatePath.'/'.$Mail->getFileName();
105
106
                $fs = new Filesystem();
107
                $mailData = $form->get('tpl_data')->getData();
108
                $mailData = StringUtil::convertLineFeed($mailData);
109
                $fs->dumpFile($filePath, $mailData);
110
111
                // HTMLファイル用
112
                $htmlMailData = $form->get('html_tpl_data')->getData();
113
                if (!is_null($htmlMailData)) {
114
                    $htmlMailData = StringUtil::convertLineFeed($htmlMailData);
115
                    $fs->dumpFile($templatePath.'/'.$htmlFileName, $htmlMailData);
116
                }
117
118
                $event = new EventArgs(
119
                    [
120
                        'form' => $form,
121
                        'Mail' => $Mail,
122
                        '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
     * HTML用テンプレート名を取得する
143
     *
144
     * @param  string $fileName
145
     *
146
     * @return string
147
     */
148
    public function getHtmlFileName($fileName)
149
    {
150
        // HTMLテンプレートファイルの取得
151
        $targetTemplate = explode('.', $fileName);
152
        $suffix = '.html';
153
154
        return $targetTemplate[0].$suffix.'.'.$targetTemplate[1];
155
    }
156
}
157