1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
namespace WellCommerce\Bundle\AppBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
16
|
|
|
use WellCommerce\Bundle\AppBundle\Entity\LayoutBox; |
17
|
|
|
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent; |
18
|
|
|
use WellCommerce\Bundle\CoreBundle\EventListener\AbstractEventSubscriber; |
19
|
|
|
use WellCommerce\Component\Form\Event\FormEvent; |
20
|
|
|
use WellCommerce\Component\Layout\Configurator\LayoutBoxConfiguratorInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class LayoutBoxSubscriber |
24
|
|
|
* |
25
|
|
|
* @author Adam Piotrowski <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class LayoutBoxSubscriber extends AbstractEventSubscriber |
28
|
|
|
{ |
29
|
|
|
public static function getSubscribedEvents() |
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
|
|
'admin.layout_box.pre_form_init' => 'onLayoutBoxFormInit', |
33
|
|
|
'layout_box.pre_update' => 'onLayoutBoxPreUpdate', |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Adds configurator fields to main layout box edit form. |
39
|
|
|
* Loops through all configurators, renders the fieldset and sets default data |
40
|
|
|
* |
41
|
|
|
* @param FormEvent $event |
42
|
|
|
*/ |
43
|
|
|
public function onLayoutBoxFormInit(FormEvent $event) |
44
|
|
|
{ |
45
|
|
|
$builder = $event->getFormBuilder(); |
46
|
|
|
$form = $event->getForm(); |
47
|
|
|
$configurators = $this->container->get('layout_box.configurator.collection')->all(); |
48
|
|
|
$resource = $event->getResource(); |
49
|
|
|
$boxSettings = $resource->getSettings(); |
50
|
|
|
|
51
|
|
|
foreach ($configurators as $configurator) { |
52
|
|
|
if ($configurator instanceof LayoutBoxConfiguratorInterface) { |
53
|
|
|
$defaults = []; |
54
|
|
|
if ($resource->getBoxType() == $configurator->getType()) { |
55
|
|
|
$defaults = $boxSettings; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$configurator->addFormFields($builder, $form, $defaults); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Sets resource settings fetched from fieldset corresponding to selected box type |
65
|
|
|
* |
66
|
|
|
* @param EntityEvent $event |
67
|
|
|
*/ |
68
|
|
|
public function onLayoutBoxPreUpdate(EntityEvent $event) |
69
|
|
|
{ |
70
|
|
|
$resource = $event->getEntity(); |
71
|
|
|
if ($resource instanceof LayoutBox) { |
72
|
|
|
$request = $this->getRequestHelper()->getCurrentRequest(); |
73
|
|
|
$settings = $this->getBoxSettingsFromRequest($request); |
|
|
|
|
74
|
|
|
$settings = $this->mergeUnmodifiedSettings($resource->getSettings(), $settings); |
75
|
|
|
$resource->setSettings($settings); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function getBoxSettingsFromRequest(Request $request) |
80
|
|
|
{ |
81
|
|
|
$settings = []; |
82
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
83
|
|
|
$parameters = $request->request->all(); |
84
|
|
|
$boxType = $accessor->getValue($parameters, '[required_data][boxType]'); |
85
|
|
|
if ($accessor->isReadable($parameters, '[' . $boxType . ']')) { |
86
|
|
|
$settings = $accessor->getValue($parameters, '[' . $boxType . ']'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return !is_array($settings) ? [] : $settings; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function mergeUnmodifiedSettings(array $oldSettings, array $newSettings): array |
93
|
|
|
{ |
94
|
|
|
foreach ($newSettings as $key => &$setting) { |
95
|
|
|
if (is_array($setting) && array_key_exists('unmodified', $setting) && isset($setting[0])) { |
96
|
|
|
$setting[0] = (0 !== (int)$setting[0]) ? $setting[0] : $oldSettings[$key][0]; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $newSettings; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: