<?php
declare(strict_types=1);
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Sylius\Component\Product\Model\ProductAttributeInterface;
use Sylius\Bundle\ProductBundle\Doctrine\ORM\ProductAttributeValueRepository;
use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
use App\Entity\Product\ProductVariant;
use App\Entity\SiteGiftIdeaLink;
/**
* Page "Idées Cadeaux"
*/
class SiteGiftIdeaController extends AbstractController
{
public function indexAction(
Request $request,
Breadcrumbs $breadcrumbs,
ProductAttributeValueRepository $productAttributeValueRepository,
ChannelContextInterface $channelContext,
LocaleContextInterface $localeContext,
ProductVariantRepositoryInterface $productVariantRepository
) {
// dump($budget);
// Récupère les liens de "Idées cadeaux"
$em = $this->get('doctrine')->getManager();
$siteGiftIdeaLinkRepository = $em->getRepository(SiteGiftIdeaLink::class);
$siteGiftIdeaLinks = $siteGiftIdeaLinkRepository->findBy([
'enabled' => true
], [
'position' => 'ASC'
]);
// if(!empty($_GET)) {
// }
// dump($_GET);
// dump($_GET);
// dump($_POST);
// dump($_REQUEST);
$budget = (!empty($_GET['budget']))?(int)$_GET['budget']:null;
$pour_qui = (!empty($_GET['pour_qui']))?$_GET['pour_qui']:null;
$quel_age = (!empty($_GET['quel_age']))?$_GET['quel_age']:null;
// $pour_qui = $request->request->get('pour_qui', null);
// $quel_age = $request->request->get('quel_age', null);
// // $data = (array) $request->request->all();
// dump($request->request->get('budget', null));
// Pour qui : Propriété "giftFor" des déclinaisons produits
// Par âge : Caractéristique "Tranche d'âge"
// Budget : <10€, <20€, <30€, <40€ ou <50€ (en dur)
// Récupère les valeurs de la caractéristiques "Tranche d'âge"
$productAttributeRepository = $em->getRepository(ProductAttributeInterface::class);
$productAttribute = $productAttributeRepository->findOneByCode('Tranche_Age');
$breadcrumbs->addRouteItem("sylius.ui.home", "sylius_shop_homepage");
$breadcrumbs->addItem("Idées cadeaux");
$displayVariants = "";
if($budget > 0) {
$variants = $this->getVariantsFromParams(
$budget*100,
$pour_qui,
$quel_age,
$channelContext,
$localeContext,
$productAttributeValueRepository,
$productVariantRepository);
$displayVariants = $this->render('site_gift_idea_ajax.html.twig', [
'variants' => $variants
])->getContent();;
}
return $this->render('site_gift_idea_page.html.twig', [
'giftFor' => ProductVariant::GIFT_FOR_VALUES,
'ages' => $productAttribute,
'siteGiftIdeaLinks' => $siteGiftIdeaLinks,
'displayProducts' => $displayVariants,
'budget' => $budget,
'pour_qui' => $pour_qui,
'quel_age' => $quel_age
]);
}
public function ajaxAction(
Request $request,
Breadcrumbs $breadcrumbs,
ChannelContextInterface $channelContext,
LocaleContextInterface $localeContext,
ProductAttributeValueRepository $productAttributeValueRepository,
ProductRepositoryInterface $productRepository,
ProductVariantRepositoryInterface $productVariantRepository
) {
$budget = intval($request->request->get('budget', null));
$pour_qui = $request->request->get('pour_qui', null);
$quel_age = $request->request->get('quel_age', null);
$variants = $this->getVariantsFromParams(
$budget,
$pour_qui,
$quel_age,
$channelContext,
$localeContext,
$productAttributeValueRepository,
$productVariantRepository);
// $channel = $channelContext->getChannel();
// $locale = $localeContext->getLocaleCode();
// $productsIds = [];
// if (empty($quel_age) == false) {
// // Récupère les formes qui ont cette valeur de caractéristique
// $productsIds = array_unique(array_map(function ($row) {
// return $row->getProduct()->getId();
// }, $productAttributeValueRepository->findByJsonChoiceKey($quel_age)));
// }
// $variants = $productVariantRepository->createQueryBuilder('o')
// ->addSelect('channelPricing')
// ->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
// ->setParameter('locale', $locale)
// ->andWhere('o.enabled = 1')
// ->andWhere('o.onHand >= 5')
// ->orderBy('RAND()');
// if (empty($budget)) {
// $variants = $variants->innerJoin('o.channelPricings', 'channelPricing', 'WITH', 'channelPricing.channelCode = :channelCode')
// ->setParameter('channelCode', $channel->getCode());
// } else {
// $variants = $variants->innerJoin('o.channelPricings', 'channelPricing', 'WITH', 'channelPricing.channelCode = :channelCode AND channelPricing.price <= :price')
// ->setParameter('price', $budget)
// ->setParameter('channelCode', $channel->getCode());
// }
// if (empty($pour_qui) == false) {
// // $variants = $variants
// // ->andWhere('o.giftFor = :giftFor')
// // ->setParameter('giftFor', $pour_qui);
// // $variants = $variants->innerJoin('o.productVariantAttribute', 'productVariantAttribute', 'WITH', 'productVariantAttribute.json_value = "'.$pour_qui.'"')
// // ->setParameter('price', $budget)
// // ->setParameter('channelCode', $channel->getCode());
// }
// if (count($productsIds) > 0) {
// $variants = $variants
// ->innerJoin('o.product', 'product', 'WITH', 'product.id IN (:productsId)')
// ->setParameter('productsId', $productsIds);
// }
// if (empty($pour_qui)) {
// $variants = $variants
// ->setMaxResults( 50 )
// ->getQuery()
// ->getResult();
// }else{
// // dump('ici');
// $allVariants = $variants
// // ->setMaxResults( 50 )
// ->getQuery()
// ->getResult();
// $giftVariant = [];
// if (count($allVariants) > 0 ) {
// foreach($allVariants as $variant){
// $index = 0;
// foreach($variant->getAttributes() as $attribute){
// if($attribute->getAttribute()->getId() == 67){
// foreach($attribute->getValue() as $value){
// if($value == $pour_qui){
// if($index == 50){
// break;
// }
// $index++;
// $giftVariant[$variant->getId()] = $variant;
// }
// }
// }
// }
// }
// }
// $variants = $giftVariant;
// }
// dump($variants);
return $this->render('site_gift_idea_ajax.html.twig', [
'variants' => $variants
]);
}
private function getVariantsFromParams(
$budget,
$pour_qui,
$quel_age,
ChannelContextInterface $channelContext,
LocaleContextInterface $localeContext,
ProductAttributeValueRepository $productAttributeValueRepository,
ProductVariantRepositoryInterface $productVariantRepository) {
$channel = $channelContext->getChannel();
$locale = $localeContext->getLocaleCode();
$productsIds = [];
if (empty($quel_age) == false) {
// Récupère les formes qui ont cette valeur de caractéristique
$productsIds = array_unique(array_map(function ($row) {
return $row->getProduct()->getId();
}, $productAttributeValueRepository->findByJsonChoiceKey($quel_age)));
}
$variants = $productVariantRepository->createQueryBuilder('o')
->addSelect('channelPricing')
->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
->setParameter('locale', $locale)
->andWhere('o.enabled = 1')
->andWhere('o.onHand >= 5')
->orderBy('RAND()');
if (empty($budget)) {
$variants = $variants->innerJoin('o.channelPricings', 'channelPricing', 'WITH', 'channelPricing.channelCode = :channelCode')
->setParameter('channelCode', $channel->getCode());
} else {
$minBudget = $budget - 1000;
$variants = $variants->innerJoin('o.channelPricings', 'channelPricing', 'WITH', 'channelPricing.channelCode = :channelCode AND channelPricing.price <= :price AND channelPricing.price >= :minPrice')
->setParameter('price', $budget)
->setParameter('minPrice', $minBudget)
->setParameter('channelCode', $channel->getCode());
}
if (empty($pour_qui) == false) {
// $variants = $variants
// ->andWhere('o.giftFor = :giftFor')
// ->setParameter('giftFor', $pour_qui);
// $variants = $variants->innerJoin('o.productVariantAttribute', 'productVariantAttribute', 'WITH', 'productVariantAttribute.json_value = "'.$pour_qui.'"')
// ->setParameter('price', $budget)
// ->setParameter('channelCode', $channel->getCode());
}
if (count($productsIds) > 0) {
$variants = $variants
->innerJoin('o.product', 'product', 'WITH', 'product.id IN (:productsId)')
->setParameter('productsId', $productsIds);
}
if (empty($pour_qui)) {
$variants = $variants
->setMaxResults( 50 )
->getQuery()
->getResult();
}else{
// dump('ici');
$allVariants = $variants
// ->setMaxResults( 50 )
->getQuery()
->getResult();
$giftVariant = [];
if (count($allVariants) > 0 ) {
foreach($allVariants as $variant){
$index = 0;
foreach($variant->getAttributes() as $attribute){
if($attribute->getAttribute()->getId() == 67){
foreach($attribute->getValue() as $value){
if($value == $pour_qui){
if($index == 50){
break;
}
$index++;
$giftVariant[$variant->getId()] = $variant;
}
}
}
}
}
}
$variants = $giftVariant;
}
return $variants;
}
}