custom/plugins/MojeBambinoExtensions/src/Subscriber/GeneralSubscriber.php line 75

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MojeBambino\Extensions\Subscriber;
  3. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  4. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  5. use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
  6. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
  7. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  8. use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
  9. use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
  10. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  11. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  12. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  14. use Shopware\Storefront\Page\Contact\ContactPageLoadedEvent;
  15. use Shopware\Storefront\Page\Maintenance\MaintenancePageLoadedEvent;
  16. use Shopware\Storefront\Page\Navigation\Error\ErrorPageLoadedEvent;
  17. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  18. use Shopware\Storefront\Page\Newsletter\Register\NewsletterRegisterPageLoadedEvent;
  19. use Shopware\Storefront\Page\Newsletter\Subscribe\NewsletterSubscribePageLoadedEvent;
  20. use Shopware\Storefront\Page\Page;
  21. use Shopware\Storefront\Page\PageLoadedEvent;
  22. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  23. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  26. use Doctrine\DBAL\Connection;
  27. //use MojeBambino\Extensions\Struct\Hreflangi;
  28. use Symfony\Component\HttpFoundation\RedirectResponse;
  29. use Symfony\Component\HttpKernel\KernelEvents;
  30. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  31. class GeneralSubscriber implements EventSubscriberInterface
  32. {
  33.     private Connection $connection;
  34.     public function __construct(Connection $connection)
  35.     {
  36.         $this->connection $connection;
  37.     }
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             KernelEvents::EXCEPTION => [['inactiveProduct'0],],
  42.             /*ProductPageLoadedEvent::class => 'onPageLoaded',
  43.             //
  44.             CheckoutCartPageLoadedEvent::class => 'onPageLoaded',
  45.             CheckoutConfirmPageLoadedEvent::class => 'onPageLoaded',
  46.             CheckoutRegisterPageLoadedEvent::class => 'onPageLoaded',
  47.             CheckoutFinishPageLoadedEvent::class => 'onPageLoaded',
  48.             //
  49.             AccountOverviewPageLoadedEvent::class => 'onPageLoaded',
  50.             AccountProfilePageLoadedEvent::class => 'onPageLoaded',
  51.             AccountLoginPageLoadedEvent::class => 'onPageLoaded',
  52.             AccountOrderPageLoadedEvent::class => 'onPageLoaded',
  53.             AccountPaymentMethodPageLoadedEvent::class => 'onPageLoaded',
  54.             AddressListingPageLoadedEvent::class => 'onPageLoaded',
  55.             //
  56.             SearchPageLoadedEvent::class => 'onPageLoaded',
  57.             //
  58.             NavigationPageLoadedEvent::class => 'onPageLoaded',
  59.             //
  60.             ErrorPageLoadedEvent::class => 'onPageLoaded',
  61.             MaintenancePageLoadedEvent::class => 'onPageLoaded',
  62.             ContactPageLoadedEvent::class => 'onPageLoaded',
  63.             //
  64.             NewsletterRegisterPageLoadedEvent::class => 'onPageLoaded',
  65.             NewsletterSubscribePageLoadedEvent::class => 'onPageLoaded',
  66.             GenericPageLoadedEvent::class => 'onPageLoaded',*/
  67.         ];
  68.     }
  69.     
  70.     public function inactiveProduct(ExceptionEvent $event)
  71.     {
  72.         //echo "<pre>"; print_r($event->getRequest()->attributes); echo "</pre>";
  73.         $attrs $event->getRequest()->attributes;
  74.         //echo "<pre>"; print_r($attrs->get('productId')); echo "</pre>";
  75.         if($attrs->get('productId')!=""){
  76.             $url $attrs->get('sw-storefront-url');
  77.             //echo $url;
  78.             $prd_id $attrs->get('productId');
  79.             $q $this->connection->createQueryBuilder();
  80.             $q->select(['active'])->from('product')->where('hex(id)= :prd_id')->setMaxResults(1);
  81.             $q->setParameter('prd_id'$prd_id);
  82.             $st $q->execute();
  83.             $d $st->fetchAll(\PDO::FETCH_ASSOC);
  84.             if(count($d)>0){
  85.                 foreach($d as $w){
  86.                     $active $w['active'];
  87.                 }
  88.                 if($active==0){
  89.                     //sprawdź, czy ma jakąś aktywna kategorię
  90.                     $qq $this->connection->createQueryBuilder();
  91.                     $qq->select(['su.seo_path_info'])->from('product_category''pc')->join('pc''category''c''pc.category_id=c.id')
  92.                     ->join('c''seo_url''su''c.id=su.foreign_key')->where('hex(pc.product_id)= :prd_id')->andWhere('c.active="1"')
  93.                     ->andWhere('su.is_canonical="1"')->andWhere('su.route_name="frontend.navigation.page"')->setMaxResults(1);
  94.                     $qq->setParameter('prd_id'$prd_id);
  95.                     $stt $qq->execute();
  96.                     $dd $stt->fetchAll(\PDO::FETCH_ASSOC);
  97.                     if(count($dd)>0){
  98.                         foreach($dd as $ww){
  99.                             $wycofany $ww['seo_path_info'];
  100.                         }
  101.                     }
  102.                     else{
  103.                         $wycofany "";
  104.                     }
  105.                     $redirectResponse = new RedirectResponse($url."/".$wycofany);
  106.                     $event->setResponse($redirectResponse);
  107.                 }
  108.             }
  109.             else{
  110.                 $redirectResponse = new RedirectResponse($url);
  111.                 $event->setResponse($redirectResponse); 
  112.             }
  113.         } 
  114.         return; 
  115.     }
  116.     /**
  117.      * Event für alle Seiten
  118.      *
  119.      * @param PageLoadedEvent $event
  120.      * @throws \Exception
  121.      */
  122.     public function onPageLoaded($event)
  123.     {
  124.         /** @var Page $page */
  125.         /*$page = $event->getPage();
  126.         
  127.         $domena_at = "https://schorngmbh.com/"; 
  128.         
  129.         $hreflangi = new Hreflangi();
  130.         $attrs = $event->getRequest()->attributes;
  131.         $url = $attrs->get('sw-storefront-url').'/'.$attrs->get('crehler-seo-original-url');
  132.         $hreflangi->setUrl($url);
  133.         //echo "<pre>"; print_r($attrs->get('_route')); echo "</pre>";
  134.         //echo "<pre>"; print_r($url); echo "</pre>";
  135.         //$salesChannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
  136.         //$containerIds = $this->datalayerService->getContainerIds($salesChannelId);
  137.         //if(!$containerIds) return;
  138.         
  139.         //print_r(get_class($event)); 
  140.         //echo "<pre>"; print_r($page); echo "</pre>";
  141.         //print_r("<br/>===========================<br/>");
  142.         switch (get_class($event)) {
  143.             case ProductPageLoadedEvent::class:
  144.                 $prd = $page->getProduct()->getProductNumber(); 
  145.                 $q = $this->connection->createQueryBuilder();
  146.                 $q->select(['*'])->from('seo_hreflangi')->where('kod= :prd')->andWhere('route_name="frontend.detail.page"')
  147.                 ->andWhere('sklep="at"')->setMaxResults(1);
  148.                 $q->setParameter('prd', $prd);
  149.                 $st = $q->execute();
  150.                 $d = $st->fetchAll(\PDO::FETCH_ASSOC);
  151.                 if(count($d)>0){
  152.                     foreach($d as $w){
  153.                         $at = $w['seo_path_info'];
  154.                     }
  155.                 }
  156.                 else{
  157.                     $at = "";
  158.                 }
  159.                 $hreflangi->setUrlAt($domena_at.$at);
  160.                 break;
  161.             case CheckoutCartPageLoadedEvent::class:
  162.             case CheckoutConfirmPageLoadedEvent::class:
  163.             case CheckoutRegisterPageLoadedEvent::class:
  164.             case CheckoutFinishPageLoadedEvent::class:
  165.             case AccountOverviewPageLoadedEvent::class:
  166.             case AccountProfilePageLoadedEvent::class:
  167.             case AccountLoginPageLoadedEvent::class:
  168.             case AccountEditOrderPageLoadedEvent::class:
  169.             case AccountOrderPageLoadedEvent::class:
  170.             case AddressListingPageLoadedEvent::class:
  171.             case AccountPaymentMethodPageLoadedEvent::class:
  172.             case SearchPageLoadedEvent::class:
  173.                 $hreflangi->setUrlAt($domena_at.'/'.$attrs->get('crehler-seo-original-url'));
  174.                 break;
  175.             case NavigationPageLoadedEvent::class:
  176.                 if($attrs->get('_route')=="frontend.home.page"){
  177.                     $at = "";
  178.                 }
  179.                 else{
  180.                     $IdKat = $page->getFooter()->getNavigation()->getActive()->getAutoIncrement();
  181.                     $q = $this->connection->createQueryBuilder();
  182.                     $q->select(['*'])->from('seo_hreflangi')->where('id= :IdKat')->andWhere('route_name="frontend.navigation.page"')
  183.                     ->andWhere('sklep="at"')->setMaxResults(1);
  184.                     $q->setParameter('IdKat', $IdKat);
  185.                     $st = $q->execute();
  186.                     $d = $st->fetchAll(\PDO::FETCH_ASSOC);
  187.                     if(count($d)>0){
  188.                         foreach($d as $w){
  189.                             $at = $w['seo_path_info'];
  190.                         }
  191.                     }
  192.                     else{
  193.                         $at = "";
  194.                     }
  195.                 }
  196.                 $hreflangi->setUrlAt($domena_at.$at);
  197.                 break;
  198.             case ErrorPageLoadedEvent::class:
  199.             case NewsletterRegisterPageLoadedEvent::class:
  200.             case NewsletterSubscribePageLoadedEvent::class:
  201.             case MaintenancePageLoadedEvent::class:
  202.             case ContactPageLoadedEvent::class:
  203.             case GenericPageLoadedEvent::class:
  204.             default:
  205.                 $route = $attrs->get('_route');
  206.                 $q = $this->connection->createQueryBuilder();
  207.                 $q->select(['*'])->from('seo_hreflangi_route')->where('route_name= :route')->andWhere('nazwa!=""')->andWhere('sklep="at"')
  208.                 ->setMaxResults(1);
  209.                 $q->setParameter('route', $route);
  210.                 $st = $q->execute();
  211.                 $d = $st->fetchAll(\PDO::FETCH_ASSOC);
  212.                 if(count($d)>0){
  213.                     foreach($d as $w){
  214.                         $at = $w['nazwa'];
  215.                     }
  216.                 }
  217.                 else{
  218.                     $at = $attrs->get('crehler-seo-original-url');
  219.                 }
  220.                 $hreflangi->setUrlAt($domena_at.$at);
  221.                 break;
  222.         } 
  223.         $page->addExtension('hreflangi', $hreflangi);
  224.         //echo "<pre>"; print_r($page->getExtension('hreflangi')); echo "</pre>";*/
  225.     }
  226. }