1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of EC-CUBE |
5
|
|
|
* |
6
|
|
|
* Copyright(c) LOCKON CO.,LTD. All Rights Reserved. |
7
|
|
|
* |
8
|
|
|
* http://www.lockon.co.jp/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Eccube\Controller\Admin\Content; |
15
|
|
|
|
16
|
|
|
use Eccube\Controller\AbstractController; |
17
|
|
|
use Eccube\Entity\Master\DeviceType; |
18
|
|
|
use Eccube\Entity\Page; |
19
|
|
|
use Eccube\Entity\PageLayout; |
20
|
|
|
use Eccube\Event\EccubeEvents; |
21
|
|
|
use Eccube\Event\EventArgs; |
22
|
|
|
use Eccube\Form\Type\Admin\MainEditType; |
23
|
|
|
use Eccube\Repository\Master\DeviceTypeRepository; |
24
|
|
|
use Eccube\Repository\PageLayoutRepository; |
25
|
|
|
use Eccube\Repository\PageRepository; |
26
|
|
|
use Eccube\Util\StringUtil; |
27
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
28
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
29
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
30
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
31
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
32
|
|
|
use Symfony\Component\HttpFoundation\Request; |
33
|
|
|
use Twig\Environment; |
34
|
|
|
|
35
|
|
|
class PageController extends AbstractController |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var PageRepository |
39
|
|
|
*/ |
40
|
|
|
protected $pageRepository; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var PageLayoutRepository |
44
|
|
|
*/ |
45
|
|
|
protected $pageLayoutRepository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var DeviceTypeRepository |
49
|
|
|
*/ |
50
|
|
|
protected $deviceTypeRepository; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* PageController constructor. |
54
|
|
|
* |
55
|
|
|
* @param PageRepository $pageRepository |
56
|
|
|
* @param DeviceTypeRepository $deviceTypeRepository |
57
|
|
|
*/ |
58
|
6 |
|
public function __construct( |
59
|
|
|
PageRepository $pageRepository, |
60
|
|
|
PageLayoutRepository $pageLayoutRepository, |
61
|
|
|
DeviceTypeRepository $deviceTypeRepository |
62
|
|
|
) { |
63
|
6 |
|
$this->pageRepository = $pageRepository; |
64
|
6 |
|
$this->pageLayoutRepository = $pageLayoutRepository; |
65
|
6 |
|
$this->deviceTypeRepository = $deviceTypeRepository; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @Route("/%eccube_admin_route%/content/page", name="admin_content_page") |
70
|
|
|
* @Template("@admin/Content/page.twig") |
71
|
|
|
*/ |
72
|
1 |
View Code Duplication |
public function index(Request $request) |
|
|
|
|
73
|
|
|
{ |
74
|
1 |
|
$DeviceType = $this->deviceTypeRepository |
75
|
1 |
|
->find(DeviceType::DEVICE_TYPE_PC); |
76
|
|
|
|
77
|
1 |
|
$Pages = $this->pageRepository->getPageList($DeviceType); |
|
|
|
|
78
|
|
|
|
79
|
1 |
|
$event = new EventArgs( |
80
|
|
|
[ |
81
|
1 |
|
'DeviceType' => $DeviceType, |
82
|
1 |
|
'Pages' => $Pages, |
83
|
|
|
], |
84
|
1 |
|
$request |
85
|
|
|
); |
86
|
1 |
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_PAGE_INDEX_COMPLETE, $event); |
87
|
|
|
|
88
|
|
|
return [ |
89
|
1 |
|
'Pages' => $Pages, |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @Route("/%eccube_admin_route%/content/page/new", name="admin_content_page_new") |
95
|
|
|
* @Route("/%eccube_admin_route%/content/page/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_page_edit") |
96
|
|
|
* @Template("@admin/Content/page_edit.twig") |
97
|
|
|
*/ |
98
|
3 |
|
public function edit(Request $request, $id = null, Environment $twig, Router $router) |
99
|
|
|
{ |
100
|
3 |
|
$DeviceType = $this->deviceTypeRepository |
101
|
3 |
|
->find(DeviceType::DEVICE_TYPE_PC); |
102
|
|
|
|
103
|
3 |
|
$Page = $this->pageRepository |
104
|
3 |
|
->findOrCreate($id, $DeviceType); |
|
|
|
|
105
|
|
|
|
106
|
3 |
|
$isUserDataPage = true; |
107
|
|
|
|
108
|
3 |
|
$builder = $this->formFactory |
109
|
3 |
|
->createBuilder(MainEditType::class, $Page); |
110
|
|
|
|
111
|
3 |
|
$event = new EventArgs( |
112
|
|
|
[ |
113
|
3 |
|
'builder' => $builder, |
114
|
3 |
|
'DeviceType' => $DeviceType, |
115
|
3 |
|
'Page' => $Page, |
116
|
|
|
], |
117
|
3 |
|
$request |
118
|
|
|
); |
119
|
3 |
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_PAGE_EDIT_INITIALIZE, $event); |
120
|
|
|
|
121
|
3 |
|
$form = $builder->getForm(); |
122
|
|
|
|
123
|
|
|
// 更新時 |
124
|
3 |
|
$fileName = null; |
125
|
3 |
|
$namespace = '@user_data/'; |
126
|
3 |
|
if ($id) { |
127
|
|
|
// 編集不可ページはURL、ページ名、ファイル名を保持 |
128
|
2 |
|
if ($Page->getEditType() == Page::EDIT_TYPE_DEFAULT) { |
129
|
2 |
|
$isUserDataPage = false; |
130
|
2 |
|
$namespace = ''; |
131
|
2 |
|
$PrevPage = clone $Page; |
132
|
|
|
} |
133
|
|
|
// テンプレートファイルの取得 |
134
|
2 |
|
$source = $twig->getLoader() |
135
|
2 |
|
->getSourceContext($namespace.$Page->getFileName().'.twig') |
136
|
2 |
|
->getCode(); |
137
|
|
|
|
138
|
2 |
|
$form->get('tpl_data')->setData($source); |
139
|
|
|
|
140
|
2 |
|
$fileName = $Page->getFileName(); |
141
|
1 |
|
} elseif ($request->getMethod() === 'GET' && !$form->isSubmitted()) { |
142
|
|
|
$source = $twig->getLoader() |
143
|
|
|
->getSourceContext('@admin/empty_page.twig') |
144
|
|
|
->getCode(); |
145
|
|
|
$form->get('tpl_data')->setData($source); |
146
|
|
|
} |
147
|
|
|
|
148
|
3 |
|
$form->handleRequest($request); |
149
|
|
|
|
150
|
3 |
|
if ($form->isSubmitted() && $form->isValid()) { |
151
|
2 |
|
$Page = $form->getData(); |
152
|
|
|
|
153
|
2 |
|
if (!$isUserDataPage) { |
154
|
|
|
$Page |
155
|
1 |
|
->setUrl($PrevPage->getUrl()) |
|
|
|
|
156
|
1 |
|
->setFileName($PrevPage->getFileName()) |
157
|
1 |
|
->setName($Page->getName()); |
158
|
|
|
} |
159
|
|
|
// DB登録 |
160
|
2 |
|
$this->entityManager->persist($Page); |
161
|
2 |
|
$this->entityManager->flush(); |
162
|
|
|
|
163
|
|
|
// ファイル生成・更新 |
164
|
2 |
|
if ($isUserDataPage) { |
165
|
1 |
|
$templatePath = $this->getParameter('eccube_theme_user_data_dir'); |
166
|
|
|
} else { |
167
|
1 |
|
$templatePath = $this->getParameter('eccube_theme_front_dir'); |
168
|
|
|
} |
169
|
2 |
|
$filePath = $templatePath.'/'.$Page->getFileName().'.twig'; |
170
|
|
|
|
171
|
2 |
|
$fs = new Filesystem(); |
172
|
2 |
|
$pageData = $form->get('tpl_data')->getData(); |
173
|
2 |
|
$pageData = StringUtil::convertLineFeed($pageData); |
174
|
2 |
|
$fs->dumpFile($filePath, $pageData); |
175
|
|
|
|
176
|
|
|
// 更新でファイル名を変更した場合、以前のファイルを削除 |
177
|
2 |
|
if ($Page->getFileName() != $fileName && !is_null($fileName)) { |
178
|
|
|
$oldFilePath = $templatePath.'/'.$fileName.'.twig'; |
179
|
|
|
if ($fs->exists($oldFilePath)) { |
180
|
|
|
$fs->remove($oldFilePath); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
2 |
|
foreach ($Page->getPageLayouts() as $PageLayout) { |
185
|
1 |
|
$Page->removePageLayout($PageLayout); |
186
|
1 |
|
$this->entityManager->remove($PageLayout); |
187
|
1 |
|
$this->entityManager->flush($PageLayout); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
2 |
|
$Layout = $form['PcLayout']->getData(); |
191
|
2 |
|
$LastPageLayout = $this->pageLayoutRepository->findOneBy([], ['sort_no' => 'DESC']); |
192
|
2 |
|
$sortNo = $LastPageLayout->getSortNo(); |
193
|
|
|
|
194
|
2 |
View Code Duplication |
if ($Layout) { |
195
|
|
|
$PageLayout = new PageLayout(); |
196
|
|
|
$PageLayout->setLayoutId($Layout->getId()); |
197
|
|
|
$PageLayout->setLayout($Layout); |
198
|
|
|
$PageLayout->setPageId($Page->getId()); |
199
|
|
|
$PageLayout->setSortNo($sortNo++); |
200
|
|
|
$PageLayout->setPage($Page); |
201
|
|
|
|
202
|
|
|
$this->entityManager->persist($PageLayout); |
203
|
|
|
$this->entityManager->flush($PageLayout); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
2 |
|
$Layout = $form['SpLayout']->getData(); |
207
|
2 |
View Code Duplication |
if ($Layout) { |
208
|
|
|
$PageLayout = new PageLayout(); |
209
|
|
|
$PageLayout->setLayoutId($Layout->getId()); |
210
|
|
|
$PageLayout->setLayout($Layout); |
211
|
|
|
$PageLayout->setPageId($Page->getId()); |
212
|
|
|
$PageLayout->setSortNo($sortNo++); |
213
|
|
|
$PageLayout->setPage($Page); |
214
|
|
|
|
215
|
|
|
$this->entityManager->persist($PageLayout); |
216
|
|
|
$this->entityManager->flush($PageLayout); |
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
2 |
|
$event = new EventArgs( |
220
|
|
|
[ |
221
|
2 |
|
'form' => $form, |
222
|
2 |
|
'Page' => $Page, |
223
|
2 |
|
'templatePath' => $templatePath, |
224
|
2 |
|
'filePath' => $filePath, |
225
|
|
|
], |
226
|
2 |
|
$request |
227
|
|
|
); |
228
|
2 |
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_PAGE_EDIT_COMPLETE, $event); |
229
|
|
|
|
230
|
2 |
|
$this->addSuccess('admin.register.complete', 'admin'); |
231
|
|
|
|
232
|
|
|
// twig キャッシュの削除. |
233
|
2 |
|
$cacheDir = $this->getParameter('kernel.cache_dir').'/twig'; |
234
|
2 |
|
$fs->remove($cacheDir); |
235
|
|
|
|
236
|
2 |
|
return $this->redirectToRoute('admin_content_page_edit', ['id' => $Page->getId()]); |
237
|
|
|
} |
238
|
|
|
|
239
|
1 |
|
if ($isUserDataPage) { |
240
|
|
|
$templatePath = $this->getParameter('eccube_theme_user_data_dir'); |
241
|
|
|
$url = ''; |
242
|
|
|
} else { |
243
|
1 |
|
$templatePath = $this->getParameter('eccube_theme_front_dir'); |
244
|
1 |
|
$url = $router->getRouteCollection()->get($PrevPage->getUrl())->getPath(); |
245
|
|
|
} |
246
|
1 |
|
$projectDir = $this->getParameter('kernel.project_dir'); |
247
|
1 |
|
$templatePath = str_replace($projectDir.'/', '', $templatePath); |
248
|
|
|
|
249
|
|
|
return [ |
250
|
1 |
|
'form' => $form->createView(), |
251
|
1 |
|
'page_id' => $Page->getId(), |
252
|
1 |
|
'is_user_data_page' => $isUserDataPage, |
253
|
1 |
|
'template_path' => $templatePath, |
254
|
1 |
|
'url' => $url, |
255
|
|
|
]; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @Method("DELETE") |
260
|
|
|
* @Route("/%eccube_admin_route%/content/page/{id}/delete", requirements={"id" = "\d+"}, name="admin_content_page_delete") |
261
|
|
|
*/ |
262
|
2 |
|
public function delete(Request $request, $id = null) |
263
|
|
|
{ |
264
|
2 |
|
$this->isTokenValid(); |
265
|
|
|
|
266
|
2 |
|
$DeviceType = $this->deviceTypeRepository |
267
|
2 |
|
->find(DeviceType::DEVICE_TYPE_PC); |
268
|
|
|
|
269
|
2 |
|
$Page = $this->pageRepository |
270
|
2 |
|
->findOneBy([ |
271
|
2 |
|
'id' => $id, |
272
|
2 |
|
'DeviceType' => $DeviceType, |
273
|
|
|
]); |
274
|
|
|
|
275
|
2 |
|
if (!$Page) { |
276
|
|
|
$this->deleteMessage(); |
277
|
|
|
|
278
|
|
|
return $this->redirectToRoute('admin_content_page'); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
// ユーザーが作ったページのみ削除する |
282
|
2 |
|
if ($Page->getEditType() == Page::EDIT_TYPE_USER) { |
283
|
1 |
|
$templatePath = $this->getParameter('eccube_theme_user_data_dir'); |
284
|
1 |
|
$file = $templatePath.'/'.$Page->getFileName().'.twig'; |
285
|
1 |
|
$fs = new Filesystem(); |
286
|
1 |
|
if ($fs->exists($file)) { |
287
|
|
|
$fs->remove($file); |
288
|
|
|
} |
289
|
1 |
|
$this->entityManager->remove($Page); |
290
|
1 |
|
$this->entityManager->flush(); |
291
|
|
|
|
292
|
1 |
|
$event = new EventArgs( |
293
|
|
|
[ |
294
|
1 |
|
'DeviceType' => $DeviceType, |
295
|
1 |
|
'Page' => $Page, |
296
|
|
|
], |
297
|
1 |
|
$request |
298
|
|
|
); |
299
|
1 |
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_PAGE_DELETE_COMPLETE, $event); |
300
|
|
|
|
301
|
1 |
|
$this->addSuccess('admin.delete.complete', 'admin'); |
302
|
|
|
} |
303
|
|
|
|
304
|
2 |
|
return $this->redirectToRoute('admin_content_page'); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
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.