src/Controller/HomePageController.php line 178

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\AdvertisingBannerType;
  4. use App\Entity\StripeProduct;
  5. use App\Entity\Statistic;
  6. use App\Entity\Card;
  7. use App\Service\ArticleEntityService;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use FOS\HttpCacheBundle\Configuration\Tag;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Contracts\Cache\ItemInterface;
  18. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  19. /**
  20.  * @Route("/_homepage")
  21.  */
  22. class HomePageController extends AbstractController
  23. {
  24.     private ParameterBagInterface $parameterBag;
  25.     /**
  26.      * @param ParameterBagInterface $parameterBag
  27.      */
  28.     public function __construct(ParameterBagInterface $parameterBag)
  29.     {
  30.         $this->parameterBag $parameterBag;
  31.     }
  32.     /**
  33.      * @param ContainerInterface $container
  34.      */
  35.     /**
  36.      * @Route("/_highlighted", name="homepage.highlighted")
  37.      *
  38.      * @Cache(smaxage="600", public="true")
  39.      *
  40.      * @Tag("articles", "homepage", expression="'homepage.highlighted'")
  41.      */
  42.     public function highlighted(ArticleEntityService $articleService): Response
  43.     {
  44.         $highlighted $articleService->getHighlightedArticles(6);
  45.         return $this->render('themes/theme-5/home/_partials/highlighted.html.twig', [
  46.             'highlighted' => $highlighted,
  47.             'page' => 1,
  48.         ]);
  49.     }
  50.     /**
  51.      * @Route("/_kiosque", name="homepage.kiosque")
  52.      *
  53.      * @Cache(smaxage="600", public="true")
  54.      *
  55.      * @Tag("products", "homepage", expression="'homepage.kiosque'")
  56.      */
  57.     public function kiosque(EntityManagerInterface $managerTagAwareCacheInterface $myDedicatedCache): Response
  58.     {
  59.         $user $this->getUser();
  60.         $isProd = ($this->parameterBag->get('app_env') === 'prod');
  61.         $httpClient = \Symfony\Component\HttpClient\HttpClient::create(['timeout' => 2.0]);
  62.         $products $myDedicatedCache->get('homepage.kiosque_4', function (ItemInterface $item) use ($manager$isProd) {
  63.             $old_storage_url=$this->parameterBag->get('storage_url');
  64.             $old_storage_url str_replace('http://'''$old_storage_url);
  65.             $new_storage_url=$this->parameterBag->get('storage_url_front');
  66.             $new_storage_url str_replace('http://'''$new_storage_url);
  67.             $item->tag(['products''homepage''homepage.kiosque']);
  68.             $item->expiresAfter(3600);
  69.             $httpClient = \Symfony\Component\HttpClient\HttpClient::create(['timeout' => 2.0]);
  70.             $productkiosque$manager->getRepository(StripeProduct::class)->getMagazines(4);
  71.             foreach ($productkiosque as $key=> $p) {
  72.                 $productkiosque[$key]['url_front'] = '';
  73.                 $url =  $p['file']."_mobile_298_406";
  74.                 $old_url =  $p['file'];
  75.                 if ($isProd) {
  76.                     try {
  77.                         $response $httpClient->request('HEAD'$url);
  78.                         $statusCode $response->getStatusCode();
  79.                         $productkiosque[$key]['url'] = ($statusCode === 200) ? $url $old_url;
  80.                     } catch (\Throwable $e) {
  81.                         $productkiosque[$key]['url'] = $old_url;
  82.                     }
  83.                 } else {
  84.                     // In dev, avoid remote HEAD requests to DO Spaces to prevent timeouts
  85.                     $productkiosque[$key]['url'] = $old_url;
  86.                 }
  87.                 if(str_contains($productkiosque[$key]['url'],$old_storage_url)) {
  88.                     $url_front str_replace($old_storage_url,$new_storage_url,$productkiosque[$key]['url']);
  89.                     $productkiosque[$key]['url_front'] = $url_front;
  90.                 }
  91.             }
  92.             return $productkiosque;
  93.         });
  94.         $old_storage_url=$this->parameterBag->get('storage_url');
  95.         $old_storage_url str_replace('http://'''$old_storage_url);
  96.         $new_storage_url=$this->parameterBag->get('storage_url_front');
  97.         $new_storage_url str_replace('http://'''$new_storage_url);
  98.         foreach ($products as $key=> $p) {
  99.             $products[$key]['url_front'] = '';
  100.             $url =  $p['file']."_mobile_298_406";
  101.             $old_url =  $p['file'];
  102.             if ($isProd) {
  103.                 try {
  104.                     $response $httpClient->request('HEAD'$url);
  105.                     $statusCode $response->getStatusCode();
  106.                     $products[$key]['url'] = ($statusCode === 200) ? $url $old_url;
  107.                 } catch (\Throwable $e) {
  108.                     $products[$key]['url'] = $old_url;
  109.                 }
  110.             } else {
  111.                 $products[$key]['url'] = $old_url;
  112.             }
  113.             if(str_contains($products[$key]['url'],$old_storage_url)) {
  114.                 $url_front str_replace($old_storage_url,$new_storage_url,$products[$key]['url']);
  115.                 $products[$key]['url_front'] = $url_front;
  116.             }
  117.         }
  118.         return $this->render('themes/theme-5/home/kiosques.html.twig', [
  119.             'products' => $products,
  120.         ]);
  121.     }
  122.     
  123.     
  124.     /**
  125.      * @Route("/_produits", name="produits.kiosque")
  126.      *
  127.      * @Cache(smaxage="600", public="true")
  128.      *
  129.      * @Tag("products", "homepage", expression="'produits.kiosque'")
  130.      */
  131.     public function produits(EntityManagerInterface $managerTagAwareCacheInterface $myDedicatedCacheRequest $request): Response
  132.     {
  133.         
  134.         $products $myDedicatedCache->get('produits.kiosque', function (ItemInterface $item) use ($manager) {
  135.             $item->tag(['products''homepage''produits.kiosque']);
  136.             $item->expiresAfter(3600);
  137.             return $manager->getRepository(StripeProduct::class)->getMagazines(9true);
  138.         });
  139.         
  140.         $page $request->get('page');
  141.         $limit 20;
  142.         $pages ceil(count($products) / $limit) != ceil(count($products) / $limit) : null;
  143.         $products array_slice($products$limit*($page 1), $limit);
  144.         $html $this->renderView('product/products.html.twig', [
  145.             'products' => $products
  146.         ]);
  147.         return $this->json([
  148.             'html' => $html,
  149.             'pages' => $pages
  150.         ]);
  151.     }
  152.     /**
  153.      * @Route("/_podcasts", name="homepage.podcasts")
  154.      *
  155.      * @Cache(smaxage="600", public="true")
  156.      *
  157.      * @Tag("articles", "homepage", expression="'homepage.podcasts'")
  158.      */
  159.     public function podcasts(ArticleEntityService $articleService): Response
  160.     {
  161.         $videoPodcasts $articleService->getVideoPodcasts();
  162.         return $this->render('themes/theme-5/home/videos-podcasts.html.twig', [
  163.             'videoPodcasts' => $videoPodcasts,
  164.         ]);
  165.     }
  166.     /**
  167.      * @Route("/_recent", name="homepage.recent")
  168.      *
  169.      * @Cache(smaxage="600", public="true")
  170.      *
  171.      * @Tag("articles", "homepage", expression="'homepage.recent'")
  172.      */
  173.     public function recent(): Response
  174.     {
  175.         return $this->render('themes/theme-5/home/recent-articles.html.twig', [
  176.             'page' => 1,
  177.             'position' => 2,
  178.         ]);
  179.     }
  180.     /**
  181.      * @Route("/_recent_mobile", name="homepage.recent_mobile")
  182.      *
  183.      * @Cache(smaxage="600", public="true")
  184.      *
  185.      * @Tag("articles", "homepage", expression="'homepage.recent_mobile'")
  186.      */
  187.     public function recentMobile(ArticleEntityService $articleService): Response
  188.     {
  189.         $highlighted $articleService->getHighlightedArticles(6);
  190.         return $this->render('themes/theme-5/home/_partials/recent-article-coments-mobile.html.twig', [
  191.             'highlighted' => $highlighted,
  192.             'page' => 1,
  193.         ]);
  194.     }
  195.     /**
  196.      * @Route("/_taggage_kiosque", name="homepage.taggage_kiosque")
  197.      */
  198.     public function updateTaggage(Request $requestEntityManagerInterface $em): Response
  199.     {
  200.         $data json_decode($request->getContent(), true);
  201.         $clicksValue 0;
  202.         $name '';
  203.         if ($data && isset($data['name']) ) {
  204.             $name $data["name"];
  205.         }
  206.         $clicksKiosque $em->getRepository(Statistic::class)->findOneBy(['name'=> $name]);
  207.         $clicksValue intval($clicksKiosque->getValue());
  208.         if($name && $name == 'clicks_kiosque'){
  209.             $clicksValue $clicksValue 1;
  210.         }elseif ($name && $name == 'affiches_kiosque') {
  211.             $clicksValue $clicksValue 4;
  212.         }
  213.         $clicksKiosque->setValue($clicksValue);
  214.         $em->persist($clicksKiosque);
  215.         $em->flush();
  216.         return $this->json([
  217.             'clicksValue' => $clicksValue
  218.         ]);
  219.     }
  220. }