<?php declare(strict_types=1);
namespace Acris\ProductVideo\Storefront\Subscriber;
use Acris\ProductVideo\Components\Service\ProductVideoService;
use Acris\ProductVideo\Custom\ProductVideoCollection;
use Acris\ProductVideo\Custom\ProductVideoEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductPageSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var ProductVideoService
*/
private $productVideoService;
public function __construct(
SystemConfigService $systemConfigService,
ProductVideoService $productVideoService
) {
$this->systemConfigService = $systemConfigService;
$this->productVideoService = $productVideoService;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageCriteriaEvent::class => 'onProductCriteriaLoaded',
ProductPageLoadedEvent::class => [
['onProductPageLoaded', 200]
]
];
}
public function onProductPageLoaded(ProductPageLoadedEvent $event): void
{
if($event->getPage()->getProduct()->hasExtension('acrisVideos') !== true) {
return;
}
/** @var ProductVideoCollection $productVideoCollection */
$productVideoCollection = $event->getPage()->getProduct()->getExtension('acrisVideos');
if ($productVideoCollection->count() === 0) return;
/** @var ProductVideoEntity $productVideo */
foreach ($productVideoCollection->getElements() as $productVideo)
{
if (!empty($productVideo) && !empty($productVideo->getTranslation('link')) && ($productVideo->getType() === ProductVideoService::DEFAULT_PRODUCT_VIDEO_YOUTUBE_TYPE || $productVideo->getType() === ProductVideoService::DEFAULT_PRODUCT_VIDEO_VIMEO_TYPE)) {
$this->productVideoService->loadProductVideo($productVideo, $event->getSalesChannelContext());
}
}
$this->productVideoService->assignPreviewImage($productVideoCollection, $event->getSalesChannelContext());
$this->productVideoService->assignProductVideoToMedia($event->getPage()->getProduct(), $productVideoCollection, $event->getSalesChannelContext());
}
public function onProductCriteriaLoaded(ProductPageCriteriaEvent $event): void
{
if(empty($event->getSalesChannelContext()->getSalesChannel())) {
return;
}
$sorting = $this->getVideoSorting($event->getSalesChannelContext()->getSalesChannel()->getId());
$event->getCriteria()->addAssociation('acrisVideos');
$event->getCriteria()->addAssociation('acrisVideos.media');
$event->getCriteria()->getAssociation('acrisVideos')->addSorting(new FieldSorting('priority', $sorting, true));
}
private function getVideoSorting(string $salesChannelId): string
{
if($this->systemConfigService->get('AcrisProductVideoCS.config.highestPriorityFirst', $salesChannelId) === true) {
return FieldSorting::DESCENDING;
} else {
return FieldSorting::ASCENDING;
}
}
}