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\Product; |
26
|
|
|
|
27
|
|
|
use Doctrine\ORM\EntityManager; |
28
|
|
|
use Eccube\Annotation\Inject; |
29
|
|
|
use Eccube\Application; |
30
|
|
|
use Eccube\Controller\AbstractController; |
31
|
|
|
use Eccube\Entity\Master\CsvType; |
32
|
|
|
use Eccube\Event\EccubeEvents; |
33
|
|
|
use Eccube\Event\EventArgs; |
34
|
|
|
use Eccube\Form\Type\Admin\CategoryType; |
35
|
|
|
use Eccube\Repository\CategoryRepository; |
36
|
|
|
use Eccube\Service\CsvExportService; |
37
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
38
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
39
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
40
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
41
|
|
|
use Symfony\Component\Form\FormFactory; |
42
|
|
|
use Symfony\Component\HttpFoundation\Request; |
43
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
44
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
45
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @Route(service=CategoryController::class) |
49
|
|
|
*/ |
50
|
|
|
class CategoryController extends AbstractController |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* @Inject(CsvExportService::class) |
54
|
|
|
* @var CsvExportService |
55
|
|
|
*/ |
56
|
|
|
protected $csvExportService; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @Inject("orm.em") |
60
|
|
|
* @var EntityManager |
61
|
|
|
*/ |
62
|
|
|
protected $entityManager; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @Inject("config") |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected $appConfig; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @Inject("eccube.event.dispatcher") |
72
|
|
|
* @var EventDispatcher |
73
|
|
|
*/ |
74
|
|
|
protected $eventDispatcher; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @Inject("form.factory") |
78
|
|
|
* @var FormFactory |
79
|
|
|
*/ |
80
|
|
|
protected $formFactory; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @Inject(CategoryRepository::class) |
84
|
|
|
* @var CategoryRepository |
85
|
|
|
*/ |
86
|
|
|
protected $categoryRepository; |
87
|
|
|
|
88
|
|
|
/** |
|
|
|
|
89
|
|
|
* @Route("/{_admin}/product/category", name="admin_product_category") |
90
|
|
|
* @Route("/{_admin}/product/category/{parent_id}", requirements={"parent_id" = "\d+"}, name="admin_product_category_show") |
91
|
|
|
* @Route("/{_admin}/product/category/{id}/edit", requirements={"id" = "\d+"}, name="admin_product_category_edit") |
92
|
|
|
* @Template("Product/category.twig") |
93
|
|
|
*/ |
|
|
|
|
94
|
5 |
|
public function index(Application $app, Request $request, $parent_id = null, $id = null) |
95
|
|
|
{ |
96
|
5 |
|
if ($parent_id) { |
97
|
2 |
|
$Parent = $this->categoryRepository->find($parent_id); |
98
|
2 |
|
if (!$Parent) { |
99
|
2 |
|
throw new NotFoundHttpException('親カテゴリが存在しません'); |
100
|
|
|
} |
101
|
|
|
} else { |
102
|
3 |
|
$Parent = null; |
103
|
|
|
} |
104
|
5 |
|
if ($id) { |
105
|
1 |
|
$TargetCategory = $this->categoryRepository->find($id); |
106
|
1 |
|
if (!$TargetCategory) { |
107
|
|
|
throw new NotFoundHttpException('カテゴリが存在しません'); |
108
|
|
|
} |
109
|
1 |
|
$Parent = $TargetCategory->getParent(); |
110
|
|
|
} else { |
111
|
4 |
|
$TargetCategory = new \Eccube\Entity\Category(); |
112
|
4 |
|
$TargetCategory->setParent($Parent); |
113
|
4 |
|
if ($Parent) { |
114
|
2 |
|
$TargetCategory->setHierarchy($Parent->getHierarchy() + 1); |
115
|
|
|
} else { |
116
|
2 |
|
$TargetCategory->setHierarchy(1); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// |
121
|
5 |
|
$builder = $this->formFactory |
122
|
5 |
|
->createBuilder(CategoryType::class, $TargetCategory); |
123
|
|
|
|
124
|
5 |
|
$event = new EventArgs( |
125
|
|
|
array( |
126
|
5 |
|
'builder' => $builder, |
127
|
5 |
|
'Parent' => $Parent, |
128
|
5 |
|
'TargetCategory' => $TargetCategory, |
129
|
|
|
), |
130
|
5 |
|
$request |
131
|
|
|
); |
132
|
5 |
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CATEGORY_INDEX_INITIALIZE, $event); |
133
|
|
|
|
134
|
5 |
|
$form = $builder->getForm(); |
135
|
|
|
|
136
|
|
|
// |
137
|
5 |
|
if ($request->getMethod() === 'POST') { |
138
|
2 |
|
$form->handleRequest($request); |
139
|
2 |
|
if ($form->isValid()) { |
140
|
2 |
|
if ($this->appConfig['category_nest_level'] < $TargetCategory->getHierarchy()) { |
141
|
|
|
throw new BadRequestHttpException('リクエストが不正です'); |
142
|
|
|
} |
143
|
2 |
|
log_info('カテゴリ登録開始', array($id)); |
144
|
|
|
|
145
|
2 |
|
$this->categoryRepository->save($TargetCategory); |
146
|
|
|
|
147
|
2 |
|
log_info('カテゴリ登録完了', array($id)); |
148
|
|
|
|
149
|
2 |
|
$event = new EventArgs( |
150
|
|
|
array( |
151
|
2 |
|
'form' => $form, |
152
|
2 |
|
'Parent' => $Parent, |
153
|
2 |
|
'TargetCategory' => $TargetCategory, |
154
|
|
|
), |
155
|
2 |
|
$request |
156
|
|
|
); |
157
|
2 |
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CATEGORY_INDEX_COMPLETE, $event); |
158
|
|
|
|
159
|
2 |
|
$app->addSuccess('admin.category.save.complete', 'admin'); |
160
|
|
|
|
161
|
2 |
|
if ($Parent) { |
162
|
1 |
|
return $app->redirect($app->url('admin_product_category_show', array('parent_id' => $Parent->getId()))); |
163
|
|
|
} else { |
164
|
1 |
|
return $app->redirect($app->url('admin_product_category')); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
3 |
|
$Categories = $this->categoryRepository->getList($Parent); |
170
|
|
|
|
171
|
|
|
// ツリー表示のため、ルートからのカテゴリを取得 |
172
|
3 |
|
$TopCategories = $this->categoryRepository->getList(null); |
173
|
|
|
|
174
|
|
|
return [ |
175
|
3 |
|
'form' => $form->createView(), |
176
|
3 |
|
'Parent' => $Parent, |
177
|
3 |
|
'Categories' => $Categories, |
178
|
3 |
|
'TopCategories' => $TopCategories, |
179
|
3 |
|
'TargetCategory' => $TargetCategory, |
180
|
|
|
]; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
|
|
|
|
184
|
|
|
* @Method("DELETE") |
185
|
|
|
* @Route("/{_admin}/product/category/{id}/delete", requirements={"id" = "\d+"}, name="admin_product_category_delete") |
186
|
|
|
*/ |
|
|
|
|
187
|
1 |
|
public function delete(Application $app, Request $request, $id) |
188
|
|
|
{ |
189
|
1 |
|
$this->isTokenValid($app); |
190
|
|
|
|
191
|
1 |
|
$TargetCategory = $this->categoryRepository->find($id); |
192
|
1 |
|
if (!$TargetCategory) { |
193
|
|
|
$app->deleteMessage(); |
194
|
|
|
return $app->redirect($app->url('admin_product_category')); |
|
|
|
|
195
|
|
|
} |
196
|
1 |
|
$Parent = $TargetCategory->getParent(); |
197
|
|
|
|
198
|
1 |
|
log_info('カテゴリ削除開始', array($id)); |
199
|
|
|
|
200
|
|
|
try { |
201
|
1 |
|
$this->categoryRepository->delete($TargetCategory); |
202
|
|
|
|
203
|
1 |
|
$event = new EventArgs( |
204
|
|
|
array( |
205
|
1 |
|
'Parent' => $Parent, |
206
|
1 |
|
'TargetCategory' => $TargetCategory, |
207
|
1 |
|
), $request |
208
|
|
|
); |
209
|
1 |
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CATEGORY_DELETE_COMPLETE, $event); |
210
|
|
|
|
211
|
1 |
|
$app->addSuccess('admin.category.delete.complete', 'admin'); |
212
|
|
|
|
213
|
1 |
|
log_info('カテゴリ削除完了', array($id)); |
214
|
|
|
|
|
|
|
|
215
|
|
|
} catch (\Exception $e) { |
216
|
|
|
log_info('カテゴリ削除エラー', [$id, $e]); |
217
|
|
|
|
218
|
|
|
$message = $app->trans('admin.delete.failed.foreign_key', ['%name%' => 'カテゴリ']); |
219
|
|
|
$app->addError($message, 'admin'); |
220
|
|
|
} |
221
|
|
|
|
222
|
1 |
|
if ($Parent) { |
223
|
|
|
return $app->redirect($app->url('admin_product_category_show', array('parent_id' => $Parent->getId()))); |
224
|
|
|
} else { |
225
|
1 |
|
return $app->redirect($app->url('admin_product_category')); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
|
|
|
|
230
|
|
|
* @Method("POST") |
231
|
|
|
* @Route("/{_admin}/product/category/sort_no/move", name="admin_product_category_sort_no_move") |
232
|
|
|
*/ |
|
|
|
|
233
|
2 |
View Code Duplication |
public function moveSortNo(Application $app, Request $request) |
|
|
|
|
234
|
|
|
{ |
235
|
2 |
|
if ($request->isXmlHttpRequest()) { |
236
|
2 |
|
$sortNos = $request->request->all(); |
237
|
2 |
|
foreach ($sortNos as $categoryId => $sortNo) { |
238
|
|
|
/* @var $Category \Eccube\Entity\Category */ |
239
|
2 |
|
$Category = $this->categoryRepository |
240
|
2 |
|
->find($categoryId); |
241
|
2 |
|
$Category->setSortNo($sortNo); |
242
|
2 |
|
$this->entityManager->persist($Category); |
243
|
|
|
} |
244
|
2 |
|
$this->entityManager->flush(); |
245
|
|
|
} |
246
|
2 |
|
return true; |
|
|
|
|
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* カテゴリCSVの出力. |
252
|
|
|
* |
253
|
|
|
* @Route("/{_admin}/product/category/export", name="admin_product_category_export") |
254
|
|
|
* |
255
|
|
|
* @param Application $app |
256
|
|
|
* @param Request $request |
|
|
|
|
257
|
|
|
* @return StreamedResponse |
258
|
|
|
*/ |
259
|
|
|
public function export(Application $app, Request $request) |
260
|
|
|
{ |
261
|
|
|
// タイムアウトを無効にする. |
262
|
|
|
set_time_limit(0); |
263
|
|
|
|
264
|
|
|
// sql loggerを無効にする. |
265
|
|
|
$em = $this->entityManager; |
266
|
|
|
$em->getConfiguration()->setSQLLogger(null); |
267
|
|
|
|
268
|
|
|
$response = new StreamedResponse(); |
269
|
|
|
$response->setCallback(function () use ($app, $request) { |
270
|
|
|
|
271
|
|
|
// CSV種別を元に初期化. |
272
|
|
|
$this->csvExportService->initCsvType(CsvType::CSV_TYPE_CATEGORY); |
273
|
|
|
|
274
|
|
|
// ヘッダ行の出力. |
275
|
|
|
$this->csvExportService->exportHeader(); |
276
|
|
|
|
277
|
|
|
$qb = $this->categoryRepository |
278
|
|
|
->createQueryBuilder('c') |
279
|
|
|
->orderBy('c.sort_no', 'DESC'); |
280
|
|
|
|
281
|
|
|
// データ行の出力. |
282
|
|
|
$this->csvExportService->setExportQueryBuilder($qb); |
283
|
|
View Code Duplication |
$this->csvExportService->exportData(function ($entity, $csvService) use ($app, $request) { |
284
|
|
|
|
285
|
|
|
$Csvs = $csvService->getCsvs(); |
286
|
|
|
|
287
|
|
|
/** @var $Category \Eccube\Entity\Category */ |
288
|
|
|
$Category = $entity; |
289
|
|
|
|
290
|
|
|
// CSV出力項目と合致するデータを取得. |
291
|
|
|
$ExportCsvRow = new \Eccube\Entity\ExportCsvRow(); |
292
|
|
|
foreach ($Csvs as $Csv) { |
293
|
|
|
$ExportCsvRow->setData($csvService->getData($Csv, $Category)); |
294
|
|
|
|
295
|
|
|
$event = new EventArgs( |
296
|
|
|
array( |
297
|
|
|
'csvService' => $csvService, |
298
|
|
|
'Csv' => $Csv, |
299
|
|
|
'Category' => $Category, |
300
|
|
|
'ExportCsvRow' => $ExportCsvRow, |
301
|
|
|
), |
302
|
|
|
$request |
303
|
|
|
); |
304
|
|
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CATEGORY_CSV_EXPORT, $event); |
305
|
|
|
|
306
|
|
|
$ExportCsvRow->pushData(); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
//$row[] = number_format(memory_get_usage(true)); |
310
|
|
|
// 出力. |
311
|
|
|
$csvService->fputcsv($ExportCsvRow->getRow()); |
312
|
|
|
}); |
313
|
|
|
}); |
314
|
|
|
|
315
|
|
|
$now = new \DateTime(); |
316
|
|
|
$filename = 'category_' . $now->format('YmdHis') . '.csv'; |
|
|
|
|
317
|
|
|
$response->headers->set('Content-Type', 'application/octet-stream'); |
318
|
|
|
$response->headers->set('Content-Disposition', 'attachment; filename=' . $filename); |
|
|
|
|
319
|
|
|
$response->send(); |
320
|
|
|
|
321
|
|
|
log_info('カテゴリCSV出力ファイル名', array($filename)); |
322
|
|
|
|
323
|
|
|
return $response; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|