LayoutController::index()   F
last analyzed

Complexity

Conditions 22
Paths 1268

Size

Total Lines 179
Code Lines 120

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
eloc 120
nc 1268
nop 4
dl 0
loc 179
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Eccube\Application;
28
use Eccube\Event\EccubeEvents;
29
use Eccube\Event\EventArgs;
30
use Symfony\Component\HttpFoundation\Request;
31
32
class LayoutController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
33
{
34
    private $isPreview = false;
35
36
    public function index(Application $app, Request $request, $id = 1, $origId = 1)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
37
    {
38
        $DeviceType = $app['eccube.repository.master.device_type']
39
            ->find(\Eccube\Entity\Master\DeviceType::DEVICE_TYPE_PC);
40
41
        $PreviewBlockPositions = $app['eccube.repository.block_position']
42
            ->findBy(array(
43
                'page_id' => 0,
44
            ));
45
        foreach ($PreviewBlockPositions as $BlockPosition) {
46
            $app['orm.em']->remove($BlockPosition);
47
        }
48
        $app['orm.em']->flush();
49
50
        // 編集対象ページ
51
        /* @var $TargetPageLayout \Eccube\Entity\PageLayout */
52
        $TargetPageLayout = $app['eccube.repository.page_layout']->get($DeviceType, $id);
53
        $OrigTargetPageLayout = $app['eccube.repository.page_layout']->get($DeviceType, $origId);
54
        $Blocks = $app['orm.em']->getRepository('Eccube\Entity\Block')
55
            ->findBy(array(
56
                'DeviceType' => $DeviceType,
57
            ));
58
        $BlockPositions = $TargetPageLayout->getBlockPositions();
59
60
61
        $builderLayout = $app['form.factory']
62
            ->createBuilder('admin_page_layout');
63
64
        // 未使用ブロックの取得
65
        $unusedBlocks = $app['eccube.repository.page_layout']->findUnusedBlocks($DeviceType, $id);
66
        foreach ($unusedBlocks as $unusedBlock) {
67
            $UnusedBlockPosition = new \Eccube\Entity\BlockPosition();
68
            $UnusedBlockPosition
69
                ->setPageId($id)
70
                ->setTargetId(\Eccube\Entity\PageLayout::TARGET_ID_UNUSED)
71
                ->setAnywhere(0)
72
                ->setBlockRow(0)
73
                ->setBlockId($unusedBlock->getId())
74
                ->setBlock($unusedBlock)
75
                ->setPageLayout($TargetPageLayout);
76
            $TargetPageLayout->addBlockPosition($UnusedBlockPosition);
77
        }
78
79
        $builder = $app['form.factory']
80
            ->createBuilder();
81
82
        $event = new EventArgs(
83
            array(
84
                'builder' => $builder,
85
                'builderLayout' => $builderLayout,
86
                'DeviceType' => $DeviceType,
87
                'TargetPageLayout' => $TargetPageLayout,
88
                'OrigTargetPageLayout' => $OrigTargetPageLayout,
89
                'Blocks' => $Blocks,
90
                'BlockPositions' => $BlockPositions,
91
            ),
92
            $request
93
        );
94
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_LAYOUT_INDEX_INITIALIZE, $event);
95
96
        $listForm = $builderLayout->getForm();
97
98
        $listForm->get('layout')->setData($TargetPageLayout);
99
100
        $form = $builder->getForm();
101
102
        if ('POST' === $request->getMethod()) {
103
            $form->handleRequest($request);
104
105
            if ($form->isValid()) {
106
                // 消す
107
                foreach ($BlockPositions as $BlockPosition) {
108
                    if ($BlockPosition->getPageId() == $id || $BlockPosition->getAnywhere() == 0) {
109
                        $TargetPageLayout->removeBlockPosition($BlockPosition);
110
                        $app['orm.em']->remove($BlockPosition);
111
                    }
112
                }
113
                $app['orm.em']->flush();
114
115
                // TODO: collection を利用
116
117
                $data = $request->request->all();
118
                $max = count($Blocks);
119
                for ($i = 0; $i < $max; $i++) {
120
                    // block_id が取得できない場合は INSERT しない
121
                    if (!isset($data['id_' . $i])) {
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
122
                        continue;
123
                    }
124
                    // 未使用は INSERT しない
125
                    if ($data['target_id_' . $i] == \Eccube\Entity\PageLayout::TARGET_ID_UNUSED) {
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
126
                        continue;
127
                    }
128
                    // 他のページに anywhere が存在する場合は INSERT しない
129
                    $anywhere = (isset($data['anywhere_' . $i]) && $data['anywhere_' . $i] == 1) ? 1 : 0;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
130
131
                    if (isset($data['anywhere_' . $i]) && $data['anywhere_' . $i] == 1) {
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
132
                        $Other = $app['orm.em']->getRepository('Eccube\Entity\BlockPosition')
133
                            ->findBy(array(
134
                                'anywhere' => 1,
135
                                'block_id' => $data['id_' . $i],
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
136
                            ));
137
                        //exist and not preview model
138
                        if (( count($Other) > 0)&&($id)) {
139
                            continue;
140
                        }
141
                    }
142
143
                    $BlockPosition = new \Eccube\Entity\BlockPosition();
144
                    $Block = $app['orm.em']->getRepository('Eccube\Entity\Block')
145
                        ->findOneBy(array(
146
                            'id' => $data['id_' . $i],
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
147
                            'DeviceType' => $DeviceType,
148
                        ));
149
                    $BlockPosition
150
                        ->setPageId($id)
151
                        ->setBlockId($data['id_' . $i])
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
152
                        ->setBlockRow($data['top_' . $i])
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
153
                        ->setTargetId($data['target_id_' . $i])
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
154
                        ->setBlock($Block)
155
                        ->setPageLayout($TargetPageLayout)
156
                        ->setAnywhere($anywhere);
157
158
                    $TargetPageLayout->addBlockPosition($BlockPosition);
159
                    $app['orm.em']->persist($BlockPosition);
160
                }
161
162
                $app['orm.em']->persist($TargetPageLayout);
163
                $app['orm.em']->flush();
164
165
                $event = new EventArgs(
166
                    array(
167
                        'form' => $form,
168
                        'DeviceType' => $DeviceType,
169
                        'TargetPageLayout' => $TargetPageLayout,
170
                        'OrigTargetPageLayout' => $OrigTargetPageLayout,
171
                        'Blocks' => $Blocks,
172
                        'BlockPositions' => $BlockPositions,
173
                    ),
174
                    $request
175
                );
176
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_LAYOUT_INDEX_COMPLETE, $event);
177
178
                if ($this->isPreview) {
179
                    if ($OrigTargetPageLayout->getEditFlg()) {
180
                        if ($OrigTargetPageLayout->getUrl() === 'product_detail') {
181
                            $products = $app['eccube.repository.product']->createQueryBUilder('p')
182
                                ->where('p.Status = 1')
183
                                ->getQuery()
184
                                ->getResult();
185
                            $product = null;
186
                            foreach ($products as $p) {
187
                                $product = $p;
188
                                break;
189
                            }
190
                            if (is_null($product)) {
191
                                return '';
192
                            }
193
                            return $app->redirect($app->url($OrigTargetPageLayout->getUrl(), array('preview' => 1, 'id' => $product->getId())));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
194
                        } else {
195
                            return $app->redirect($app->url($OrigTargetPageLayout->getUrl(), array('preview' => 1)));
196
                        }
197
                    } else {
198
                        return $app->redirect($app->url('homepage').$app['config']['user_data_route']."/".$OrigTargetPageLayout->getUrl().'?preview=1');
199
                    }
200
                } else {
201
                    $app->addSuccess('admin.register.complete', 'admin');
202
                    return $app->redirect($app->url('admin_content_layout_edit', array('id' => $id)));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
203
                }
204
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
205
            }
206
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
207
        }
208
209
        return $app->render('Content/layout.twig', array(
210
            'form' => $form->createView(),
211
            'list_form' => $listForm->createView(),
212
            'TargetPageLayout' => $TargetPageLayout,
213
        ));
214
    }
215
216
    public function preview(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
217
    {
218
        $this->isPreview = true;
219
        return $this->index($app, $request, 0, $id);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
220
    }
221
222
}
223