src/Controller/Frontend/CMSController.php line 54

Open in your IDE?
  1. <?php
  2. /**
  3.  * User: remmel
  4.  * Date: 3/5/16
  5.  * Time: 2:11 PM
  6.  */
  7. namespace App\Controller\Frontend;
  8. use App\Entity\Block;
  9. use App\Entity\model\Conveyance;
  10. use App\Repository\BlockRepository;
  11. use App\Service\BlockPathService;
  12. use App\Service\UrlRewrite;
  13. use App\Utils\StringUtils;
  14. use App\Utils\Utils;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. class CMSController extends AbstractController {
  23.     /** @required */
  24.     public BlockPathService $blockPathService;
  25.     /** @required */
  26.     public BlockRepository $blockRepo;
  27.     /**
  28.      * CMS Pages, where a block will be loaded
  29.      * Routes defined in routes.xx.yml and "routes!" in https://docs.google.com/spreadsheets/d/1Gfe__ygVlGisvrtO1GfcIqUyca_8p2MPAQuPEu_HdzM/edit#gid=1627886470
  30.      * Cannot remove the path below because of the slug
  31.      * @Route("/company/{slug}",       name="page_bus_company", defaults={"type" = "bus"})
  32.      * @Route("/train/company/{slug}", name="page_train_company", defaults={"type" = "train"})
  33.      * @Route(name="page_whoweare")
  34.      * @Route(name="page_partner", options={"i18n_locales"={"fr"}})
  35.      * @Route("/flight/company/{slug}", name="page_flight_company",   options={"i18n_locales"={"fr"}}, defaults={"type" = "flight"})
  36.      * @Route("/carpooling/company/{slug}", name="page_carpooling_company"  , options={"i18n_locales"={"fr", "en", "en_GB", "it", "es"}}, defaults={"type" = "carpooling"})
  37.      *
  38.      * @Route("/faq/{slug}", name="page_busfaq_home", options={"i18n_locales"={"fr", "en", "en_GB", "it", "es"}}, defaults={"type" = "bus"})
  39.      * @Route("/train/faq/{slug}", name="page_trainfaq_home", options={"i18n_locales"={"fr", "en", "en_GB", "it", "es"}}, defaults={"type" = "train"})
  40.      *
  41.      * @Route("/partner", name="page_partner", options={"i18n_locales"={"fr"}})
  42.      *
  43.      * @Route("/pays", options={"i18n_locales"={"fr"}})
  44.      *
  45.      * @Route("/tren/empresas-ferroviarias/avlo/conseguir-billete-a-5-euros", options={"i18n_locales"={"es"}})
  46.      * @Template("Default/page_CMS.html.twig")
  47.      */
  48.     public function cmsPageAction(Request $requestUrlRewrite $rewrite, ?string $slug ""string $type Conveyance::BUS) {
  49.         $block $this->getBlock($request);
  50.         //handle redirection
  51.         if(StringUtils::startsWith($block->getContent(), 'redirect:')) {
  52.             return $this->redirect(explode(':',$block->getContent())[1]);
  53.         }
  54.         return [
  55.             'slug' => $slug,
  56.             'block' => $block,
  57.             'breadcrumbLinks' => $this->createBreadcrumb($block),
  58.             'odpage' => $rewrite->generateODWebappLink($type),
  59.         ];
  60.     }
  61.     /**
  62.      * @Route("/bus-pas-cher", options={"i18n_locales"={"fr"}}))
  63.      * Template("Default/cheap_page.html.twig")
  64.      * Cache(expires="+1days", public=true)
  65.      */
  66.     public function cheapbusPageAction(Request $request$slug "") {
  67.         $block $this->getBlock($request);
  68.         // FIXME temporary disable that, until fixed
  69.         $prices = [];
  70. //        $prices = $this->getPriceRepo()->findLastCheapestPrice();
  71.         $response $this->render('Default/cheap_page.html.twig', [
  72.             'slug' => $slug,
  73.             'block' => $block,
  74.             'prices' => $prices
  75.         ]);
  76.         $response->setSharedMaxAge(Utils::DAY_IN_SEC);
  77.         $response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER'true');
  78.         return $response;
  79.     }
  80.     /**
  81.      * @Route("/destination/{depStopName}-{depStopId}", name="citypage", options={"i18n_locales"={"fr","en"}}))
  82.      * @Template("Default/city_page.html.twig")
  83. //     * @Cache(expires="+2days", public=true)
  84.      */
  85.     public function citypageAction(Request $request$depStopId$depStopName) {
  86.         $block $this->getBlock($request);
  87. //        $prices = $repoPrice->findLastPriceByStop(10);
  88.         return [
  89.             'block' => $block
  90.         ];
  91.     }
  92.     /**
  93.      * @Route("/{_locale}/bus-o-d/destination/{stopId}", name="fake_page_bus_d", options={"i18n"=false})
  94.      */
  95.     public function fakeAction(Request $request) {
  96.         return new Response('Fake block to be used in od page.');
  97.     }
  98.     /**
  99.      * @return Link[]
  100.      */
  101.     public function createBreadcrumb(Block $block): array {
  102.         /** @var Link[] $links */
  103.         $links = [];
  104.         while($block) {
  105.             $path $block->getPath();
  106.             $label array_key_exists('h1'$block->getMeta()) ? $block->getMeta()['h1'] : $block->getId(); //could be replace with $block->getMeta()['h1'] ?? null;
  107.             if($label === null) return $links;
  108.             $links[] = new Link($label$path);
  109.             //search last "/" but ignore last / if last character rtrim($path, '/')
  110.             $pos strrpos($path'/', -2); //get parent folder
  111.             if($pos 0) {
  112.                 $path substr($path0$pos);
  113.                 $block $this->blockRepo
  114.                     ->findOneByPath($path);
  115.             } else {
  116.                 $block null;
  117.             }
  118.         }
  119.         return array_reverse($links);
  120.     }
  121.     /**
  122.      * Get the block if exists
  123.      */
  124.     private function getBlock(Request $request): Block {
  125.         $block $this->blockPathService->getBlock($request);
  126.         if (!$block) {
  127.             throw $this->createNotFoundException("Page/Blog does not exist for ".$request->getPathInfo());
  128.         }
  129.         return $block;
  130.     }
  131.     /**
  132.      * @Route("/country/bus-{depCountryName}-{arrCountryName}-{depCountryId}-{arrCountryId}", name="fromatobPageCountry", requirements = {"depCountryId" = "[a-zA-Z]+", "arrCountryId" = "[a-zA-Z]+"}, host="%domainprefix%www.comparabus.com", options={"i18n_locales"={"fr","en"}})
  133.      * @Template("Default/page_CMS.html.twig")
  134.      * @Cache(expires="+2days", public=true)
  135.      * "depStopId" = "\d+"
  136.      */
  137.     public function fromAtoBpageCountryAction(Request $requeststring $depCountryNamestring $arrCountryNameString $depCountryIdstring $arrCountryIdUrlRewrite $rewritestring $type Conveyance::BUS) {
  138.         $block $this->getBlock($request);
  139. //        $prices = $this->getPriceRepo()
  140. //            ->findLastPriceByCountry($depCountryId, $arrCountryId);
  141.         $prices = [];
  142.         return [
  143.             'depCountryName' => $depCountryName,
  144.             'arrCountryName' => $arrCountryName,
  145.             'prices' => $prices,
  146.             'block' => $block,
  147.             'breadcrumbLinks' => $this->createBreadcrumb($block),
  148.             'odpage' => $rewrite->generateODWebappLink($type),
  149.         ];
  150.     }
  151. }