Failed Conditions
Pull Request — experimental/3.1 (#2299)
by chihiro
40:22
created

LayoutController::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 3
dl 19
loc 19
ccs 14
cts 14
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Controller\Admin\Content;
26
27
use Doctrine\ORM\NoResultException;
28
use Eccube\Application;
29
use Eccube\Controller\AbstractController;
30
use Eccube\Entity\BlockPosition;
31
use Eccube\Entity\Layout;
32
use Eccube\Form\Type\Master\DeviceTypeType;
33
use Symfony\Component\Form\Extension\Core\Type\TextType;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
36
use Symfony\Component\Validator\Constraints\NotBlank;
37 6
38
// todo プレビュー実装
39 6
class LayoutController extends AbstractController
0 ignored issues
show
introduced by
You must use "/**" style comments for a class comment
Loading history...
40 6
{
41
    public function index(Application $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
introduced by
Missing function doc comment
Loading history...
42
    {
43
        $Layouts = $app['eccube.repository.layout']->findBy([], ['id' => 'DESC']);
44 6
45 6
        return $app->render(
46 6
            'Content/layout_list.twig',
47 6
            [
48 6
                'Layouts' => $Layouts,
49
            ]
50 6
        );
51
    }
52
53 6 View Code Duplication
    public function delete(Application $app, Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
introduced by
Missing function doc comment
Loading history...
54 6
    {
55
        $this->isTokenValid($app);
56
57 6
        $Layout = $app['eccube.repository.layout']->find($id);
58 6
        if (!$Layout) {
59 2
            $app->deleteMessage();
60
61 2
            return $app->redirect($app->url('admin_content_layout'));
62 2
        }
63 2
64 2
        $app['orm.em']->remove($Layout);
65 2
        $app['orm.em']->flush($Layout);
66 2
67 2
68 6
        $app->addSuccess('admin.delete.complete', 'admin');
69
70
        return $app->redirect($app->url('admin_content_layout'));
71 6
    }
72 6
73
    public function edit(Application $app, Request $request, $id = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
74 6
    {
75
        if (is_null($id)) {
76 6
            $Layout = new Layout();
77 6
        } else {
78 6
            // todo レポジトリへ移動
79 6
            try {
80 6
                $Layout = $app['eccube.repository.layout']->createQueryBuilder('l')
81 6
                    ->select('l, bp, b')
82 6
                    ->leftJoin('l.BlockPositions', 'bp')
83
                    ->leftJoin('bp.Block', 'b')
84
                    ->where('l.id = :layout_id')
85
                    ->orderBy('bp.block_row', 'ASC')
86 6
                    ->setParameter('layout_id', $id)
87
                    ->getQuery()
88 6
                    ->getSingleResult();
89
            } catch (NoResultException $e) {
90 6
                throw new NotFoundHttpException();
91
            }
92 6
        }
93
94 6
        // todo レポジトリへ移動
95 4
        // 未使用ブロックの取得
96
        $Blocks = $Layout->getBlocks();
97 4
        if (empty($Blocks)) {
98
            $UnusedBlocks = $app['eccube.repository.block']->findAll();
99 4
        } else {
100 4
            $UnusedBlocks = $app['eccube.repository.block']
101 4
                ->createQueryBuilder('b')
102 4
                ->select('b')
103
                ->where('b not in (:blocks)')
104
                ->setParameter('blocks', $Blocks)
105 4
                ->getQuery()
106
                ->getResult();
107
        }
108
109 4
        $builder = $app->form($Layout);
110 4
        $builder
111 4
            ->add(
112
                'name',
113 4
                TextType::class,
114 4
                [
115
                    'constraints' => [
116
                        new NotBlank(),
117 4
                    ],
118
                    'required' => false,
119
                    'label' => 'レイアウト名',
120
                ]
121 4
            )->add(
122 4
                'DeviceType',
123
                DeviceTypeType::class,
124
                [
125
                    'constraints' => [
126
                        new NotBlank(),
127
                    ],
128
                    'required' => false,
129
                ]
130
            );
131
132
        $form = $builder->getForm();
133 4
        $form->handleRequest($request);
134 4
135 4
        if ($form->isSubmitted() && $form->isValid()) {
136 4
            // Layoutの更新
137 4
            $Layout = $form->getData();
138
            $app['orm.em']->persist($Layout);
139
            $app['orm.em']->flush($Layout);
140 4
141 4
            // BlockPositionの更新
142 4
            // delete/insertのため、一度削除する.
143 4
            $BlockPositions = $Layout->getBlockPositions();
144 4
            foreach ($BlockPositions as $BlockPosition) {
145 4
                $Layout->removeBlockPosition($BlockPosition);
146 4
                $app['orm.em']->remove($BlockPosition);
147 4
                $app['orm.em']->flush($BlockPosition);
148 2
            }
149
150 4
            // ブロックの個数分登録を行う.
151 4
            $max = count($Blocks) + count($UnusedBlocks);
152
            $data = $request->request->all();
153
            for ($i = 0; $i < $max; $i++) {
154 4
                // block_idが取得できない場合はinsertしない
155 4
                if (!isset($data['block_id_'.$i])) {
156
                    continue;
157 4
                }
158
                // 未使用ブロックはinsertしない
159 4
                if ($data['target_id_'.$i] == \Eccube\Entity\PageLayout::TARGET_ID_UNUSED) {
160 4
                    continue;
161 4
                }
162 4
                $Block = $app['eccube.repository.block']->find($data['block_id_'.$i]);
163 4
                $BlockPosition = new BlockPosition();
164 4
                $BlockPosition
165
                    ->setBlockId($data['block_id_'.$i])
166
                    ->setLayoutId($Layout->getId())
167
                    ->setBlockRow($data['block_row_'.$i])
168 4
                    ->setTargetId($data['target_id_'.$i])
169
                    ->setBlock($Block)
170 4
                    ->setLayout($Layout);
171 2
                $Layout->addBlockPosition($BlockPosition);
172 2
                $app['orm.em']->persist($BlockPosition);
173
                $app['orm.em']->flush($BlockPosition);
174
            }
175
176
            $app->addSuccess('admin.register.complete', 'admin');
177
178
            return $app->redirect($app->url('admin_content_layout_edit', array('id' => $Layout->getId())));
179
        }
180
181
        return $app->render(
182
            'Content/layout.twig',
183
            array(
184
                'form' => $form->createView(),
185
                'Layout' => $Layout,
186
                'UnusedBlocks' => $UnusedBlocks,
187 2
            )
188
        );
189
    }
190
}
191