src/Controller/API/ApiController.php line 301

Open in your IDE?
  1. <?php
  2. namespace App\Controller\API;
  3. use App\Entity\Attachment;
  4. use App\Entity\Collection;
  5. use App\Entity\Color;
  6. use App\Entity\Country;
  7. use App\Entity\KindProject;
  8. use App\Entity\Newsletter;
  9. use App\Entity\Product;
  10. use App\Entity\Project;
  11. use App\Entity\Section;
  12. use App\Entity\Shape;
  13. use App\Entity\User;
  14. use App\Service\DocumentFileUploader;
  15. use App\Service\FilePathAndUrlService;
  16. use App\Service\LocaleService;
  17. use App\Service\SearchService;
  18. use App\Traits\AttachmentUrls;
  19. use MailchimpTransactional\ApiClient;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\HttpFoundation\JsonResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  25. use Symfony\Component\Intl\Countries;
  26. use Symfony\Component\Intl\Languages;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Symfony\Component\Validator\Constraints\Json;
  29. use Symfony\Component\Validator\Validator\ValidatorInterface;
  30. use Symfony\Contracts\Translation\TranslatorInterface;
  31. use Symfony\Component\Validator\Constraints\Email as ConstraintEmail;
  32. class ApiController extends AbstractController
  33. {
  34.     use AttachmentUrls;
  35.     private $translator;
  36.     private $localeService;
  37.     private $filePathAndUrlService;
  38.     protected $documentFileUploader;
  39.     private $translatorService;
  40.     public function __construct(TranslatorInterface $translatorLocaleService $localeService$translatorService,
  41.                                 FilePathAndUrlService $filePathAndUrlServiceDocumentFileUploader $documentFileUploader)
  42.     {
  43.         $this->translator $translator;
  44.         $this->localeService $localeService;
  45.         $this->filePathAndUrlService $filePathAndUrlService;
  46.         $this->documentFileUploader $documentFileUploader;
  47.         $this->translatorService $translatorService;
  48.     }
  49.     /**
  50.      * @Route("/translations/{locale}", name="translations", methods={"GET"})
  51.      */
  52.     public function getAllTranslations($locale$noJSON false) {
  53.         $this->translatorService->setLocale($locale);
  54.         $translations $this->translatorService->getCatalogue()->all('messages');
  55.         if ($noJSON)
  56.             return $translations;
  57.         return new JsonResponse(['translations' => $translations]);
  58.     }
  59.     /**
  60.      * @Route("/urls/language/{locale}", name="route_links", methods={"GET"})
  61.      */
  62.     public function getAllRouteLink($locale) {
  63.         return new JsonResponse([
  64.             'routes' => [
  65.                 'home' => $this->generateUrl('pages_home_index', ['_locale' => $locale]),
  66.                 'projects' => $this->generateUrl('pages_projects_overview', ['_locale' => $locale]),
  67.                 'projectsFilterByProduct' => $this->generateUrl('pages_projects_filter_by_products', ['_locale' => $locale'productSlug' => 'productSlug']),
  68.                 'projectsFilterByCountry' => $this->generateUrl('pages_projects_filter_by_countries', ['_locale' => $locale'alpha2' => 'alpha2']),
  69.                 'projectsFilterByKindProject' => $this->generateUrl('pages_projects_filter_by_kind_project', ['_locale' => $locale'kindProjectSlug' => 'kindProjectSlug']),
  70.                 'products' => $this->generateUrl('pages_products_overview', ['_locale' => $locale]),
  71.                 'productsFilterByColor' => $this->generateUrl('pages_products_filter_by_colors', ['_locale' => $locale'colorSlug' => 'colorSlug']),
  72.                 'productsFilterByShape' => $this->generateUrl('pages_products_filter_by_shapes', ['_locale' => $locale'shapeSlug' => 'shapeSlug']),
  73.                 'productsFilterByProduct' => $this->generateUrl('pages_products_filter_by_products', ['_locale' => $locale'productSlug' => 'productSlug']),
  74.                 'productsFilterByCollection' => $this->generateUrl('pages_products_filter_by_collection', ['_locale' => $locale'collectionSlug' => 'collectionSlug']),
  75.                 'collections' => $this->generateUrl('pages_collections_overview', ['_locale' => $locale]),
  76.                 '3DSimulation' => $this->generateUrl('pages_3d_simulation_index', ['_locale' => $locale]),
  77.                 'samples' => $this->generateUrl('pages_sample_index', ['_locale' => $locale]),
  78.                 'contact' => 'https://carpet-tiles.jp/ja/pages/contact',
  79.             ]
  80.         ]);
  81.     }
  82.     /**
  83.      * @Route("/technical-details/product/{name}/language/{locale}", name="technical_details", methods={"GET"})
  84.      */
  85.     public function getTechnicalDetails($name$localeRequest $request) {
  86.         $product =  $this->getDoctrine()->getRepository(Product::class)->findOneBy(['name' => $name]);
  87.         $request->setLocale($locale);
  88.         if (! $product) {
  89.             return new JsonResponse([
  90.                 'error' => 'no product find with name : ' $name
  91.             ]);
  92.         }
  93.         $collection $product->getCollection();
  94.         $format $collection->getTile() ? $this->translator->trans('Available all',[],null$locale) : $this->translator->trans('Available roll',[],null$locale);
  95.         $array = [
  96.             $collection->ismonolithic() ? $this->translator->trans('Monolithic',[],null,$locale) : null,
  97.             $collection->isBrick() ? $this->translator->trans('Brick',[],null$locale) : null,
  98.             $collection->isAshlar()? $this->translator->trans('Ashlar',[],null$locale) : null];
  99.         $installation '';
  100.         foreach ($array as $item) {
  101.             if ($item)
  102.                 $installation $item .', ';
  103.         }
  104.         if (strlen($installation) > 2)
  105.             $installation substr($installation,0, -2);
  106.         return new JsonResponse([
  107.             'technical-details-translation' => $this->translator->trans('Technical details',[],null$locale),
  108.             'technical-details' => [
  109.                 'format-translation' => $this->translator->trans('Format',[],null$locale),
  110.                 'format' => $format,
  111.                 'installation-translation' =>  $this->translator->trans('Installation',[],null$locale),
  112.                 'installation' => $installation,
  113.                 'composition-translation' => $this->translator->trans('Composition',[],null$locale),
  114.                 'composition' => $this->translator->trans('Composition description',[],null$locale),
  115.                 'warranty-translation' => $this->translator->trans('Warranty',[],null$locale),
  116.                 'warranty' => $this->translator->trans('Warranty 10 years',[],null$locale),
  117.                 'total-thickness-translation' => $this->translator->trans('Total thickness',[],null$locale),
  118.                 'total-thickness' => $this->translator->trans('Total thickness description',[],null$locale),
  119.                 'total-weight-translation' => $this->translator->trans('Total weight',[],null$locale),
  120.                 'total-weight' => $this->translator->trans('Total weight description',[],null$locale),
  121.                 'tile-size-translation' => $this->translator->trans('Tile size',[],null$locale),
  122.                 'tile-size' => $this->translator->trans('Tile size description',[],null$locale),
  123.                 'level-of-use-translation' => $this->translator->trans('Level of use',[],null$locale),
  124.                 'level-of-use' => $this->translator->trans('Level of use description',[],null$locale),
  125.                 'reaction-to-fire-translation' => $this->translator->trans('Reaction to fire',[],null$locale),
  126.                 'reaction-to-fire' => $this->translator->trans('Reaction to fire description',[],null$locale),
  127.                 'impact-sound-reduction-translation' => $this->translator->trans('Impact sound reduction',[],null$locale),
  128.                 'impact-sound-reduction' => $this->translator->trans('Impact sound reduction description',[],null$locale),
  129.             ],
  130.         ]);
  131.     }
  132.     /**
  133.      * @Route("/currentLanguage", name="get_currentLocale", methods={"GET"})
  134.      */
  135.     public function getCurrentLocale(Request $requestSessionInterface $sessiont$noJSON false) {
  136.         $session $request->getSession();
  137.         $session->start();
  138.         $sessiont->start();
  139.         if ($noJSON)
  140.             return $request->getLocale();
  141.         return new JsonResponse(['currentLocale' => $request->getLocale()]);
  142.     }
  143.     /**
  144.      * @Route("/languages", name="get_locales", methods={"GET"})
  145.      */
  146.     public function getAllLocales($noJSON false)
  147.     {
  148.         if ($noJSON)
  149.             return $this->localeService->getLocales();
  150.         return new JsonResponse(['currentLocale' => $this->localeService->getLocales()]);
  151.     }
  152.     /**
  153.      * @Route("/products/genericPDFs", name="get_generic_pdfs", methods={"GET","POST"})
  154.      */
  155.     public function getGenericPDFs(Request $request$noJSON false) {
  156.         $productsId json_decode($request->getContent())->products;
  157.         $products = [];
  158.         if (is_array($productsId)) {
  159.             foreach ($productsId as $productId) {
  160.                 $products[] = $this->getDoctrine()->getRepository(Product::class)->find($productId);
  161.             }
  162.             if ($noJSON)
  163.                 return $this->documentFileUploader->getGenericPDF($products$request);
  164.             return $this->sendResponse(['documents' => $this->documentFileUploader->getGenericPDF($products$request)]);
  165.         }
  166.         return new JsonResponse([
  167.             'error' => 'wrong data format pass to the API.'
  168.         ]);
  169.     }
  170.     /**
  171.      * @Route("/{locale}/countries", methods={"GET"})
  172.      */
  173.     public function getCountries(Request $request$locale$noJSON false) {
  174.         $countries Countries::getNames($locale);
  175.         $result = [];
  176.         $i 0;
  177.         foreach ($countries as $key => $country)   {
  178.             $result[$i]['name'] = $country;
  179.             $result[$i]['id'] = $i;
  180.             $result[$i]['alpha2'] = $key;
  181.             $i++;
  182.         }
  183.         if ($noJSON)
  184.             return $result;
  185.         return $this->sendResponse(['countries' => $result]);
  186.     }
  187.     /**
  188.      * @Route("/{locale}/2tec2Countries", name="get_all_countries", methods={"GET"})
  189.      */
  190.     public function getAllCountries($locale$noJSON false) {
  191.         $activeCountries $this->getDoctrine()->getRepository(Country::class)->findCountryWithProjects();
  192.         $countries Countries::getNames($locale);
  193.         $result = [];
  194.        //dd([$activeCountries, $countries]);
  195.         foreach ($countries as $alphaTwo => $country) {
  196.             foreach ($activeCountries as $activeCountry) {
  197.                 if ($alphaTwo === $activeCountry->getAlpha2()) {
  198.                     $activeCountry->setName($country);
  199.                     $result[] = $activeCountry;
  200.                 }
  201.             }
  202.         }
  203.         if ($noJSON)
  204.             return  $result;
  205.         return $this->sendResponse(['countries' => $result]);
  206.     }
  207.     /**
  208.      * @Route("/{locale}/colors", name="get_all_colors", methods={"GET"})
  209.      */
  210.     public function getAllColors($locale$noJSON false) {
  211.         $colors $this->localeService->getAllEntitiesTranslated(Color::class, $locale);
  212.         if ($noJSON)
  213.             return $colors;
  214.         return $this->sendResponse(['colors' => $colors]);
  215.     }
  216.     /**
  217.      * @Route("/{locale}/shapes", name="get_all_shapes", methods={"GET"})
  218.      */
  219.     public function getAllShapes($locale$noJSON false) {
  220.         $shapes $this->localeService->getAllEntitiesTranslated(Shape::class, $locale);
  221.         if ($noJSON)
  222.             return $shapes;
  223.         return $this->sendResponse(['shapes' => $shapes]);
  224.     }
  225.     /**
  226.      * @Route("/{locale}/projects/filter/country/{alpha2}", methods={"GET"})
  227.      */
  228.     public function getProjectsFilterByCountry($locale$alpha2$noJSON false) {
  229.         $country $this->getDoctrine()->getRepository(Country::class)->findOneBy(['alpha2' => strtoupper($alpha2)]);
  230.         $projects = [];
  231.         foreach ($this->getDoctrine()->getRepository(Project::class)->findBy(['country' => $country->getId()]) as $project) {
  232.             if ($project->getActif())
  233.                 $projects[] = $project;
  234.         }
  235.         usort($projects, function($a$b) {
  236.             return $a->getCreatedAt() < $b->getCreatedAt();
  237.         });
  238.         $this->setAttachmentUrls($projects$this->filePathAndUrlService);
  239.         $this->localeService->setEntitiesTranslated($projects$locale);
  240.         if ($noJSON)
  241.             return $projects;
  242.         return $this->sendResponse(['projects' => $projects]);
  243.     }
  244.     /**
  245.      * @Route("/{locale}/projects/filter/product/{productSlug}", methods={"GET"})
  246.      */
  247.     public function getProjectsFilterByProduct($locale$productSlug$noJSON false) {
  248.         $projects = [];
  249.         $allProjects $this->localeService->getAllEntitiesTranslated(Project::class, $locale);
  250.         $currentProduct $this->getDoctrine()->getRepository(Product::class)->projectsFilterByProduct($productSlug);
  251.         foreach ($allProjects as $project){
  252.             foreach ($project->getProducts() as $product) {
  253.                 if ($product->getId() === $currentProduct->getId() && $project->getActif()) {
  254.                     $projects[] = $project;
  255.                 }
  256.             }
  257.         }
  258.         usort($projects, function($a$b) {
  259.             return $a->getCreatedAt() < $b->getCreatedAt();
  260.         });
  261.         $this->setAttachmentUrls($projects$this->filePathAndUrlService);
  262.         if ($noJSON)
  263.             return $projects;
  264.         return $this->sendResponse(['projects' => $projects]);
  265.     }
  266.     /**
  267.      * @Route("/{locale}/projects/filter/kindProject/{kindProjectSlug}", methods={"GET"})
  268.      */
  269.     public function getProjectsFilterByKindProjects($locale$kindProjectSlug$noJSON false) {
  270.         $projects = array();
  271.         $currentKind null;
  272.         $allProjects $this->localeService->getAllEntitiesTranslated(Project::class, $locale);
  273.         foreach ($this->getAllKindProjects($localetrue) as $kind) {
  274.             if ($kind->getSlug() === $kindProjectSlug)
  275.                 $currentKind $kind;
  276.         }
  277.         foreach ($allProjects as $project) {
  278.             if ($currentKind !== null && $project->getKindProject()->getId() === $currentKind->getId() && $project->getActif()) {
  279.                 $projects[] = $project;
  280.             }
  281.         }
  282.         usort($projects, function($a$b) {
  283.             return $a->getCreatedAt() < $b->getCreatedAt();
  284.         });
  285.         $this->setAttachmentUrls($projects$this->filePathAndUrlService);
  286.         if ($noJSON)
  287.             return $projects;
  288.         return $this->sendResponse(['projects' => $projects]);
  289.     }
  290.     /**
  291.      * @Route("/{locale}/projects", name="get_all_projects", methods={"GET"})
  292.      */
  293.     public function getAllProjects($locale$noJSON false) {
  294.         $projects = [];
  295.         foreach ($this->localeService->getAllEntitiesTranslated(Project::class, $locale) as $project) {
  296.             if ($project->getActif())
  297.                 $projects[] = $project;
  298.         }
  299.         usort($projects, function($a$b) {
  300.             return $a->getCreatedAt() < $b->getCreatedAt();
  301.                 });
  302.         $this->setAttachmentUrls($projects$this->filePathAndUrlService);
  303.         if ($noJSON)
  304.             return $projects;
  305.         return $this->sendResponse(['projects' => $projects]);
  306.     }
  307.     /**
  308.      * @Route("/{locale}/products/filter/shapes/{shapeSlug}", methods={"GET"})
  309.      */
  310.     public function getProductsFilterByShape($locale$shapeSlug$noJSON false)
  311.     {
  312.         $products = [];
  313.         $allProducts $this->localeService->getAllEntitiesTranslated(Product::class, $locale);
  314.         if ($shapeSlug !== 'rolls') {
  315.             $collections $this->getDoctrine()->getRepository(Collection::class)->findBy(['tile' => 1]);
  316.         } else {
  317.             $collections $this->getDoctrine()->getRepository(Collection::class)->findAll();
  318.         }
  319.         foreach ($allProducts as $product) {
  320.             foreach ($collections as $collection) {
  321.                 if ($collection === $product->getCollection() && $product->getActif() && $product->getCollection()->getActif()) {
  322.                     $products[] = $product;
  323.                 }
  324.             }
  325.         }
  326.         $this->setAttachmentUrls($products$this->filePathAndUrlService);
  327.         if ($noJSON)
  328.             return $products;
  329.         return $this->sendResponse(['products' => $products]);
  330.     }
  331.     /**
  332.      * @Route("/{locale}/products/filter/collection/{collectionSlug}", methods={"GET"})
  333.      */
  334.     public function getProductsFilterByCollection($locale$collectionSlug$noJSON false) {
  335.         $products = [];
  336.         $allProducts $this->localeService->getAllEntitiesTranslated(Product::class, $locale);
  337.         $currentCollection $this->getDoctrine()->getRepository(Collection::class)->findOneBy(['slug' => $collectionSlug]);
  338.         foreach ($allProducts as $product) {
  339.             if ($currentCollection->getId() === $product->getCollection()->getId() && $product->getActif() && $product->getCollection()->getActif()) {
  340.                 $products[] = $product;
  341.             }
  342.         }
  343.         $this->setAttachmentUrls($products$this->filePathAndUrlService);
  344.         if ($noJSON)
  345.             return $products;
  346.         return $this->sendResponse(['products' => $products]);
  347.     }
  348.     /**
  349.      * @Route("/{locale}/products/filter/color/{colorSlug}", methods={"GET"})
  350.      */
  351.     public function getProductsFilterByColor($locale$colorSlug$noJSON false) {
  352.         $products = [];
  353.         $currentColor null;
  354.         $allProducts $this->localeService->getAllEntitiesTranslated(Product::class, $locale);
  355.         $colors $this->getAllColors($localetrue);
  356.         foreach ($colors as $color) {
  357.             if ($color->getSlug() === $colorSlug)
  358.                 $currentColor $color;
  359.         }
  360.         foreach ($allProducts as $product) {
  361.             foreach ($product->getColors() as $color) {
  362.                 if ($currentColor !== null &&  $currentColor->getId() === $color->getId() && $product->getActif() && $product->getCollection()->getActif()) {
  363.                     $products[] = $product;
  364.                 }
  365.             }
  366.         }
  367.         $this->setAttachmentUrls($products$this->filePathAndUrlService);
  368.         if ($noJSON)
  369.             return $products;
  370.         return $this->sendResponse(['products' => $products]);
  371.     }
  372.     /**
  373.      * @Route("/{locale}/products/filter/product/{productSlug}", methods={"GET"})
  374.      */
  375.     public function getProductsFilterByproduct($locale$productSlug$noJSON false) {
  376.         $product $this->getDoctrine()->getRepository(Product::class)->findOneBy(['slug' => $productSlug]);
  377.         $product =  $this->localeService->getEntityTranslated(Product::class, $product->getId(), $locale);
  378.         $products[] = $product;
  379.         $this->setAttachmentUrls($products$this->filePathAndUrlService);
  380.         if ($noJSON)
  381.             return $products;
  382.         return $this->sendResponse(['products' => $products]);
  383.     }
  384.     /**
  385.      * @Route("/{locale}/products/orderBy/appearance", methods={"GET"})
  386.      */
  387.     public function getAllProductsOrderByAppearance($localeRequest $request$noJSON false) {
  388.         $products = [];
  389.         foreach ($this->localeService->getAllEntitiesTranslated(Product::class, $locale) as $product) {
  390.             if($product->getActif() && $product->getCollection()->getActif())
  391.              $products[] = $product;
  392.         }
  393.         $this->setAttachmentUrls($products$this->filePathAndUrlService);
  394.         usort($products, function($a$b) {
  395.             return $a->getOrderAppearance() - $b->getOrderAppearance();
  396.         });
  397.         if ($noJSON)
  398.             return $products;
  399.         return $this->sendResponse(['products' => $products]);
  400.     }
  401.     /**
  402.      * @Route("/{locale}/products", methods={"GET"})
  403.      */
  404.     public function getAllProducts($locale$noJSON false) {
  405.         $products = [];
  406.         foreach ($this->localeService->getAllEntitiesTranslated(Product::class, $locale) as $product) {
  407.             if($product->getActif() && $product->getCollection()->getActif())
  408.                 $products[] = $product;
  409.         }
  410.         $this->setAttachmentUrls($products$this->filePathAndUrlService);
  411.         if ($noJSON)
  412.             return $products;
  413.         return $this->sendResponse(['products' => $products]);
  414.     }
  415.     /**
  416.      * @Route("/{locale}/products/orderBy/name", methods={"GET"})
  417.      */
  418.     public function getAllProductsOrderByName($localeRequest $request$noJSON false) {
  419.         $products = [];
  420.         foreach ($this->localeService->getAllEntitiesTranslated(Product::class, $locale) as $product) {
  421.             if($product->getActif() && $product->getCollection()->getActif())
  422.                 $products[] = $product;
  423.         }
  424.         $this->setAttachmentUrls($products$this->filePathAndUrlService);
  425.         if ($noJSON)
  426.             return $products;
  427.         return $this->sendResponse(['products' => $products]);
  428.     }
  429.     /**
  430.      * @Route("/{locale}/collections/orderBy/appearance", methods={"GET"})
  431.      */
  432.     public function getAllCollectionsOrderByAppearance($locale$noJSON false) {
  433.         $collections = [];
  434.         foreach ($this->localeService->getAllEntitiesTranslated(Collection::class, $locale) as $collection) {
  435.             if ($collection->getActif())
  436.                 $collections[] = $collection;
  437.         }
  438.         $this->setAttachmentUrls($collections$this->filePathAndUrlService);
  439.         usort($collections, function($a$b) {
  440.             return $a->getOrderAppearance() - $b->getOrderAppearance();
  441.         });
  442.         if ($noJSON)
  443.             return $collections;
  444.         return $this->sendResponse(['collections' => $collections]);
  445.     }
  446.     /**
  447.      * @Route("/{locale}/collections", methods={"GET"})
  448.      */
  449.     public function getAllCollections($locale$noJSON false) {
  450.         $collections = [];
  451.         foreach ($this->localeService->getAllEntitiesTranslated(Collection::class, $locale) as $collection) {
  452.             if ($collection->getActif())
  453.                 $collections[] = $collection;
  454.         }
  455.         $this->setAttachmentUrls($collections$this->filePathAndUrlService);
  456.         if ($noJSON)
  457.             return $collections;
  458.         return $this->sendResponse(['collections' => $collections]);
  459.     }
  460.     /**
  461.      * @Route("/{locale}/collections/orderBy/name", methods={"GET"})
  462.      */
  463.     public function getAllCollectionsOrderByName($locale$noJSON false) {
  464.         $collections = [];
  465.         foreach ($this->localeService->getAllEntitiesTranslated(Collection::class, $locale) as $collection) {
  466.             if ($collection->getActif())
  467.                 $collections[] = $collection;
  468.         }
  469.         $this->setAttachmentUrls($collections$this->filePathAndUrlService);
  470.         if ($noJSON)
  471.             return $collections;
  472.         return $this->sendResponse(['collections' => $collections]);
  473.     }
  474.     /**
  475.      * @Route("/{locale}/kindProjects", methods={"GET"})
  476.      */
  477.     public function getAllKindProjects($locale$noJSON false) {
  478.         $kindProjects $this->localeService->getAllEntitiesTranslated(KindProject::class, $locale);
  479.         usort($kindProjects, function($a$b) {
  480.             return strcmp($a->getName(), $b->getName());
  481.         });
  482.         if ($noJSON)
  483.             return $kindProjects;
  484.         return $this->sendResponse(['kindProjects' => $kindProjects]);
  485.     }
  486.     public function sendResponse($array) {
  487.         return new JsonResponse(
  488.             $array
  489.         Response::HTTP_OK);
  490.     }
  491.     /**
  492.      * @Route("/{locale}/sections", methods={"GET"})
  493.      * @param $locale
  494.      * @param bool $noJSON
  495.      * @return array|JsonResponse
  496.      */
  497.         public function getAllSections($locale$noJSON false)
  498.         {
  499.             $sections $this->localeService->getAllEntitiesTranslated(Section::class, $locale);
  500.             usort($sections, function ($a$b) {
  501.                 return strcmp($a->getId(), $b->getId());
  502.             });
  503.             if ($noJSON)
  504.                 return $sections;
  505.             return $this->sendResponse(['sections' => $sections]);
  506.         }
  507.     /**
  508.      * @Route("/{locale}/sections", methods={"GET"})
  509.      */
  510.     public function getAllSectionsOrderByID($locale$noJSON false)
  511.     {
  512.         $sections $this->localeService->getAllEntitiesTranslated(Section::class, $localetrue);
  513.         if ($noJSON)
  514.             return $sections;
  515.         return $this->sendResponse(['sections' => $sections]);
  516.     }
  517.     /**
  518.      * @Route("/{locale}/search", methods={"GET","POST"})
  519.      */
  520.     public function search($localeRequest $requestSearchService $searchService$noJSON false) {
  521.         $search json_decode($request->getContent());
  522.         if (strlen($search->search) < 4)
  523.             return new JsonResponse(null401);
  524.         $words explode(' 'strtolower($search->search));
  525.         if ($noJSON)
  526.             return $searchService->search($words$locale);
  527.         return new JsonResponse($searchService->search($words$locale));
  528.     }
  529.     /**
  530.      * @Route("/lastAttachment", methods={"GET"})
  531.      */
  532.     public function getLastAttachment() {
  533.         $result $this->getDoctrine()->getManager()->createQueryBuilder()->select('a.id')->from('App\\Entity\\Attachment''a')->orderBy('a.id''DESC')->setMaxResults(1)->getQuery()->getOneOrNullResult();
  534.         return new JsonResponse(
  535.            ['attachmentId' => ($result['id']  + 1)]
  536.         );
  537.     }
  538.     /**
  539.      * @Route("/getAttachments", methods={"GET"})
  540.      */
  541.     public function getAttachments(Request $request) {
  542.         $type $request->get('type');
  543.         $id $request->get('id');
  544.         if(!$id)
  545.             return new JsonResponse(['attachments' => []]);
  546.         $obj $this->getDoctrine()->getRepository("App\Entity\\".$type)->find($id);
  547.         $attachments $obj->getAttachments()->getValues();
  548.         $this->localeService->getAllTranslationsFromEntities($attachments);
  549.         return new JsonResponse(['attachments' => $attachments]);
  550.     }
  551.     /**
  552.      * @Route("/submitNewsletter/{email}", methods={"GET"})
  553.      */
  554.     public function submitNewsletter($emailRequest $requestValidatorInterface $validator) {
  555.         $emailConstraint = new ConstraintEmail();
  556.         $emailConstraint->message 'Please enter a valid email';
  557.         $errors $validator->validate($email$emailConstraint);
  558.         if ($errors->count() > 0)
  559.             return new JsonResponse([
  560.                 'error' => $errors
  561.             ], 400);
  562.         $user = new User();
  563.         $user->setEmail($email);
  564.         $user->setFirstName('Newsletter');
  565.         $user->setLastName('Newsletter');
  566.         $this->getDoctrine()->getManager()->persist($user);
  567.         $this->getDoctrine()->getManager()->flush();
  568.         $newsletter = new Newsletter($user$request->getLocale());
  569.         $this->getDoctrine()->getManager()->persist($newsletter);
  570.         $this->getDoctrine()->getManager()->flush();
  571.         return new JsonResponse(compact('email'));
  572.     }
  573. }