Failed Conditions
Push — master ( 36ccc8...f8bd2f )
by Kentaro
34:47
created

Controller/Admin/Content/BlockController.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Controller\AbstractController;
29
use Eccube\Entity\Master\DeviceType;
30
use Eccube\Event\EccubeEvents;
31
use Eccube\Event\EventArgs;
32
use Symfony\Component\Filesystem\Filesystem;
33
use Symfony\Component\Finder\Finder;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
36 1
37
class BlockController extends AbstractController
38
{
39 View Code Duplication
    public function index(Application $app, Request $request)
0 ignored issues
show
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...
Missing function doc comment
Loading history...
40
    {
41
        $DeviceType = $app['eccube.repository.master.device_type']
42
            ->find(DeviceType::DEVICE_TYPE_PC);
43
44 1
        // 登録されているブロック一覧の取得
45
        $Blocks = $app['eccube.repository.block']->getList($DeviceType);
46
47 1
        $event = new EventArgs(
48
            array(
49 2
                'DeviceType' => $DeviceType,
50
                'Blocks' => $Blocks,
51
            ),
52
            $request
53
        );
54
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_INDEX_COMPLETE, $event);
55
56
        return $app->render('Content/block.twig', array(
57 2
            'Blocks' => $Blocks,
58
        ));
59
    }
60
61
    public function edit(Application $app, Request $request, $id = null)
0 ignored issues
show
Missing function doc comment
Loading history...
62
    {
63
        $DeviceType = $app['eccube.repository.master.device_type']
64
            ->find(DeviceType::DEVICE_TYPE_PC);
65 2
66 2
        $Block = $app['eccube.repository.block']
67
            ->findOrCreate($id, $DeviceType);
68
69 2
        if (!$Block) {
70
            throw new NotFoundHttpException();
71
        }
72
73
        $builder = $app['form.factory']
74 2
            ->createBuilder('block', $Block);
75
76
        $html = '';
77
        $previous_filename = null;
78
        $deletable = $Block->getDeletableFlg();
79
80
        if ($id) {
81
            // テンプレートファイルの取得
82
            $previous_filename = $Block->getFileName();
83
            $file = $app['eccube.repository.block']
84
                ->getReadTemplateFile($previous_filename, $deletable);
85
            $html = $file['tpl_data'];
86
        }
87
88
        $event = new EventArgs(
89
            array(
90
                'builder' => $builder,
91
                'DeviceType' => $DeviceType,
92
                'Block' => $Block,
93
                'html' => $html,
94
            ),
95
            $request
96
        );
97 1
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_EDIT_INITIALIZE, $event);
98
        $html = $event->getArgument('html');
99
100
        $form = $builder->getForm();
101
102
        $form->get('block_html')->setData($html);
103
104
        if ($app['request']->getMethod() === 'POST') {
105
            $form->handleRequest($app['request']);
106
            if ($form->isValid()) {
107
                $Block = $form->getData();
108
109
                // DB登録
110
                $app['orm.em']->persist($Block);
111
                $app['orm.em']->flush();
112 1
113 1
                // ファイル生成・更新
114
                $tplDir = $app['config']['block_realdir'];
115
116
                $filePath = $tplDir . '/' . $Block->getFileName() . '.twig';
117 2
118
                $fs = new Filesystem();
119 1
                $fs->dumpFile($filePath, $form->get('block_html')->getData());
120
                // 更新でファイル名を変更した場合、以前のファイルを削除
121
                if ($Block->getFileName() != $previous_filename && !is_null($previous_filename)) {
122
                    $oldFilePath = $tplDir . $previous_filename;
123
                    if ($fs->exists($oldFilePath)) {
124
                        $fs->remove($oldFilePath);
125
                    }
126
                }
127
128
                \Eccube\Util\Cache::clear($app, false);
129
130
                $event = new EventArgs(
131 1
                    array(
132
                        'form' => $form,
133
                        'Block' => $Block,
134
                    ),
135
                    $request
136
                );
137
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_EDIT_COMPLETE, $event);
138
139
                $app->addSuccess('admin.register.complete', 'admin');
140
141
                return $app->redirect($app->url('admin_content_block_edit', array('id' => $Block->getId())));
142
            }
143
        }
144
145
146
        return $app->render('Content/block_edit.twig', array(
147
            'form' => $form->createView(),
148
            'block_id' => $id,
149
            'deletable' => $deletable,
150
        ));
151
    }
152
153
    public function delete(Application $app, Request $request, $id)
0 ignored issues
show
Missing function doc comment
Loading history...
154 1
    {
155
        $this->isTokenValid($app);
156
157
        $DeviceType = $app['eccube.repository.master.device_type']
158
            ->find(DeviceType::DEVICE_TYPE_PC);
159
160
        $Block = $app['eccube.repository.block']->findOneBy(array(
161
            'id' => $id,
162
            'DeviceType' => $DeviceType
163
        ));
164
165
        if (!$Block) {
166
            $app->deleteMessage();
167
            return $app->redirect($app->url('admin_content_block'));
168
        }
169
170
        // ユーザーが作ったブロックのみ削除する
171
        // テンプレートが変更されていた場合、DBからはブロック削除されるがtwigファイルは残る
172 View Code Duplication
        if ($Block->getDeletableFlg() > 0) {
0 ignored issues
show
This code seems to be duplicated across 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...
173
            $tplDir = $app['config']['block_realdir'];
174
            $file = $tplDir . '/' . $Block->getFileName() . '.twig';
175
            $fs = new Filesystem();
176
            if ($fs->exists($file)) {
177
                $fs->remove($file);
178
            }
179
            $app['orm.em']->remove($Block);
180
            $app['orm.em']->flush();
181
182
            $event = new EventArgs(
183
                array(
184
                    'Block' => $Block,
185
                ),
186
                $request
187
            );
188
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_DELETE_COMPLETE, $event);
189
190
            $app->addSuccess('admin.delete.complete', 'admin');
191
            \Eccube\Util\Cache::clear($app, false);
192
        }
193
194
195
        return $app->redirect($app->url('admin_content_block'));
196
    }
197
}
198