1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Platine\App\Http\Action\Product; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Platine\Http\ResponseInterface; |
9
|
|
|
use Platine\Http\ServerRequestInterface; |
10
|
|
|
use Platine\Framework\Http\RequestData; |
11
|
|
|
use Platine\Framework\Http\Response\TemplateResponse; |
12
|
|
|
use Platine\Framework\Http\Response\RedirectResponse; |
13
|
|
|
use Platine\Lang\Lang; |
14
|
|
|
use Platine\Pagination\Pagination; |
15
|
|
|
use Platine\Template\Template; |
16
|
|
|
use Platine\Framework\Helper\Flash; |
17
|
|
|
use Platine\Framework\Http\RouteHelper; |
18
|
|
|
use Platine\Logger\LoggerInterface; |
19
|
|
|
use Platine\App\Model\Repository\ProductCategoryRepository; |
20
|
|
|
use Platine\App\Model\Entity\ProductCategory; |
21
|
|
|
use Platine\App\Param\ProductCategoryParam; |
22
|
|
|
use Platine\App\Validator\ProductCategoryValidator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @class CategoryAction |
26
|
|
|
* @package Platine\App\Http\Action\Product |
27
|
|
|
*/ |
28
|
|
|
class CategoryAction |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The Lang instance |
32
|
|
|
* @var Lang |
33
|
|
|
*/ |
34
|
|
|
protected Lang $lang; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The Pagination instance |
38
|
|
|
* @var Pagination |
39
|
|
|
*/ |
40
|
|
|
protected Pagination $pagination; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The Template instance |
44
|
|
|
* @var Template |
45
|
|
|
*/ |
46
|
|
|
protected Template $template; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The Flash instance |
50
|
|
|
* @var Flash |
51
|
|
|
*/ |
52
|
|
|
protected Flash $flash; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The RouteHelper instance |
56
|
|
|
* @var RouteHelper |
57
|
|
|
*/ |
58
|
|
|
protected RouteHelper $routeHelper; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The LoggerInterface instance |
62
|
|
|
* @var LoggerInterface |
63
|
|
|
*/ |
64
|
|
|
protected LoggerInterface $logger; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The ProductCategoryRepository instance |
68
|
|
|
* @var ProductCategoryRepository |
69
|
|
|
*/ |
70
|
|
|
protected ProductCategoryRepository $productCategoryRepository; |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Create new instance |
76
|
|
|
* @param Lang $lang |
77
|
|
|
* @param Pagination $pagination |
78
|
|
|
* @param Template $template |
79
|
|
|
* @param Flash $flash |
80
|
|
|
* @param RouteHelper $routeHelper |
81
|
|
|
* @param LoggerInterface $logger |
82
|
|
|
* @param ProductCategoryRepository $productCategoryRepository |
83
|
|
|
*/ |
84
|
|
|
public function __construct( |
85
|
|
|
Lang $lang, |
86
|
|
|
Pagination $pagination, |
87
|
|
|
Template $template, |
88
|
|
|
Flash $flash, |
89
|
|
|
RouteHelper $routeHelper, |
90
|
|
|
LoggerInterface $logger, |
91
|
|
|
ProductCategoryRepository $productCategoryRepository |
92
|
|
|
) { |
93
|
|
|
$this->lang = $lang; |
94
|
|
|
$this->pagination = $pagination; |
95
|
|
|
$this->template = $template; |
96
|
|
|
$this->flash = $flash; |
97
|
|
|
$this->routeHelper = $routeHelper; |
98
|
|
|
$this->logger = $logger; |
99
|
|
|
$this->productCategoryRepository = $productCategoryRepository; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* List all entities |
104
|
|
|
* @param ServerRequestInterface $request |
105
|
|
|
* @return ResponseInterface |
106
|
|
|
*/ |
107
|
|
|
public function index(ServerRequestInterface $request): ResponseInterface |
108
|
|
|
{ |
109
|
|
|
$context = []; |
110
|
|
|
$param = new RequestData($request); |
111
|
|
|
$totalItems = $this->productCategoryRepository->query() |
112
|
|
|
->count('id'); |
113
|
|
|
|
114
|
|
|
$currentPage = (int) $param->get('page', 1); |
115
|
|
|
|
116
|
|
|
$this->pagination->setTotalItems($totalItems) |
117
|
|
|
->setCurrentPage($currentPage); |
118
|
|
|
|
119
|
|
|
$limit = $this->pagination->getItemsPerPage(); |
120
|
|
|
$offset = $this->pagination->getOffset(); |
121
|
|
|
|
122
|
|
|
$results = $this->productCategoryRepository->query() |
123
|
|
|
->offset($offset) |
124
|
|
|
->limit($limit) |
125
|
|
|
->orderBy('name', 'ASC') |
126
|
|
|
->all(); |
127
|
|
|
|
128
|
|
|
$context['list'] = $results; |
129
|
|
|
$context['pagination'] = $this->pagination->render(); |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
return new TemplateResponse( |
133
|
|
|
$this->template, |
134
|
|
|
'product/category/list', |
135
|
|
|
$context |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* List entity detail |
141
|
|
|
* @param ServerRequestInterface $request |
142
|
|
|
* @return ResponseInterface |
143
|
|
|
*/ |
144
|
|
|
public function detail(ServerRequestInterface $request): ResponseInterface |
145
|
|
|
{ |
146
|
|
|
$context = []; |
147
|
|
|
$id = (int) $request->getAttribute('id'); |
148
|
|
|
|
149
|
|
|
/** @var ProductCategory|null $category */ |
150
|
|
|
$category = $this->productCategoryRepository->find($id); |
151
|
|
|
|
152
|
|
|
if ($category === null) { |
153
|
|
|
$this->flash->setError($this->lang->tr('This record doesn\'t exist')); |
154
|
|
|
|
155
|
|
|
return new RedirectResponse( |
156
|
|
|
$this->routeHelper->generateUrl('product_category_list') |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
$context['category'] = $category; |
160
|
|
|
|
161
|
|
|
return new TemplateResponse( |
162
|
|
|
$this->template, |
163
|
|
|
'product/category/detail', |
164
|
|
|
$context |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Create new entity |
170
|
|
|
* @param ServerRequestInterface $request |
171
|
|
|
* @return ResponseInterface |
172
|
|
|
*/ |
173
|
|
|
public function create(ServerRequestInterface $request): ResponseInterface |
174
|
|
|
{ |
175
|
|
|
$context = []; |
176
|
|
|
$param = new RequestData($request); |
177
|
|
|
|
178
|
|
|
$formParam = new ProductCategoryParam($param->posts()); |
179
|
|
|
$context['param'] = $formParam; |
180
|
|
|
|
181
|
|
|
if ($request->getMethod() === 'GET') { |
182
|
|
|
return new TemplateResponse( |
183
|
|
|
$this->template, |
184
|
|
|
'product/category/create', |
185
|
|
|
$context |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$validator = new ProductCategoryValidator($formParam, $this->lang); |
190
|
|
|
if ($validator->validate() === false) { |
191
|
|
|
$context['errors'] = $validator->getErrors(); |
192
|
|
|
|
193
|
|
|
return new TemplateResponse( |
194
|
|
|
$this->template, |
195
|
|
|
'product/category/create', |
196
|
|
|
$context |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$entityExist = $this->productCategoryRepository->findBy([ |
201
|
|
|
'name' => $formParam->getName(), |
202
|
|
|
]); |
203
|
|
|
|
204
|
|
|
if ($entityExist !== null) { |
205
|
|
|
$this->flash->setError($this->lang->tr('This record already exist')); |
206
|
|
|
|
207
|
|
|
return new TemplateResponse( |
208
|
|
|
$this->template, |
209
|
|
|
'product/category/create', |
210
|
|
|
$context |
211
|
|
|
); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** @var ProductCategory $category */ |
215
|
|
|
$category = $this->productCategoryRepository->create([ |
216
|
|
|
'name' => $formParam->getName(), |
217
|
|
|
'description' => $formParam->getDescription(), |
218
|
|
|
]); |
219
|
|
|
|
220
|
|
|
try { |
221
|
|
|
$this->productCategoryRepository->save($category); |
222
|
|
|
|
223
|
|
|
$this->flash->setSuccess($this->lang->tr('Data successfully created')); |
224
|
|
|
|
225
|
|
|
return new RedirectResponse( |
226
|
|
|
$this->routeHelper->generateUrl('product_category_list') |
227
|
|
|
); |
228
|
|
|
} catch (Exception $ex) { |
229
|
|
|
$this->logger->error('Error when saved the data {error}', ['error' => $ex->getMessage()]); |
230
|
|
|
|
231
|
|
|
$this->flash->setError($this->lang->tr('Data processing error')); |
232
|
|
|
|
233
|
|
|
return new TemplateResponse( |
234
|
|
|
$this->template, |
235
|
|
|
'product/category/create', |
236
|
|
|
$context |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Update existing entity |
243
|
|
|
* @param ServerRequestInterface $request |
244
|
|
|
* @return ResponseInterface |
245
|
|
|
*/ |
246
|
|
|
public function update(ServerRequestInterface $request): ResponseInterface |
247
|
|
|
{ |
248
|
|
|
$context = []; |
249
|
|
|
$param = new RequestData($request); |
250
|
|
|
|
251
|
|
|
$id = (int) $request->getAttribute('id'); |
252
|
|
|
|
253
|
|
|
/** @var ProductCategory|null $category */ |
254
|
|
|
$category = $this->productCategoryRepository->find($id); |
255
|
|
|
|
256
|
|
|
if ($category === null) { |
257
|
|
|
$this->flash->setError($this->lang->tr('This record doesn\'t exist')); |
258
|
|
|
|
259
|
|
|
return new RedirectResponse( |
260
|
|
|
$this->routeHelper->generateUrl('product_category_list') |
261
|
|
|
); |
262
|
|
|
} |
263
|
|
|
$context['category'] = $category; |
264
|
|
|
$context['param'] = (new ProductCategoryParam())->fromEntity($category); |
265
|
|
|
if ($request->getMethod() === 'GET') { |
266
|
|
|
return new TemplateResponse( |
267
|
|
|
$this->template, |
268
|
|
|
'product/category/update', |
269
|
|
|
$context |
270
|
|
|
); |
271
|
|
|
} |
272
|
|
|
$formParam = new ProductCategoryParam($param->posts()); |
273
|
|
|
$context['param'] = $formParam; |
274
|
|
|
|
275
|
|
|
$validator = new ProductCategoryValidator($formParam, $this->lang); |
276
|
|
|
if ($validator->validate() === false) { |
277
|
|
|
$context['errors'] = $validator->getErrors(); |
278
|
|
|
|
279
|
|
|
return new TemplateResponse( |
280
|
|
|
$this->template, |
281
|
|
|
'product/category/update', |
282
|
|
|
$context |
283
|
|
|
); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
$entityExist = $this->productCategoryRepository->findBy([ |
287
|
|
|
'name' => $formParam->getName(), |
288
|
|
|
]); |
289
|
|
|
|
290
|
|
|
if ($entityExist !== null && $entityExist->id !== $id) { |
291
|
|
|
$this->flash->setError($this->lang->tr('This record already exist')); |
292
|
|
|
|
293
|
|
|
return new TemplateResponse( |
294
|
|
|
$this->template, |
295
|
|
|
'product/category/update', |
296
|
|
|
$context |
297
|
|
|
); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$category->name = $formParam->getName(); |
|
|
|
|
301
|
|
|
$category->description = $formParam->getDescription(); |
|
|
|
|
302
|
|
|
|
303
|
|
|
try { |
304
|
|
|
$this->productCategoryRepository->save($category); |
305
|
|
|
|
306
|
|
|
$this->flash->setSuccess($this->lang->tr('Data successfully updated')); |
307
|
|
|
|
308
|
|
|
return new RedirectResponse( |
309
|
|
|
$this->routeHelper->generateUrl('product_category_detail', ['id' => $id]) |
310
|
|
|
); |
311
|
|
|
} catch (Exception $ex) { |
312
|
|
|
$this->logger->error('Error when saved the data {error}', ['error' => $ex->getMessage()]); |
313
|
|
|
|
314
|
|
|
$this->flash->setError($this->lang->tr('Data processing error')); |
315
|
|
|
|
316
|
|
|
return new TemplateResponse( |
317
|
|
|
$this->template, |
318
|
|
|
'product/category/update', |
319
|
|
|
$context |
320
|
|
|
); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Delete the entity |
326
|
|
|
* @param ServerRequestInterface $request |
327
|
|
|
* @return ResponseInterface |
328
|
|
|
*/ |
329
|
|
|
public function delete(ServerRequestInterface $request): ResponseInterface |
330
|
|
|
{ |
331
|
|
|
$id = (int) $request->getAttribute('id'); |
332
|
|
|
|
333
|
|
|
/** @var ProductCategory|null $category */ |
334
|
|
|
$category = $this->productCategoryRepository->find($id); |
335
|
|
|
|
336
|
|
|
if ($category === null) { |
337
|
|
|
$this->flash->setError($this->lang->tr('This record doesn\'t exist')); |
338
|
|
|
|
339
|
|
|
return new RedirectResponse( |
340
|
|
|
$this->routeHelper->generateUrl('product_category_list') |
341
|
|
|
); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
try { |
345
|
|
|
$this->productCategoryRepository->delete($category); |
346
|
|
|
|
347
|
|
|
$this->flash->setSuccess($this->lang->tr('Data successfully deleted')); |
348
|
|
|
|
349
|
|
|
return new RedirectResponse( |
350
|
|
|
$this->routeHelper->generateUrl('product_category_list') |
351
|
|
|
); |
352
|
|
|
} catch (Exception $ex) { |
353
|
|
|
$this->logger->error('Error when delete the data {error}', ['error' => $ex->getMessage()]); |
354
|
|
|
|
355
|
|
|
$this->flash->setError($this->lang->tr('Data processing error')); |
356
|
|
|
|
357
|
|
|
return new RedirectResponse( |
358
|
|
|
$this->routeHelper->generateUrl('product_category_list') |
359
|
|
|
); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|