Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
created

Controller/Admin/Content/BlockController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Content;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Entity\Block;
18
use Eccube\Entity\Master\DeviceType;
19
use Eccube\Event\EccubeEvents;
20
use Eccube\Event\EventArgs;
21
use Eccube\Form\Type\Admin\BlockType;
22
use Eccube\Repository\BlockRepository;
23
use Eccube\Repository\Master\DeviceTypeRepository;
24
use Eccube\Util\CacheUtil;
25
use Eccube\Util\StringUtil;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
27
use Symfony\Component\Filesystem\Filesystem;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
30
use Symfony\Component\Routing\Annotation\Route;
31
use Twig\Environment;
32
33
class BlockController extends AbstractController
34
{
35
    /**
36
     * @var BlockRepository
37
     */
38
    protected $blockRepository;
39
40
    /**
41
     * @var DeviceTypeRepository
42
     */
43
    protected $deviceTypeRepository;
44
45 4
    public function __construct(
46
        BlockRepository $blockRepository,
47
        DeviceTypeRepository $deviceTypeRepository
48
    ) {
49 4
        $this->blockRepository = $blockRepository;
50 4
        $this->deviceTypeRepository = $deviceTypeRepository;
51
    }
52
53
    /**
54
     * @Route("/%eccube_admin_route%/content/block", name="admin_content_block")
55
     * @Template("@admin/Content/block.twig")
56
     */
57 1 View Code Duplication
    public function index(Request $request)
58
    {
59 1
        $DeviceType = $this->deviceTypeRepository
60 1
            ->find(DeviceType::DEVICE_TYPE_PC);
61
62
        // 登録されているブロック一覧の取得
63 1
        $Blocks = $this->blockRepository->getList($DeviceType);
0 ignored issues
show
$DeviceType is of type object|null, but the function expects a object<Eccube\Entity\Master\DeviceType>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
65 1
        $event = new EventArgs(
66
            [
67 1
                'DeviceType' => $DeviceType,
68 1
                'Blocks' => $Blocks,
69
            ],
70 1
            $request
71
        );
72 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_INDEX_COMPLETE, $event);
73
74
        return [
75 1
            'Blocks' => $Blocks,
76
        ];
77
    }
78
79
    /**
80
     * @Route("/%eccube_admin_route%/content/block/new", name="admin_content_block_new")
81
     * @Route("/%eccube_admin_route%/content/block/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_block_edit")
82
     * @Template("@admin/Content/block_edit.twig")
83
     */
84 2
    public function edit(Request $request, $id = null, Environment $twig, FileSystem $fs, CacheUtil $cacheUtil)
85
    {
86 2
        $DeviceType = $this->deviceTypeRepository
87 2
            ->find(DeviceType::DEVICE_TYPE_PC);
88
89 2
        if (null === $id) {
90 2
            $Block = $this->blockRepository->newBlock($DeviceType);
91
        } else {
92 2
            $Block = $this->blockRepository->findOneBy(
93
                [
94
                    'id' => $id,
95
                    'DeviceType' => $DeviceType,
96 2
                ]
97 2
            );
98
        }
99 2
100 2
        if (!$Block) {
101
            throw new NotFoundHttpException();
102 2
        }
103 2
104 2
        $builder = $this->formFactory
105 2
            ->createBuilder(BlockType::class, $Block);
106 2
107
        $html = '';
108
        $previousFileName = null;
109 2
110
        if ($id) {
111 2
            $previousFileName = $Block->getFileName();
112 2
            $html = $twig->getLoader()
113 2
                ->getSourceContext('Block/'.$Block->getFileName().'.twig')
114 2
                ->getCode();
115
        }
116 2
117
        $event = new EventArgs(
118 2
            [
119 2
                'builder' => $builder,
120
                'DeviceType' => $DeviceType,
121 2
                'Block' => $Block,
122 2
                'html' => $html,
123
            ],
124 2
            $request
125
        );
126 2
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_EDIT_INITIALIZE, $event);
127 1
        $html = $event->getArgument('html');
128 1
129 1
        $form = $builder->getForm();
130
        $form->get('block_html')->setData($html);
131 1
132 1
        $form->handleRequest($request);
133 1
134
        if ($form->isSubmitted() && $form->isValid()) {
135 1
            $Block = $form->getData();
136
            $this->entityManager->persist($Block);
137 1
            $this->entityManager->flush();
138 1
139 1
            $dir = sprintf('%s/app/template/%s/Block',
140
                $this->getParameter('kernel.project_dir'),
141
                $this->getParameter('eccube.theme'));
142 1
143 1
            $file = $dir.'/'.$Block->getFileName().'.twig';
144 1
145
            $source = $form->get('block_html')->getData();
146
            $source = StringUtil::convertLineFeed($source);
147
            $fs->dumpFile($file, $source);
148
149
            // 更新でファイル名を変更した場合、以前のファイルを削除
150 1
            if (null !== $previousFileName && $Block->getFileName() !== $previousFileName) {
151 1
                $old = $dir.'/'.$previousFileName.'.twig';
152
                if ($fs->exists($old)) {
153 1
                    $fs->remove($old);
154
                }
155 1
            }
156 1
157
            // キャッシュの削除
158 1
            $cacheUtil->clearTwigCache();
159
            $cacheUtil->clearDoctrineCache();
160 1
161
            $event = new EventArgs(
162 1
                [
163
                    'form' => $form,
164 1
                    'Block' => $Block,
165
                ],
166
                $request
167
            );
168 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_EDIT_COMPLETE, $event);
169 1
170 1
            $this->addSuccess('admin.common.save_complete', 'admin');
171
172
            return $this->redirectToRoute('admin_content_block_edit', ['id' => $Block->getId()]);
173
        }
174
175
        return [
176
            'form' => $form->createView(),
177
            'block_id' => $id,
178 1
            'deletable' => $Block->isDeletable(),
179
        ];
180 1
    }
181
182
    /**
183 1
     * @Route("/%eccube_admin_route%/content/block/{id}/delete", requirements={"id" = "\d+"}, name="admin_content_block_delete", methods={"DELETE"})
184
     */
185
    public function delete(Request $request, Block $Block, Filesystem $fs, CacheUtil $cacheUtil)
186
    {
187
        $this->isTokenValid();
188
189
        // ユーザーが作ったブロックのみ削除する
190
        if ($Block->isDeletable()) {
191
            $dir = sprintf('%s/app/template/%s/Block',
192
                $this->getParameter('kernel.project_dir'),
193
                $this->getParameter('eccube.theme'));
194
195
            $file = $dir.'/'.$Block->getFileName().'.twig';
196
197
            if ($fs->exists($file)) {
198
                $fs->remove($file);
199
            }
200
201
            $this->entityManager->remove($Block);
202
            $this->entityManager->flush();
203
204
            $event = new EventArgs(
205
                [
206
                    'Block' => $Block,
207
                ],
208
                $request
209
            );
210
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_DELETE_COMPLETE, $event);
211
212 1
            $this->addSuccess('admin.common.delete_complete', 'admin');
213
214
            // キャッシュの削除
215
            $cacheUtil->clearTwigCache();
216
            $cacheUtil->clearDoctrineCache();
217
        }
218
219
        return $this->redirectToRoute('admin_content_block');
220
    }
221
}
222