26
26
27
27
namespace PrestaShopBundle \Controller \Admin ;
28
28
29
- use Context ;
30
29
use PrestaShop \PrestaShop \Adapter \Tools ;
31
30
use PrestaShop \PrestaShop \Core \Domain \Notification \Command \UpdateEmployeeNotificationLastElementCommand ;
32
31
use PrestaShop \PrestaShop \Core \Domain \Notification \Query \GetNotificationLastElements ;
33
32
use PrestaShop \PrestaShop \Core \Domain \Notification \QueryResult \NotificationsResults ;
34
33
use PrestaShop \PrestaShop \Core \Grid \Definition \Factory \AbstractGridDefinitionFactory ;
35
34
use PrestaShop \PrestaShop \Core \Grid \Definition \Factory \FilterableGridDefinitionFactoryInterface ;
36
- use PrestaShop \PrestaShop \Core \Grid \Definition \Factory \GridDefinitionFactoryInterface ;
35
+ use PrestaShop \PrestaShop \Core \Grid \Definition \Factory \GridDefinitionFactoryProvider ;
37
36
use PrestaShop \PrestaShop \Core \Grid \Position \Exception \PositionUpdateException ;
38
- use PrestaShop \PrestaShop \Core \Grid \Position \GridPositionUpdaterInterface ;
39
- use PrestaShop \PrestaShop \Core \Grid \Position \PositionDefinitionInterface ;
40
- use PrestaShop \PrestaShop \Core \Grid \Position \PositionUpdateFactoryInterface ;
37
+ use PrestaShop \PrestaShop \Core \Grid \Position \PositionDefinitionProvider ;
41
38
use PrestaShop \PrestaShop \Core \Kpi \Row \KpiRowInterface ;
42
- use PrestaShopBundle \Entity \Employee \Employee ;
39
+ use PrestaShop \PrestaShop \Core \Kpi \Row \KpiRowPresenter ;
40
+ use PrestaShopBundle \Entity \Repository \AdminFilterRepository ;
43
41
use PrestaShopBundle \Security \Attribute \AdminSecurity ;
44
42
use PrestaShopBundle \Service \Grid \ControllerResponseBuilder ;
45
- use PrestaShopBundle \Service \Grid \ResponseBuilder ;
46
43
use ReflectionClass ;
47
44
use Symfony \Component \HttpFoundation \JsonResponse ;
48
45
use Symfony \Component \HttpFoundation \RedirectResponse ;
52
49
/**
53
50
* Admin controller for the common actions across the whole admin interface.
54
51
*/
55
- class CommonController extends FrameworkBundleAdminController
52
+ class CommonController extends PrestaShopAdminController
56
53
{
54
+ public static function getSubscribedServices (): array
55
+ {
56
+ return parent ::getSubscribedServices () + [
57
+ ControllerResponseBuilder::class => ControllerResponseBuilder::class,
58
+ ];
59
+ }
60
+
57
61
/**
58
62
* Get a summary of recent events on the shop.
59
63
* This includes:
@@ -63,11 +67,10 @@ class CommonController extends FrameworkBundleAdminController
63
67
*
64
68
* @return JsonResponse
65
69
*/
66
- public function notificationsAction ()
70
+ public function notificationsAction (): JsonResponse
67
71
{
68
- $ employeeId = Context::getContext ()->employee ->id ;
69
72
/** @var NotificationsResults $elements */
70
- $ elements = $ this ->getQueryBus ()-> handle ( new GetNotificationLastElements ($ employeeId ));
73
+ $ elements = $ this ->dispatchQuery ( new GetNotificationLastElements ($ this -> getEmployeeContext ()-> getEmployee ()-> getId () ));
71
74
72
75
return new JsonResponse ($ elements ->getNotificationsResultsForJS ());
73
76
}
@@ -79,10 +82,9 @@ public function notificationsAction()
79
82
*
80
83
* @return JsonResponse
81
84
*/
82
- public function notificationsAckAction (Request $ request )
85
+ public function notificationsAckAction (Request $ request ): JsonResponse
83
86
{
84
- $ type = $ request ->request ->get ('type ' );
85
- $ this ->getCommandBus ()->handle (new UpdateEmployeeNotificationLastElementCommand ($ type ));
87
+ $ this ->dispatchCommand (new UpdateEmployeeNotificationLastElementCommand ($ request ->request ->get ('type ' )));
86
88
87
89
return new JsonResponse (true );
88
90
}
@@ -112,7 +114,7 @@ public function notificationsAckAction(Request $request)
112
114
*
113
115
* @return Response
114
116
*/
115
- public function paginationAction (Request $ request , $ limit = 10 , $ offset = 0 , $ total = 0 , $ view = 'full ' , $ prefix = '' ): Response
117
+ public function paginationAction (Request $ request , ? int $ limit = 10 , ? int $ offset = 0 , ? int $ total = 0 , string $ view = 'full ' , string $ prefix = '' ): Response
116
118
{
117
119
$ offsetParam = empty ($ prefix ) ? 'offset ' : sprintf ('%s[offset] ' , $ prefix );
118
120
$ limitParam = empty ($ prefix ) ? 'limit ' : sprintf ('%s[limit] ' , $ prefix );
@@ -210,10 +212,12 @@ public function paginationAction(Request $request, $limit = 10, $offset = 0, $to
210
212
*
211
213
* @return Response
212
214
*/
213
- public function renderSidebarAction ($ url , $ title = '' , $ footer = '' )
214
- {
215
- $ tools = $ this ->get (Tools::class);
216
-
215
+ public function renderSidebarAction (
216
+ Tools $ tools ,
217
+ string $ url ,
218
+ string $ title = '' ,
219
+ string $ footer = '' ,
220
+ ): Response {
217
221
return $ this ->render ('@PrestaShop/Admin/Common/_partials/_sidebar.html.twig ' , [
218
222
'footer ' => $ tools ->purifyHTML ($ footer ),
219
223
'title ' => $ title ,
@@ -228,12 +232,12 @@ public function renderSidebarAction($url, $title = '', $footer = '')
228
232
*
229
233
* @return Response
230
234
*/
231
- public function renderKpiRowAction (KpiRowInterface $ kpiRow )
232
- {
233
- $ presenter = $ this -> get ( ' prestashop.core.kpi_row.presenter ' );
234
-
235
+ public function renderKpiRowAction (
236
+ KpiRowInterface $ kpiRow ,
237
+ KpiRowPresenter $ kpiRowPresenter ,
238
+ ): Response {
235
239
return $ this ->render ('@PrestaShop/Admin/Common/Kpi/kpi_row.html.twig ' , [
236
- 'kpiRow ' => $ presenter ->present ($ kpiRow ),
240
+ 'kpiRow ' => $ kpiRowPresenter ->present ($ kpiRow ),
237
241
]);
238
242
}
239
243
@@ -246,11 +250,14 @@ public function renderKpiRowAction(KpiRowInterface $kpiRow)
246
250
*
247
251
* @throws \Doctrine\ORM\OptimisticLockException
248
252
*/
249
- public function resetSearchAction ($ controller = '' , $ action = '' , $ filterId = '' )
250
- {
251
- $ adminFiltersRepository = $ this ->get ('prestashop.core.admin.admin_filter.repository ' );
252
- $ employeeId = $ this ->getUser () instanceof Employee ? $ this ->getUser ()->getId () : 0 ;
253
- $ shopId = $ this ->getContext ()->shop ->id ;
253
+ public function resetSearchAction (
254
+ AdminFilterRepository $ adminFiltersRepository ,
255
+ string $ controller = '' ,
256
+ string $ action = '' ,
257
+ string $ filterId = '' ,
258
+ ): JsonResponse {
259
+ $ employeeId = $ this ->getEmployeeContext ()->getEmployee ()->getId ();
260
+ $ shopId = $ this ->getShopContext ()->getId ();
254
261
255
262
// for compatibility when $controller and $action are used
256
263
if (!empty ($ controller ) && !empty ($ action )) {
@@ -270,33 +277,6 @@ public function resetSearchAction($controller = '', $action = '', $filterId = ''
270
277
return new JsonResponse ();
271
278
}
272
279
273
- /**
274
- * Specific action to render a specific field twice.
275
- *
276
- * @param string $formName the form name
277
- * @param string $formType the form type FQCN
278
- * @param string $fieldName the field name
279
- * @param array $fieldData the field data
280
- *
281
- * @return Response
282
- */
283
- public function renderFieldAction ($ formName , $ formType , $ fieldName , $ fieldData )
284
- {
285
- $ formData = [
286
- $ formName => [
287
- $ fieldName => $ fieldData ,
288
- ],
289
- ];
290
-
291
- $ form = $ this ->createFormBuilder ($ formData );
292
- $ form ->add ($ formName , $ formType );
293
-
294
- return $ this ->render ('@PrestaShop/Admin/Common/_partials/_form_field.html.twig ' , [
295
- 'form ' => $ form ->getForm ()->get ($ formName )->get ($ fieldName )->createView (),
296
- 'formId ' => $ formName . '_ ' . $ fieldName . '_rendered ' ,
297
- ]);
298
- }
299
-
300
280
/**
301
281
* Process Grid search.
302
282
*
@@ -309,16 +289,15 @@ public function renderFieldAction($formName, $formType, $fieldName, $fieldData)
309
289
*/
310
290
#[AdminSecurity("is_granted('read', request.get('_legacy_controller')) " )]
311
291
public function searchGridAction (
292
+ GridDefinitionFactoryProvider $ gridDefinitionFactoryCollection ,
312
293
Request $ request ,
313
- $ gridDefinitionFactoryServiceId ,
314
- $ redirectRoute ,
294
+ string $ gridDefinitionFactoryServiceId ,
295
+ string $ redirectRoute ,
315
296
array $ redirectQueryParamsToKeep = []
316
297
) {
317
- /** @var GridDefinitionFactoryInterface $definitionFactory */
318
- $ definitionFactory = $ this ->get ($ gridDefinitionFactoryServiceId );
298
+ $ definitionFactory = $ gridDefinitionFactoryCollection ->getFactory ($ gridDefinitionFactoryServiceId );
319
299
320
300
$ filterId = null ;
321
-
322
301
if ($ definitionFactory instanceof FilterableGridDefinitionFactoryInterface) {
323
302
$ filterId = $ definitionFactory ->getFilterId ();
324
303
} elseif ($ definitionFactory instanceof AbstractGridDefinitionFactory) {
@@ -332,21 +311,18 @@ public function searchGridAction(
332
311
}
333
312
334
313
if (null !== $ filterId ) {
335
- /** @var ResponseBuilder $responseBuilder */
336
- $ responseBuilder = $ this ->get ('prestashop.bundle.grid.response_builder ' );
337
-
338
- return $ responseBuilder ->buildSearchResponse (
314
+ return $ this ->buildSearchResponse (
339
315
$ definitionFactory ,
340
316
$ request ,
341
317
$ filterId ,
342
318
$ redirectRoute ,
343
- $ redirectQueryParamsToKeep
319
+ $ redirectQueryParamsToKeep,
344
320
);
345
321
}
346
322
347
323
// Legacy grid definition which use controller/action as filter keys (and no scope for parameters)
348
324
/** @var ControllerResponseBuilder $controllerResponseBuilder */
349
- $ controllerResponseBuilder = $ this ->get (' prestashop.bundle.grid.controller_response_builder ' );
325
+ $ controllerResponseBuilder = $ this ->container -> get (ControllerResponseBuilder::class );
350
326
351
327
return $ controllerResponseBuilder ->buildSearchResponse (
352
328
$ definitionFactory ,
@@ -362,24 +338,21 @@ public function searchGridAction(
362
338
* @return RedirectResponse
363
339
*/
364
340
#[AdminSecurity("is_granted('update', request.get('_legacy_controller')) " )]
365
- public function updatePositionAction (Request $ request ): RedirectResponse
366
- {
341
+ public function updatePositionAction (
342
+ Request $ request ,
343
+ PositionDefinitionProvider $ positionDefinitionProvider ,
344
+ ): RedirectResponse {
367
345
$ positionsData = [
368
346
'positions ' => $ request ->request ->all ('positions ' ),
369
347
];
370
348
371
- /** @var PositionDefinitionInterface $positionDefinition */
372
- $ positionDefinition = $ this ->get ($ request ->attributes ->get ('position_definition ' ));
373
- $ positionUpdateFactory = $ this ->get (PositionUpdateFactoryInterface::class);
374
-
349
+ $ positionDefinition = $ positionDefinitionProvider ->getPositionDefinition ($ request ->attributes ->get ('position_definition ' ));
375
350
try {
376
- $ positionUpdate = $ positionUpdateFactory ->buildPositionUpdate ($ positionsData , $ positionDefinition );
377
- $ updater = $ this ->get (GridPositionUpdaterInterface::class);
378
- $ updater ->update ($ positionUpdate );
379
- $ this ->addFlash ('success ' , $ this ->trans ('Successful update ' , 'Admin.Notifications.Success ' ));
351
+ $ this ->updateGridPosition ($ positionDefinition , $ positionsData );
352
+ $ this ->addFlash ('success ' , $ this ->trans ('Successful update ' , [], 'Admin.Notifications.Success ' ));
380
353
} catch (PositionUpdateException $ e ) {
381
354
$ errors = [$ e ->toArray ()];
382
- $ this ->flashErrors ($ errors );
355
+ $ this ->addFlashErrors ($ errors );
383
356
}
384
357
385
358
return $ this ->redirectToRoute ($ request ->attributes ->get ('redirect_route ' ));
0 commit comments