<?php
namespace MojeBambino\Extensions\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Doctrine\DBAL\Connection;
use MojeBambino\Extensions\Struct\Dopiski;
class ProductPageSubscriber implements EventSubscriberInterface {
private Connection $connection;
public function __construct(Connection $connection){
$this->connection = $connection;
}
public static function getSubscribedEvents(): array{
return [ProductPageLoadedEvent::class => 'onProductPageLoaded'];
}
public function onProductPageLoaded(ProductPageLoadedEvent $event): void{
$page = $event->getPage();
$product = $page->getProduct();
//echo "<pre>"; print_r($product); echo "</pre>";
$kod = $product->getProductNumber();
$dopiski = new Dopiski();
$q = $this->connection->createQueryBuilder();
$q->select(['d.*'])->from('dopiski_kody', 'dk');
$q->join('dk', 'dopiski', 'd', 'dk.Id=d.Id');
$q->where('dk.kod= :kod');
$q->setParameter('kod', $kod);
$st = $q->execute();
$dop = $st->fetchAll(\PDO::FETCH_ASSOC);
$dopiski->setDopiski($dop);
$q6 = $this->connection->createQueryBuilder();
$q6->select(['*'])->from('wzrost_ceny')->where('kod= :kod')->setMaxResults(1);
$q6->setParameter('kod', $kod);
$st6 = $q6->execute();
$d6 = $st6->fetchAll(\PDO::FETCH_ASSOC);
if(count($d6)>0){
foreach($d6 as $dop){
$dopiski->setWzrostCeny($dop['data']);
}
}
else{
$dopiski->setWzrostCeny(count($d6));
}
$q7 = $this->connection->createQueryBuilder();
$q7->select(['*'])->from('cena_do_daty')->where('kod= :kod')->andWhere('data>="'.date("Y-m-d").'"')->setMaxResults(1);
$q7->setParameter('kod', $kod);
//echo $q7->getSQL();
$st7 = $q7->execute();
$d7 = $st7->fetchAll(\PDO::FETCH_ASSOC);
if(count($d7)>0){
foreach($d7 as $dop){
$dopiski->setCenaDoDaty($dop['data']);
}
}
else{
$dopiski->setCenaDoDaty(count($d7));
}
$q8 = $this->connection->createQueryBuilder();
$q8->select(['*'])->from('omnibus_cena')->where('kod= :kod')->setMaxResults(1);
$q8->setParameter('kod', $kod);
$st8 = $q8->execute();
$d8 = $st8->fetchAll(\PDO::FETCH_ASSOC);
if(count($d8)>0){
foreach($d8 as $dop){
$dopiski->setOmnibusCena(str_replace(".", ",", $dop['cena'])." zł");
}
}
else{
$dopiski->setOmnibusCena(count($d8));
}
$q9 = $this->connection->createQueryBuilder();
$q9->select(['*'])->from('omnibus_info')->setMaxResults(1);
$st9 = $q9->execute();
$d9 = $st9->fetchAll(\PDO::FETCH_ASSOC);
if(count($d9)>0){
foreach($d9 as $dop){
$dopiski->setOmnibusInfo($dop['info']);
}
}
else{
$dopiski->setOmnibusInfo(count($d9));
}
$product->addExtension('dopiski', $dopiski);
}
}