src/WellCommerce/Bundle/AppBundle/EventListener/LayoutBoxSubscriber.php line 72

Open in your IDE?
  1. <?php
  2. declare(strict_types=0);
  3. /*
  4.  * WellCommerce Foundation
  5.  *
  6.  * This file is part of the WellCommerce package.
  7.  *
  8.  * (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
  9.  *
  10.  * For the full copyright and license information,
  11.  * please view the LICENSE file that was distributed with this source code.
  12.  */
  13. namespace WellCommerce\Bundle\AppBundle\EventListener;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\PropertyAccess\PropertyAccess;
  17. use WellCommerce\Bundle\AppBundle\Entity\LayoutBox;
  18. use WellCommerce\Bundle\AppBundle\Service\Shop\Storage\ShopStorage;
  19. use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
  20. use WellCommerce\Bundle\CoreBundle\Helper\Request\RequestHelper;
  21. use WellCommerce\Component\Form\Event\FormEvent;
  22. use WellCommerce\Component\Layout\Configurator\LayoutBoxConfiguratorCollection;
  23. use WellCommerce\Component\Layout\Configurator\LayoutBoxConfiguratorInterface;
  24. /**
  25.  * Class LayoutBoxSubscriber
  26.  *
  27.  * @author  Adam Piotrowski <adam@wellcommerce.org>
  28.  */
  29. class LayoutBoxSubscriber implements EventSubscriberInterface
  30. {
  31.     /**
  32.      * @var ShopStorage
  33.      */
  34.     protected $shopStorage;
  35.     /**
  36.      * @var RequestHelper
  37.      */
  38.     protected $requestHelper;
  39.     /**
  40.      * @var LayoutBoxConfiguratorCollection
  41.      */
  42.     protected $configuratorCollection;
  43.     public function __construct(ShopStorage $shopStorageRequestHelper $requestHelperLayoutBoxConfiguratorCollection $collection)
  44.     {
  45.         $this->shopStorage            $shopStorage;
  46.         $this->requestHelper          $requestHelper;
  47.         $this->configuratorCollection $collection;
  48.     }
  49.     public static function getSubscribedEvents()
  50.     {
  51.         return [
  52.             'admin.layout_box.pre_form_init' => 'onLayoutBoxFormInit',
  53.             'layout_box.pre_create'          => 'onLayoutBoxPreCreate',
  54.             'layout_box.pre_update'          => 'onLayoutBoxPreUpdate',
  55.             'layout_box.post_init'           => 'onLayoutBoxPostInit',
  56.         ];
  57.     }
  58.     /**
  59.      * Adds configurator fields to main layout box edit form.
  60.      * Loops through all configurators, renders the fieldset and sets default data
  61.      *
  62.      * @param FormEvent $event
  63.      */
  64.     public function onLayoutBoxFormInit(FormEvent $event)
  65.     {
  66.         $builder       $event->getFormBuilder();
  67.         $form          $event->getForm();
  68.         $configurators $this->configuratorCollection->all();
  69.         $resource      $event->getResource();
  70.         $settings      $resource->getSettings();
  71.         foreach ($configurators as $configurator) {
  72.             if ($configurator instanceof LayoutBoxConfiguratorInterface) {
  73.                 $defaults = [];
  74.                 if ($resource->getBoxType() == $configurator->getType()) {
  75.                     $defaults $settings;
  76.                 }
  77.                 $configurator->addFormFields($builder$form$defaults);
  78.             }
  79.         }
  80.     }
  81.     public function onLayoutBoxPreCreate(EntityEvent $event)
  82.     {
  83.         $resource $event->getEntity();
  84.         if ($resource instanceof LayoutBox) {
  85.             $request  $this->requestHelper->getCurrentRequest();
  86.             $settings $this->getBoxSettingsFromRequest($request);
  87.             $resource->setSettings($settings);
  88.             $this->syncExtraTranslations($resource);
  89.         }
  90.     }
  91.     public function onLayoutBoxPreUpdate(EntityEvent $event)
  92.     {
  93.         $resource $event->getEntity();
  94.         if ($resource instanceof LayoutBox) {
  95.             $request  $this->requestHelper->getCurrentRequest();
  96.             $settings $this->getBoxSettingsFromRequest($request);
  97.             $settings $this->mergeUnmodifiedSettings($resource->getSettings(), $settings);
  98.             $resource->setSettings($settings);
  99.             $this->syncExtraTranslations($resource);
  100.         }
  101.     }
  102.     protected function syncExtraTranslations(LayoutBox $layoutBox)
  103.     {
  104.         $translations $layoutBox->getSettings()['translations'] ?? [];
  105.         foreach ($translations as $locale => $extra) {
  106.             $layoutBox->translate($locale)->setExtra1($extra['extra1'] ?? '');
  107.             $layoutBox->translate($locale)->setExtra2($extra['extra2'] ?? '');
  108.             $layoutBox->translate($locale)->setExtra3($extra['extra3'] ?? '');
  109.             $layoutBox->translate($locale)->setExtra4($extra['extra4'] ?? '');
  110.             $layoutBox->translate($locale)->setExtra5($extra['extra5'] ?? '');
  111.             $layoutBox->translate($locale)->setExtra6($extra['extra6'] ?? '');
  112.             $layoutBox->translate($locale)->setExtra7($extra['extra7'] ?? '');
  113.             $layoutBox->translate($locale)->setExtra8($extra['extra8'] ?? '');
  114.             $layoutBox->translate($locale)->setExtra9($extra['extra9'] ?? '');
  115.             $layoutBox->translate($locale)->setExtra10($extra['extra10'] ?? '');
  116.         }
  117.         $layoutBox->mergeNewTranslations();
  118.     }
  119.     public function onLayoutBoxPostInit(EntityEvent $event)
  120.     {
  121.         $resource $event->getEntity();
  122.         if ($resource instanceof LayoutBox) {
  123.             $resource->setShop($this->shopStorage->getCurrentShop());
  124.         }
  125.     }
  126.     private function getBoxSettingsFromRequest(Request $request)
  127.     {
  128.         $settings   = [];
  129.         $accessor   PropertyAccess::createPropertyAccessor();
  130.         $parameters $request->request->all();
  131.         $boxType    $accessor->getValue($parameters'[required_data][boxType]');
  132.         if ($accessor->isReadable($parameters'[' $boxType ']')) {
  133.             $settings $accessor->getValue($parameters'[' $boxType ']');
  134.         }
  135.         return !is_array($settings) ? [] : $settings;
  136.     }
  137.     private function mergeUnmodifiedSettings(array $oldSettings, array $newSettings): array
  138.     {
  139.         foreach ($newSettings as $key => &$setting) {
  140.             if (is_array($setting) && array_key_exists('unmodified'$setting) && isset($setting[0])) {
  141.                 $setting[0] = (!== (int) $setting[0]) ? $setting[0] : $oldSettings[$key][0];
  142.             }
  143.         }
  144.         return $newSettings;
  145.     }
  146. }