<?php
namespace App\Controller\Frontend\Pages;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use App\Helper\ConfigHelper;
use App\Helper\ContentHelper;
use App\Entity\Content\Pages\Page;
use App\Entity\Properties\Area;
use App\Entity\Properties\Property;
use App\Entity\Properties\Type;
final class PageController extends AbstractController
{
public function __construct(EntityManagerInterface $entityManager, PaginatorInterface $paginator, ConfigHelper $configHelper, ContentHelper $contentHelper)
{
$this->em = $entityManager;
$this->paginator = $paginator;
$this->configHelper = $configHelper;
$this->contentHelper = $contentHelper;
}
/**
* @Route("/", name="frontend_index", priority="-1")
*/
public function index(Request $request, $path = ''): Response
{
// Get featured properties
$featuredProperties = $this->em->getRepository(Property::class)->findFiltered([
"showOnWebsite" => true
], [], 20);
// Get all property types
$allTypes = $this->em->getRepository(Type::class)->findFiltered([]);
// Get all areas
$allAreas = $this->em->getRepository(Area::class)->findFiltered([]);
// Render view
return $this->render('Frontend/Pages/index.html.twig', [
'featuredProperties' => $featuredProperties,
'allTypes' => $allTypes,
'allAreas' => $allAreas
]);
}
/**
* @Route("/{path}", name="frontend_page", priority="-1", requirements={"path"=".+"})
*/
public function viewPage(Request $request, $path = ''): Response
{
// Clean the path
$cleanPath = $this->contentHelper->cleanPath($path) ? $this->contentHelper->cleanPath($path) : "index";
// Url path is index?
if((strtolower(trim($request->getPathInfo(), "/")) == "index"))
return $this->redirect(str_replace("index", "/", $cleanPath));
// Find the page
$page = $this->em->getRepository(Page::class)->findOneFiltered(array(
"path" => $cleanPath
));
// Not found?
if(!$page)
throw $this->createNotFoundException("The page you're looking for was not found.");
// Grab rendered content
$renderedContent = $this->contentHelper->getRenderedPageContent($page, $this->getUser());
// Render view
return $this->render('Frontend/Pages/page.html.twig', [
'page' => $page,
'renderedContent' => $renderedContent
]);
}
}