vendor/store.shopware.com/acrisfiltercs/src/Storefront/Subscriber/PropertyGroupSubscriber.php line 84

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\Filter\Storefront\Subscriber;
  3. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionEntity;
  4. use Shopware\Core\Content\Property\PropertyEvents;
  5. use Shopware\Core\Content\Property\PropertyGroupEntity;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  13. use Shopware\Core\Framework\Struct\ArrayEntity;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class PropertyGroupSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var EntityRepositoryInterface
  19.      */
  20.     private $propertyRepository;
  21.     /**
  22.      * @var EntityRepositoryInterface
  23.      */
  24.     private $optionRepository;
  25.     /**
  26.      * @var EntityRepositoryInterface
  27.      */
  28.     private $languageRepository;
  29.     public function __construct(
  30.         EntityRepositoryInterface $propertyRepository,
  31.         EntityRepositoryInterface $optionRepository,
  32.         EntityRepositoryInterface $languageRepository
  33.     )
  34.     {
  35.         $this->propertyRepository $propertyRepository;
  36.         $this->optionRepository $optionRepository;
  37.         $this->languageRepository $languageRepository;
  38.     }
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             PropertyEvents::PROPERTY_GROUP_WRITTEN_EVENT => [
  43.                 ['updateByPropertyGroup'2000]
  44.             ],
  45.             PropertyEvents::PROPERTY_GROUP_OPTION_WRITTEN_EVENT => [
  46.                 ['updateByPropertyGroupOption'2000]
  47.             ]
  48.         ];
  49.     }
  50.     public function updateByPropertyGroup(EntityWrittenEvent $event): void
  51.     {
  52.         // don't update if only group was updated
  53.         if (is_array($event->getPayloads()) && array_key_exists(0$event->getPayloads()) && empty($event->getPayloads()[0])) {
  54.             return;
  55.         }
  56.         $context $event->getContext();
  57.         $contextExtensions $context->getExtensions();
  58.         if (!empty($contextExtensions) && array_key_exists('acrisFilter'$contextExtensions) && $contextExtensions['acrisFilter']->has('entityUpdated') && ($contextExtensions['acrisFilter']->get('entityUpdated') === true)) {
  59.             return;
  60.         }
  61.         $ids $event->getIds();
  62.         $criteria = new Criteria($ids);
  63.         $criteria->addAssociation('options');
  64.         /** @var PropertyGroupEntity $property */
  65.         $property $this->propertyRepository->search($criteria$context)->first();
  66.         if (!$property) return;
  67.         $updateData $this->updatePropertyGroupAndOption($propertynull$context);
  68.         if (!empty($updateData)) {
  69.             $context->addExtension('acrisFilter', new ArrayEntity(['entityUpdated' => true]));
  70.             $this->optionRepository->update($updateData$context);
  71.         }
  72.     }
  73.     public function updateByPropertyGroupOption(EntityWrittenEvent $event): void
  74.     {
  75.         $context $event->getContext();
  76.         $contextExtensions $context->getExtensions();
  77.         if (!empty($contextExtensions) && array_key_exists('acrisFilter'$contextExtensions) && $contextExtensions['acrisFilter']->has('entityUpdated') && ($contextExtensions['acrisFilter']->get('entityUpdated') === true)) {
  78.             return;
  79.         }
  80.         $ids $event->getIds();
  81.         $criteria = new Criteria($ids);
  82.         $criteria->addAssociation('group');
  83.         /** @var PropertyGroupOptionEntity $option */
  84.         $option $this->optionRepository->search($criteria$context)->first();
  85.         if (!$option) return;
  86.         $updateData $this->updatePropertyGroupAndOption($option->getGroup(), $option$context);
  87.         if (!empty($updateData)) {
  88.             $context->addExtension('acrisFilter', new ArrayEntity(['entityUpdated' => true]));
  89.             $this->optionRepository->update($updateData$context);
  90.         }
  91.     }
  92.     public function updatePropertyGroupAndOption(PropertyGroupEntity $propertyGroupEntity, ?PropertyGroupOptionEntity $propertyGroupOptionEntityContext $context): array
  93.     {
  94.         $customFields $propertyGroupEntity->getTranslation('customFields');
  95.         $update = [];
  96.         if (!empty($customFields) && array_key_exists('acris_filter_type'$customFields)) {
  97.             $filterType $customFields['acris_filter_type'];
  98.             if ($filterType === 'range_min_max' || $filterType === 'range_slider') {
  99.                 $otherLanguageIds $this->languageRepository->searchIds((new Criteria())->addFilter(new NotFilter(MultiFilter::CONNECTION_AND, [new EqualsFilter('id'$context->getLanguageId())])), $context)->getIds();
  100.                 if ($propertyGroupOptionEntity) {
  101.                     $update $this->addToUpdateArray($update$propertyGroupOptionEntity->getId(), $propertyGroupOptionEntity->getTranslation('customFields'), $propertyGroupOptionEntity->getTranslation('name'), $otherLanguageIds);
  102.                 } else {
  103.                     foreach ($propertyGroupEntity->getOptions() as $propertyGroupOptionEntity) {
  104.                         $update $this->addToUpdateArray($update$propertyGroupOptionEntity->getId(), $propertyGroupOptionEntity->getTranslation('customFields'), $propertyGroupOptionEntity->getTranslation('name'), $otherLanguageIds);
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.         return $update;
  110.     }
  111.     private function addToUpdateArray(array $updatestring $id, ?array $customFields, ?string $name, ?array $otherLanguageIds): array
  112.     {
  113.         $updateCustomFields = [];
  114.         $numericValue null;
  115.         if (str_contains($name",")) {
  116.             $val str_replace(",""."$name);
  117.             if (substr_count($val".") > 1) {
  118.                 $numericValue = (double)preg_replace('/\.(?=.*\.)/'''$val);
  119.             } else {
  120.                 $numericValue = (double)$val;
  121.             }
  122.         } elseif (substr_count($name".") > 1) {
  123.             $numericValue = (double)preg_replace('/\.(?=.*\.)/'''$name);
  124.         } else {
  125.             $numericValue = (double)$name;
  126.         }
  127.         if (empty($customFields) || (array_key_exists('acris_filter_numeric'$customFields) && floatval($customFields['acris_filter_numeric']) !== $numericValue)) {
  128.             $updateCustomFields['acris_filter_numeric'] = $numericValue;
  129.             $translations = [];
  130.             foreach ($otherLanguageIds as $otherLanguageId) {
  131.                 $translations[] = ['languageId' => $otherLanguageId'customFields' => $updateCustomFields];
  132.             }
  133.             if (!empty($translations)) {
  134.                 $update[] = ['id' => $id'customFields' => $updateCustomFields'translations' => $translations];
  135.             } else {
  136.                 $update[] = ['id' => $id'customFields' => $updateCustomFields];
  137.             }
  138.         }
  139.         return $update;
  140.     }
  141. }