<?php
namespace BitBag\OpenMarketplace\Controller\Bikers;
use BitBag\OpenMarketplace\Entity\ProductListing\ProductDraft;
use BitBag\OpenMarketplace\Entity\ProductListing\ProductListingInterface;
use BitBag\OpenMarketplace\Repository\ProductListing\ProductDraftRepository;
use BitBag\OpenMarketplace\Repository\ProductListing\ProductListingRepositoryInterface;
use BitBag\OpenMarketplace\Repository\ProductRepositoryInterface;
use BitBag\OpenMarketplace\Updater\ProductAttributeUpdaterInterface;
use Doctrine\Persistence\ObjectManager;
use Sylius\Bundle\ResourceBundle\Controller\AuthorizationCheckerInterface;
use Sylius\Bundle\ResourceBundle\Controller\EventDispatcherInterface;
use Sylius\Bundle\ResourceBundle\Controller\FlashHelperInterface;
use Sylius\Bundle\ResourceBundle\Controller\NewResourceFactoryInterface;
use Sylius\Bundle\ResourceBundle\Controller\RedirectHandlerInterface;
use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactoryInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Bundle\ResourceBundle\Controller\ResourceDeleteHandlerInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourceFormFactoryInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProviderInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourceUpdateHandlerInterface;
use Sylius\Bundle\ResourceBundle\Controller\SingleResourceProviderInterface;
use Sylius\Bundle\ResourceBundle\Controller\StateMachineInterface;
use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Metadata\MetadataInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Resource\ResourceActions;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
final class ListProductIndexController extends ResourceController
{
private $taxonRepository;
private $productRepository;
private $productDraftRepository;
private $productListingRepository;
public function __construct(
MetadataInterface $metadata,
RequestConfigurationFactoryInterface $requestConfigurationFactory,
?ViewHandlerInterface $viewHandler,
RepositoryInterface $repository,
FactoryInterface $factory,
NewResourceFactoryInterface $newResourceFactory,
ObjectManager $manager,
SingleResourceProviderInterface $singleResourceProvider,
ResourcesCollectionProviderInterface $resourcesFinder,
ResourceFormFactoryInterface $resourceFormFactory,
RedirectHandlerInterface $redirectHandler,
FlashHelperInterface $flashHelper,
AuthorizationCheckerInterface $authorizationChecker,
EventDispatcherInterface $eventDispatcher,
?StateMachineInterface $stateMachine,
ResourceUpdateHandlerInterface $resourceUpdateHandler,
ResourceDeleteHandlerInterface $resourceDeleteHandler,
TaxonRepositoryInterface $taxonRepository,
ProductRepositoryInterface $productRepository,
ProductDraftRepository $productDraftRepository,
ProductListingRepositoryInterface $productListingRepository
) {
parent::__construct(
$metadata,
$requestConfigurationFactory,
$viewHandler,
$repository,
$factory,
$newResourceFactory,
$manager,
$singleResourceProvider,
$resourcesFinder,
$resourceFormFactory,
$redirectHandler,
$flashHelper,
$authorizationChecker,
$eventDispatcher,
$stateMachine,
$resourceUpdateHandler,
$resourceDeleteHandler,
);
$this->taxonRepository = $taxonRepository;
$this->productRepository = $productRepository;
$this->productDraftRepository = $productDraftRepository;
$this->productListingRepository = $productListingRepository;
}
public function indexAction(Request $request): Response
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$this->isGrantedOr403($configuration, ResourceActions::INDEX);
$resources = $this->resourcesCollectionProvider->get($configuration, $this->repository);
$taxons = $this->taxonRepository->findBy(['parent' => null]);
$taxons = $taxons[0]->getChildren()->toArray();
$url = $request->getUri();
$url = explode('/', $url);
$url = end($url);
//
$category = $this->taxonRepository->findOneBy(['code' => $url]);
$url = $category;
// Date d'aujourd'hui
// Date d'aujourd'hui
$aujourdHui = new \DateTime();
$nombreJours = [];
$product = $this->productRepository->findAll();
foreach ($product as $resource) {
// Date de création de la ressource
$createdAt = $resource->getCreatedAt();
$id = $resource->getId();
// Calculer la différence entre les deux dates
$difference = $aujourdHui->diff($createdAt);
// dd($resource);
// Obtenir le nombre de jours à partir de la différence
$nombreJours [$id] = $difference->days;
}
// dd($nombreJours);
$event = $this->eventDispatcher->dispatchMultiple(ResourceActions::INDEX, $configuration, $resources);
$eventResponse = $event->getResponse();
if (null !== $eventResponse) {
return $eventResponse;
}
if ($request->isMethod('POST')) {
$products = $this->productRepository->findAll();
// Récupérer le paramètre 'category-product' sous forme de tableau
$categories = $request->get('category-product', []);
// Récupérer les paramètres 'color-product' sous forme de tableau
$colors = $request->get('color-product', []);
$colorseri = [];
foreach ($colors as $color) {
$colorseri [] = serialize([$color]);
}
// Récupérer les paramètres 'size-product' sous forme de tableau
$sizes = $request->get('size-product', []);
$sizeseri = [];
foreach ($sizes as $size) {
$sizeseri [] = serialize([$size]);
}
// Récupérer le paramètre 'min-price-product'
$minPrice = $request->get('min-price-product');
$minPrice = $minPrice * 100;
// Récupérer le paramètre 'max-price-product'
$maxPrice = $request->get('max-price-product');
$maxPrice = $maxPrice * 100;
// Récupérer le paramètre 'state-product'
$state = $request->get('state-product', []);
// Récupérer le paramètre 'brand-product'
$brand = $request->get('brand-product', []);
// Récupérer le paramètre 'vendor-type-product'
$vendorType = $request->get('vendor-type-product', []);
// Récupérer le paramètre 'location-product'
$location = $request->get('location-product', []);
//find first 50 products
$e = $this->productListingRepository->findBy([], null, 50);
$d = [];
foreach ($e as $product) {
$id = $this->productDraftRepository->findLatestDraft($product);
$d [] = $id->getId();
}
$a = $this->productDraftRepository->findAll();
$qb = $this->productDraftRepository->createQueryBuilder('p')
->leftJoin('p.productListingPrice', 'plp')
->andWhere('plp.price BETWEEN :minPrice AND :maxPrice')
->setParameter('minPrice', $minPrice)
->setParameter('maxPrice', $maxPrice);
$qb->andWhere(
$qb->expr()->in(
'p.id',
$d
)
);
if($categories){
$qb->leftJoin('p.productDraftTaxons', 'pt')
->leftJoin('pt.taxon', 't')
->leftJoin('t.parent', 'tp') // Ajout d'une jointure avec le parent du taxon
->andWhere(
$qb->expr()->orX(
$qb->expr()->in('t.code', ':categories'),
$qb->expr()->in('tp.code', ':categories') // Ajout d'une condition pour le code du parent
)
)
->setParameter('categories', $categories);
}
$results = $qb->getQuery()->getResult();
$matching_products = array();
$matching_products_2 = array();
if($colorseri){
foreach ($results as $product){
$color_product = $product->getColor();
if (count(array_intersect($color_product, $colors)) > 0) {
$matching_products[] = $product;
}
}
}else{
$matching_products = $results;
}
if($sizeseri){
foreach($matching_products as $product){
$size_product = $product->getSize();
if (count(array_intersect($size_product, $sizes)) > 0) {
$matching_products_2[] = $product;
}
}
}
if(!$colorseri && !$sizeseri){
$matching_products_2 = $results;
}
$products = [];
foreach ($matching_products_2 as $draft){
$products [] = $draft->getProductListing()->getProduct();
}
return $this->render($configuration->getTemplate(ResourceActions::INDEX . '.html'), [
'configuration' => $configuration,
'metadata' => $this->metadata,
'resources' => $resources,
'taxons' => $taxons,
'product_filter' => $products,
'slug' => $url,
'nombreJours' => $nombreJours,
$this->metadata->getPluralName() => $resources,
]);
}
if ($configuration->isHtmlRequest()) {
return $this->render($configuration->getTemplate(ResourceActions::INDEX . '.html'), [
'configuration' => $configuration,
'metadata' => $this->metadata,
'resources' => $resources,
'taxons' => $taxons,
'nombreJours' => $nombreJours,
'product_filter' => null,
'slug' => $url,
$this->metadata->getPluralName() => $resources,
]);
}
return $this->createRestView($configuration, $resources);
}
}