<?php declare(strict_types=1);
namespace zenit\PlatformDataBadges\Subscriber;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\ThemeCompilerEnrichScssVariablesEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ThemeVariablesSubscriber implements EventSubscriberInterface
{
/**
* @var string
*/
private $configPath = 'zenitPlatformDataBadges.config.';
/**
* @var SystemConfigService
*/
protected $systemConfig;
// add the `SystemConfigService` to your constructor
public function __construct(SystemConfigService $systemConfig)
{
$this->systemConfig = $systemConfig;
}
public static function getSubscribedEvents(): array
{
return [
ThemeCompilerEnrichScssVariablesEvent::class => 'onAddVariables'
];
}
public function onAddVariables(ThemeCompilerEnrichScssVariablesEvent $event)
{
$shopId = $event->getSalesChannelId();
/**
* @var string $shippingfreeBadgeColor
* @var string $variantBadgeColor
* @var string $stockBadgeColor
* @var string $closeoutBadgeColor
* @var string $manufacturerBadgeColor
* @var string $custom1BadgeColor
* @var string $custom2BadgeColor
* @var string $custom3BadgeColor
*/
$shippingfreeBadgeColor = $this->systemConfig->get($this->configPath . 'shippingfreeBadgeColor', $shopId);
$variantBadgeColor = $this->systemConfig->get($this->configPath . 'variantBadgeColor', $shopId);
$stockBadgeColor = $this->systemConfig->get($this->configPath . 'stockBadgeColor', $shopId);
$closeoutBadgeColor = $this->systemConfig->get($this->configPath . 'closeoutBadgeColor', $shopId);
$manufacturerBadgeColor = $this->systemConfig->get($this->configPath . 'manufacturerBadgeColor', $shopId);
$custom1BadgeColor = $this->systemConfig->get($this->configPath . 'custom1BadgeColor', $shopId);
$custom2BadgeColor = $this->systemConfig->get($this->configPath . 'custom2BadgeColor', $shopId);
$custom3BadgeColor = $this->systemConfig->get($this->configPath . 'custom3BadgeColor', $shopId);
if (isset($shippingfreeBadgeColor) && !empty($shippingfreeBadgeColor)) {
$event->addVariable('databadges-shipping-free-color', $shippingfreeBadgeColor);
} else {
$event->addVariable('databadges-shipping-free-color', '#333333');
};
if (isset($variantBadgeColor) && !empty($variantBadgeColor)) {
$event->addVariable('databadges-variant-color', $variantBadgeColor);
} else {
$event->addVariable('databadges-variant-color', '#333333');
};
if (isset($stockBadgeColor) && !empty($stockBadgeColor)) {
$event->addVariable('databadges-stock-color', $stockBadgeColor);
} else {
$event->addVariable('databadges-stock-color', '#333333');
};
if (isset($closeoutBadgeColor) && !empty($closeoutBadgeColor)) {
$event->addVariable('databadges-close-out-color', $closeoutBadgeColor);
} else {
$event->addVariable('databadges-close-out-color', '#333333');
};
if (isset($manufacturerBadgeColor) && !empty($manufacturerBadgeColor)) {
$event->addVariable('databadges-manufacturer-color', $manufacturerBadgeColor);
} else {
$event->addVariable('databadges-manufacturer-color', '#333333');
};
if (isset($custom1BadgeColor) && !empty($custom1BadgeColor)) {
$event->addVariable('databadges-custom-first-color', $custom1BadgeColor);
} else {
$event->addVariable('databadges-custom-first-color', '#333333');
};
if (isset($custom2BadgeColor) && !empty($custom2BadgeColor)) {
$event->addVariable('databadges-custom-second-color', $custom2BadgeColor);
} else {
$event->addVariable('databadges-custom-second-color', '#333333');
};
if (isset($custom3BadgeColor) && !empty($custom3BadgeColor)) {
$event->addVariable('databadges-custom-third-color', $custom3BadgeColor);
} else {
$event->addVariable('databadges-custom-third-color', '#333333');
};
/**
* @var string $listingsFontSize
* @var string $listingsRadiusLeft
* @var string $listingsRadiusRight
* @var string $listingsPadding
* @var string $listingsMarginLeft
*/
$listingsFontSize = $this->systemConfig->get($this->configPath . 'listingsFontSize', $shopId);
$listingsRadiusLeft = $this->systemConfig->get($this->configPath . 'listingsRadiusLeft', $shopId) ?? '0';
$listingsRadiusRight = $this->systemConfig->get($this->configPath . 'listingsRadiusRight', $shopId) ?? '0';
$listingsPadding = $this->systemConfig->get($this->configPath . 'listingsPadding', $shopId) ?? '0';
$listingsPaddingVertical = ($listingsPadding >= 5) ? (int)$listingsPadding - 5 : '0';
$listingsMarginLeft = ($listingsRadiusLeft > 0) ? '10' : '0';
$event->addVariable('databadges-listings-font-size', $listingsFontSize . 'px');
$event->addVariable('databadges-listings-height', (int)$listingsFontSize + 5 . 'px');
$event->addVariable('databadges-listings-radius-left', $listingsRadiusLeft . 'px');
$event->addVariable('databadges-listings-radius-right', $listingsRadiusRight . 'px');
$event->addVariable('databadges-listings-padding-horizontal', $listingsPadding . 'px');
$event->addVariable('databadges-listings-padding-vertical', $listingsPaddingVertical. 'px');
$event->addVariable('databadges-listings-margin-left', $listingsMarginLeft . 'px');
/**
* @var string $detailsFontSize
* @var string $detailsRadiusLeft
* @var string $detailsRadiusRight
* @var string $detailsPadding
* @var string $detailsMarginLeft
*/
$detailsFontSize = $this->systemConfig->get($this->configPath . 'detailsFontSize', $shopId);
$detailsRadiusLeft = $this->systemConfig->get($this->configPath . 'detailsRadiusLeft', $shopId) ?? '0';
$detailsRadiusRight = $this->systemConfig->get($this->configPath . 'detailsRadiusRight', $shopId) ?? '0';
$detailsPadding = $this->systemConfig->get($this->configPath . 'detailsPadding', $shopId) ?? '0';
$detailsPaddingVertical = ($detailsPadding >= 5) ? (int)$detailsPadding - 5 : '0';
$detailsMarginLeft = ($detailsRadiusLeft > 0) ? '10' : '0';
$event->addVariable('databadges-details-font-size', $detailsFontSize . 'px');
$event->addVariable('databadges-details-height', (int)$detailsFontSize + 5 . 'px');
$event->addVariable('databadges-details-radius-left', $detailsRadiusLeft . 'px');
$event->addVariable('databadges-details-radius-right', $detailsRadiusRight . 'px');
$event->addVariable('databadges-details-padding-horizontal', $detailsPadding . 'px');
$event->addVariable('databadges-details-padding-vertical', $detailsPaddingVertical . 'px');
$event->addVariable('databadges-details-margin-left', $detailsMarginLeft . 'px');
}
}