<?php
namespace App\Controller;
use App\Entity\Color;
use App\Entity\ColorCategory;
use App\Entity\Product;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class FrontController extends AbstractController
{
/**
* @Route("/", name="front_landing")
*/
public function landing(Request $request)
{
$repoColorCategory = $this->getDoctrine()->getRepository(ColorCategory::class);
$colorCategories = $repoColorCategory->findBy([], ["position" => "asc"]);
return $this->render('front/landing.html.twig', array(
"colorCategories" => $colorCategories
));
}
/**
* @Route("/mentions-legales", name="front_mentions")
*/
public function mentions(Request $request)
{
return $this->render('front/mentions.html.twig', array(
));
}
/**
* @Route("/products", name="front_ajax_products")
*/
public function ajaxProducts(Request $request)
{
$id = $request->request->get('id', null);
$repoColor = $this->getDoctrine()->getRepository(Color::class);
$color = $repoColor->find($id);
$template = null;
if ($color) {
$status = 'success';
$repoProduct = $this->getDoctrine()->getRepository(Product::class);
$products = $repoProduct->findBy(["color" => $color], ["position" => "asc"]);
$productsClean = [];
foreach ($products as $key => $product) {
$brand = $product->getBrand();
$brandId = $brand->getId();
$productsClean[$brandId]["brand"] = $brand;
$productsClean[$brandId]["products"][] = $product;
}
usort($productsClean, function ($a, $b) {
return (($a["brand"]->getPosition() > $b["brand"]->getPosition()) ? 1 : -1);
});
$template = $this->renderView('front/products.html.twig', array(
"productsClean" => $productsClean
));
} else {
$status = "failed";
}
$response = new Response(json_encode(array('status' => $status, "template" => $template)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}