custom/plugins/EnnoTabsAdvanced/Subscriber/ProductSubscriber.php line 49

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace EnnoTabsAdvanced\Subscriber;
  3. use EnnoTabsAdvanced\Struct\EnnoTabsStruct;
  4. use Shopware\Core\Content\Product\ProductEntity;
  5. use Shopware\Core\Content\Product\ProductEvents;
  6. use Shopware\Core\Content\Product\SalesChannel\CrossSelling\AbstractProductCrossSellingRoute;
  7. use Shopware\Core\Framework\Struct\ArrayEntity;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. class ProductSubscriber implements EventSubscriberInterface
  19. {
  20.     private $systemConfigService;
  21.     private $ennoTabRepository;
  22.     private $mediaRepository;
  23.     private AbstractProductCrossSellingRoute $crossSellingRoute;
  24.     public function __construct(
  25.         SystemConfigService $systemConfigService,
  26.         EntityRepositoryInterface $ennoTabRepository,
  27.         EntityRepositoryInterface $mediaRepository,
  28.         AbstractProductCrossSellingRoute $crossSellingRoute
  29.     )
  30.     {
  31.         $this->ennoTabRepository $ennoTabRepository;
  32.         $this->systemConfigService $systemConfigService;
  33.         $this->mediaRepository $mediaRepository;
  34.         $this->crossSellingRoute $crossSellingRoute;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             ProductPageLoadedEvent::class => 'onProductsLoaded'
  40.         ];
  41.     }
  42.     public function onProductsLoaded(ProductPageLoadedEvent $event): void
  43.     {
  44.         $salesChannel $event->getSalesChannelContext()->getSalesChannel();
  45.         if( $salesChannel && $this->systemConfigService->get('EnnoTabsAdvanced.config.active'$salesChannel->getId() ) )
  46.         {
  47.             $hideEmpty $this->systemConfigService->get('EnnoTabsAdvanced.config.hideEmpty'$salesChannel->getId());
  48.             $customer $event->getSalesChannelContext()->getCustomer();
  49.             $customerGroup $event->getSalesChannelContext()->getCurrentCustomerGroup();
  50.             $product $event->getPage()->getProduct();
  51.             $productTranslatedFields $product->getTranslated();
  52.             $productCategories $product->getCategoryTree();
  53.             $criteria = (new Criteria())
  54.                 ->addAssociation('salesChannels')
  55.                 ->addAssociation('categoriesList')
  56.                 ->addAssociation('productsList')
  57.                 ->addAssociation('productCustomFields')
  58.                 ->addAssociation('productCustomFields.customField')
  59.                 ->addAssociation('productCustomFields.customField.customFieldSet')
  60.                 ->addFilter(new EqualsFilter('enno_tab.salesChannels.salesChannel.id'$salesChannel->getId()))
  61.                 ->addFilter(new EqualsFilter('enno_tab.active'1))
  62.                 ->addSorting(new FieldSorting('enno_tab.position'));
  63.             // customFieldPosition Wert ist in einem json Feld und kann daher nicht zur Sortierung genutzt werden
  64.             // $criteria->getAssociation('productCustomFields.customField')->addSorting(new FieldSorting('customField.config.customFieldPosition'));
  65.             $ennoTabs $this->ennoTabRepository->search($criteria$event->getContext())->getEntities();
  66.             $tabIterator $ennoTabs->getIterator();
  67.             foreach ($tabIterator as $ennoTabKey => $ennoTab)
  68.             {
  69.                 // if($this->isCustomergroupExcluded($article, $tab)) continue;
  70.                 if(
  71.                     $this->isProductExcluded($product$ennoTab)
  72.                     || $this->isCategoryExcluded($productCategories$ennoTab)
  73.                     // || $this->isCustomergroupExcluded($product, $ennoTab)
  74.                 )
  75.                 {
  76.                     $ennoTabs->remove($ennoTabKey);
  77.                     continue;
  78.                 }
  79.                 $tabContentLength 0;
  80.                 $productManufacturer $product->getManufacturer();
  81.                 $productManufacturerTranslated $productManufacturer $productManufacturer->getTranslated() : [];
  82.                 $tabProductCustomFields $ennoTab->getProductCustomFields();
  83.                 if($tabProductCustomFields->count() > 0)
  84.                 {
  85.                     $this->removeDeletedCustomFields($tabProductCustomFields);
  86.                     $tabProductCustomFields->sort(function($one,$two){
  87.                         $a $one->getCustomField()->getConfig()["customFieldPosition"];
  88.                         $b $two->getCustomField()->getConfig()["customFieldPosition"];
  89.                         if ($a == $b) {
  90.                             return 0;
  91.                         }
  92.                         return ($a $b) ? -1;
  93.                     });
  94.                     $productCustomFields =  $productTranslatedFields['customFields'];
  95.                     $locale $event->getRequest()->getLocale();
  96.                     $defaultLocale $event->getRequest()->getDefaultLocale();
  97.                     $tabProductCustomFieldsIterator $tabProductCustomFields->getIterator();
  98.                     foreach ($tabProductCustomFieldsIterator as $customFieldKey => $tabCustomField)
  99.                     {
  100.                         $customFieldName $tabCustomField->getCustomFieldName();
  101.                         $customFieldValue = isset($productCustomFields[$customFieldName]) ? $productCustomFields[$customFieldName] : '';
  102.                         if( $customFieldValue !== '' || ( $ennoTab->getCustomFieldsAsTable() && !$ennoTab->getCustomFieldsTableHideEmpty() ) )
  103.                         {
  104.                             $customFieldSetLabels $tabCustomField->getCustomField()->getCustomFieldSet()->getConfig()['label'];
  105.                             $customFieldSetLabel '';
  106.                             if ( array_key_exists($locale$customFieldSetLabels) )
  107.                             {
  108.                                 $customFieldSetLabel $customFieldSetLabels[$locale];
  109.                             }
  110.                             else if ( array_key_exists($defaultLocale$customFieldSetLabels) )
  111.                             {
  112.                                 $customFieldSetLabel $customFieldSetLabels[$defaultLocale];
  113.                             }
  114.                             $customFieldLabels $tabCustomField->getCustomField()->getConfig()['label'];
  115.                             $customFieldLabel '';
  116.                             if ( array_key_exists($locale$customFieldLabels) )
  117.                             {
  118.                                 $customFieldLabel $customFieldLabels[$locale];
  119.                             }
  120.                             else if ( array_key_exists($defaultLocale$customFieldLabels) )
  121.                             {
  122.                                 $customFieldLabel $customFieldLabels[$defaultLocale];
  123.                             }
  124.                             $customFieldType $tabCustomField->getCustomField()->getConfig()['customFieldType'];
  125.                             $customFieldTitle '';
  126.                             $customFieldAlt '';
  127.                             if($customFieldType === 'media' && $customFieldValue !== '')
  128.                             {
  129.                                 $media $this->mediaRepository->search(new Criteria([$customFieldValue]), $event->getContext())->get($customFieldValue);
  130.                                 if($media)
  131.                                 {
  132.                                     $customFieldValue $media->getUrl();
  133.                                     $customFieldType  $media->getMediaType() ? strtolower($media->getMediaType()->getName()) : $media->getFileExtension();
  134.                                     $customFieldTitle $media->getTitle() ? $media->getTitle() : $media->getFileName();
  135.                                     $customFieldAlt   $media->getAlt() ? $media->getAlt() : $customFieldTitle;
  136.                                 }
  137.                             }
  138.                             $tabCustomField->setCustomFieldInfo([
  139.                                 'value'    => $customFieldValue,
  140.                                 'label'    => $customFieldLabel,
  141.                                 'setLabel' => $customFieldSetLabel,
  142.                                 'type'     => $customFieldType,
  143.                                 'title'    => $customFieldTitle,
  144.                                 'alt'      => $customFieldAlt,
  145.                             ]);
  146.                             $tabContentLength += 1;
  147.                         }
  148.                         else
  149.                         {
  150.                             $tabProductCustomFields->remove($customFieldKey);
  151.                         }
  152.                     }
  153.                      $ennoTab->setProductCustomFields($tabProductCustomFields);
  154.                 }
  155.                 if($ennoTab->getTemplateFile())
  156.                 {
  157.                     $ennoTab->setTemplateFile($this->normalizePath('storefront/page/product-detail/tabs/' $ennoTab->getTemplateFile()));
  158.                     $tabContentLength += 1;
  159.                 }
  160.                 $tabContentLength += (is_string($ennoTab->getHeadline()) ? 0);
  161.                 $tabContentLength += (is_string($ennoTab->getContent()) ? 0);
  162.                 $tabContentLength += ($ennoTab->getShowProperties() && $product->getSortedProperties()) ? 0;
  163.                 $tabContentLength += ($ennoTab->getShowProperties() && $product->getSortedProperties()) ? 0;
  164.                 $tabContentLength += ($ennoTab->getShowDescription() && is_array($productTranslatedFields) && array_key_exists('description'$productTranslatedFields) && $productTranslatedFields['description']) ? 0;
  165.                 $tabContentLength += ($ennoTab->getShowManufacturer() && is_array($productManufacturerTranslated) && array_key_exists('description'$productManufacturerTranslated) && $productManufacturerTranslated['description']) ? 0;
  166.                 $tabContentLength += ($ennoTab->getShowManufacturerImage() && $productManufacturer && $productManufacturer->getMedia() ) ? 0;
  167.                 $tabContentLength += ($ennoTab->getShowReviews()) ? 0;
  168.                 // $tabContentLength += ($tab['cmsSupportID']) ? 1 : 0;
  169.                 // $tabContentLength += ($tab['showDownloads'] && $article['sDownloads']) ? 1 : 0;
  170.                 // $tabContentLength += ($tab['showLinks'] && $article['sLinks']) ? 1 : 0;
  171.                 if($hideEmpty && $tabContentLength <= 0)
  172.                 {
  173.                     $ennoTabs->remove($ennoTabKey);
  174.                 }
  175.                 if(!$ennoTab->getSorting())
  176.                 {
  177.                     $ennoTab->setSorting("h,c,d,p,f,m,r");
  178.                 }
  179.             }
  180.             $showCrossSelling $this->systemConfigService->get('EnnoTabsAdvanced.config.crossSelling'$salesChannel->getId());
  181.             if ( $showCrossSelling && $product->getCmsPage() )
  182.             {
  183.                 $crossSellings $this->crossSellingRoute->load($product->getId(), new Request(), $event->getSalesChannelContext(), new Criteria());
  184.                 $event->getPage()->setCrossSellings($crossSellings->getResult());
  185.             }
  186.             $event->getPage()->getProduct()->addExtension('ennoTabs',$ennoTabs );
  187.         }
  188.     }
  189.     private function isProductExcluded($product$ennoTab)
  190.     {
  191.         $isProductListed false;
  192.         if($ennoTab->getProductsList()->count() > 0)
  193.         {
  194.             $isProductListed $ennoTab->getProductsList()->has$product->getId() );
  195.         }
  196.         return $ennoTab->getProductsWhitelist() ? !$isProductListed:$isProductListed;
  197.     }
  198.     private function isCategoryExcluded($productCategories$ennoTab)
  199.     {
  200.         $isCategoryListed false;
  201.         if(is_array($productCategories) && !empty($productCategories))
  202.         {
  203.             $categoriesList $ennoTab->getCategoriesList();
  204.             $categoriesListIterator $categoriesList->getIterator();
  205.             foreach ($categoriesListIterator as $category)
  206.             {
  207.                 if(in_array($category->getId(), $productCategories) )
  208.                 {
  209.                     $isCategoryListed true;
  210.                     break;
  211.                 }
  212.             }
  213.         }
  214.         return $ennoTab->getCategoriesWhitelist() ? !$isCategoryListed:$isCategoryListed;
  215.     }
  216.     private function removeDeletedCustomFields($tabProductCustomFields)
  217.     {
  218.         $tabProductCustomFieldsIterator $tabProductCustomFields->getIterator();
  219.         foreach ($tabProductCustomFieldsIterator as $customFieldKey => $tabCustomField)
  220.         {
  221.             if(!$tabCustomField->getCustomField())
  222.             {
  223.                 $tabProductCustomFields->remove($customFieldKey);
  224.             }
  225.         }
  226.         return $tabProductCustomFields;
  227.     }
  228.     private function normalizePath($path) {
  229.         return array_reduce(
  230.             explode('/'$path),
  231.             function($a$b)
  232.             {
  233.                 if($a === 0)
  234.                     $a "/";
  235.                 if($b === "" || $b === ".")
  236.                     return $a;
  237.                 if($b === "..")
  238.                     return dirname($a);
  239.                 return preg_replace("/\/+/""/""$a/$b");
  240.             },
  241.             0
  242.         );
  243.     }
  244. }