<?php declare(strict_types=1);
namespace Verign\DvAccessibility\Subscriber\Storefront;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Contracts\Translation\TranslatorInterface;
class FrontendSubscriber implements EventSubscriberInterface
{
const DOMAIN = 'VerignDvAccessibilitySw6';
const CONFIG_ACCESS_NAME = 'VerignDvAccessibilitySw6.config.';
private SystemConfigService $configService;
private TranslatorInterface $translator;
private EntityRepositoryInterface $salesChannelRepository;
private string $settingType = 'default';
public function __construct(SystemConfigService $configService, TranslatorInterface $translator) {
$this->configService = $configService;
$this->translator = $translator;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
StorefrontRenderEvent::class => 'onStorefrontRender'
];
}
/**
* @param StorefrontRenderEvent $event
*/
public function onStorefrontRender(StorefrontRenderEvent $event)
{
$salesChannelId = $event->getContext()->getSource()->getSalesChannelId();
$defaultConfiguration = $this->configService->getDomain(self::DOMAIN);
$salesChannelConfiguration = $this->configService->getDomain(self::DOMAIN, $salesChannelId);
$configuration = $this->buildJson($defaultConfiguration, $salesChannelConfiguration);
$event->setParameter('dvAccessibilityConfiguration', $configuration);
}
public function buildJson($config, $salesChannelConfig): string {
$json = [];
$json['language'] = 'en';
$toolConfiguration = [
'default' => $config,
'specific' => $salesChannelConfig
];
$json = $this->addEnabledModules($json, $toolConfiguration);
$json = $this->addEnabledButtons($json, $toolConfiguration);
$json = $this->addModules($json, $toolConfiguration);
$json = $this->addStandardToolPosition($json, $toolConfiguration);
$json = $this->addTranslation($json);
$json = $this->addFilters($json, $toolConfiguration);
return json_encode($json);
}
public function setSettingType($config, $key): void {
if(isset($config['specific'][self::CONFIG_ACCESS_NAME . $key]) && $config['specific'][self::CONFIG_ACCESS_NAME . $key] != null) {
$this->settingType = 'specific';
} else {
$this->settingType = 'default';
}
}
public function addEnabledModules($json, $config): array {
$enabledModules = [
'keyboardNav',
'contrast',
'highlightLinks',
'biggerText',
'textSpacing',
'legibleFonts',
'bigCursor',
'readingGuide',
'tooltips',
'pauseAnimations'
];
foreach($enabledModules as $module) {
$this->setSettingType($config, $module);
if($config[$this->settingType][self::CONFIG_ACCESS_NAME . $module] === true) {
$config[$this->settingType][self::CONFIG_ACCESS_NAME . $module] = "1";
}
$json['enabledModules'][$module] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . $module];
}
return $json;
}
public function addEnabledButtons($json, $config): array {
$enabledButtons = [
'enablePageStructureButton',
'enablePositionButton',
'enableResetButton'
];
foreach($enabledButtons as $button) {
$this->setSettingType($config, $button);
if($config[$this->settingType][self::CONFIG_ACCESS_NAME . $button] === true) {
$config[$this->settingType][self::CONFIG_ACCESS_NAME . $button] = "1";
}
$json[$button] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . $button];
}
return $json;
}
public function addModules($json, $config): array {
$generalModules = [
'moduleKeyboardNav',
'moduleContrast',
'moduleHighlightLinks',
'moduleBiggerText',
'moduleTextSpacing',
'moduleBigCursor',
'moduleReadingGuide',
'toggleBackgroundColor',
];
$moduleContrast = [
'useInvertColors',
'useDarkContrast',
'useLightContrast',
'useDesaturate'
];
foreach($generalModules as $module) {
switch($module) {
case 'moduleKeyboardNav':
$this->setSettingType($config, $module);
$json[$module]['frameColor'] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . $module];
break;
case 'moduleContrast':
foreach($moduleContrast as $contrast) {
$this->setSettingType($config, $contrast);
if($config[$this->settingType][self::CONFIG_ACCESS_NAME . $contrast] === true) {
$config[$this->settingType][self::CONFIG_ACCESS_NAME . $contrast] = "1";
}
$json[$module][$contrast] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . $contrast];
}
break;
case 'moduleHighlightLinks':
$this->setSettingType($config, 'moduleHighlightLinksColor');
$json[$module]['color'] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . 'moduleHighlightLinksColor'];
$this->setSettingType($config, 'moduleHighlightLinksBackground');
$json[$module]['background'] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . 'moduleHighlightLinksBackground'];
$this->setSettingType($config, 'moduleHighlightLinksHighlightColor');
$json[$module]['highlightColor'] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . 'moduleHighlightLinksHighlightColor'];
break;
case 'moduleTextSpacing':
case 'moduleBiggerText':
$this->setSettingType($config, $module);
$json[$module]['factor'] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . $module];
break;
case 'moduleBigCursor':
$this->setSettingType($config, $module);
$json[$module]['imageURL'] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . $module];
break;
case 'moduleReadingGuide':
$this->setSettingType($config, 'moduleReadingGuideColor');
$json[$module]['color'] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . 'moduleReadingGuideColor'];
$this->setSettingType($config, 'moduleReadingGuideBackgroundColor');
$json[$module]['background'] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . 'moduleReadingGuideBackgroundColor'];
$this->setSettingType($config, 'moduleHighlightLinksHighlightColor');
$json[$module]['readingGuideColor'] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . 'moduleHighlightLinksHighlightColor'];
break;
case 'toggleBackgroundColor':
$this->setSettingType($config, $module);
$json[$module] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . $module];
break;
default:
$this->setSettingType($config, 'default');
$json[$module] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . $module];
break;
}
}
$json['moduleLegibleFonts'] = ['dvaccess-legible-fonts', 'dvaccess-legible-fonts-1'];
return $json;
}
public function addFilters($json, $config)
{
/*$filters = [
'filterHeight',
'filterDisplayBlock',
'filterLineHeight'
];*/
$this->setSettingType($config, 'filterHeight');
$json['filters']['height'] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . 'filterHeight'];
$this->setSettingType($config, 'filterDisplayBlock');
$json['filters']['displayBlock'] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . 'filterDisplayBlock'];
$this->setSettingType($config, 'filterLineHeight');
$json['filters']['lineHeight'] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . 'filterLineHeight'];
$this->setSettingType($config, 'filterWrapInput');
$json['filters']['wrapInput'] = $config[$this->settingType][self::CONFIG_ACCESS_NAME . 'filterWrapInput'];
return $json;
}
public function addStandardToolPosition($json, $config): array {
$this->setSettingType($config, 'standardToolPosition');
$setStandardPosition = $config[$this->settingType][self::CONFIG_ACCESS_NAME . 'standardToolPosition'];
$defaultPosition = 'LeftBottom';
if($defaultPosition == $setStandardPosition) {
$json['standardToolPosition'] = 6;
} else {
switch($setStandardPosition) {
case 'leftTop':
$json['standardToolPosition'] = 0;
break;
case 'leftMiddle':
$json['standardToolPosition'] = 7;
break;
case 'rightTop':
$json['standardToolPosition'] = 2;
break;
case 'rightMiddle':
$json['standardToolPosition'] = 3;
break;
case 'rightBottom':
$json['standardToolPosition'] = 4;
break;
case 'centerTop':
$json['standardToolPosition'] = 1;
break;
case 'centerBottom':
$json['standardToolPosition'] = 5;
break;
default:
$json['standardToolPosition'] = 6;
break;
}
}
return $json;
}
public function addTranslation($json): array {
$languages = ['en', 'de'];
$labelNames = [
'app.toggle.label',
'menu.title',
'menu.button.move',
'menu.button.page-structure',
'menu.button.reset',
'menu.header.headers',
'menu.header.landmarks',
'menu.header.links',
'menu.position.left-top',
'menu.position.left-center',
'menu.position.left-bottom',
'menu.position.right-top',
'menu.position.right-center',
'menu.position.right-bottom',
'menu.position.center-top',
'menu.position.center-bottom',
'module.button.bigger-text',
'module.button.bigger-text.0',
'module.button.bigger-text.1',
'module.button.bigger-text.2',
'module.button.bigger-text.3',
'module.button.cursor',
'module.button.cursor.0',
'module.button.contrast',
'module.button.contrast.0',
'module.button.contrast.1',
'module.button.contrast.2',
'module.button.contrast.3',
'module.button.highlight-links',
'module.button.keyboard-nav',
'module.button.legible-fonts',
'module.button.legible-fonts.0',
'module.button.legible-fonts.1',
'module.button.pause-animations',
'module.button.pause-animations.0',
'module.button.reading-guide',
'module.button.text-spacing',
'module.button.text-spacing.0',
'module.button.text-spacing.1',
'module.button.text-spacing.2',
'module.button.tooltips'
];
foreach($languages as $language) {
foreach($labelNames as $label) {
$translation = $this->translator->trans($label);
if($translation == $label) {
$translation = $this->translator->trans($label . '.origin');
}
$json['snippets'][$language][$label] = $translation;
}
}
return $json;
}
}