Failed Conditions
Push — dev/plugin-misc ( f22d99...066a4d )
by Kiyotaka
10:54 queued 03:43
created

LayoutController::preview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 7
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\Content;
15
16
use Doctrine\ORM\NoResultException;
17
use Eccube\Controller\AbstractController;
18
use Eccube\Entity\Layout;
19
use Eccube\Form\Type\Admin\LayoutType;
20
use Eccube\Entity\Master\ProductStatus;
21
use Eccube\Repository\BlockRepository;
22
use Eccube\Repository\BlockPositionRepository;
23
use Eccube\Repository\LayoutRepository;
24
use Eccube\Repository\PageLayoutRepository;
25
use Eccube\Repository\PageRepository;
26
use Eccube\Repository\ProductRepository;
27
use Eccube\Repository\Master\DeviceTypeRepository;
28
use Eccube\Util\CacheUtil;
29
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
30
use Symfony\Component\HttpFoundation\JsonResponse;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
33
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
34
use Symfony\Component\Routing\Annotation\Route;
35
use Twig\Environment as Twig;
36
use Symfony\Component\HttpFoundation\RedirectResponse;
37
38
class LayoutController extends AbstractController
39
{
40
    const DUMMY_BLOCK_ID = 9999999999;
41
42
    /**
43
     * @var BlockRepository
44
     */
45
    protected $blockRepository;
46
    /**
47
     * @var BlockPositionRepository
48
     */
49
    protected $blockPositionRepository;
50
51
    /**
52
     * @var LayoutRepository
53
     */
54
    protected $layoutRepository;
55
56
    /**
57
     * @var PageLayoutRepository
58
     */
59
    protected $pageLayoutRepository;
60
61
    /**
62
     * @var PageRepository
63
     */
64
    protected $pageRepository;
65
66
    /**
67
     * @var ProductRepository
68
     */
69
    protected $productRepository;
70
71
    /**
72
     * @var DeviceTypeRepository
73
     */
74
    protected $deviceTypeRepository;
75
76
    /**
77
     * @var boolean
78
     */
79
    protected $isPreview = false;
80
81
    /**
82
     * LayoutController constructor.
83
     *
84
     * @param BlockRepository $blockRepository
85
     * @param LayoutRepository $layoutRepository
86
     * @param PageLayoutRepository $pageLayoutRepository
87
     * @param pageRepository $pageRepository
88
     * @param ProductRepository $productRepository
89
     * @param DeviceTypeRepository $deviceTypeRepository
90
     */
91
    public function __construct(BlockRepository $blockRepository, BlockPositionRepository $blockPositionRepository, LayoutRepository $layoutRepository, PageLayoutRepository $pageLayoutRepository, PageRepository $pageRepository, ProductRepository $productRepository, DeviceTypeRepository $deviceTypeRepository)
92
    {
93
        $this->blockRepository = $blockRepository;
94
        $this->blockPositionRepository = $blockPositionRepository;
95
        $this->layoutRepository = $layoutRepository;
96
        $this->pageLayoutRepository = $pageLayoutRepository;
97
        $this->pageRepository = $pageRepository;
98
        $this->productRepository = $productRepository;
99
        $this->deviceTypeRepository = $deviceTypeRepository;
100
    }
101
102
    /**
103
     * @Route("/%eccube_admin_route%/content/layout", name="admin_content_layout")
104
     * @Template("@admin/Content/layout_list.twig")
105
     */
106
    public function index()
107
    {
108
        $qb = $this->layoutRepository->createQueryBuilder('l');
109
        $Layouts = $qb->where('l.id != :DefaultLayoutPreviewPage')
110
                    ->orderBy('l.DeviceType', 'DESC')
111
                    ->addOrderBy('l.id', 'ASC')
112
                    ->setParameter('DefaultLayoutPreviewPage', Layout::DEFAULT_LAYOUT_PREVIEW_PAGE)
113
                    ->getQuery()
114
                    ->getResult();
115
116
        return [
117
            'Layouts' => $Layouts,
118
        ];
119
    }
120
121
    /**
122
     * @Route("/%eccube_admin_route%/content/layout/{id}/delete", requirements={"id" = "\d+"}, name="admin_content_layout_delete", methods={"DELETE"})
123
     *
124
     * @param Layout $Layout
125
     *
126
     * @return RedirectResponse
127
     */
128
    public function delete(Layout $Layout, CacheUtil $cacheUtil)
129
    {
130
        $this->isTokenValid();
131
132
        /** @var Layout $Layout */
133
        if (!$Layout->isDeletable()) {
134
            $this->deleteMessage();
135
136
            return $this->redirectToRoute('admin_content_layout');
137
        }
138
139
        $this->entityManager->remove($Layout);
140
        $this->entityManager->flush($Layout);
0 ignored issues
show
Unused Code introduced by
The call to EntityManagerInterface::flush() has too many arguments starting with $Layout.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
141
142
        $this->addSuccess('admin.common.delete_complete', 'admin');
143
144
        // キャッシュの削除
145
        $cacheUtil->clearDoctrineCache();
146
147
        return $this->redirectToRoute('admin_content_layout');
148
    }
149
150
    /**
151
     * @Route("/%eccube_admin_route%/content/layout/new", name="admin_content_layout_new")
152
     * @Route("/%eccube_admin_route%/content/layout/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_layout_edit")
153
     * @Template("@admin/Content/layout.twig")
154
     */
155
    public function edit(Request $request, $id = null, $previewPageId = null, CacheUtil $cacheUtil)
156
    {
157
        if (is_null($id)) {
158
            $Layout = new Layout();
159
        } else {
160
            $Layout = $this->layoutRepository->get($this->isPreview ? 0 : $id);
161
            if (is_null($Layout)) {
162
                throw new NotFoundHttpException();
163
            }
164
        }
165
166
        // 未使用ブロックの取得
167
        $Blocks = $Layout->getBlocks();
168
        if (empty($Blocks)) {
169
            $UnusedBlocks = $this->blockRepository->findAll();
170
        } else {
171
            $UnusedBlocks = $this->blockRepository->getUnusedBlocks($Blocks);
172
        }
173
174
        $builder = $this->formFactory->createBuilder(LayoutType::class, $Layout, ['layout_id' => $id]);
175
176
        $form = $builder->getForm();
177
        $form->handleRequest($request);
178
179
        if ($form->isSubmitted() && $form->isValid()) {
180
            // Layoutの更新
181
            $Layout = $form->getData();
182
            $this->entityManager->persist($Layout);
183
            $this->entityManager->flush($Layout);
0 ignored issues
show
Unused Code introduced by
The call to EntityManagerInterface::flush() has too many arguments starting with $Layout.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
184
185
            // BlockPositionの更新
186
            // delete/insertのため、一度削除する.
187
            $BlockPositions = $Layout->getBlockPositions();
188
            foreach ($BlockPositions as $BlockPosition) {
189
                $Layout->removeBlockPosition($BlockPosition);
190
                $this->entityManager->remove($BlockPosition);
191
                $this->entityManager->flush($BlockPosition);
0 ignored issues
show
Unused Code introduced by
The call to EntityManagerInterface::flush() has too many arguments starting with $BlockPosition.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
192
            }
193
194
            // ブロックの個数分登録を行う.
195
            $data = $request->request->all();
196
            $this->blockPositionRepository->register($data, $Blocks, $UnusedBlocks, $Layout);
197
198
            // キャッシュの削除
199
            $cacheUtil->clearDoctrineCache();
200
201
            // プレビューモード
202
            if ($this->isPreview) {
203
                // プレビューする画面を取得
204
                try {
205
                    $Page = $this->pageRepository->find($previewPageId);
206
                } catch (NoResultException $e) {
207
                    throw new NotFoundHttpException();
208
                }
209
210
                if ($Page->getEditType() == \Eccube\Entity\Page::EDIT_TYPE_DEFAULT) {
211
                    if ($Page->getUrl() === 'product_detail') {
212
                        $product = $this->productRepository->findOneBy(['Status' => ProductStatus::DISPLAY_SHOW]);
213
                        if (is_null($product)) {
214
                            throw new NotFoundHttpException();
215
                        }
216
217
                        return $this->redirectToRoute($Page->getUrl(), ['preview' => 1, 'id' => $product->getId()]);
218
                    } else {
219
                        return $this->redirectToRoute($Page->getUrl(), ['preview' => 1]);
220
                    }
221
                }
222
223
                return $this->redirectToRoute('user_data', ['route' => $Page->getUrl(), 'preview' => 1]);
224
            }
225
226
            $this->addSuccess('admin.common.save_complete', 'admin');
227
228
            return $this->redirectToRoute('admin_content_layout_edit', ['id' => $Layout->getId()]);
229
        }
230
231
        return [
232
            'form' => $form->createView(),
233
            'Layout' => $Layout,
234
            'UnusedBlocks' => $UnusedBlocks,
235
        ];
236
    }
237
238
    /**
239
     * @Route("/%eccube_admin_route%/content/layout/view_block", name="admin_content_layout_view_block", methods={"GET"})
240
     *
241
     * @param Request $request
242
     * @param Twig $twig
243
     *
244
     * @return JsonResponse
245
     */
246
    public function viewBlock(Request $request, Twig $twig)
247
    {
248
        if (!$request->isXmlHttpRequest()) {
249
            throw new BadRequestHttpException();
250
        }
251
252
        $id = $request->get('id');
253
254
        if (is_null($id)) {
255
            throw new BadRequestHttpException();
256
        }
257
258
        $Block = $this->blockRepository->find($id);
259
260
        if (null === $Block) {
261
            throw new NotFoundHttpException();
262
        }
263
264
        $source = $twig->getLoader()
265
            ->getSourceContext('Block/'.$Block->getFileName().'.twig')
266
            ->getCode();
267
268
        return $this->json([
269
            'id' => $Block->getId(),
270
            'source' => $source,
271
        ]);
272
    }
273
274
    /**
275
     * @Route("/%eccube_admin_route%/content/layout/{id}/preview", requirements={"id" = "\d+"}, name="admin_content_layout_preview")
276
     */
277
    public function preview(Request $request, $id, CacheUtil $cacheUtil)
278
    {
279
        $form = $request->get('admin_layout');
280
        $this->isPreview = true;
281
282
        return $this->edit($request, $id, $form['Page'], $cacheUtil);
283
    }
284
}
285