Completed
Pull Request — experimental/3.1 (#2313)
by chihiro
42:46
created

LayoutController::viewBlock()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 4
nop 2
dl 0
loc 27
ccs 9
cts 9
cp 1
crap 4
rs 8.5806
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\BadRequestHttpException;
36
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
37 6
use Symfony\Component\Validator\Constraints\NotBlank;
38
39 6
// todo プレビュー実装
40 6
class LayoutController extends AbstractController
0 ignored issues
show
introduced by
You must use "/**" style comments for a class comment
Loading history...
41
{
42
    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...
43
    {
44 6
        $Layouts = $app['eccube.repository.layout']->findBy([], ['id' => 'DESC']);
45 6
46 6
        return $app->render(
47 6
            'Content/layout_list.twig',
48 6
            [
49
                'Layouts' => $Layouts,
50 6
            ]
51
        );
52
    }
53 6
54 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...
55
    {
56
        $this->isTokenValid($app);
57 6
58 6
        $Layout = $app['eccube.repository.layout']->find($id);
59 2
        if (!$Layout) {
60
            $app->deleteMessage();
61 2
62 2
            return $app->redirect($app->url('admin_content_layout'));
63 2
        }
64 2
65 2
        $app['orm.em']->remove($Layout);
66 2
        $app['orm.em']->flush($Layout);
67 2
68 6
69
        $app->addSuccess('admin.delete.complete', 'admin');
70
71 6
        return $app->redirect($app->url('admin_content_layout'));
72 6
    }
73
74 6
    public function edit(Application $app, Request $request, $id = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
75
    {
76 6
        if (is_null($id)) {
77 6
            $Layout = new Layout();
78 6
        } else {
79 6
            // todo レポジトリへ移動
80 6
            try {
81 6
                $Layout = $app['eccube.repository.layout']->createQueryBuilder('l')
82 6
                    ->select('l, bp, b')
83
                    ->leftJoin('l.BlockPositions', 'bp')
84
                    ->leftJoin('bp.Block', 'b')
85
                    ->where('l.id = :layout_id')
86 6
                    ->orderBy('bp.block_row', 'ASC')
87
                    ->setParameter('layout_id', $id)
88 6
                    ->getQuery()
89
                    ->getSingleResult();
90 6
            } catch (NoResultException $e) {
91
                throw new NotFoundHttpException();
92 6
            }
93
        }
94 6
95 4
        // todo レポジトリへ移動
96
        // 未使用ブロックの取得
97 4
        $Blocks = $Layout->getBlocks();
98
        if (empty($Blocks)) {
99 4
            $UnusedBlocks = $app['eccube.repository.block']->findAll();
100 4
        } else {
101 4
            $UnusedBlocks = $app['eccube.repository.block']
102 4
                ->createQueryBuilder('b')
103
                ->select('b')
104
                ->where('b not in (:blocks)')
105 4
                ->setParameter('blocks', $Blocks)
106
                ->getQuery()
107
                ->getResult();
108
        }
109 4
110 4
        $builder = $app->form($Layout);
111 4
        $builder
112
            ->add(
113 4
                'name',
114 4
                TextType::class,
115
                [
116
                    'constraints' => [
117 4
                        new NotBlank(),
118
                    ],
119
                    'required' => false,
120
                    'label' => 'レイアウト名',
121 4
                ]
122 4
            )->add(
123
                'DeviceType',
124
                DeviceTypeType::class,
125
                [
126
                    'constraints' => [
127
                        new NotBlank(),
128
                    ],
129
                    'required' => false,
130
                ]
131
            );
132
133 4
        $form = $builder->getForm();
134 4
        $form->handleRequest($request);
135 4
136 4
        if ($form->isSubmitted() && $form->isValid()) {
137 4
            // Layoutの更新
138
            $Layout = $form->getData();
139
            $app['orm.em']->persist($Layout);
140 4
            $app['orm.em']->flush($Layout);
141 4
142 4
            // BlockPositionの更新
143 4
            // delete/insertのため、一度削除する.
144 4
            $BlockPositions = $Layout->getBlockPositions();
145 4
            foreach ($BlockPositions as $BlockPosition) {
146 4
                $Layout->removeBlockPosition($BlockPosition);
147 4
                $app['orm.em']->remove($BlockPosition);
148 2
                $app['orm.em']->flush($BlockPosition);
149
            }
150 4
151 4
            // ブロックの個数分登録を行う.
152
            $max = count($Blocks) + count($UnusedBlocks);
153
            $data = $request->request->all();
154 4
            for ($i = 0; $i < $max; $i++) {
155 4
                // block_idが取得できない場合はinsertしない
156
                if (!isset($data['block_id_'.$i])) {
157 4
                    continue;
158
                }
159 4
                // 未使用ブロックはinsertしない
160 4
                if ($data['target_id_'.$i] == \Eccube\Entity\PageLayout::TARGET_ID_UNUSED) {
161 4
                    continue;
162 4
                }
163 4
                $Block = $app['eccube.repository.block']->find($data['block_id_'.$i]);
164 4
                $BlockPosition = new BlockPosition();
165
                $BlockPosition
166
                    ->setBlockId($data['block_id_'.$i])
167
                    ->setLayoutId($Layout->getId())
168 4
                    ->setBlockRow($data['block_row_'.$i])
169
                    ->setTargetId($data['target_id_'.$i])
170 4
                    ->setBlock($Block)
171 2
                    ->setLayout($Layout);
172 2
                $Layout->addBlockPosition($BlockPosition);
173
                $app['orm.em']->persist($BlockPosition);
174
                $app['orm.em']->flush($BlockPosition);
175
            }
176
177
            $app->addSuccess('admin.register.complete', 'admin');
178
179
            return $app->redirect($app->url('admin_content_layout_edit', array('id' => $Layout->getId())));
180
        }
181
182
        return $app->render(
183
            'Content/layout.twig',
184
            array(
185
                'form' => $form->createView(),
186
                'Layout' => $Layout,
187 2
                'UnusedBlocks' => $UnusedBlocks,
188
            )
189
        );
190
    }
191
192
    public function viewBlock(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
193 2
    {
194 2
        if (!$request->isXmlHttpRequest()) {
195
            throw new BadRequestHttpException();
196
        }
197
198
        $id = $request->get('id');
199
200
        if (is_null($id)) {
201 2
            throw new BadRequestHttpException();
202 2
        }
203 2
204 2
        $Block = $app['eccube.repository.block']->find($id);
205
206
        if (is_null($Block)) {
207
            return $app->json('みつかりませんでした');
208 2
        }
209
210 2
        // ブロックのソースコードの取得.
211 2
        $file = $app['eccube.repository.block']->getReadTemplateFile($Block->getFileName());
212
        $source = $file['tpl_data'];
213
214
        return $app->json([
215
            'id' => $Block->getId(),
216
            'source' => $source,
217
        ]);
218
    }
219
}
220