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