src/Controller/Frontend/HomepageController.php line 144

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\Block;
  4. use App\Entity\model\Conveyance;
  5. use App\Entity\Stop;
  6. use App\Extension\LocaleListener;
  7. use App\Repository\BlockRepository;
  8. use App\Repository\StopRepository;
  9. use App\Service\OdSeoService;
  10. use App\Service\UrlRewrite;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\Asset\Packages;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. /**
  20.  */
  21. class HomepageController extends AbstractController {
  22.     protected string $domainprefix;
  23.     protected TranslatorInterface $translator;
  24.     protected UrlRewrite $urlRewriteService;
  25.     protected StopRepository $stopRepo;
  26.     public function __construct(string $domainprefixTranslatorInterface $translatorUrlRewrite $urlRewriteServiceStopRepository $stopRepo) {
  27.         $this->domainprefix $domainprefix;
  28.         $this->translator $translator;
  29.         $this->urlRewriteService $urlRewriteService;
  30.         $this->stopRepo $stopRepo;
  31.     }
  32.     /**
  33.      * Homepage. Default language is french.
  34.      * @Route("/", name="homepage", host="%domainprefix%www.comparabus.com", options={"i18n"=false})
  35.      * @Template("Default/homepage/homepage.html.twig")
  36.      *
  37.      * - curl -I https://www.comparabus.com => https://www.comparabus.com/fr/
  38.      * - curl -I http://local-www.comparabus.com => http://local-www.comparabus.com/fr/
  39.      */
  40.     public function homepageAction() {
  41.         $url $this->generateUrl('homepageLocale', ['_locale' => 'fr']);
  42.         return $this->redirect($url);
  43.     }
  44.     /**
  45.      * Cache(expires="+24hours", public=true)
  46.      * @Route("/", name="homepageLocale")
  47.      * Template("Default/homepage/homepage.html.twig")
  48.      */
  49.     public function homepageLocaleAction(UrlRewrite $rewrite) {
  50.         $response $this->render('Default/homepage/homepage.html.twig', [
  51.             'isHomepage' => true,
  52.             'alternates' => $this->generateAlternate(),
  53.             'topdestinations' => $this->generateTopDestination(),
  54.             'partners' => $this->generatePartners(),
  55.             'odpage' => $rewrite->generateODWebappLink(Conveyance::BUS),
  56.             'search' => [
  57.                 'type' => 'bus'
  58.             ],
  59.         ]);
  60.         $response->setSharedMaxAge(3600);
  61.         $response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER'true');
  62.         return $response;
  63.     }
  64.     /**
  65.      * @Route("/covoiturage/", name="page_carpooling_home", options={"i18n_locales"={"fr"}}, defaults={"type" = "carpooling"})
  66.      * @Route("/avion/", name="page_flight_home", options={"i18n_locales"={"fr"}}, defaults={"type" = "flight"})
  67.      * @Route("/train/", name="page_train_home", options={"i18n_locales"={"fr", "es", "it", "en", "en_GB"}}, defaults={"type" = "train"})
  68.      * @Route("/billet/", name="page_ticket_home", defaults={"type" = "all"})
  69.      * @Template("Default/searchhomecms_page.html.twig")
  70.      */
  71.     public function indexAction(Request $rUrlRewrite $rewriteBlockRepository $blockRepo, ?string $type Conveyance::BUS) {
  72.         if(!$type) throw new \Exception("type cannot be null");
  73.         $block $blockRepo->findOnebyPathNotNull($r->getPathInfo());
  74.         return [
  75.             'block' => $block,
  76.             'search' => [
  77.                 'type' => $type
  78.             ],
  79.             'odpage' => $rewrite->generateODWebappLink($type)
  80.         ];
  81.     }
  82.     /**
  83.      * @Route("/it/", name="redirectIT_home", options={"i18n"=false}, host="%domainprefix%www.comparabus.com")
  84.      * @Route("/it/{slug}", name="redirectIT_others", options={"i18n"=false}, host="%domainprefix%www.comparabus.com")
  85.      * @Route("/it/{slug}/{slug2}", name="redirectIT_others2", options={"i18n"=false}, host="%domainprefix%www.comparabus.com")
  86.      */
  87.     public function redirectITAction(Request $request) {
  88.         $currentUrl $request->getRequestUri();
  89.         $location '//'.$this->domainprefix.'www.comparabus.it'.str_replace('/it'''$currentUrl);
  90.         return $this->redirect($location);
  91.     }
  92.     /**
  93.      * @Route("/", name="redirect_home_es", options={"i18n"=false}, host="%domainprefix%www.comparabus.es")
  94.      */
  95.     public function redirectES() {
  96.         return $this->redirectToRoute('homepageLocale', ['_locale' => 'es']);
  97.     }
  98.     /**
  99.      * @Route("/", name="redirect_home_fr", options={"i18n"=false}, host="%domainprefix%www.comparabus.fr")
  100.      */
  101.     public function redirectFR() {
  102.         return $this->redirectToRoute('homepageLocale', ['_locale' => 'fr']);
  103.     }
  104.     /**
  105.      * @Route("/", name="redirect_home_pl", options={"i18n"=false}, host="%domainprefix%www.comparabus.pl")
  106.      */
  107.     public function redirectPL() {
  108.         return $this->redirectToRoute('homepageLocale', ['_locale' => 'pl']);
  109.     }
  110.     /**
  111.      * @Route("/", name="redirect_home_cs", options={"i18n"=false}, host="%domainprefix%www.comparabus.cz")
  112.      */
  113.     public function redirectCS() {
  114.         return $this->redirectToRoute('homepageLocale', ['_locale' => 'cs']);
  115.     }
  116.     /**
  117.      * @Route("/", name="redirect_home_de", options={"i18n"=false}, host="%domainprefix%www.comparabus.de")
  118.      */
  119.     public function redirectDE() {
  120.         return $this->redirectToRoute('homepageLocale', ['_locale' => 'de']);
  121.     }
  122.     /**
  123.      * @Route("{_locale}/map", name="mapLocale", options={"i18n"=false})
  124.      * @Template("Default/map.html.twig")
  125.      */
  126.     public function mapLocaleAction() {
  127.         return [
  128.             'isHomepage' => true
  129.         ];
  130.     }
  131.     /**
  132.      * Returns the list of prices in euro of the 4 destinations
  133.      */
  134.     public function generateTopDestination(): array {
  135.         $destinations = [];
  136.         for ($i 1$i <= 5$i++) {
  137.             $depStopId $this->translator->trans("homepage.topdestination.$i.from");
  138.             $arrStopId $this->translator->trans("homepage.topdestination.$i.to");
  139.             $depStop $this->stopRepo->find($depStopId);
  140.             if (!$depStop) throw new \Exception("Stop #$depStopId not found");
  141.             $arrStop $this->stopRepo->find($arrStopId);
  142.             if (!$arrStop) throw new \Exception("Stop #$arrStopId not found");
  143.             $url $this->urlRewriteService->generateFromAtoBUrlWithoutDate($depStop$arrStop);
  144. //            $price = $repoPrice->findOneLastCheapestPriceByStops($depStopId, $arrStopId);
  145.             $destinations[$i] = [
  146.                 'url' => $url,
  147.                 'price' => null //$price ? $price->getCents() / 100 :
  148.             ];
  149.         }
  150.         return $destinations;
  151.     }
  152.     protected function generateAlternate():array {
  153.         //TODO must use same data source that layout footer language. Might use %app.locales%
  154.         $locales = ['fr''fr_MA''fr_BE''es''en''de''it''cs'];
  155.         $alternates = [];
  156.         foreach($locales as $locale){
  157.             $alternates[] = [
  158.                 'hreflang' => LocaleListener::underscoreToDash($locale),
  159.                 'href' => $this->generateUrl('homepageLocale', ['_locale' => $locale], UrlGeneratorInterface::ABSOLUTE_URL)
  160.             ];
  161.         }
  162.         return $alternates;
  163.     }
  164.     protected function generatePartners(): array {
  165.         $partnersCsv $this->translator->trans("homepage.partner.list");
  166.         $partners = [];
  167.         foreach (explode(PHP_EOL$partnersCsv) as $line) {
  168.             $partner explode(';'$line);
  169.             if (count($partner) < 2) throw new \Exception("must delimiter with ';' : eg : 'img;alt' or 'img;alt;url'. Instead : '$line''");
  170.             $partners[] = [
  171.                 'img' => $partner[0], //img.src
  172.                 'alt' => $partner[1], //img.alt
  173.                 'url' => isset($partner[2]) ? $partner[2] : null //a.href
  174.             ];
  175.         }
  176.         return $partners;
  177.     }
  178.     /**
  179.      * @Route("favicon.ico", name="root_favicon", host="%domainprefix%www.comparabus.com", options={"i18n"=false})
  180.      * as some browser fetch /favicon.ico
  181.      */
  182.     public function redirectToFavicon(Packages $packages) {
  183.         $url $packages->getUrl('images/favicon_cb/favicon.ico');
  184.         return $this->redirect($url);
  185.     }
  186.     /**
  187.      * @Route("apple-touch-icon.png", name="root_appletouchicon", host="%domainprefix%www.comparabus.com", options={"i18n"=false})
  188.      */
  189.     public function redirectToAppleTouchIcon(Packages $packages) {
  190.         $url $packages->getUrl('images/favicon_cb/apple-touch-icon.png');
  191.         return $this->redirect($url);
  192.     }
  193. }