src/Controller/FrontController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Color;
  4. use App\Entity\ColorCategory;
  5. use App\Entity\Product;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class FrontController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/", name="front_landing")
  14.      */
  15.     public function landing(Request $request)
  16.     {
  17.         $repoColorCategory $this->getDoctrine()->getRepository(ColorCategory::class);
  18.         $colorCategories $repoColorCategory->findBy([], ["position" => "asc"]);
  19.         return $this->render('front/landing.html.twig', array(
  20.             "colorCategories" => $colorCategories
  21.         ));
  22.     }
  23.     /**
  24.      * @Route("/mentions-legales", name="front_mentions")
  25.      */
  26.     public function mentions(Request $request)
  27.     {
  28.         return $this->render('front/mentions.html.twig', array(
  29.         ));
  30.     }
  31.     /**
  32.      * @Route("/products", name="front_ajax_products")
  33.      */
  34.     public function ajaxProducts(Request $request)
  35.     {
  36.         $id $request->request->get('id'null);
  37.         $repoColor $this->getDoctrine()->getRepository(Color::class);
  38.         $color $repoColor->find($id);
  39.         $template null;
  40.         if ($color) {
  41.             $status 'success';
  42.             $repoProduct $this->getDoctrine()->getRepository(Product::class);
  43.             $products $repoProduct->findBy(["color" => $color], ["position" => "asc"]);
  44.             $productsClean = [];
  45.             foreach ($products as $key => $product) {
  46.                 $brand $product->getBrand();
  47.                 $brandId $brand->getId();
  48.                 $productsClean[$brandId]["brand"] = $brand;
  49.                 $productsClean[$brandId]["products"][] = $product;
  50.             }
  51.             usort($productsClean, function ($a$b) {
  52.                 return (($a["brand"]->getPosition() > $b["brand"]->getPosition()) ? : -1);
  53.             });
  54.             $template $this->renderView('front/products.html.twig', array(
  55.                 "productsClean" => $productsClean
  56.             ));
  57.         } else {
  58.             $status "failed";
  59.         }
  60.         $response = new Response(json_encode(array('status' => $status"template" => $template)));
  61.         $response->headers->set('Content-Type''application/json');
  62.         return $response;
  63.     }
  64. }