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

Eccube/Controller/Admin/Content/PageController.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\Entity\PageLayout;
31
use Eccube\Event\EccubeEvents;
32
use Eccube\Event\EventArgs;
33
use Symfony\Component\Filesystem\Filesystem;
34
use Symfony\Component\Finder\Finder;
35
use Symfony\Component\HttpFoundation\Request;
36 1
37
class PageController 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 1
44
        $PageLayouts = $app['eccube.repository.page_layout']->getPageList($DeviceType);
45
46 1
        $event = new EventArgs(
47
            array(
48 1
                'DeviceType' => $DeviceType,
49
                'PageLayouts' => $PageLayouts,
50
            ),
51
            $request
52
        );
53
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_PAGE_INDEX_COMPLETE, $event);
54
55
        return $app->render('Content/page.twig', array(
56 1
            'PageLayouts' => $PageLayouts,
57
        ));
58
    }
59
60
    public function edit(Application $app, Request $request, $id = null)
0 ignored issues
show
Missing function doc comment
Loading history...
61
    {
62 1
        $DeviceType = $app['eccube.repository.master.device_type']
63
            ->find(DeviceType::DEVICE_TYPE_PC);
64
65 1
        $PageLayout = $app['eccube.repository.page_layout']
66 1
            ->findOrCreate($id, $DeviceType);
67
68
        $editable = true;
69
70
        $builder = $app['form.factory']
71
            ->createBuilder('main_edit', $PageLayout);
72
73
        $event = new EventArgs(
74
            array(
75
                'builder' => $builder,
76
                'DeviceType' => $DeviceType,
77
                'PageLayout' => $PageLayout,
78
            ),
79
            $request
80
        );
81
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_PAGE_EDIT_INITIALIZE, $event);
82
83
        $form = $builder->getForm();
84
85
        // 更新時
86
        if ($id) {
87
            // 編集不可ページはURL、ページ名、ファイル名を保持
88
            if ($PageLayout->getEditFlg() == PageLayout::EDIT_FLG_DEFAULT) {
89
                $editable = false;
90
                $PrevPageLayout = clone $PageLayout;
91
            }
92
            // テンプレートファイルの取得
93
            $file = $app['eccube.repository.page_layout']
94
                ->getReadTemplateFile($PageLayout->getFileName(), $editable);
95
96
            $form->get('tpl_data')->setData($file['tpl_data']);
97
        }
98
99
        if ('POST' === $app['request']->getMethod()) {
100
            $form->handleRequest($app['request']);
101
            if ($form->isValid()) {
102
                $PageLayout = $form->getData();
103
104
                if (!$editable) {
105
                    $PageLayout
106
                        ->setUrl($PrevPageLayout->getUrl())
107
                        ->setFileName($PrevPageLayout->getFileName())
108
                        ->setName($PrevPageLayout->getName());
109 1
                }
110 1
                // DB登録
111 1
                $app['orm.em']->persist($PageLayout);
112
                $app['orm.em']->flush();
113
114
                // ファイル生成・更新
115 1
                $templatePath = $app['eccube.repository.page_layout']->getWriteTemplatePath($editable);
116
                $filePath = $templatePath . '/' . $PageLayout->getFileName() . '.twig';
117 1
118
                $fs = new Filesystem();
119
                $fs->dumpFile($filePath, $form->get('tpl_data')->getData());
120
121
                $event = new EventArgs(
122
                    array(
123
                        'form' => $form,
124
                        'PageLayout' => $PageLayout,
125
                        'templatePath' => $templatePath,
126
                        'filePath' => $filePath,
127
                    ),
128
                    $request
129
                );
130 1
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_PAGE_EDIT_COMPLETE, $event);
131
132
                $app->addSuccess('admin.register.complete', 'admin');
133
134
                // twig キャッシュの削除.
135
                $finder = Finder::create()->in($app['config']['root_dir'] . '/app/cache/twig');
136
                $fs->remove($finder);
137
138
                return $app->redirect($app->url('admin_content_page_edit', array('id' => $PageLayout->getId())));
139
            }
140
        }
141
142
        $templatePath = $app['eccube.repository.page_layout']->getWriteTemplatePath($editable);
143
144
        return $app->render('Content/page_edit.twig', array(
145
            'form' => $form->createView(),
146
            'page_id' => $PageLayout->getId(),
147
            'editable' => $editable,
148
            'template_path' => $templatePath,
149
        ));
150
    }
151 1
152
    public function delete(Application $app, Request $request, $id = null)
0 ignored issues
show
Missing function doc comment
Loading history...
153
    {
154
        $this->isTokenValid($app);
155
156
        $DeviceType = $app['eccube.repository.master.device_type']
157
            ->find(DeviceType::DEVICE_TYPE_PC);
158
159
        $PageLayout = $app['eccube.repository.page_layout']
160
            ->findOneBy(array(
161
                'id' => $id,
162
                'DeviceType' => $DeviceType
163
            ));
164
165
        if (!$PageLayout) {
166
            $app->deleteMessage();
167
            return $app->redirect($app->url('admin_content_page'));
168
        }
169
170
        // ユーザーが作ったページのみ削除する
171 View Code Duplication
        if ($PageLayout->getEditFlg() == PageLayout::EDIT_FLG_USER) {
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...
172
            $templatePath = $app['eccube.repository.page_layout']
173
                ->getWriteTemplatePath($DeviceType, true);
174
            $file = $templatePath . '/' . $PageLayout->getFileName() . '.twig';
175
            $fs = new Filesystem();
176
            if ($fs->exists($file)) {
177
                $fs->remove($file);
178
            }
179
            $app['orm.em']->remove($PageLayout);
180
            $app['orm.em']->flush();
181
182
            $event = new EventArgs(
183
                array(
184
                    'DeviceType' => $DeviceType,
185
                    'PageLayout' => $PageLayout,
186
                ),
187
                $request
188
            );
189
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_PAGE_DELETE_COMPLETE, $event);
190
191
            $app->addSuccess('admin.delete.complete', 'admin');
192
        }
193
194
        return $app->redirect($app->url('admin_content_page'));
195
    }
196
}
197