<?php
namespace MojeBambino\Extensions\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\DBAL\Connection;
use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use MojeBambino\Extensions\Struct\Kafelki;
class CategoryPageSubscriber implements EventSubscriberInterface {
private Connection $connection;
public function __construct(Connection $connection){
$this->connection = $connection;
}
public static function getSubscribedEvents(): array{
return [NavigationPageLoadedEvent::class => 'onCategoryPageLoaded'];
}
public function onCategoryPageLoaded(NavigationPageLoadedEvent $event): void{
$category = $event->getPage()->getCmsPage();
$IdKat = "";
$sections = $category->getSections();
$i = 1;
foreach($sections->getElements() as $elem){
foreach($elem->getBlocks()->getElements() as $elem1){
if($elem1->getType()=="product-listing"){
foreach($elem1->getSlots() as $slot){
$ost = $slot->getData()->getListing()->getCurrentFilters();
$IdKat = $ost['navigationId'];
}
}
}
$i++;
}
$kafelki = new Kafelki();
if($IdKat && $IdKat!=""){
$query = $this->connection->createQueryBuilder();
$query->select(['k.*'])->from('kafelki_kategorie', 'kk');
$query->join('kk', 'kafelki', 'k', 'kk.Id=k.Id');
$query->join('kk', 'category', 'c', 'kk.IdKat=c.auto_increment');
$query->where('hex(c.id)= :IdKat')->andWhere("k.active=1");
$query->setParameter('IdKat', $IdKat);
$statement = $query->execute();
$kaf = $statement->fetchAll(\PDO::FETCH_ASSOC);
$kafelki->setKafle($kaf);
}
$category->addExtension('kafelki', $kafelki);
}
}