<?php declare(strict_types=1);
namespace MojeBambino\Extensions\Subscriber;
use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
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\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\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Shopware\Storefront\Page\Contact\ContactPageLoadedEvent;
use Shopware\Storefront\Page\Maintenance\MaintenancePageLoadedEvent;
use Shopware\Storefront\Page\Navigation\Error\ErrorPageLoadedEvent;
use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use Shopware\Storefront\Page\Newsletter\Register\NewsletterRegisterPageLoadedEvent;
use Shopware\Storefront\Page\Newsletter\Subscribe\NewsletterSubscribePageLoadedEvent;
use Shopware\Storefront\Page\Page;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Doctrine\DBAL\Connection;
//use MojeBambino\Extensions\Struct\Hreflangi;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
class GeneralSubscriber implements EventSubscriberInterface
{
private Connection $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::EXCEPTION => [['inactiveProduct', 0],],
/*ProductPageLoadedEvent::class => 'onPageLoaded',
//
CheckoutCartPageLoadedEvent::class => 'onPageLoaded',
CheckoutConfirmPageLoadedEvent::class => 'onPageLoaded',
CheckoutRegisterPageLoadedEvent::class => 'onPageLoaded',
CheckoutFinishPageLoadedEvent::class => 'onPageLoaded',
//
AccountOverviewPageLoadedEvent::class => 'onPageLoaded',
AccountProfilePageLoadedEvent::class => 'onPageLoaded',
AccountLoginPageLoadedEvent::class => 'onPageLoaded',
AccountOrderPageLoadedEvent::class => 'onPageLoaded',
AccountPaymentMethodPageLoadedEvent::class => 'onPageLoaded',
AddressListingPageLoadedEvent::class => 'onPageLoaded',
//
SearchPageLoadedEvent::class => 'onPageLoaded',
//
NavigationPageLoadedEvent::class => 'onPageLoaded',
//
ErrorPageLoadedEvent::class => 'onPageLoaded',
MaintenancePageLoadedEvent::class => 'onPageLoaded',
ContactPageLoadedEvent::class => 'onPageLoaded',
//
NewsletterRegisterPageLoadedEvent::class => 'onPageLoaded',
NewsletterSubscribePageLoadedEvent::class => 'onPageLoaded',
GenericPageLoadedEvent::class => 'onPageLoaded',*/
];
}
public function inactiveProduct(ExceptionEvent $event)
{
//echo "<pre>"; print_r($event->getRequest()->attributes); echo "</pre>";
$attrs = $event->getRequest()->attributes;
//echo "<pre>"; print_r($attrs->get('productId')); echo "</pre>";
if($attrs->get('productId')!=""){
$url = $attrs->get('sw-storefront-url');
//echo $url;
$prd_id = $attrs->get('productId');
$q = $this->connection->createQueryBuilder();
$q->select(['active'])->from('product')->where('hex(id)= :prd_id')->setMaxResults(1);
$q->setParameter('prd_id', $prd_id);
$st = $q->execute();
$d = $st->fetchAll(\PDO::FETCH_ASSOC);
if(count($d)>0){
foreach($d as $w){
$active = $w['active'];
}
if($active==0){
//sprawdź, czy ma jakąś aktywna kategorię
$qq = $this->connection->createQueryBuilder();
$qq->select(['su.seo_path_info'])->from('product_category', 'pc')->join('pc', 'category', 'c', 'pc.category_id=c.id')
->join('c', 'seo_url', 'su', 'c.id=su.foreign_key')->where('hex(pc.product_id)= :prd_id')->andWhere('c.active="1"')
->andWhere('su.is_canonical="1"')->andWhere('su.route_name="frontend.navigation.page"')->setMaxResults(1);
$qq->setParameter('prd_id', $prd_id);
$stt = $qq->execute();
$dd = $stt->fetchAll(\PDO::FETCH_ASSOC);
if(count($dd)>0){
foreach($dd as $ww){
$wycofany = $ww['seo_path_info'];
}
}
else{
$wycofany = "";
}
$redirectResponse = new RedirectResponse($url."/".$wycofany);
$event->setResponse($redirectResponse);
}
}
else{
$redirectResponse = new RedirectResponse($url);
$event->setResponse($redirectResponse);
}
}
return;
}
/**
* Event für alle Seiten
*
* @param PageLoadedEvent $event
* @throws \Exception
*/
public function onPageLoaded($event)
{
/** @var Page $page */
/*$page = $event->getPage();
$domena_at = "https://schorngmbh.com/";
$hreflangi = new Hreflangi();
$attrs = $event->getRequest()->attributes;
$url = $attrs->get('sw-storefront-url').'/'.$attrs->get('crehler-seo-original-url');
$hreflangi->setUrl($url);
//echo "<pre>"; print_r($attrs->get('_route')); echo "</pre>";
//echo "<pre>"; print_r($url); echo "</pre>";
//$salesChannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
//$containerIds = $this->datalayerService->getContainerIds($salesChannelId);
//if(!$containerIds) return;
//print_r(get_class($event));
//echo "<pre>"; print_r($page); echo "</pre>";
//print_r("<br/>===========================<br/>");
switch (get_class($event)) {
case ProductPageLoadedEvent::class:
$prd = $page->getProduct()->getProductNumber();
$q = $this->connection->createQueryBuilder();
$q->select(['*'])->from('seo_hreflangi')->where('kod= :prd')->andWhere('route_name="frontend.detail.page"')
->andWhere('sklep="at"')->setMaxResults(1);
$q->setParameter('prd', $prd);
$st = $q->execute();
$d = $st->fetchAll(\PDO::FETCH_ASSOC);
if(count($d)>0){
foreach($d as $w){
$at = $w['seo_path_info'];
}
}
else{
$at = "";
}
$hreflangi->setUrlAt($domena_at.$at);
break;
case CheckoutCartPageLoadedEvent::class:
case CheckoutConfirmPageLoadedEvent::class:
case CheckoutRegisterPageLoadedEvent::class:
case CheckoutFinishPageLoadedEvent::class:
case AccountOverviewPageLoadedEvent::class:
case AccountProfilePageLoadedEvent::class:
case AccountLoginPageLoadedEvent::class:
case AccountEditOrderPageLoadedEvent::class:
case AccountOrderPageLoadedEvent::class:
case AddressListingPageLoadedEvent::class:
case AccountPaymentMethodPageLoadedEvent::class:
case SearchPageLoadedEvent::class:
$hreflangi->setUrlAt($domena_at.'/'.$attrs->get('crehler-seo-original-url'));
break;
case NavigationPageLoadedEvent::class:
if($attrs->get('_route')=="frontend.home.page"){
$at = "";
}
else{
$IdKat = $page->getFooter()->getNavigation()->getActive()->getAutoIncrement();
$q = $this->connection->createQueryBuilder();
$q->select(['*'])->from('seo_hreflangi')->where('id= :IdKat')->andWhere('route_name="frontend.navigation.page"')
->andWhere('sklep="at"')->setMaxResults(1);
$q->setParameter('IdKat', $IdKat);
$st = $q->execute();
$d = $st->fetchAll(\PDO::FETCH_ASSOC);
if(count($d)>0){
foreach($d as $w){
$at = $w['seo_path_info'];
}
}
else{
$at = "";
}
}
$hreflangi->setUrlAt($domena_at.$at);
break;
case ErrorPageLoadedEvent::class:
case NewsletterRegisterPageLoadedEvent::class:
case NewsletterSubscribePageLoadedEvent::class:
case MaintenancePageLoadedEvent::class:
case ContactPageLoadedEvent::class:
case GenericPageLoadedEvent::class:
default:
$route = $attrs->get('_route');
$q = $this->connection->createQueryBuilder();
$q->select(['*'])->from('seo_hreflangi_route')->where('route_name= :route')->andWhere('nazwa!=""')->andWhere('sklep="at"')
->setMaxResults(1);
$q->setParameter('route', $route);
$st = $q->execute();
$d = $st->fetchAll(\PDO::FETCH_ASSOC);
if(count($d)>0){
foreach($d as $w){
$at = $w['nazwa'];
}
}
else{
$at = $attrs->get('crehler-seo-original-url');
}
$hreflangi->setUrlAt($domena_at.$at);
break;
}
$page->addExtension('hreflangi', $hreflangi);
//echo "<pre>"; print_r($page->getExtension('hreflangi')); echo "</pre>";*/
}
}