<?php
declare(strict_types=1);
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
use Doctrine\Persistence\ManagerRegistry;
// use Symfony\Component\DependencyInjection\ContainerInterface;
// use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Symfony\Component\DependencyInjection\ContainerInterface;
// use BitBag\SyliusCmsPlugin\Resolver\PageResourceResolverInterface;
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
use Sylius\Component\Product\Repository\ProductRepositoryInterface;
// use App\Entity\SiteProductOptionValueMatch;
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use App\Services\ElasticSearchService;
use App\Entity\SiteFilterFacet;
use App\Entity\SiteCollection;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
// use Doctrine\ORM\Query;
class TaxonsController /*extends ResourceController*/
{
public function __construct(
Environment $twigEnvironment,
ManagerRegistry $doctrine,
ContainerInterface $container,
PageRepositoryInterface $pageRepository,
ProductRepositoryInterface $productRepository
){
$this->twigEnvironment = $twigEnvironment;
$this->doctrine = $doctrine;
$this->container = $container;
$this->pageRepository = $pageRepository;
$this->productRepository = $productRepository;
}
public function renderBlockAction(
Request $request,
LocaleContextInterface $localeContext,
ProductRepositoryInterface $productRepository,
ProductVariantRepositoryInterface $productVariantRepository,
ElasticSearchService $elasticSearch,
TranslatorInterface $translator,
): Response
{
$em = $this->doctrine->getManager();
$taxoncode = $request->query->get('code');
$typeblock = $request->query->get('typeblock');
// dump( $typeblock);
// exit();
$repository = $this->container->get('sylius.repository.taxon');
if(!empty($taxoncode) && !empty( $typeblock) && $typeblock != 21 && $typeblock != 25 ){
$taxonsChilds = $repository->findChildren($taxoncode);
$taxons = array();
if(is_array($taxonsChilds) && count($taxonsChilds) > 0){
foreach($taxonsChilds as $taxon){
$taxons[] = $taxon;
}
}
}
if($typeblock == 7){
// return new Response(
// $this->twigEnvironment->render('@SyliusShop/Webapic/page/taxon.html.twig', [
// 'taxons' => $taxons,
// ])
// );
// }else if($typeblock == 9){
// return new Response(
// $this->twigEnvironment->render('@SyliusShop/Webapic/page/encartbleu.html.twig', [
// 'taxons' => $taxons,
// ])
// );
}else if($typeblock == 17){
// if($request->query->has('select')){
$taxon = $repository->find($request->query->get('select'));
$taxonsChilds = $repository->findChildren($taxon->getCode());
$taxons = array();
if(is_array($taxonsChilds) && count($taxonsChilds) > 0){
foreach($taxonsChilds as $taxon){
$taxons[] = $taxon;
}
}
return new Response(
$this->twigEnvironment->render('@SyliusShop/Webapic/page/select-taxon.html.twig', [
'taxons' => $taxons,
'name' => $request->query->get('name'),
])
);
// }else{
// return new Response(
// $this->twigEnvironment->render('@SyliusShop/Webapic/page/block-cadeau.html.twig', [
// // 'taxon' => $taxon,
// 'taxons' => $taxons,
// ])
// );
// }
}else if($typeblock == 23){
$taxonsLastlevel = array();
// dump($taxons);
if(is_array($taxons) && count($taxons) > 0){
foreach($taxons as $taxon){
$taxonsChilds2 = $repository->findChildren($taxon->getCode());
if(is_array($taxonsChilds2) && count($taxonsChilds2) > 0){
foreach($taxonsChilds2 as $taxon2){
$taxonsChilds3 = $repository->findChildren($taxon2->getCode());
if(is_array($taxonsChilds3) && count($taxonsChilds3) > 0){
foreach($taxonsChilds3 as $taxon3){
$taxonsLastlevel[$taxon3->getId()] = $taxon3;
}
}
}
}
}
}
return new Response(
$this->twigEnvironment->render('@SyliusShop/Webapic/page/articles-last.html.twig', [
'articles' => $taxonsLastlevel,
])
);
}else if($typeblock == 25){
$tstart = microtime(true);
$sorting = $request->query->get('sorting');
$sortBy = null;
$sortOrder = null;
if (is_array($sorting)) {
$sortBy = key($sorting);
$sortOrder = $sorting[key($sorting)];
}
if(!empty($request->query->get('filtreUrl'))){
$filtre = $request->query->get('filtreUrl');
}else{
$filtre = $request->query->get('filtre', '');
}
$filtreUrl = $filtre;
dump($filtreUrl);
dump($request);
$selectedFacets = array();
$t1 = microtime(true);
// $isBadge = (array) $request->query->get('filtre', []);
// if(count($isBadge) > 0 ){
// foreach($isBadge as $key => $facet){
// if (str_contains($facet, 'facet')) {
// $value = explode("=", $facet);
// $selectedFacets = (array) ['badge' => $value[1]];
// }
// }
// }
// dump($isBadge);
if(!empty($filtreUrl)){
$values = explode("/", $filtreUrl);
if($values[1] == 'prix-bas' && $localeContext->getLocaleCode() == 'en_US' ){
$filtreUrl = 'badge/low-price';
}
}
dump($localeContext->getLocaleCode());
$t2 = microtime(true);
// $results = $elasticSearch->search($localeContext->getLocaleCode(), '', 0, $selectedFacets, $sortBy, $sortOrder);
// $results = $elasticSearch->getElementsByTaxon($localeContext->getLocaleCode(), 1, 0, $selectedFacets, $sortBy, $sortOrder, true);
// $variants = [];
// if (count($results['hits']['hits']) > 0) {
// $variants = $productVariantRepository->findByIdsAndOrderByFieldId(array_map(function ($row) {
// return str_ireplace('VARIANT_', '', $row['_id']);
// }, $results['hits']['hits']));
// }
// dump($variants);
// Récupère tous les filtres à facettes disponible pour
// cette recherche
$siteFilterFacets = $em->getRepository(SiteFilterFacet::class)
->findBy([
'enabled' => true
], [
'position' => 'ASC'
]);
$t21 = microtime(true);
$esAggregations = $elasticSearch->getFilterFacetsSearch($localeContext->getLocaleCode(), '', $selectedFacets, array_map(function ($row) {
return $row->getId();
}, $siteFilterFacets));
$t3 = microtime(true);
$siteFilterShape = [];
if (count($esAggregations['shape']['buckets']) > 1) {
$siteFilterShape = $productRepository->findBy([
'id' => array_map(function ($row) {
return $row['key'];
}, $esAggregations['shape']['buckets']),
'enabled' => true
], [
'code' => 'ASC'
]);
usort($siteFilterShape, function ($a, $b) {
// dump($a->getTranslation()->getName());
return strcmp( $a->getTranslation()->getName() , $b->getTranslation()->getName());
});
}
dump($esAggregations);
$t4 = microtime(true);
$siteFilterColor = [];
if (count($esAggregations['color']['buckets']) > 1) {
$siteFilterColor = $em->getRepository(SiteCollection::class)->findBy([
'productOptionValue' => array_map(function ($row) {
return $row['key'];
}, $esAggregations['color']['buckets']),
]);
}
$siteFilterBadge = [];
$siteFilterBadgeVariants = [];
if (count($esAggregations['badge']['buckets']) > 1) {
foreach($esAggregations['badge']['buckets'] as $badge){
$siteFilterBadge[] = array(
'name' => $translator->trans("app.ui.product_callout.".$badge['key']),
'id' => $badge['key'],
);
}
// $siteFilterBadgeVariants = $productVariantRepository->findBy([
// 'badge' => array_map(function ($row) {
// return $row['key'];
// }, $esAggregations['badge']['buckets']),
// 'enabled' => true
// ], [
// 'code' => 'ASC'
// ]);
// if(count($siteFilterBadgeVariants) > 0){
// foreach($siteFilterBadgeVariants as $siteFilterBadgeVariant){
// $siteFilterBadge[$siteFilterBadgeVariant->getBadge()] = array(
// 'name' => $translator->trans("app.ui.product_callout.".$siteFilterBadgeVariant->getBadge()),
// 'id' => $siteFilterBadgeVariant->getBadge(),
// );
// }
// }
}
dump($siteFilterBadge);
$t5 = microtime(true);
$siteFilterFacetsValues = [];
foreach ($esAggregations as $keyEsAggregation => $rowEsAggregation) {
if (isset($rowEsAggregation['buckets']) && count($rowEsAggregation['buckets']) > 1) {
$siteFilterFacetsValues[$keyEsAggregation] = array_map(function ($row) {
return $row['key'];
}, $rowEsAggregation['buckets']);
// On met dans l'ordre alphabétique
sort($siteFilterFacetsValues[$keyEsAggregation]);
}
}
// Conserve que les filtres à facettes disponible pour cette recherche
$siteFilterFacets = array_filter($siteFilterFacets, function ($row) use (&$siteFilterFacetsValues) {
return in_array($row->getId(), array_keys($siteFilterFacetsValues));
});
$t6 = microtime(true);
$response = $this->twigEnvironment->render('/partials/block-listing-produits.html.twig', [
'filtre' => $filtre,
'filtreUrl' => $filtreUrl,
'variants' => array(),
'fromPage' => 1,
'selectedFacets' => $selectedFacets,
'siteFilterShape' => $siteFilterShape,
'siteFilterColor' => $siteFilterColor,
'siteFilterBadge' => $siteFilterBadge,
'siteFilterFacets' => $siteFilterFacets,
'siteFilterFacetsValues' => $siteFilterFacetsValues,
'siteFilterMinPrice' => $esAggregations[ElasticSearchService::FACET_PRICE_MIN]['value'],
'siteFilterMaxPrice' => $esAggregations[ElasticSearchService::FACET_PRICE_MAX]['value'],
'maxViewResults' => ElasticSearchService::MAX_SEARCH_RESULTS,
// 'totalResults' => $results['hits']['total']['value'],
]);
$tend = microtime(true);
$log = [];
$log[] = 'Time to retrieve taxon: ' . ($t1 - $tstart) . ' seconds';
$log[] = 'Time to retrieve facets: ' . ($t2 - $t1) . ' seconds';
$log[] = 'Time to retrieve facets values: ' . ($t21 - $t2) . ' seconds';
$log[] = 'Time to retrieve shape: ' . ($t3 - $t21) . ' seconds';
$log[] = 'Time to retrieve color: ' . ($t4 - $t3) . ' seconds';
$log[] = 'Time to retrieve badge: ' . ($t5 - $t4) . ' seconds';
$log[] = 'Time to retrieve facets values: ' . ($t6 - $t5) . ' seconds';
$log[] = 'Time to render: ' . ($tend - $t6) . ' seconds';
$log[] = 'Total time: ' . ($tend - $tstart) . ' seconds';
dump($log);
return new Response($response);
}else{
return new Response(
$this->twigEnvironment->render('@SyliusShop/Webapic/page/caroussel.html.twig', [
'taxons' => $taxons,
])
);
}
}
public function menuScrollAction(Request $request): Response
{
$repository = $this->container->get('sylius.repository.taxon');
$taxonsChilds = $repository->findChildren('Menu-deploye');
return new Response(
$this->twigEnvironment->render('@SyliusShop/Webapic/menu-scroll.html.twig', [
'taxons' => $taxonsChilds,
])
);
}
public function pageFiltreWithFiltreEngage(
Request $request,
LocaleContextInterface $localeContext,
ChannelContextInterface $channelContext,
): Response
{
$channel = $channelContext->getChannel();
$basepage = $request->get('base');
dump($basepage);
$page = $this->pageRepository->findOneEnabledBySlugAndChannelCode($basepage, $localeContext->getLocaleCode(), $channel->getCode());
$filtreUrl = '';
if(!empty($request->get('forme')) && !empty($request->get('slug'))){
$filtreUrl = $request->get('forme').'/'.$request->get('slug');
}
// dump($filtreUrl);
return new Response(
$this->twigEnvironment->render('/bundles/BitBagSyliusCmsPlugin/Shop/Page/show.html.twig', [
'typeblock' => 25,
'page' => $page,
'filtreUrl' => $filtreUrl
])
);
}
}