custom/plugins/SEMKNOX/src/Service/AddDataToPage.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace semknox\search\Service;
  3. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use semknox\search\Framework\SemknoxsearchHelper;
  6. use Shopware\Core\Framework\Struct\ArrayEntity;
  7. use semknox\search\api\SemknoxAjaxParamsCallbackEvent;
  8. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  9. class AddDataToPage implements EventSubscriberInterface
  10. {
  11.     /**
  12.      *
  13.      * @var SemknoxsearchHelper
  14.      */
  15.     private $helper;    
  16.     /**
  17.      * @var EventDispatcherInterface
  18.      */
  19.     private $eventDispatcher;
  20.     public function __construct(SemknoxsearchHelper $helperEventDispatcherInterface $eventDispatcher)
  21.     {
  22.             $this->helper $helper;
  23.             $this->eventDispatcher $eventDispatcher;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             HeaderPageletLoadedEvent::class => 'addConfigData'
  29.         ];
  30.     }
  31.     public function addConfigData(HeaderPageletLoadedEvent $event): void
  32.     {
  33.         $config = [
  34.             'active' => false ,
  35.             'activateAutosuggest' => false,
  36.             'projectId' => '' ,
  37.             'apiUrl' => '' ,
  38.             'dataPoints'=> [] ,
  39.             'currentStoreUrl' => '' ,
  40.             'addParams' => [] ,
  41.         ];
  42.         $context=$event->getSalesChannelContext();
  43.         $scID=$this->helper->getSalesChannelFromSCContext($context);
  44.         $langID $this->helper->getLanguageFromSCContext($context);
  45.         $domainID $this->helper->getDomainFromSCContext($context);
  46.         $domainUrl $this->helper->getDomainURLFromSCContext($context);
  47.         $configdb $this->helper->allowSalesChannel($scID$domainID);
  48.         if ( ($configdb) && (is_array($configdb)) && ($configdb['semknoxActivate'])  ) {
  49.             $config['active'] = $configdb['semknoxActivate'];
  50.             $config['projectId'] = $configdb['semknoxCustomerId'];
  51.             $config['apiUrl'] = $configdb['semknoxBaseUrl'];
  52.             $config['currentStoreUrl'] = $domainUrl;
  53.             if (isset($configdb['semknoxActivateAutosuggest'])) { $config['activateAutosuggest'] = $configdb['semknoxActivateAutosuggest']; }
  54.             $params=[];
  55.             if ($this->eventDispatcher != null) {
  56.                 $callBackEvent = new SemknoxAjaxParamsCallbackEvent($params'');
  57.                 $this->eventDispatcher->dispatch($callBackEvent$callBackEvent::NAME);
  58.                 if ( ($callBackEvent->isChanged()) && ($callBackEvent->checkParams() > 0) ) {
  59.                     $params $callBackEvent->getParams();
  60.                 }
  61.             }
  62.             $config['addParams']=$params;
  63.         }
  64.         $event->getPagelet()->addExtension('semknoxConfig', new ArrayEntity($config));
  65.     }
  66. }