<?php
/**
* To plugin Subscriber file.
*
* Copyright (C) BrandCrock GmbH. All rights reserved
*
* If you have found this script useful a small
* recommendation as well as a comment on our
* home page(https://brandcrock.com/)
* would be greatly appreciated.
*
* @author BrandCrock GmbH
* @package BrandCrockWhatsappChat
*/
declare(strict_types=1);
namespace Brandcrock\BrandCrockWhatsappChat\Subscriber;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent;
use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
class MySubscriber implements EventSubscriberInterface
{
private $systemConfigService;
private $userRespository;
/**
* @var string
*/
protected $shopVersion;
/**
* @param $systemConfigService SystemConfigService
*/
public function __construct(SystemConfigService $systemConfigService,EntityRepository $userRespository, string $shopVersion)
{
$this->systemConfigService = $systemConfigService;
$this->userRespository = $userRespository;
$this->shopVersion = $shopVersion;
}
public static function getSubscribedEvents(): array
{
return[
ProductPageLoadedEvent::class => 'onProductsLoaded',
NavigationPageLoadedEvent::class => 'onProductsLoadedDetail',
SearchPageLoadedEvent::class => 'onSearchPageLoadedEvent',
AccountProfilePageLoadedEvent::class => 'onAccountProfilePageLoadedEvent',
AccountPaymentMethodPageLoadedEvent::class => 'onAccountPaymentMethodPageLoadedEvent',
AccountOverviewPageLoadedEvent::class => 'onAccountOverviewPageLoadedEvent',
AccountOrderPageLoadedEvent::class => 'onAccountOrderPageLoadedEvent',
AccountLoginPageLoadedEvent::class => 'onAccountLoginPageLoadedEventt',
AddressDetailPageLoadedEvent::class => 'onAddressDetailPageLoadedEvent',
AddressListingPageLoadedEvent::class => 'onAddressListingPageLoadedEvent',
CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPageLoadedEvent',
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoadedEvent',
CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoadedEvent',
CheckoutRegisterPageLoadedEvent::class => 'onCheckoutRegisterPageLoadedEvent'
];
}
//on listing
public function onProductsLoaded(ProductPageLoadedEvent $event)
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
//on detail page
public function onProductsLoadedDetail(NavigationPageLoadedEvent $event): void
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
//on search result page
public function onSearchPageLoadedEvent(SearchPageLoadedEvent $event): void
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
//on account profile page
public function onAccountProfilePageLoadedEvent(AccountProfilePageLoadedEvent $event): void
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
//on payment method
public function onAccountPaymentMethodPageLoadedEvent(AccountPaymentMethodPageLoadedEvent $event): void
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
//on account overview page
public function onAccountOverviewPageLoadedEvent(AccountOverviewPageLoadedEvent $event): void
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
//on account order page
public function onAccountOrderPageLoadedEvent(AccountOrderPageLoadedEvent $event): void
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
//on account login page
public function onAccountLoginPageLoadedEventt(AccountLoginPageLoadedEvent $event): void
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
//on address detail page
public function onAddressDetailPageLoadedEvent(AddressDetailPageLoadedEvent $event): void
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
//on address listing page
public function onAddressListingPageLoadedEvent(AddressListingPageLoadedEvent $event): void
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
//on cart page
public function onCheckoutCartPageLoadedEvent(CheckoutCartPageLoadedEvent $event): void
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
//on checkout confirm page
public function onCheckoutConfirmPageLoadedEvent(CheckoutConfirmPageLoadedEvent $event): void
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
//on checkout finish page
public function onCheckoutFinishPageLoadedEvent(CheckoutFinishPageLoadedEvent $event): void
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
// on checkout register page
public function onCheckoutRegisterPageLoadedEvent(CheckoutRegisterPageLoadedEvent $event): void
{
$salechannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$systemConfig = $this->getAssignedConfig($salechannelId);
$event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
}
//get plugin configuration
private function getAssignedConfig($salechannelId)
{
$systemConfig = $this->systemConfigService->get('BrandCrockWhatsappChat.config', $salechannelId);
$imagepath = 'bundles/brandcrockwhatsappchat/storefront/assets/img/';
if ((array_key_exists('avatar', $systemConfig) && $systemConfig['avatar'])) {
$systemConfig['avatar'] = $imagepath.$systemConfig['avatar'];
}
if ((array_key_exists('salesAvatar', $systemConfig) && $systemConfig['salesAvatar'])) {
$systemConfig['salesAvatar'] = $imagepath.$systemConfig['salesAvatar'];
}
if(isset($systemConfig['openingHours']) && isset($systemConfig['closingsHours'])){
$systemConfig['from'] = $systemConfig['openingHours'] ;
$systemConfig['untill'] = $systemConfig['closingsHours'] ;
}
$context = Context::createDefaultContext();
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('admin', 1));
$data = $this->userRespository->search($criteria, $context)->first();
if($this->shopVersion < '6.4.5.0'){
$adminTimeZone = (array) $data->getCreatedat();
$systemConfig['timeZone'] = $adminTimeZone['timezone'];
}else{
$systemConfig['timeZone'] = $data->timeZone;
}
return $systemConfig;
}
}