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
|
|
|
namespace Eccube\Controller\Admin\Content; |
25
|
|
|
|
26
|
|
|
use Eccube\Annotation\Component; |
27
|
|
|
use Eccube\Annotation\Inject; |
28
|
|
|
use Eccube\Application; |
29
|
|
|
use Eccube\Common\Constant; |
30
|
|
|
use Eccube\Controller\AbstractController; |
31
|
|
|
use Eccube\Entity\News; |
32
|
|
|
use Eccube\Event\EccubeEvents; |
33
|
|
|
use Eccube\Event\EventArgs; |
34
|
|
|
use Eccube\Form\Type\Admin\NewsType; |
35
|
|
|
use Eccube\Repository\NewsRepository; |
36
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
37
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
38
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
39
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
40
|
|
|
use Symfony\Component\Form\FormFactory; |
41
|
|
|
use Symfony\Component\HttpFoundation\Request; |
42
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 新着情報のコントローラクラス |
46
|
|
|
* |
47
|
|
|
* @Component |
48
|
|
|
* @Route(service=NewsController::class) |
49
|
|
|
*/ |
50
|
|
|
class NewsController extends AbstractController |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* @Inject("form.factory") |
54
|
|
|
* @var FormFactory |
55
|
|
|
*/ |
56
|
|
|
protected $formFactory; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @Inject("eccube.event.dispatcher") |
60
|
|
|
* @var EventDispatcher |
61
|
|
|
*/ |
62
|
|
|
protected $eventDispatcher; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @Inject(NewsRepository::class) |
66
|
|
|
* @var NewsRepository |
67
|
|
|
*/ |
68
|
|
|
protected $newsRepository; |
69
|
|
|
|
70
|
|
|
/** |
|
|
|
|
71
|
|
|
* 新着情報一覧を表示する。 |
72
|
|
|
* |
73
|
|
|
* @Route("/{_admin}/content/news", name="admin_content_news") |
74
|
|
|
* @Template("Content/news.twig") |
75
|
|
|
* |
76
|
|
|
* @param Application $app |
77
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
78
|
|
|
*/ |
79
|
1 |
View Code Duplication |
public function index(Application $app, Request $request) |
|
|
|
|
80
|
|
|
{ |
81
|
1 |
|
$NewsList = $this->newsRepository->findBy(array(), array('rank' => 'DESC')); |
82
|
|
|
|
83
|
1 |
|
$builder = $this->formFactory->createBuilder(); |
84
|
|
|
|
85
|
1 |
|
$event = new EventArgs( |
86
|
|
|
array( |
87
|
1 |
|
'builder' => $builder, |
88
|
1 |
|
'NewsList' => $NewsList, |
89
|
|
|
), |
90
|
1 |
|
$request |
91
|
|
|
); |
92
|
1 |
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_INDEX_INITIALIZE, $event); |
93
|
|
|
|
94
|
1 |
|
$form = $builder->getForm(); |
95
|
|
|
|
96
|
|
|
return [ |
97
|
1 |
|
'form' => $form->createView(), |
98
|
1 |
|
'NewsList' => $NewsList, |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 新着情報を登録・編集する。 |
104
|
|
|
* |
105
|
|
|
* @Route("/{_admin}/content/news/new", name="admin_content_news_new") |
106
|
|
|
* @Route("/{_admin}/content/news/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_news_edit") |
107
|
|
|
* @Template("Content/news_edit.twig") |
108
|
|
|
* |
109
|
|
|
* @param Application $app |
110
|
|
|
* @param Request $request |
|
|
|
|
111
|
|
|
* @param null $id |
|
|
|
|
112
|
|
|
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse |
113
|
|
|
*/ |
114
|
2 |
|
public function edit(Application $app, Request $request, $id = null) |
115
|
|
|
{ |
116
|
2 |
|
if ($id) { |
117
|
1 |
|
$News = $this->newsRepository->find($id); |
118
|
1 |
|
if (!$News) { |
119
|
1 |
|
throw new NotFoundHttpException(); |
120
|
|
|
} |
121
|
|
|
} else { |
122
|
1 |
|
$News = new \Eccube\Entity\News(); |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
$News->setLinkMethod((bool)$News->getLinkMethod()); |
|
|
|
|
126
|
|
|
|
127
|
2 |
|
$builder = $this->formFactory |
128
|
2 |
|
->createBuilder(NewsType::class, $News); |
129
|
|
|
|
130
|
2 |
|
$event = new EventArgs( |
131
|
|
|
array( |
132
|
2 |
|
'builder' => $builder, |
133
|
2 |
|
'News' => $News, |
134
|
|
|
), |
135
|
2 |
|
$request |
136
|
|
|
); |
137
|
2 |
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_EDIT_INITIALIZE, $event); |
138
|
|
|
|
139
|
2 |
|
$form = $builder->getForm(); |
140
|
2 |
|
$form->handleRequest($request); |
141
|
|
|
|
142
|
2 |
|
if ($form->isSubmitted() && $form->isValid()) { |
143
|
|
|
if (!$News->getUrl()) { |
144
|
|
|
$News->setLinkMethod(Constant::DISABLED); |
145
|
|
|
} |
146
|
|
|
$this->newsRepository->save($News); |
147
|
|
|
|
148
|
|
|
$event = new EventArgs( |
149
|
|
|
array( |
150
|
|
|
'form' => $form, |
151
|
|
|
'News' => $News, |
152
|
|
|
), |
153
|
|
|
$request |
154
|
|
|
); |
155
|
|
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_EDIT_COMPLETE, $event); |
156
|
|
|
|
157
|
|
|
$app->addSuccess('admin.news.save.complete', 'admin'); |
158
|
|
|
|
159
|
|
|
return $app->redirect($app->url('admin_content_news')); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return [ |
163
|
2 |
|
'form' => $form->createView(), |
164
|
2 |
|
'News' => $News, |
165
|
|
|
]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* 指定した新着情報の表示順を1つ上げる。 |
170
|
|
|
* |
171
|
|
|
* @Method("PUT") |
172
|
|
|
* @Route("/{_admin}/content/news/{id}/up", requirements={"id" = "\d+"}, name="admin_content_news_up") |
173
|
|
|
* |
174
|
|
|
* @param Application $app |
175
|
|
|
* @param Request $request |
|
|
|
|
176
|
|
|
* @param News $News |
|
|
|
|
177
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
178
|
|
|
*/ |
179
|
1 |
View Code Duplication |
public function up(Application $app, Request $request, News $News) |
|
|
|
|
180
|
|
|
{ |
181
|
1 |
|
$this->isTokenValid($app); |
182
|
|
|
|
183
|
|
|
try { |
184
|
1 |
|
$this->newsRepository->up($News); |
185
|
|
|
|
186
|
1 |
|
$app->addSuccess('admin.news.up.complete', 'admin'); |
187
|
|
|
} catch (\Exception $e) { |
|
|
|
|
188
|
|
|
|
189
|
|
|
log_error('新着情報表示順更新エラー', [$News->getId(), $e]); |
190
|
|
|
|
191
|
|
|
$app->addError('admin.news.up.error', 'admin'); |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
return $app->redirect($app->url('admin_content_news')); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* 指定した新着情報の表示順を1つ下げる。 |
199
|
|
|
* |
200
|
|
|
* @Method("PUT") |
201
|
|
|
* @Route("/{_admin}/content/news/{id}/down", requirements={"id" = "\d+"}, name="admin_content_news_down") |
202
|
|
|
* |
203
|
|
|
* @param Application $app |
204
|
|
|
* @param Request $request |
|
|
|
|
205
|
|
|
* @param News $News |
|
|
|
|
206
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
207
|
|
|
*/ |
208
|
1 |
View Code Duplication |
public function down(Application $app, Request $request, News $News) |
|
|
|
|
209
|
|
|
{ |
210
|
1 |
|
$this->isTokenValid($app); |
211
|
|
|
|
212
|
|
|
try { |
213
|
1 |
|
$this->newsRepository->down($News); |
214
|
|
|
|
215
|
1 |
|
$app->addSuccess('admin.news.down.complete', 'admin'); |
216
|
|
|
} catch (\Exception $e) { |
|
|
|
|
217
|
|
|
|
218
|
|
|
log_error('新着情報表示順更新エラー', [$News->getId(), $e]); |
219
|
|
|
|
220
|
|
|
$app->addError('admin.news.down.error', 'admin'); |
221
|
|
|
} |
222
|
|
|
|
223
|
1 |
|
return $app->redirect($app->url('admin_content_news')); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* 指定した新着情報を削除する。 |
228
|
|
|
* |
229
|
|
|
* @Method("DELETE") |
230
|
|
|
* @Route("/{_admin}/content/news/{id}/delete", requirements={"id" = "\d+"}, name="admin_content_news_delete") |
231
|
|
|
* |
232
|
|
|
* @param Application $app |
233
|
|
|
* @param Request $request |
|
|
|
|
234
|
|
|
* @param News $News |
|
|
|
|
235
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
236
|
|
|
*/ |
237
|
1 |
View Code Duplication |
public function delete(Application $app, Request $request, News $News) |
|
|
|
|
238
|
|
|
{ |
239
|
1 |
|
$this->isTokenValid($app); |
240
|
|
|
|
241
|
1 |
|
log_info('新着情報削除開始', [$News->getId()]); |
242
|
|
|
|
243
|
|
|
try { |
244
|
1 |
|
$this->newsRepository->delete($News); |
245
|
|
|
|
246
|
1 |
|
$event = new EventArgs(['News' => $News], $request); |
247
|
1 |
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_DELETE_COMPLETE, $event); |
248
|
|
|
|
249
|
1 |
|
$app->addSuccess('admin.news.delete.complete', 'admin'); |
250
|
|
|
|
251
|
1 |
|
log_info('新着情報削除完了', [$News->getId()]); |
252
|
|
|
|
|
|
|
|
253
|
|
|
} catch (\Exception $e) { |
|
|
|
|
254
|
|
|
|
255
|
|
|
$message = $app->trans('admin.delete.failed.foreign_key', ['%name%' => '新着情報']); |
256
|
|
|
$app->addError($message, 'admin'); |
257
|
|
|
|
258
|
|
|
log_error('新着情報削除エラー', [$News->getId(), $e]); |
259
|
|
|
} |
260
|
|
|
|
261
|
1 |
|
return $app->redirect($app->url('admin_content_news')); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|