<?php
declare(strict_types=1);
namespace App\Controller;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Component\Resource\ResourceActions;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Repository\ProductRepository;
use App\Entity\ProductAttribute;
use App\Repository\ProductAttributeRepository;
use App\Form\SearchProductFormType;
use Twig\Environment;
class ProductController extends ResourceController
{
public function filters (Environment $twig, Request $request, ProductRepository $productRepository): Response
{
$originalRequest = $this->get('request_stack')->getMasterRequest();
$locale = $originalRequest->getLocale();
//Liste des attributs
$productAttributeRepository = $this->container->get('sylius.repository.product_attribute');
$attrs = $productAttributeRepository->findAll();
$taxonRepository = $this->container->get('sylius.repository.taxon');
$availableTaxons = array();
$taxons = $taxonRepository->findAll();
foreach ($taxons as $taxon) {
$availableTaxons[$taxon->getName()] = $taxon;
}
$typelait_choices = $this->getChoicesValues ($productAttributeRepository, "typelait", $locale);
$traitement_choices = $this->getChoicesValues ($productAttributeRepository, "traitement", $locale);
//$poids_choices = $this->getChoicesValues ($productAttributeRepository, "poids", $locale);
$pays_choices = $this->getChoicesTaxonValuesSameLevel2 ($taxonRepository, "pays", $locale);
$typepate_choices = $this->getChoicesValues ($productAttributeRepository, "typepate", $locale);
$typeproduction_choices = $this->getChoicesValues ($productAttributeRepository, "typeproduction", $locale);
//$bio_choices = $this->getChoicesValues ($productAttributeRepository, "bio", $locale);
/*$productAttributes = $productAttributeRepository->findAll();
foreach ($productAttributes as $productAttribute) {
$configuration = $productAttribute->getConfiguration();
}*/
//Formulaire de recherche
$view_form = null;
/*
$form = $this->createForm(SearchProductFormType::class, [
'typelait_choices' => $typelait_choices,
'traitement_choices' => $traitement_choices,
'poids_choices' => $poids_choices,
'pays_choices' => $pays_choices,
'typepate_choices' => $typepate_choices,
'typeproduction_choices' => $typeproduction_choices,
]);
$form->handleRequest($request); //important !
if ($form->isSubmitted() && $form->isValid()) {
//Recherche
}
$view_form = $form->createView();*/
//Rendu
$render = $this->render('@SyliusShop/Product/_filters.html.twig', [
'search_form' => $view_form,
'typelait_choices' => $typelait_choices,
'traitement_choices' => $traitement_choices,
'pays_choices' => $pays_choices,
'typepate_choices' => $typepate_choices,
'typeproduction_choices' => $typeproduction_choices,
'originalRequest' => $originalRequest,
]);
return $render;
}
private function getChoicesTaxonValuesSameLevel2 ( $taxonRepository, string $code, string $locale, $inverse = false)
{
$res = array();
//TODO faire une fonction recursive
// Find the parent taxon
$taxons = $taxonRepository->findChildren($code);
foreach ($taxons as $key => $taxon) {
$res[$taxon->getId()]["name"] = $taxon->getName();
$taxons_child = $taxonRepository->findChildren($taxon->getCode());
$mychild = array();
foreach ($taxons_child as $keyc => $taxonc) {
$mychild[$taxonc->getId()] = $taxonc->getName();
}
asort ($mychild);
$res[$taxon->getId()]["childs"] = $mychild;
}
return $res;
}
private function getChoicesTaxonValuesSameLevel ( $taxonRepository, string $code, string $locale, $inverse = false)
{
$res = array();
//TODO faire une fonction recursive
// Find the parent taxon
$taxons = $taxonRepository->findChildren($code);
foreach ($taxons as $key => $taxon) {
if ($inverse)
$res[ $taxon->getName()] = $taxon->getId();
else
$res[ $taxon->getId()] = $taxon->getName();
$taxons_child = $taxonRepository->findChildren($taxon->getCode());
foreach ($taxons_child as $keyc => $taxonc) {
if ($inverse)
$res[ $taxonc->getName()] = $taxonc->getId();
else
$res[ $taxonc->getId()] = $taxonc->getName();
}
}
return sort($res);
}
private function getChoicesTaxonValues ( $taxonRepository, string $code, string $locale)
{
$res = array();
//TODO faire une fonction recursive
// Find the parent taxon
$taxons = $taxonRepository->findChildren($code);
foreach ($taxons as $key => $taxon) {
$res[ $taxon->getId()] = array();
$res[ $taxon->getId()]["name"] = $taxon->getName();
$res[ $taxon->getId()]["code"] = $taxon->getCode();
$res[ $taxon->getId()]["id"] = $taxon->getId();
$res[ $taxon->getId()]["children"] = array();
$taxons_child = $taxonRepository->findChildren($taxon->getCode());
foreach ($taxons_child as $keyc => $taxonc) {
$res[ $taxon->getId()]["children"][ $taxonc->getId()] = array();
$res[ $taxon->getId()]["children"][ $taxonc->getId()]["name"] = $taxonc->getName();
$res[ $taxon->getId()]["children"][ $taxonc->getId()]["code"] = $taxonc->getCode();
$res[ $taxon->getId()]["children"][ $taxonc->getId()]["id"] = $taxonc->getId();
$res[ $taxon->getId()]["children"][ $taxonc->getId()]["children"] = false;
}
}
return $res;
}
private function getChoicesValues ( $productAttributeRepository, string $code, string $locale, $inverse = false)
{
$res = array();
$attribute = $productAttributeRepository->findOneBy(array('code' => $code));
$configuration = $attribute->getConfiguration();
$choices = $configuration["choices"];
//$locale = str_replace ("fr", "fr_FR", $locale);
//$locale = str_replace ("en", "en_US" , $locale);
foreach ($choices as $key => $choice) {
if (array_key_exists($locale, $choice))
{
if ($inverse)
$res[$choice[$locale]] = $key;
else
$res[$key] = $choice[$locale];
}
}
return $res;
}
public function showAction(Request $request): Response
{
/*$this->get('sylius.email_sender')->send(
'giftcard',
['john.doe.mon@yopmail.com'],
[
'message' => "",
'code' => "xxxxx",
'user' => $this->getUser(),
'channel' => $channel,
'localeCode' => $locale
]);*/
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$this->isGrantedOr403($configuration, ResourceActions::SHOW);
$product = $this->findOr404($configuration);
$locale = $request->getLocale();
$defaultlocale = $request->getDefaultLocale();
$mode = 'product';
// some custom provider service to retrieve recommended products
//$recommendationService = $this->get('app.provider.product');
//$recommendedProducts = $recommendationService->getRecommendedProducts($product);
$this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $product);
$AttributesByCode[] = array();
$taxons = $product->getTaxons();
$regions = array ();
$pays = "";
foreach ($taxons as $key => $taxonValue) {
//On restructure
$root = $taxonValue->getRoot();
if ($root->getCode() == "pays" && $taxonValue->getLevel() == 2)
{
$regions[] = $taxonValue->getName() ;
}
if ($root->getCode() == "categorie" && $taxonValue->getCode() == "cadeau")
{
$mode = 'gift';
}
if ($root->getCode() == "pays" && $taxonValue->getLevel() == 1)
{
$pays = $taxonValue->getName() ;
}
}
$regions_str = implode (" / ", $regions);
/*
{% for ancestor in ancestors %}
{% if ancestor.isRoot() or not ancestor.enabled %}
<div class="section">{{ ancestor.name }}</div>
{% else %}
<a href="{{ path('sylius_shop_product_index', {'slug': ancestor.slug, '_locale': ancestor.translation.locale}) }}" class="section">{{ ancestor.name }}</a>
{% endif %}
<div class="divider"> / </div>
{% endfor %}*/
$attributes = $product->getAttributesByLocale($locale, $defaultlocale);
foreach ($attributes as $key => $attributevalue){
$attribute = $attributevalue->getAttribute();
$typeAttribute = $attribute->getType();
if ($typeAttribute == "select")
{
$valuechoice = array();
$configurationA = $attribute->getConfiguration();
$choices = $configurationA["choices"];
foreach ($attributevalue->getValue() as $keyc => $value){
if (array_key_exists($locale, $choices[$value]))
$valuechoice[] = $choices[$value][$locale];
else
$valuechoice[] = $choices[$value]["fr"];
}
$value = implode ( ", " , $valuechoice);
$AttributesByCode[$attribute->getCode()] = array("value" => $value, "name" => $attributevalue->getName(), "attribute" => $attributevalue);
}
else
{
$AttributesByCode[$attribute->getCode()] = array("value" => $attributevalue->getValue(), "name" => $attributevalue->getName(), "attribute" => $attributevalue);
}
}
// if ($mode=="gift")
// $template = "@SyliusShop/Product/gift.html.twig";
// else
$template = $configuration->getTemplate(ResourceActions::SHOW . '.html');
if ($configuration->isHtmlRequest()) {
return $this->render($template, [
'configuration' => $configuration,
'metadata' => $this->metadata,
'resource' => $product,
'mode' => $mode,
'SimpleAttributes' => $AttributesByCode,
'regions' => $regions_str,
'pays_origine' => $pays,
$this->metadata->getName() => $product,
]);
}
return $this->createRestView($configuration, $product);
}
}