<?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\ProductVariantRepositoryInterface;
use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
use App\Entity\SiteCollection;
use App\Entity\SiteCollectionTranslation;
use App\Entity\SiteProductOptionValueMatch;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Services\PPMCErpSynchroService;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Page Collection
*/
class SiteCollectionController extends AbstractController
{
public const CATEGORIES = [
'fr_FR' => [
'children' => 'enfant',
'classic' => 'classique',
'temporary' => 'temporaire'
],
'en_US' => [
'children' => 'children',
'classic' => 'classic',
'temporary' => 'temporary'
]
];
public const CATEGORIESREVERT = [
'fr_FR' => [
'enfant' => 'children',
'classique' => 'classic',
'temporaire' => 'temporary'
],
'en_US' => [
'children' => 'children',
'classic' => 'classic',
'temporary' => 'temporary'
]
];
public function indexAction(
Breadcrumbs $breadcrumbs,
LocaleContextInterface $localeContext,
)
{
$em = $this->get('doctrine')->getManager();
$collectionsTemporary = $em->getRepository(SiteCollection::class)->findBy(
['category' => 'temporary', 'enabled' => true, 'visible' => false],
['dateRelease' => 'DESC'],
4
);
$collectionsClassic = $em->getRepository(SiteCollection::class)->findBy(
['category' => 'classic', 'enabled' => true, 'visible' => false],
['dateRelease' => 'DESC'],
4
);
$collectionsChildren = $em->getRepository(SiteCollection::class)->findBy(
['category' => 'children', 'enabled' => true, 'visible' => false],
['dateRelease' => 'DESC'],
4
);
$breadcrumbs->addRouteItem("sylius.ui.home", "sylius_shop_homepage");
$breadcrumbs->addItem("Collections");
return $this->render('site_collection_index.html.twig', [
'collectionsTemporary' => $collectionsTemporary,
'collectionsClassic' => $collectionsClassic,
'collectionsChildren' => $collectionsChildren,
'linkscat' => $this::CATEGORIES[$localeContext->getLocaleCode()]
]);
}
public function categoryAction(
Breadcrumbs $breadcrumbs,
$slugCategory,
TranslatorInterface $translator,
LocaleContextInterface $localeContext,
)
{
if(isset($this::CATEGORIESREVERT[$localeContext->getLocaleCode()][$slugCategory])){
$slugBase = $this::CATEGORIESREVERT[$localeContext->getLocaleCode()][$slugCategory];
}else{
$slugBase = $this::CATEGORIES[$localeContext->getLocaleCode()][$slugCategory];
return $this->redirectToRoute('app_collection_category', ['slugCategory' => $slugBase]);
}
// dump($slugBase);
$em = $this->get('doctrine')->getManager();
$collectionsCategory = $em->getRepository(SiteCollection::class)->findBy(
['category' => $slugBase, 'enabled' => true, 'visible' => false],
['dateRelease' => 'DESC']
);
$breadcrumbs->addRouteItem("sylius.ui.home", "sylius_shop_homepage");
$breadcrumbs->addRouteItem("Collections", "app_collection_index");
$breadcrumbs->addItem("app.form.site_collection_category.". $slugBase);
return $this->render('site_collection_category.html.twig', [
'collectionsCategory' => $collectionsCategory,
'title' => $translator->trans("app.form.site_collection_category.". $slugBase).'s',
'slug' => $slugCategory,
// 'linkscat' => $slugCategory
]);
}
public function pageCollectionAction(
Request $request,
Breadcrumbs $breadcrumbs,
ChannelContextInterface $channelContext,
LocaleContextInterface $localeContext,
ProductVariantRepositoryInterface $productVariantRepository,
TranslatorInterface $translator,
$slugCategory,
$slug
) {
$em = $this->get('doctrine')->getManager();
$collectionTranslationRepository = $em->getRepository(SiteCollectionTranslation::class);
$collectionTranslation = $collectionTranslationRepository->findOneBySlug($slug);
$collection = $collectionTranslation->getTranslatable();
if (empty($collectionTranslation)) {
throw new NotFoundHttpException("Cette collection n'existe pas.");
}
$isAdmin = intval($request->query->get('admin'));
if ($collection->getEnabled() == false && $isAdmin == 0) {
throw new NotFoundHttpException("Cette collection est désactivée");
}
if(isset($this::CATEGORIESREVERT[$localeContext->getLocaleCode()][$slugCategory])){
$slugBase = $this::CATEGORIESREVERT[$localeContext->getLocaleCode()][$slugCategory];
}else{
$slugBase = $this::CATEGORIES[$localeContext->getLocaleCode()][$slugCategory];
return $this->redirectToRoute('app_collection_show', ['slugCategory' => $slugBase, 'slug' => $slug]);
}
// dump( $this::CATEGORIESREVERT[$localeContext->getLocaleCode()]);
// dump($slugCategory);
$breadcrumbs->addRouteItem("sylius.ui.home", "sylius_shop_homepage");
$breadcrumbs->addRouteItem("Nos collections", "app_collection_index");
$breadcrumbs->addRouteItem($translator->trans("app.form.site_collection_category.". $slugBase), "app_collection_category", [
'slugCategory' => $slugCategory
]);
$breadcrumbs->addItem($collectionTranslation->getTitle());
$channel = $channelContext->getChannel();
$locale = $localeContext->getLocaleCode();
// Récupère les déclinaisons de cette collection
$variants = $productVariantRepository->findVariantsByColors(
$channel,
$locale,
[$collection->getProductOptionValue()]
);
$complementaryFabric = null;
if (count($variants) > 0) {
// Récupère un produit tissu s'il existe avec la même couleur
// pour l'afficher en bas de page
$complementaryFabric = $productVariantRepository->findOneFabricForThisProductVariant(
$channel,
$variants[0],
$locale
);
}
// Récupère les collections qui matchent (5 maximum)
$em = $this->get('doctrine')->getManager();
$matchingColors = $em->getRepository(SiteProductOptionValueMatch::class)
->findByProductOptionValue($collection->getProductOptionValue());
$collectionMatchs = $em->getRepository(SiteCollection::class)->findBy([
'productOptionValue' => array_map(function ($row) {
return $row->getMatchedProductOptionValue()->getId();
}, $matchingColors)
], [], 5);
// Pour chaque collection qui matchent, on récupère 5 produits finaux
$variantsMatch = [];
foreach ($collectionMatchs as $row) {
$rowVariants = $productVariantRepository->findVariantsByColors(
$channel,
$locale,
[$row->getProductOptionValue()],
5
);
$variantsMatch[$row->getId()] = $rowVariants;
}
return $this->render('site_collection_show.html.twig', [
'collection' => $collection,
'collectionTranslation' => $collectionTranslation,
'complementaryFabric' => $complementaryFabric,
'variants' => $variants,
'collectionMatchs' => $collectionMatchs,
'variantsMatch' => $variantsMatch,
'type' => $translator->trans("app.form.site_collection_category.". $slugBase)
]);
}
public function syncImageAction(
ContainerInterface $container,
PPMCErpSynchroService $ppmcErpSynchroService,
$idCollection
) {
$em = $this->get('doctrine')->getManager();
$collectionRepository = $em->getRepository(SiteCollection::class);
$collection = $collectionRepository->findOneById($idCollection);
$dones = $ppmcErpSynchroService->syncCollectionImages($container, $collection->getProductOptionValue()->getCode());
$this->get('session')->getFlashBag()->add('success', $dones['variantsDone'].' variants synchronisés');
$this->get('session')->getFlashBag()->add('success', $dones['imagesDone'].' images synchronisées');
// dump($collection->getProductOptionValue()->getCode());
return $this->redirectToRoute('app_admin_site_collection_index');
}
}