<?php
namespace App\Controller;
use App\Entity\AdvertisingBannerType;
use App\Entity\StripeProduct;
use App\Entity\Statistic;
use App\Entity\Card;
use App\Service\ArticleEntityService;
use Doctrine\ORM\EntityManagerInterface;
use FOS\HttpCacheBundle\Configuration\Tag;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
/**
* @Route("/_homepage")
*/
class HomePageController extends AbstractController
{
private ParameterBagInterface $parameterBag;
/**
* @param ParameterBagInterface $parameterBag
*/
public function __construct(ParameterBagInterface $parameterBag)
{
$this->parameterBag = $parameterBag;
}
/**
* @param ContainerInterface $container
*/
/**
* @Route("/_highlighted", name="homepage.highlighted")
*
* @Cache(smaxage="600", public="true")
*
* @Tag("articles", "homepage", expression="'homepage.highlighted'")
*/
public function highlighted(ArticleEntityService $articleService): Response
{
$highlighted = $articleService->getHighlightedArticles(6);
return $this->render('themes/theme-5/home/_partials/highlighted.html.twig', [
'highlighted' => $highlighted,
'page' => 1,
]);
}
/**
* @Route("/_kiosque", name="homepage.kiosque")
*
* @Cache(smaxage="600", public="true")
*
* @Tag("products", "homepage", expression="'homepage.kiosque'")
*/
public function kiosque(EntityManagerInterface $manager, TagAwareCacheInterface $myDedicatedCache): Response
{
$user = $this->getUser();
$isProd = ($this->parameterBag->get('app_env') === 'prod');
$httpClient = \Symfony\Component\HttpClient\HttpClient::create(['timeout' => 2.0]);
$products = $myDedicatedCache->get('homepage.kiosque_4', function (ItemInterface $item) use ($manager, $isProd) {
$old_storage_url=$this->parameterBag->get('storage_url');
$old_storage_url = str_replace('http://', '', $old_storage_url);
$new_storage_url=$this->parameterBag->get('storage_url_front');
$new_storage_url = str_replace('http://', '', $new_storage_url);
$item->tag(['products', 'homepage', 'homepage.kiosque']);
$item->expiresAfter(3600);
$httpClient = \Symfony\Component\HttpClient\HttpClient::create(['timeout' => 2.0]);
$productkiosque= $manager->getRepository(StripeProduct::class)->getMagazines(4);
foreach ($productkiosque as $key=> $p) {
$productkiosque[$key]['url_front'] = '';
$url = $p['file']."_mobile_298_406";
$old_url = $p['file'];
if ($isProd) {
try {
$response = $httpClient->request('HEAD', $url);
$statusCode = $response->getStatusCode();
$productkiosque[$key]['url'] = ($statusCode === 200) ? $url : $old_url;
} catch (\Throwable $e) {
$productkiosque[$key]['url'] = $old_url;
}
} else {
// In dev, avoid remote HEAD requests to DO Spaces to prevent timeouts
$productkiosque[$key]['url'] = $old_url;
}
if(str_contains($productkiosque[$key]['url'],$old_storage_url)) {
$url_front = str_replace($old_storage_url,$new_storage_url,$productkiosque[$key]['url']);
$productkiosque[$key]['url_front'] = $url_front;
}
}
return $productkiosque;
});
$old_storage_url=$this->parameterBag->get('storage_url');
$old_storage_url = str_replace('http://', '', $old_storage_url);
$new_storage_url=$this->parameterBag->get('storage_url_front');
$new_storage_url = str_replace('http://', '', $new_storage_url);
foreach ($products as $key=> $p) {
$products[$key]['url_front'] = '';
$url = $p['file']."_mobile_298_406";
$old_url = $p['file'];
if ($isProd) {
try {
$response = $httpClient->request('HEAD', $url);
$statusCode = $response->getStatusCode();
$products[$key]['url'] = ($statusCode === 200) ? $url : $old_url;
} catch (\Throwable $e) {
$products[$key]['url'] = $old_url;
}
} else {
$products[$key]['url'] = $old_url;
}
if(str_contains($products[$key]['url'],$old_storage_url)) {
$url_front = str_replace($old_storage_url,$new_storage_url,$products[$key]['url']);
$products[$key]['url_front'] = $url_front;
}
}
return $this->render('themes/theme-5/home/kiosques.html.twig', [
'products' => $products,
]);
}
/**
* @Route("/_produits", name="produits.kiosque")
*
* @Cache(smaxage="600", public="true")
*
* @Tag("products", "homepage", expression="'produits.kiosque'")
*/
public function produits(EntityManagerInterface $manager, TagAwareCacheInterface $myDedicatedCache, Request $request): Response
{
$products = $myDedicatedCache->get('produits.kiosque', function (ItemInterface $item) use ($manager) {
$item->tag(['products', 'homepage', 'produits.kiosque']);
$item->expiresAfter(3600);
return $manager->getRepository(StripeProduct::class)->getMagazines(9, true);
});
$page = $request->get('page');
$limit = 20;
$pages = ceil(count($products) / $limit) != 1 ? ceil(count($products) / $limit) : null;
$products = array_slice($products, $limit*($page - 1), $limit);
$html = $this->renderView('product/products.html.twig', [
'products' => $products
]);
return $this->json([
'html' => $html,
'pages' => $pages
]);
}
/**
* @Route("/_podcasts", name="homepage.podcasts")
*
* @Cache(smaxage="600", public="true")
*
* @Tag("articles", "homepage", expression="'homepage.podcasts'")
*/
public function podcasts(ArticleEntityService $articleService): Response
{
$videoPodcasts = $articleService->getVideoPodcasts();
return $this->render('themes/theme-5/home/videos-podcasts.html.twig', [
'videoPodcasts' => $videoPodcasts,
]);
}
/**
* @Route("/_recent", name="homepage.recent")
*
* @Cache(smaxage="600", public="true")
*
* @Tag("articles", "homepage", expression="'homepage.recent'")
*/
public function recent(): Response
{
return $this->render('themes/theme-5/home/recent-articles.html.twig', [
'page' => 1,
'position' => 2,
]);
}
/**
* @Route("/_recent_mobile", name="homepage.recent_mobile")
*
* @Cache(smaxage="600", public="true")
*
* @Tag("articles", "homepage", expression="'homepage.recent_mobile'")
*/
public function recentMobile(ArticleEntityService $articleService): Response
{
$highlighted = $articleService->getHighlightedArticles(6);
return $this->render('themes/theme-5/home/_partials/recent-article-coments-mobile.html.twig', [
'highlighted' => $highlighted,
'page' => 1,
]);
}
/**
* @Route("/_taggage_kiosque", name="homepage.taggage_kiosque")
*/
public function updateTaggage(Request $request, EntityManagerInterface $em): Response
{
$data = json_decode($request->getContent(), true);
$clicksValue = 0;
$name = '';
if ($data && isset($data['name']) ) {
$name = $data["name"];
}
$clicksKiosque = $em->getRepository(Statistic::class)->findOneBy(['name'=> $name]);
$clicksValue = intval($clicksKiosque->getValue());
if($name && $name == 'clicks_kiosque'){
$clicksValue = $clicksValue + 1;
}elseif ($name && $name == 'affiches_kiosque') {
$clicksValue = $clicksValue + 4;
}
$clicksKiosque->setValue($clicksValue);
$em->persist($clicksKiosque);
$em->flush();
return $this->json([
'clicksValue' => $clicksValue
]);
}
}