<?php
namespace App\Controller\Frontend;
use App\Entity\Block;
use App\Entity\model\Conveyance;
use App\Entity\Stop;
use App\Extension\LocaleListener;
use App\Repository\BlockRepository;
use App\Repository\StopRepository;
use App\Service\OdSeoService;
use App\Service\UrlRewrite;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Asset\Packages;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
*/
class HomepageController extends AbstractController {
protected string $domainprefix;
protected TranslatorInterface $translator;
protected UrlRewrite $urlRewriteService;
protected StopRepository $stopRepo;
public function __construct(string $domainprefix, TranslatorInterface $translator, UrlRewrite $urlRewriteService, StopRepository $stopRepo) {
$this->domainprefix = $domainprefix;
$this->translator = $translator;
$this->urlRewriteService = $urlRewriteService;
$this->stopRepo = $stopRepo;
}
/**
* Homepage. Default language is french.
* @Route("/", name="homepage", host="%domainprefix%www.comparabus.com", options={"i18n"=false})
* @Template("Default/homepage/homepage.html.twig")
*
* - curl -I https://www.comparabus.com => https://www.comparabus.com/fr/
* - curl -I http://local-www.comparabus.com => http://local-www.comparabus.com/fr/
*/
public function homepageAction() {
$url = $this->generateUrl('homepageLocale', ['_locale' => 'fr']);
return $this->redirect($url);
}
/**
* Cache(expires="+24hours", public=true)
* @Route("/", name="homepageLocale")
* Template("Default/homepage/homepage.html.twig")
*/
public function homepageLocaleAction(UrlRewrite $rewrite) {
$response = $this->render('Default/homepage/homepage.html.twig', [
'isHomepage' => true,
'alternates' => $this->generateAlternate(),
'topdestinations' => $this->generateTopDestination(),
'partners' => $this->generatePartners(),
'odpage' => $rewrite->generateODWebappLink(Conveyance::BUS),
'search' => [
'type' => 'bus'
],
]);
$response->setSharedMaxAge(3600);
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
return $response;
}
/**
* @Route("/covoiturage/", name="page_carpooling_home", options={"i18n_locales"={"fr"}}, defaults={"type" = "carpooling"})
* @Route("/avion/", name="page_flight_home", options={"i18n_locales"={"fr"}}, defaults={"type" = "flight"})
* @Route("/train/", name="page_train_home", options={"i18n_locales"={"fr", "es", "it", "en", "en_GB"}}, defaults={"type" = "train"})
* @Route("/billet/", name="page_ticket_home", defaults={"type" = "all"})
* @Template("Default/searchhomecms_page.html.twig")
*/
public function indexAction(Request $r, UrlRewrite $rewrite, BlockRepository $blockRepo, ?string $type = Conveyance::BUS) {
if(!$type) throw new \Exception("type cannot be null");
$block = $blockRepo->findOnebyPathNotNull($r->getPathInfo());
return [
'block' => $block,
'search' => [
'type' => $type
],
'odpage' => $rewrite->generateODWebappLink($type)
];
}
/**
* @Route("/it/", name="redirectIT_home", options={"i18n"=false}, host="%domainprefix%www.comparabus.com")
* @Route("/it/{slug}", name="redirectIT_others", options={"i18n"=false}, host="%domainprefix%www.comparabus.com")
* @Route("/it/{slug}/{slug2}", name="redirectIT_others2", options={"i18n"=false}, host="%domainprefix%www.comparabus.com")
*/
public function redirectITAction(Request $request) {
$currentUrl = $request->getRequestUri();
$location = '//'.$this->domainprefix.'www.comparabus.it'.str_replace('/it', '', $currentUrl);
return $this->redirect($location);
}
/**
* @Route("/", name="redirect_home_es", options={"i18n"=false}, host="%domainprefix%www.comparabus.es")
*/
public function redirectES() {
return $this->redirectToRoute('homepageLocale', ['_locale' => 'es']);
}
/**
* @Route("/", name="redirect_home_fr", options={"i18n"=false}, host="%domainprefix%www.comparabus.fr")
*/
public function redirectFR() {
return $this->redirectToRoute('homepageLocale', ['_locale' => 'fr']);
}
/**
* @Route("/", name="redirect_home_pl", options={"i18n"=false}, host="%domainprefix%www.comparabus.pl")
*/
public function redirectPL() {
return $this->redirectToRoute('homepageLocale', ['_locale' => 'pl']);
}
/**
* @Route("/", name="redirect_home_cs", options={"i18n"=false}, host="%domainprefix%www.comparabus.cz")
*/
public function redirectCS() {
return $this->redirectToRoute('homepageLocale', ['_locale' => 'cs']);
}
/**
* @Route("/", name="redirect_home_de", options={"i18n"=false}, host="%domainprefix%www.comparabus.de")
*/
public function redirectDE() {
return $this->redirectToRoute('homepageLocale', ['_locale' => 'de']);
}
/**
* @Route("{_locale}/map", name="mapLocale", options={"i18n"=false})
* @Template("Default/map.html.twig")
*/
public function mapLocaleAction() {
return [
'isHomepage' => true
];
}
/**
* Returns the list of prices in euro of the 4 destinations
*/
public function generateTopDestination(): array {
$destinations = [];
for ($i = 1; $i <= 5; $i++) {
$depStopId = $this->translator->trans("homepage.topdestination.$i.from");
$arrStopId = $this->translator->trans("homepage.topdestination.$i.to");
$depStop = $this->stopRepo->find($depStopId);
if (!$depStop) throw new \Exception("Stop #$depStopId not found");
$arrStop = $this->stopRepo->find($arrStopId);
if (!$arrStop) throw new \Exception("Stop #$arrStopId not found");
$url = $this->urlRewriteService->generateFromAtoBUrlWithoutDate($depStop, $arrStop);
// $price = $repoPrice->findOneLastCheapestPriceByStops($depStopId, $arrStopId);
$destinations[$i] = [
'url' => $url,
'price' => null //$price ? $price->getCents() / 100 :
];
}
return $destinations;
}
protected function generateAlternate():array {
//TODO must use same data source that layout footer language. Might use %app.locales%
$locales = ['fr', 'fr_MA', 'fr_BE', 'es', 'en', 'de', 'it', 'cs'];
$alternates = [];
foreach($locales as $locale){
$alternates[] = [
'hreflang' => LocaleListener::underscoreToDash($locale),
'href' => $this->generateUrl('homepageLocale', ['_locale' => $locale], UrlGeneratorInterface::ABSOLUTE_URL)
];
}
return $alternates;
}
protected function generatePartners(): array {
$partnersCsv = $this->translator->trans("homepage.partner.list");
$partners = [];
foreach (explode(PHP_EOL, $partnersCsv) as $line) {
$partner = explode(';', $line);
if (count($partner) < 2) throw new \Exception("must delimiter with ';' : eg : 'img;alt' or 'img;alt;url'. Instead : '$line''");
$partners[] = [
'img' => $partner[0], //img.src
'alt' => $partner[1], //img.alt
'url' => isset($partner[2]) ? $partner[2] : null //a.href
];
}
return $partners;
}
/**
* @Route("favicon.ico", name="root_favicon", host="%domainprefix%www.comparabus.com", options={"i18n"=false})
* as some browser fetch /favicon.ico
*/
public function redirectToFavicon(Packages $packages) {
$url = $packages->getUrl('images/favicon_cb/favicon.ico');
return $this->redirect($url);
}
/**
* @Route("apple-touch-icon.png", name="root_appletouchicon", host="%domainprefix%www.comparabus.com", options={"i18n"=false})
*/
public function redirectToAppleTouchIcon(Packages $packages) {
$url = $packages->getUrl('images/favicon_cb/apple-touch-icon.png');
return $this->redirect($url);
}
}