Issues (3641)

Communication/Controller/SlotController.php (2 issues)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace Spryker\Zed\ConfigurableBundleGui\Communication\Controller;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\ConfigurableBundleTemplateFilterTransfer;
12
use Generated\Shared\Transfer\ConfigurableBundleTemplateSlotEditFormTransfer;
13
use Generated\Shared\Transfer\ConfigurableBundleTemplateSlotFilterTransfer;
14
use Generated\Shared\Transfer\ConfigurableBundleTemplateSlotTransfer;
15
use Generated\Shared\Transfer\ConfigurableBundleTemplateTransfer;
16
use Spryker\Service\UtilText\Model\Url\Url;
17
use Spryker\Zed\Kernel\Communication\Controller\AbstractController;
18
use Symfony\Component\HttpFoundation\JsonResponse;
19
use Symfony\Component\HttpFoundation\RedirectResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
22
/**
23
 * @method \Spryker\Zed\ConfigurableBundleGui\Communication\ConfigurableBundleGuiCommunicationFactory getFactory()
24
 */
25
class SlotController extends AbstractController
26
{
27
    /**
28
     * @uses \Spryker\Zed\ProductListGui\Communication\Plugin\ConfigurableBundleGui\ProductConcreteRelationConfigurableBundleTemplateSlotEditSubTabsProviderPlugin::AVAILABLE_PRODUCT_CONCRETE_RELATION_TABS_NAME
29
     *
30
     * @var string
31
     */
32
    protected const AVAILABLE_PRODUCT_CONCRETE_RELATION_TABS_NAME = 'availableProductConcreteRelationTabs';
33
34
    /**
35
     * @uses \Spryker\Zed\ProductListGui\Communication\Plugin\ConfigurableBundleGui\ProductConcreteRelationConfigurableBundleTemplateSlotEditSubTabsProviderPlugin::ASSIGNED_PRODUCT_CONCRETE_RELATION_TABS_NAME
36
     *
37
     * @var string
38
     */
39
    protected const ASSIGNED_PRODUCT_CONCRETE_RELATION_TABS_NAME = 'assignedProductConcreteRelationTabs';
40
41
    /**
42
     * @uses \Spryker\Zed\ProductListGui\Communication\Plugin\ConfigurableBundleGui\ProductConcreteRelationConfigurableBundleTemplateSlotEditTablesProviderPlugin::AVAILABLE_PRODUCT_CONCRETE_TABLE_NAME
43
     *
44
     * @var string
45
     */
46
    protected const AVAILABLE_PRODUCT_CONCRETE_TABLE_NAME = 'availableProductConcreteTable';
47
48
    /**
49
     * @uses \Spryker\Zed\ProductListGui\Communication\Plugin\ConfigurableBundleGui\ProductConcreteRelationConfigurableBundleTemplateSlotEditTablesProviderPlugin::ASSIGNED_PRODUCT_CONCRETE_TABLE_NAME
50
     *
51
     * @var string
52
     */
53
    protected const ASSIGNED_PRODUCT_CONCRETE_TABLE_NAME = 'assignedProductConcreteTable';
54
55
    /**
56
     * @uses \Spryker\Zed\ProductListGui\Communication\Controller\ProductListAbstractController::URL_PARAM_ID_PRODUCT_LIST
57
     *
58
     * @var string
59
     */
60
    protected const URL_PARAM_ID_PRODUCT_LIST = 'id-product-list';
61
62
    /**
63
     * @uses \Spryker\Zed\ConfigurableBundleGui\Communication\Controller\SlotController::editAction()
64
     *
65
     * @var string
66
     */
67
    protected const ROUTE_EDIT_TEMPLATE_SLOT = '/configurable-bundle-gui/slot/edit';
68
69
    /**
70
     * @uses \Spryker\Zed\ConfigurableBundleGui\Communication\Controller\TemplateController::indexAction()
71
     *
72
     * @var string
73
     */
74
    protected const ROUTE_TEMPLATES_LIST = '/configurable-bundle-gui/template';
75
76
    /**
77
     * @uses \Spryker\Zed\ConfigurableBundleGui\Communication\Controller\TemplateController::editAction()
78
     *
79
     * @var string
80
     */
81
    protected const ROUTE_EDIT_TEMPLATE = '/configurable-bundle-gui/template/edit';
82
83
    /**
84
     * @var string
85
     */
86
    protected const ERROR_MESSAGE_SLOT_NOT_FOUND = 'Configurable bundle template slot with id "%id%" was not found.';
87
88
    /**
89
     * @var string
90
     */
91
    protected const ERROR_MESSAGE_SLOT_CREATE_FAIL = 'Configurable bundle template slot has not been created.';
92
93
    /**
94
     * @var string
95
     */
96
    protected const ERROR_MESSAGE_SLOT_UPDATE_FAIL = 'Configurable bundle template slot has not been updated.';
97
98
    /**
99
     * @var string
100
     */
101
    protected const ERROR_MESSAGE_SLOT_DELETE_FAIL = 'Configurable bundle template slot has not been deleted.';
102
103
    /**
104
     * @var string
105
     */
106
    protected const SUCCESS_MESSAGE_SLOT_CREATED = 'Configurable bundle template slot was successfully created.';
107
108
    /**
109
     * @var string
110
     */
111
    protected const SUCCESS_MESSAGE_SLOT_UPDATED = 'Configurable bundle template slot was successfully updated.';
112
113
    /**
114
     * @var string
115
     */
116
    protected const SUCCESS_MESSAGE_SLOT_DELETED = 'Configurable bundle template slot was successfully deleted.';
117
118
    /**
119
     * @var string
120
     */
121
    protected const SLOTS_TAB_ANCHOR = '#tab-content-slots';
122
123
    /**
124
     * @var string
125
     */
126
    protected const PARAM_ID_CONFIGURABLE_BUNDLE_TEMPLATE = 'id-configurable-bundle-template';
127
128
    /**
129
     * @var string
130
     */
131
    protected const PARAM_ID_CONFIGURABLE_BUNDLE_TEMPLATE_SLOT = 'id-configurable-bundle-template-slot';
132
133
    /**
134
     * @var string
135
     */
136
    protected const ERROR_MESSAGE_PARAM_ID = '%id%';
137
138
    /**
139
     * @param \Symfony\Component\HttpFoundation\Request $request
140
     *
141
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|array
142
     */
143
    public function createAction(Request $request)
144
    {
145
        $response = $this->executeCreateAction($request);
146
147
        if (!is_array($response)) {
148
            return $response;
149
        }
150
151
        return $this->viewResponse($response);
152
    }
153
154
    /**
155
     * @param \Symfony\Component\HttpFoundation\Request $request
156
     *
157
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|array
158
     */
159
    public function editAction(Request $request)
160
    {
161
        $response = $this->executeEditAction($request);
162
163
        if (!is_array($response)) {
164
            return $response;
165
        }
166
167
        return $this->viewResponse($response);
168
    }
169
170
    /**
171
     * @param \Symfony\Component\HttpFoundation\Request $request
172
     *
173
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
174
     */
175
    public function deleteAction(Request $request): RedirectResponse
176
    {
177
        $response = $this->executeDeleteAction($request);
178
179
        return $response;
180
    }
181
182
    /**
183
     * @return \Symfony\Component\HttpFoundation\JsonResponse
184
     */
185
    public function availableProductConcreteTableAction(): JsonResponse
186
    {
187
        $availableProductConcreteTable = $productConcreteRelationTables = $this->getFactory()
0 ignored issues
show
The assignment to $productConcreteRelationTables is dead and can be removed.
Loading history...
188
            ->createProductConcreteRelationTablesProvider()
189
            ->getTables()['availableProductConcreteTable'];
190
191
        return $this->jsonResponse(
192
            $availableProductConcreteTable->fetchData(),
193
        );
194
    }
195
196
    /**
197
     * @return \Symfony\Component\HttpFoundation\JsonResponse
198
     */
199
    public function assignedProductConcreteTableAction(): JsonResponse
200
    {
201
        $assignedProductConcreteTable = $productConcreteRelationTables = $this->getFactory()
0 ignored issues
show
The assignment to $productConcreteRelationTables is dead and can be removed.
Loading history...
202
            ->createProductConcreteRelationTablesProvider()
203
            ->getTables()['assignedProductConcreteTable'];
204
205
        return $this->jsonResponse(
206
            $assignedProductConcreteTable->fetchData(),
207
        );
208
    }
209
210
    /**
211
     * @param \Symfony\Component\HttpFoundation\Request $request
212
     *
213
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|array
214
     */
215
    protected function executeCreateAction(Request $request)
216
    {
217
        $idConfigurableBundleTemplate = $this->castId(
218
            $request->get(static::PARAM_ID_CONFIGURABLE_BUNDLE_TEMPLATE),
219
        );
220
221
        $formDataProvider = $this->getFactory()->createConfigurableBundleTemplateSlotCreateFormDataProvider();
222
223
        $form = $this->getFactory()
224
            ->getConfigurableBundleTemplateSlotCreateForm(
225
                $formDataProvider->getData($idConfigurableBundleTemplate),
226
                $formDataProvider->getOptions(),
227
            )->handleRequest($request);
228
229
        if ($form->isSubmitted() && $form->isValid()) {
230
            $configurableBundleTemplateSlotResponseTransfer = $this->getFactory()
231
                ->getConfigurableBundleFacade()
232
                ->createConfigurableBundleTemplateSlot($form->getData());
233
234
            if ($configurableBundleTemplateSlotResponseTransfer->getIsSuccessful()) {
235
                $idConfigurableBundleTemplateSlot = $configurableBundleTemplateSlotResponseTransfer
236
                    ->getConfigurableBundleTemplateSlot()
237
                    ->getIdConfigurableBundleTemplateSlot();
238
239
                $redirectUrl = Url::generate(static::ROUTE_EDIT_TEMPLATE_SLOT, [
240
                    static::PARAM_ID_CONFIGURABLE_BUNDLE_TEMPLATE_SLOT => $idConfigurableBundleTemplateSlot,
241
                ]);
242
243
                $this->addSuccessMessage(static::SUCCESS_MESSAGE_SLOT_CREATED);
244
245
                return $this->redirectResponse($redirectUrl);
246
            }
247
248
            $this->addErrorMessage(static::ERROR_MESSAGE_SLOT_CREATE_FAIL);
249
        }
250
251
        return [
252
            'tabs' => $this->getFactory()->createConfigurableBundleTemplateSlotCreateTabs()->createView(),
253
            'form' => $form->createView(),
254
            'currentLocale' => $this->getFactory()->getLocaleFacade()->getCurrentLocale(),
255
            'configurableBundleTemplate' => $this->findConfigurableBundleTemplateById($idConfigurableBundleTemplate),
256
        ];
257
    }
258
259
    /**
260
     * @param \Symfony\Component\HttpFoundation\Request $request
261
     *
262
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|array
263
     */
264
    protected function executeEditAction(Request $request)
265
    {
266
        $idConfigurableBundleTemplateSlot = $this->castId(
267
            $request->get(static::PARAM_ID_CONFIGURABLE_BUNDLE_TEMPLATE_SLOT),
268
        );
269
270
        $formDataProvider = $this->getFactory()->createConfigurableBundleTemplateSlotEditFormDataProvider();
271
        $configurableBundleTemplateSlotEditFormTransfer = $formDataProvider->getData($idConfigurableBundleTemplateSlot);
272
273
        if (!$configurableBundleTemplateSlotEditFormTransfer->getConfigurableBundleTemplateSlot()) {
274
            $this->addErrorMessage(static::ERROR_MESSAGE_SLOT_NOT_FOUND, [
275
                static::ERROR_MESSAGE_PARAM_ID => $idConfigurableBundleTemplateSlot,
276
            ]);
277
278
            return $this->redirectResponse(static::ROUTE_TEMPLATES_LIST);
279
        }
280
281
        $form = $this->getFactory()
282
            ->getConfigurableBundleTemplateSlotEditForm(
283
                $configurableBundleTemplateSlotEditFormTransfer,
284
                $formDataProvider->getOptions(),
285
            )->handleRequest($request);
286
287
        if ($form->isSubmitted() && $form->isValid()) {
288
            $configurableBundleTemplateSlotEditFormTransfer = $this->getFactory()
289
                ->createConfigurableBundleTemplateSlotEditFormFileUploadHandler()
290
                ->handleFileUploads($form, $form->getData());
291
292
            $configurableBundleTemplateSlotTransfer = $this->mapFormTransferToRegularTransfer($configurableBundleTemplateSlotEditFormTransfer);
293
294
            $configurableBundleTemplateSlotResponseTransfer = $this->getFactory()
295
                ->getConfigurableBundleFacade()
296
                ->updateConfigurableBundleTemplateSlot($configurableBundleTemplateSlotTransfer);
297
298
            if ($configurableBundleTemplateSlotResponseTransfer->getIsSuccessful()) {
299
                $redirectUrl = Url::generate(static::ROUTE_EDIT_TEMPLATE_SLOT, [
300
                    static::PARAM_ID_CONFIGURABLE_BUNDLE_TEMPLATE_SLOT => $idConfigurableBundleTemplateSlot,
301
                ]);
302
303
                $this->addSuccessMessage(static::SUCCESS_MESSAGE_SLOT_UPDATED);
304
305
                return $this->redirectResponse($redirectUrl);
306
            }
307
308
            $this->addErrorMessage(static::ERROR_MESSAGE_SLOT_UPDATE_FAIL);
309
        }
310
311
        $request->query->set(
312
            static::URL_PARAM_ID_PRODUCT_LIST,
313
            (string)$configurableBundleTemplateSlotEditFormTransfer->getConfigurableBundleTemplateSlot()->getProductList()->getIdProductList(),
314
        );
315
316
        $configurableBundleTemplateTransfer = $this->findConfigurableBundleTemplateById(
317
            $configurableBundleTemplateSlotEditFormTransfer->getConfigurableBundleTemplateSlot()->getFkConfigurableBundleTemplate(),
318
        );
319
320
        $viewData = [
321
            'tabs' => $this->getFactory()->createConfigurableBundleTemplateSlotEditTabs()->createView(),
322
            'form' => $form->createView(),
323
            'currentLocale' => $this->getFactory()->getLocaleFacade()->getCurrentLocale(),
324
            'configurableBundleTemplate' => $configurableBundleTemplateTransfer,
325
        ];
326
327
        $viewData = array_merge($viewData, $this->getProductListManagementTabsAndTables());
328
329
        return $viewData;
330
    }
331
332
    /**
333
     * @param \Symfony\Component\HttpFoundation\Request $request
334
     *
335
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
336
     */
337
    protected function executeDeleteAction(Request $request): RedirectResponse
338
    {
339
        $idConfigurableBundleTemplateSlot = $this->castId(
340
            $request->query->get(static::PARAM_ID_CONFIGURABLE_BUNDLE_TEMPLATE_SLOT),
341
        );
342
343
        $configurableBundleTemplateSlotFilterTransfer = $this->createConfigurableBundleTemplateSlotFilter($idConfigurableBundleTemplateSlot);
344
345
        $configurableBundleTemplateSlotTransfer = $this->getFactory()
346
            ->getConfigurableBundleFacade()
347
            ->getConfigurableBundleTemplateSlot($configurableBundleTemplateSlotFilterTransfer)
348
            ->getConfigurableBundleTemplateSlot();
349
350
        if (!$configurableBundleTemplateSlotTransfer) {
351
            $this->addErrorMessage(static::ERROR_MESSAGE_SLOT_NOT_FOUND, [
352
                static::ERROR_MESSAGE_PARAM_ID => $idConfigurableBundleTemplateSlot,
353
            ]);
354
355
            return $this->redirectResponse(static::ROUTE_TEMPLATES_LIST);
356
        }
357
358
        $redirectUrl = Url::generate(static::ROUTE_EDIT_TEMPLATE, [
359
                static::PARAM_ID_CONFIGURABLE_BUNDLE_TEMPLATE => $configurableBundleTemplateSlotTransfer->getFkConfigurableBundleTemplate(),
360
            ]) . static::SLOTS_TAB_ANCHOR;
361
362
        $form = $this->getFactory()->createDeleteConfigurableBundleSlotForm()->handleRequest($request);
363
364
        if (!$form->isSubmitted() || !$form->isValid()) {
365
            $this->addErrorMessage('CSRF token is not valid');
366
367
            return $this->redirectResponse($redirectUrl);
368
        }
369
370
        $configurableBundleTemplateSlotResponseTransfer = $this->getFactory()
371
            ->getConfigurableBundleFacade()
372
            ->deleteConfigurableBundleTemplateSlot($configurableBundleTemplateSlotFilterTransfer);
373
374
        if ($configurableBundleTemplateSlotResponseTransfer->getIsSuccessful()) {
375
            $this->addSuccessMessage(static::SUCCESS_MESSAGE_SLOT_DELETED);
376
377
            return $this->redirectResponse($redirectUrl);
378
        }
379
380
        $this->addErrorMessage(static::ERROR_MESSAGE_SLOT_DELETE_FAIL);
381
382
        return $this->redirectResponse($redirectUrl);
383
    }
384
385
    /**
386
     * @return array
387
     */
388
    protected function getProductListManagementTabsAndTables(): array
389
    {
390
        $productConcreteRelationSubTabs = $this->getFactory()->createProductConcreteRelationSubTabsProvider()->getSubTabs();
391
        $productConcreteRelationTables = $this->getFactory()->createProductConcreteRelationTablesProvider()->getTables();
392
393
        $keyAvailableProductConcreteRelationTabs = static::AVAILABLE_PRODUCT_CONCRETE_RELATION_TABS_NAME;
394
        $keyAssignedProductConcreteRelationTabs = static::ASSIGNED_PRODUCT_CONCRETE_RELATION_TABS_NAME;
395
396
        $keyAvailableProductConcreteRelationTable = static::AVAILABLE_PRODUCT_CONCRETE_TABLE_NAME;
397
        $keyAssignedProductConcreteRelationTable = static::ASSIGNED_PRODUCT_CONCRETE_TABLE_NAME;
398
399
        return [
400
            $keyAvailableProductConcreteRelationTabs => $productConcreteRelationSubTabs[$keyAvailableProductConcreteRelationTabs]->createView(),
401
            $keyAssignedProductConcreteRelationTabs => $productConcreteRelationSubTabs[$keyAssignedProductConcreteRelationTabs]->createView(),
402
            $keyAvailableProductConcreteRelationTable => $productConcreteRelationTables[$keyAvailableProductConcreteRelationTable]->render(),
403
            $keyAssignedProductConcreteRelationTable => $productConcreteRelationTables[$keyAssignedProductConcreteRelationTable]->render(),
404
        ];
405
    }
406
407
    /**
408
     * @param \Generated\Shared\Transfer\ConfigurableBundleTemplateSlotEditFormTransfer $configurableBundleTemplateSlotEditFormTransfer
409
     *
410
     * @return \Generated\Shared\Transfer\ConfigurableBundleTemplateSlotTransfer
411
     */
412
    protected function mapFormTransferToRegularTransfer(
413
        ConfigurableBundleTemplateSlotEditFormTransfer $configurableBundleTemplateSlotEditFormTransfer
414
    ): ConfigurableBundleTemplateSlotTransfer {
415
        $configurableBundleTemplateSlotTransfer = $configurableBundleTemplateSlotEditFormTransfer->getConfigurableBundleTemplateSlot();
416
        $productListAggregateFormTransfer = $configurableBundleTemplateSlotEditFormTransfer->getProductListAggregateForm();
417
418
        $productListTransfer = $configurableBundleTemplateSlotTransfer->getProductList();
419
420
        $productListTransfer
421
            ->setProductListCategoryRelation($productListAggregateFormTransfer->getProductListCategoryRelation())
422
            ->setProductListProductConcreteRelation($productListAggregateFormTransfer->getProductListProductConcreteRelation());
423
424
        $configurableBundleTemplateSlotTransfer->setProductList($productListTransfer);
425
426
        return $configurableBundleTemplateSlotTransfer;
427
    }
428
429
    /**
430
     * @param int $idConfigurableBundleTemplate
431
     *
432
     * @return \Generated\Shared\Transfer\ConfigurableBundleTemplateTransfer|null
433
     */
434
    protected function findConfigurableBundleTemplateById(int $idConfigurableBundleTemplate): ?ConfigurableBundleTemplateTransfer
435
    {
436
        $configurableBundleTemplateFilterTransfer = (new ConfigurableBundleTemplateFilterTransfer())
437
            ->setIdConfigurableBundleTemplate($idConfigurableBundleTemplate)
438
            ->setTranslationLocales(new ArrayObject([$this->getFactory()->getLocaleFacade()->getCurrentLocale()]));
439
440
        return $this->getFactory()
441
            ->getConfigurableBundleFacade()
442
            ->getConfigurableBundleTemplate($configurableBundleTemplateFilterTransfer)
443
            ->getConfigurableBundleTemplate();
444
    }
445
446
    /**
447
     * @param int $idConfigurableBundleTemplateSlot
448
     *
449
     * @return \Generated\Shared\Transfer\ConfigurableBundleTemplateSlotFilterTransfer
450
     */
451
    protected function createConfigurableBundleTemplateSlotFilter(int $idConfigurableBundleTemplateSlot): ConfigurableBundleTemplateSlotFilterTransfer
452
    {
453
        return (new ConfigurableBundleTemplateSlotFilterTransfer())
454
            ->setIdConfigurableBundleTemplateSlot($idConfigurableBundleTemplateSlot)
455
            ->setTranslationLocales(new ArrayObject([$this->getFactory()->getLocaleFacade()->getCurrentLocale()]));
456
    }
457
}
458