<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\BusStop;
use App\Entity\Inspiration;
use App\Entity\Page;
use App\Entity\PageSection;
use App\Repository\BusStopRepository;
use App\Repository\InspirationRepository;
use App\Repository\OpinionRepository;
use App\Repository\OutdoorSafetyRepository;
use App\Repository\PageRepository;
use App\Repository\RouteRepository;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use JMS\Serializer\SerializerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class IndexController extends BaseController
{
private PageRepository $pageRepository;
public function __construct(EntityManagerInterface $em, SerializerInterface $serializer, TokenStorageInterface $tokenStorage, RequestStack $requestStack, PaginatorInterface $paginator, ValidatorInterface $validator, UserPasswordEncoderInterface $passwordEncoder, PageRepository $pageRepository)
{
parent::__construct($em, $serializer, $tokenStorage, $requestStack, $paginator, $validator, $passwordEncoder);
$this->pageRepository = $pageRepository;
}
/**
* @Route("/", name="app_homepage", methods={"GET"})
*/
public function index(RouteRepository $routeRepository, OpinionRepository $opinionRepository, OutdoorSafetyRepository $outdoorSafetyRepository): Response
{
list($domain, $locale) = $this->getRequestParameter();
/** @var Page $pageObject */
$pageObject = $this->pageRepository->getPageByParameterByUrl('homepage', $domain, $locale);
if (!$pageObject) {
throw $this->createNotFoundException();
}
$seoPage = [];
$seoPage['meta-title'] = $pageObject->getMetaTitle();
$seoPage['meta-desc'] = $pageObject->getMetaDescription();
$seoPage['meta-keywords'] = $pageObject->getMetaKeywords();
$seoPage['nofollow'] = $pageObject->getInformationNoFollow();
return $this->render('main/index.html.twig', [
'locale' => $locale,
'seo' => $seoPage,
'page' => $pageObject,
'travelMenu' => $this->pageRepository->getPageByListed($domain, $locale, 4, ['p.id', 'DESC']),
'routes' => $routeRepository->findActive(),
'opinions' => $opinionRepository->findAllSortedBySortPosition(),
'outdoorSafety' => $outdoorSafetyRepository->findByPageTypeAndLang($domain, $locale, true),
]);
}
/**
* @Route("/sitemap.xml", name="main_sitemap", defaults={"_format"="xml"})
* @Template()
*/
public function sitemapAction(Request $request, InspirationRepository $inspirationRepository): Response
{
list($domain, $locale) = $this->getRequestParameter();
$em = $this->getDoctrine()->getManager();
$urls = [];
$scheme = $request->getScheme();
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/en/bus-stops'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/en/term-and-conditions'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/en/privacy-policy'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/en/faq'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/en/about-us'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/en/contact'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/en/signin'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/en/registration'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/en/inspirations'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/de/bus-stops'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/de/term-and-conditions'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/de/privacy-policy'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/de/faq'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/de/about-us'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/de/contact'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/de/signin'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/de/registration'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/de/inspirations'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/pt/bus-stops'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/pt/term-and-conditions'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/pt/privacy-policy'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/pt/faq'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/pt/about-us'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/pt/contact'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/pt/signin'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/pt/registration'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/pt/inspirations'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/fr/bus-stops'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/fr/term-and-conditions'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/fr/privacy-policy'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/fr/faq'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/fr/about-us'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/fr/contact'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/fr/signin'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/fr/registration'];
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/fr/inspirations'];
$inspirations = $inspirationRepository->findActive();
/** @var Inspiration $inspiration */
foreach ($inspirations as $inspiration) {
$locale = $inspiration->getDomainLang()->getSlug();
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/' . $locale . '/inspiration/' . $inspiration->getSlug()];
}
$pages = $this->pageRepository->findAll();
/** @var Page $page */
foreach ($pages as $page) {
if ($page->getId() > 23) {
$locale = $page->getDomainLang()->getSlug();
$urls[] = ['loc' => $scheme . '://' . $domain->getName() . '/' . $locale . '/' . $page->getSlug()];
}
}
$response = new Response($this->renderView('main/sitemap.html.twig', ['urls' => $urls]), 200);
$response->headers->set('Content-Type', 'text/xml');
return $response;
}
/**
* @Route("/bus-stops", name="app_bus_stop", methods={"GET"})
*/
public function busStops(): Response
{
list($domain, $locale) = $this->getRequestParameter();
/** @var Page $pageObject */
$pageObject = $this->pageRepository->getPageByParameterByUrl('bus-stops', $domain, $locale);
if (!$pageObject) {
throw $this->createNotFoundException();
}
$seoPage = [];
$seoPage['meta-title'] = $pageObject->getMetaTitle();
$seoPage['meta-desc'] = $pageObject->getMetaDescription();
$seoPage['meta-keywords'] = $pageObject->getMetaKeywords();
$seoPage['nofollow'] = $pageObject->getInformationNoFollow();
return $this->render('main/bus-stops.html.twig', [
'locale' => $locale,
'seo' => $seoPage,
'page' => $pageObject
]);
}
// /**
// * @Route("/contact", name="app_contact", methods={"GET"})
// */
// public function contact(): Response
// {
// list($domain, $locale) = $this->getRequestParameter();
//
// /** @var Page $pageObject */
// $pageObject = $this->pageRepository->getPageByParameterByUrl('contact', $domain, $locale);
// if (!$pageObject) {
// throw $this->createNotFoundException();
// }
//
// var_dump($pageObject->getId());
// die("ELLO");
//
// $seoPage = [];
// $seoPage['meta-title'] = $pageObject->getMetaTitle();
// $seoPage['meta-desc'] = $pageObject->getMetaDescription();
// $seoPage['meta-keywords'] = $pageObject->getMetaKeywords();
// $seoPage['nofollow'] = $pageObject->getInformationNoFollow();
//
// return $this->render('main/contact.html.twig', [
// 'locale' => $locale,
// 'seo' => $seoPage,
// 'page' => $pageObject
// ]);
// }
/**
* @Route("/term-and-conditions", name="app_term_conditions", methods={"GET"})
*/
public function termConditions(): Response
{
list($domain, $locale) = $this->getRequestParameter();
/** @var Page $pageObject */
$pageObject = $this->pageRepository->getPageByParameterByUrl('terms-and-conditions', $domain, $locale);
if (!$pageObject) {
throw $this->createNotFoundException();
}
$seoPage = [];
$seoPage['meta-title'] = $pageObject->getMetaTitle();
$seoPage['meta-desc'] = $pageObject->getMetaDescription();
$seoPage['meta-keywords'] = $pageObject->getMetaKeywords();
$seoPage['nofollow'] = $pageObject->getInformationNoFollow();
return $this->render('main/termConditions.html.twig', [
'locale' => $locale,
'seo' => $seoPage,
'page' => $pageObject
]);
}
/**
* @Route("/privacy-policy", name="app_privacy_policy", methods={"GET"})
*/
public function privacyPolicy(): Response
{
list($domain, $locale) = $this->getRequestParameter();
/** @var Page $pageObject */
$pageObject = $this->pageRepository->getPageByParameterByUrl('privacy-policy', $domain, $locale);
if (!$pageObject) {
throw $this->createNotFoundException();
}
$seoPage = [];
$seoPage['meta-title'] = $pageObject->getMetaTitle();
$seoPage['meta-desc'] = $pageObject->getMetaDescription();
$seoPage['meta-keywords'] = $pageObject->getMetaKeywords();
$seoPage['nofollow'] = $pageObject->getInformationNoFollow();
return $this->render('main/privacyPolicy.html.twig', [
'locale' => $locale,
'seo' => $seoPage,
'page' => $pageObject
]);
}
/**
* @Route("/inspirations", name="app_inspirations", methods={"GET"})
*/
public function inspirationsList(InspirationRepository $inspirationRepository): Response
{
list($domain, $locale) = $this->getRequestParameter();
/** @var Page $pageObject */
$pageObject = $this->pageRepository->getPageByParameterByUrl('inspirations', $domain, $locale);
if (!$pageObject) {
throw $this->createNotFoundException();
}
$inspirations = $inspirationRepository->getItemsByParameters($domain, $locale);
$seoPage = [];
$seoPage['meta-title'] = $pageObject->getMetaTitle();
$seoPage['meta-desc'] = $pageObject->getMetaDescription();
$seoPage['meta-keywords'] = $pageObject->getMetaKeywords();
$seoPage['nofollow'] = $pageObject->getInformationNoFollow();
return $this->render('main/inspirationsList.html.twig', [
'locale' => $locale,
'seo' => $seoPage,
'page' => $pageObject,
'inspirations' => $inspirations
]);
}
/**
* @Route("/travel-info", name="app_travel_info", methods={"GET"})
*/
public function articleList(Request $request): Response
{
$page = $request->query->getInt('page', 1);
$limit = $request->query->getInt('limit', 10);
list($domain, $locale) = $this->getRequestParameter();
/** @var Page $pageObject */
$pageObject = $this->pageRepository->getPageByParameterByUrl('inspirations', $domain, $locale);
if (!$pageObject) {
throw $this->createNotFoundException();
}
$seoPage = [];
$seoPage['meta-title'] = $pageObject->getMetaTitle();
$seoPage['meta-desc'] = $pageObject->getMetaDescription();
$seoPage['meta-keywords'] = $pageObject->getMetaKeywords();
$seoPage['nofollow'] = $pageObject->getInformationNoFollow();
$pages = $this->pageRepository->getPageByListed($domain, $locale);
$pagination = $this->paginator->paginate(
$pages,
$page,
$limit
);
return $this->render('main/articleList.html.twig', [
'locale' => $locale,
'seo' => $seoPage,
'page' => $pageObject,
'pages' => $pagination
]);
}
/**
* @Route("/hike-trails", name="app_hike_trails", methods={"GET"})`
*/
public function hikeTrails(Request $request): Response
{
$page = $request->query->getInt('page', 1);
$limit = $request->query->getInt('limit', 10);
list($domain, $locale) = $this->getRequestParameter();
/** @var Page $pageObject */
$pageObject = $this->pageRepository->getPageByParameterByUrl('hike-trails', $domain, $locale);
if (!$pageObject) {
throw $this->createNotFoundException();
}
$seoPage = [];
$seoPage['meta-title'] = $pageObject->getMetaTitle();
$seoPage['meta-desc'] = $pageObject->getMetaDescription();
$seoPage['meta-keywords'] = $pageObject->getMetaKeywords();
$seoPage['nofollow'] = $pageObject->getInformationNoFollow();
$pages = $this->pageRepository->getPageIsHikeTrails($domain, $locale);
$pagination = $this->paginator->paginate(
$pages,
$page,
$limit
);
return $this->render('main/hikeTrails.html.twig', [
'locale' => $locale,
'seo' => $seoPage,
'page' => $pageObject,
'pages' => $pagination
]);
}
/**
* @Route("/inspiration/{slug}", name="app_inspiration_item", methods={"GET"})
*/
public function inspirationsItem(Inspiration $inspiration): Response
{
list($domain, $locale) = $this->getRequestParameter();
$seoPage = [];
$seoPage['meta-title'] = $inspiration->getMetaTitle();
$seoPage['meta-desc'] = $inspiration->getMetaDescription();
;
$seoPage['meta-keywords'] = $inspiration->getMetaKeywords();
$seoPage['nofollow'] = $inspiration->getInformationNoFollow();
return $this->render('main/inspirationsItem.html.twig', [
'locale' => $locale,
'seo' => $seoPage,
'inspiration' => $inspiration
]);
}
/**
* @Route("/faq", name="app_faq", methods={"GET"})
*/
public function faq(): Response
{
list($domain, $locale) = $this->getRequestParameter();
/** @var Page $pageObject */
$pageObject = $this->pageRepository->getPageByParameterByUrl('faq', $domain, $locale);
if (!$pageObject) {
throw $this->createNotFoundException();
}
$seoPage = [];
$seoPage['meta-title'] = $pageObject->getMetaTitle();
$seoPage['meta-desc'] = $pageObject->getMetaDescription();
$seoPage['meta-keywords'] = $pageObject->getMetaKeywords();
$seoPage['nofollow'] = $pageObject->getInformationNoFollow();
return $this->render('main/faq.html.twig', [
'locale' => $locale,
'seo' => $seoPage,
'page' => $pageObject
]);
}
/**
* @Route("/about-us", name="app_about_us", methods={"GET"})
*/
public function aboutUs(): Response
{
list($domain, $locale) = $this->getRequestParameter();
/** @var Page $pageObject */
$pageObject = $this->pageRepository->getPageByParameterByUrl('about-us', $domain, $locale);
if (!$pageObject) {
throw $this->createNotFoundException();
}
$seoPage = [];
$seoPage['meta-title'] = $pageObject->getMetaTitle();
$seoPage['meta-desc'] = $pageObject->getMetaDescription();
$seoPage['meta-keywords'] = $pageObject->getMetaKeywords();
$seoPage['nofollow'] = $pageObject->getInformationNoFollow();
return $this->render('main/about-us.html.twig', [
'locale' => $locale,
'seo' => $seoPage,
'page' => $pageObject
]);
}
/**
* @Route("/transfer/{slug}", name="app_transfer", methods={"GET"})
*/
public function transfer(string $slug, RouteRepository $routeRepository, Request $request): Response
{
$route = $routeRepository->findOneBySlug($slug);
list($domain, $locale) = $this->getRequestParameter();
if (!$route) {
throw $this->createNotFoundException('The route does not exist');
}
return $this->render('transfer/show.html.twig', [
'transfer' => $route,
'seo' => $route->getSeoArray(),
'og' => $route->getOgArray(),
'locale' => $locale,
]);
}
/**
* @Route("/outdoor-safety", name="app_outdoor_safety", methods={"GET"})
*/
public function outdoorSafety(OutdoorSafetyRepository $outdoorSafetyRepository): Response
{
list($domain, $locale) = $this->getRequestParameter();
/** @var Page $pageObject */
$pageObject = $this->pageRepository->getPageByParameterByUrl('outdoor-safety', $domain, $locale);
if (!$pageObject) {
throw $this->createNotFoundException();
}
return $this->render('outdoorSafety/index.html.twig', [
'page' => $pageObject,
'seo' => $pageObject->getSeoArray(),
'og' => $pageObject->getOgArray(),
'locale' => $locale,
'outdoorSafetyList' => $outdoorSafetyRepository->findByPageTypeAndLang($domain, $locale, false),
]);
}
/**
* @Route("/webapi/routes", name="api_routes_list", methods={"GET"})
*/
public function apiRoutes(BusStopRepository $busStopRepository): JsonResponse
{
$results = [
'startStops' => $busStopRepository->getBusStopsNamesByTypes([BusStop::TYPE_FIRST_STOP, BusStop::TYPE_OTHER]),
'endStops' => $busStopRepository->getBusStopsNamesByTypes([BusStop::TYPE_TRIP_START]),
];
return new JsonResponse($results, Response::HTTP_OK, []);
}
/**
* @Route("/{slug}", name="app_page_item", methods={"GET"}, requirements={"slug"="[A-Za-z0-9\-]+"}, priority=-255)
*/
public function pageItem(Page $page): Response
{
if (!$page->isVisible()) {
throw $this->createNotFoundException('Page not found');
}
list($domain, $locale) = $this->getRequestParameter();
$seoPage = [];
$seoPage['meta-title'] = $page->getMetaTitle();
$seoPage['meta-desc'] = $page->getMetaDescription();
$seoPage['meta-keywords'] = $page->getMetaKeywords();
$seoPage['nofollow'] = $page->getInformationNoFollow();
return $this->render('main/pageItem.html.twig', [
'locale' => $locale,
'seo' => $seoPage,
'page' => $page,
'travelMenu' => $this->pageRepository->getPageByListed($domain, $locale, 10, ['p.id', 'DESC']),
]);
}
/**
* @Route("/{slug}", name="language_redirect", priority=-256)
*/
public function redirectIfNoLanguagePrefix(
Request $request,
string $slug
): Response {
$slug = $request->getRequestUri();
$uriParts = explode('/', $slug);
$firstPart = $uriParts[0] ?? '';
if (!strlen($firstPart) !== 2 && count($uriParts) > 1) {
return new RedirectResponse('/en' . $slug, 301);
}
throw new NotFoundHttpException('Page not found');
}
}