<?php
/**
* User: remmel
* Date: 3/5/16
* Time: 2:11 PM
*/
namespace App\Controller\Frontend;
use App\Entity\Block;
use App\Entity\model\Conveyance;
use App\Repository\BlockRepository;
use App\Service\BlockPathService;
use App\Service\UrlRewrite;
use App\Utils\StringUtils;
use App\Utils\Utils;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
use Symfony\Component\Routing\Annotation\Route;
class CMSController extends AbstractController {
/** @required */
public BlockPathService $blockPathService;
/** @required */
public BlockRepository $blockRepo;
/**
* CMS Pages, where a block will be loaded
* Routes defined in routes.xx.yml and "routes!" in https://docs.google.com/spreadsheets/d/1Gfe__ygVlGisvrtO1GfcIqUyca_8p2MPAQuPEu_HdzM/edit#gid=1627886470
* Cannot remove the path below because of the slug
* @Route("/company/{slug}", name="page_bus_company", defaults={"type" = "bus"})
* @Route("/train/company/{slug}", name="page_train_company", defaults={"type" = "train"})
* @Route(name="page_whoweare")
* @Route(name="page_partner", options={"i18n_locales"={"fr"}})
* @Route("/flight/company/{slug}", name="page_flight_company", options={"i18n_locales"={"fr"}}, defaults={"type" = "flight"})
* @Route("/carpooling/company/{slug}", name="page_carpooling_company" , options={"i18n_locales"={"fr", "en", "en_GB", "it", "es"}}, defaults={"type" = "carpooling"})
*
* @Route("/faq/{slug}", name="page_busfaq_home", options={"i18n_locales"={"fr", "en", "en_GB", "it", "es"}}, defaults={"type" = "bus"})
* @Route("/train/faq/{slug}", name="page_trainfaq_home", options={"i18n_locales"={"fr", "en", "en_GB", "it", "es"}}, defaults={"type" = "train"})
*
* @Route("/partner", name="page_partner", options={"i18n_locales"={"fr"}})
*
* @Route("/pays", options={"i18n_locales"={"fr"}})
*
* @Route("/tren/empresas-ferroviarias/avlo/conseguir-billete-a-5-euros", options={"i18n_locales"={"es"}})
* @Template("Default/page_CMS.html.twig")
*/
public function cmsPageAction(Request $request, UrlRewrite $rewrite, ?string $slug = "", string $type = Conveyance::BUS) {
$block = $this->getBlock($request);
//handle redirection
if(StringUtils::startsWith($block->getContent(), 'redirect:')) {
return $this->redirect(explode(':',$block->getContent())[1]);
}
return [
'slug' => $slug,
'block' => $block,
'breadcrumbLinks' => $this->createBreadcrumb($block),
'odpage' => $rewrite->generateODWebappLink($type),
];
}
/**
* @Route("/bus-pas-cher", options={"i18n_locales"={"fr"}}))
* Template("Default/cheap_page.html.twig")
* Cache(expires="+1days", public=true)
*/
public function cheapbusPageAction(Request $request, $slug = "") {
$block = $this->getBlock($request);
// FIXME temporary disable that, until fixed
$prices = [];
// $prices = $this->getPriceRepo()->findLastCheapestPrice();
$response = $this->render('Default/cheap_page.html.twig', [
'slug' => $slug,
'block' => $block,
'prices' => $prices
]);
$response->setSharedMaxAge(Utils::DAY_IN_SEC);
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
return $response;
}
/**
* @Route("/destination/{depStopName}-{depStopId}", name="citypage", options={"i18n_locales"={"fr","en"}}))
* @Template("Default/city_page.html.twig")
// * @Cache(expires="+2days", public=true)
*/
public function citypageAction(Request $request, $depStopId, $depStopName) {
$block = $this->getBlock($request);
// $prices = $repoPrice->findLastPriceByStop(10);
return [
'block' => $block
];
}
/**
* @Route("/{_locale}/bus-o-d/destination/{stopId}", name="fake_page_bus_d", options={"i18n"=false})
*/
public function fakeAction(Request $request) {
return new Response('Fake block to be used in od page.');
}
/**
* @return Link[]
*/
public function createBreadcrumb(Block $block): array {
/** @var Link[] $links */
$links = [];
while($block) {
$path = $block->getPath();
$label = array_key_exists('h1', $block->getMeta()) ? $block->getMeta()['h1'] : $block->getId(); //could be replace with $block->getMeta()['h1'] ?? null;
if($label === null) return $links;
$links[] = new Link($label, $path);
//search last "/" but ignore last / if last character rtrim($path, '/')
$pos = strrpos($path, '/', -2); //get parent folder
if($pos > 0) {
$path = substr($path, 0, $pos);
$block = $this->blockRepo
->findOneByPath($path);
} else {
$block = null;
}
}
return array_reverse($links);
}
/**
* Get the block if exists
*/
private function getBlock(Request $request): Block {
$block = $this->blockPathService->getBlock($request);
if (!$block) {
throw $this->createNotFoundException("Page/Blog does not exist for ".$request->getPathInfo());
}
return $block;
}
/**
* @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"}})
* @Template("Default/page_CMS.html.twig")
* @Cache(expires="+2days", public=true)
* "depStopId" = "\d+"
*/
public function fromAtoBpageCountryAction(Request $request, string $depCountryName, string $arrCountryName, String $depCountryId, string $arrCountryId, UrlRewrite $rewrite, string $type = Conveyance::BUS) {
$block = $this->getBlock($request);
// $prices = $this->getPriceRepo()
// ->findLastPriceByCountry($depCountryId, $arrCountryId);
$prices = [];
return [
'depCountryName' => $depCountryName,
'arrCountryName' => $arrCountryName,
'prices' => $prices,
'block' => $block,
'breadcrumbLinks' => $this->createBreadcrumb($block),
'odpage' => $rewrite->generateODWebappLink($type),
];
}
}