Failed Conditions
Pull Request — 4.0 (#3831)
by Ryo
31:54 queued 17:09
created

LayoutController::preview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
29
use Symfony\Component\HttpFoundation\JsonResponse;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
32
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
33
use Symfony\Component\Routing\Annotation\Route;
34
use Twig\Environment as Twig;
35
use Symfony\Component\HttpFoundation\RedirectResponse;
36
37
class LayoutController extends AbstractController
38
{
39
    const DUMMY_BLOCK_ID = 9999999999;
40
41
    /**
42
     * @var BlockRepository
43
     */
44
    protected $blockRepository;
45
    /**
46
     * @var BlockPositionRepository
47
     */
48
    protected $blockPositionRepository;
49
50
    /**
51
     * @var LayoutRepository
52
     */
53
    protected $layoutRepository;
54
55
    /**
56
     * @var PageLayoutRepository
57
     */
58
    protected $pageLayoutRepository;
59
60
    /**
61
     * @var PageRepository
62
     */
63
    protected $pageRepository;
64 6
65
    /**
66 6
     * @var ProductRepository
67 6
     */
68 6
    protected $productRepository;
69
70
    /**
71
     * @var DeviceTypeRepository
72
     */
73
    protected $deviceTypeRepository;
74
75 3
    /**
76
     * @var boolean
77 3
     */
78
    protected $isPreview = false;
79
80 3
    /**
81
     * LayoutController constructor.
82
     *
83
     * @param BlockRepository $blockRepository
84
     * @param LayoutRepository $layoutRepository
85
     * @param PageLayoutRepository $pageLayoutRepository
86
     * @param pageRepository $pageRepository
87
     * @param ProductRepository $productRepository
88
     * @param DeviceTypeRepository $deviceTypeRepository
89
     */
90
    public function __construct(BlockRepository $blockRepository, BlockPositionRepository $blockPositionRepository, LayoutRepository $layoutRepository, PageLayoutRepository $pageLayoutRepository, PageRepository $pageRepository, ProductRepository $productRepository, DeviceTypeRepository $deviceTypeRepository)
91
    {
92 2
        $this->blockRepository = $blockRepository;
93
        $this->blockPositionRepository = $blockPositionRepository;
94 2
        $this->layoutRepository = $layoutRepository;
95
        $this->pageLayoutRepository = $pageLayoutRepository;
96
        $this->pageRepository = $pageRepository;
97 2
        $this->productRepository = $productRepository;
98 1
        $this->deviceTypeRepository = $deviceTypeRepository;
99
    }
100 1
101
    /**
102
     * @Route("/%eccube_admin_route%/content/layout", name="admin_content_layout")
103 1
     * @Template("@admin/Content/layout_list.twig")
104 1
     */
105
    public function index()
106 1
    {
107
        $qb = $this->layoutRepository->createQueryBuilder('l');
108 1
        $Layouts = $qb->where('l.id != 0')
109
                    ->orderBy('l.DeviceType', 'DESC')
110
                    ->addOrderBy('l.id', 'ASC')
111
                    ->getQuery()
112
                    ->getResult();
113
114
        return [
115
            'Layouts' => $Layouts,
116 2
        ];
117
    }
118 2
119 1
    /**
120
     * @Route("/%eccube_admin_route%/content/layout/{id}/delete", requirements={"id" = "\d+"}, name="admin_content_layout_delete", methods={"DELETE"})
121
     *
122
     * @param Layout $Layout
123 1
     *
124 1
     * @return RedirectResponse
125 1
     */
126 1
    public function delete(Layout $Layout)
127 1
    {
128 1
        $this->isTokenValid();
129 1
130 1
        /** @var Layout $Layout */
131 1
        if (!$Layout->isDeletable()) {
132
            $this->deleteMessage();
133
134
            return $this->redirectToRoute('admin_content_layout');
135
        }
136
137
        $this->entityManager->remove($Layout);
138
        $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...
139 2
140 2
        $this->addSuccess('admin.common.delete_complete', 'admin');
141 1
142
        return $this->redirectToRoute('admin_content_layout');
143 1
    }
144 1
145 1
    /**
146 1
     * @Route("/%eccube_admin_route%/content/layout/new", name="admin_content_layout_new")
147 1
     * @Route("/%eccube_admin_route%/content/layout/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_layout_edit")
148 1
     * @Template("@admin/Content/layout.twig")
149 1
     */
150
    public function edit(Request $request, $id = null, $previewPageId = null)
151
    {
152 2
        if (is_null($id)) {
153
            $Layout = new Layout();
154 2
        } else {
155 2
            $Layout = $this->layoutRepository->get($this->isPreview ? 0 : $id);
156 2
            if (is_null($Layout)) {
157
                throw new NotFoundHttpException();
158 2
            }
159 2
        }
160
161
        // 未使用ブロックの取得
162 2
        $Blocks = $Layout->getBlocks();
163
        if (empty($Blocks)) {
164 2
            $UnusedBlocks = $this->blockRepository->findAll();
165 2
        } else {
166 2
            $UnusedBlocks = $this->blockRepository->getUnusedBlocks($Blocks);
167
        }
168 2
169 2
        $builder = $this->formFactory->createBuilder(LayoutType::class, $Layout, ['layout_id' => $id]);
170
171
        $form = $builder->getForm();
172
        $form->handleRequest($request);
173
174
        if ($form->isSubmitted() && $form->isValid()) {
175 2
            // Layoutの更新
176 2
            $Layout = $form->getData();
177
            $this->entityManager->persist($Layout);
178 2
            $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...
179
180 1
            // BlockPositionの更新
181 1
            // delete/insertのため、一度削除する.
182 1
            $BlockPositions = $Layout->getBlockPositions();
183
            foreach ($BlockPositions as $BlockPosition) {
184
                $Layout->removeBlockPosition($BlockPosition);
185
                $this->entityManager->remove($BlockPosition);
186 1
                $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...
187 1
            }
188 1
189 1
            // ブロックの個数分登録を行う.
190 1
            $data = $request->request->all();
191
            $this->blockPositionRepository->register($data, $Blocks, $UnusedBlocks, $Layout);
192
193
            // プレビューモード
194 1
            if ($this->isPreview) {
195 1
                // プレビューする画面を取得
196 1
                try {
197
                    $Page = $this->pageRepository->find($previewPageId);
198 1
                } catch (NoResultException $e) {
199 1
                    throw new NotFoundHttpException();
200
                }
201
202 1
                if ($Page->getEditType() == \Eccube\Entity\Page::EDIT_TYPE_DEFAULT) {
203
                    if ($Page->getUrl() === 'product_detail') {
204
                        $product = $this->productRepository->findOneBy(['Status' => ProductStatus::DISPLAY_SHOW]);
205 1
                        if (is_null($product)) {
206 1
                            throw new NotFoundHttpException();
207
                        }
208 1
209 1
                        return $this->redirectToRoute($Page->getUrl(), ['preview' => 1, 'id' => $product->getId()]);
210 1
                    } else {
211 1
                        return $this->redirectToRoute($Page->getUrl(), ['preview' => 1]);
212 1
                    }
213 1
                }
214 1
215 1
                return $this->redirectToRoute('user_data', ['route' => $Page->getUrl(), 'preview' => 1]);
216 1
            }
217
218
            $this->addSuccess('admin.common.save_complete', 'admin');
219 1
220
            return $this->redirectToRoute('admin_content_layout_edit', ['id' => $Layout->getId()]);
221 1
222
        }
223
224
        return [
225 1
            'form' => $form->createView(),
226 1
            'Layout' => $Layout,
227 1
            'UnusedBlocks' => $UnusedBlocks,
228
        ];
229
    }
230
231
    /**
232
     * @Route("/%eccube_admin_route%/content/layout/view_block", name="admin_content_layout_view_block", methods={"GET"})
233
     *
234
     * @param Request $request
235
     * @param Twig $twig
236
     *
237
     * @return JsonResponse
238
     */
239
    public function viewBlock(Request $request, Twig $twig)
240
    {
241
        if (!$request->isXmlHttpRequest()) {
242
            throw new BadRequestHttpException();
243
        }
244
245
        $id = $request->get('id');
246
247
        if (is_null($id)) {
248
            throw new BadRequestHttpException();
249
        }
250
251
        $Block = $this->blockRepository->find($id);
252
253
        if (null === $Block) {
254
            throw new NotFoundHttpException();
255
        }
256
257
        $source = $twig->getLoader()
258
            ->getSourceContext('Block/'.$Block->getFileName().'.twig')
259
            ->getCode();
260
261
        return $this->json([
262
            'id' => $Block->getId(),
263
            'source' => $source,
264
        ]);
265
    }
266
267
    /**
268
     * @Route("/%eccube_admin_route%/content/layout/{id}/preview", requirements={"id" = "\d+"}, name="admin_content_layout_preview")
269
     */
270
    public function preview(Request $request, $id)
271
    {
272
        $form = $request->get('admin_layout');
273
        $this->isPreview = true;
274
275
        return $this->edit($request, $id, $form['Page']);
276
    }
277
}
278