<?php
namespace App\Controller\Frontend;
use App\Entity\Content\Item;
use App\Entity\Content\Type;
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\Helpers\Content\ContentHelper;
use App\Helpers\System\ConfigHelper;
class PageController extends AbstractController
{
public function __construct(EntityManagerInterface $entityManager, ContentHelper $contentHelper, PaginatorInterface $paginator, ConfigHelper $configHelper)
{
$this->em = $entityManager;
$this->contentHelper = $contentHelper;
$this->paginator = $paginator;
$this->configHelper = $configHelper;
}
/**
* @Route("/{path}", name="frontend_viewpage")
*/
public function viewPage(Request $request, $path = '')
{
// No active language?
if(!$this->contentHelper->getActiveLanguage())
throw $this->createNotFoundException("No active language was found!");
// Url path is index?
if((strtolower(trim($request->getPathInfo(), "/")) == "index"))
{
return $this->redirect(str_replace("index", "/", $this->contentHelper->cleanPath($path)));
}
else
{
// get the url into regments
$urlParts = explode("/", trim($request->getPathInfo(), '/'));
// More than 2 url parts
if(count($urlParts) >= 2)
{
// Locale and index? We need to redirect...
if((strlen($urlParts[0]) == 2) && ($urlParts[1] == "index"))
return $this->redirect(str_replace("index", "", $request->getPathInfo()));
}
}
// Find the page
$page = $this->em->getRepository(Item::class)->findOneFiltered(array(
"path" => (strlen($this->contentHelper->cleanPath($path)) ? $this->contentHelper->cleanPath($path) : 'index'),
"languageOrNull" => $this->contentHelper->getActiveLanguage(),
"status" => "published"
));
// Not found?
if(!$page)
throw $this->createNotFoundException("The page you're looking for was not found.");
// Check page access
$this->contentHelper->checkItemAccess($page, $this->getUser());
// Grab rendered content
$renderedContent = $this->contentHelper->getRenderedItemContent($page, $this->getUser());
// Handle form submission
$emailSent = $this->contentHelper->handleFormSubmission($request, $this->getUser());
if($emailSent)
{
// Redirect to thanks page
return $this->redirect($this->generateUrl('frontend_viewpage', array(
'path' => $path . "-thanks"
)));
}
// News?
if($page->getSlug() == "news")
return $this->news($request, $page, $renderedContent);
// Render view
return $this->render('Frontend/Layouts/default.html.twig', array(
'page' => $page,
'renderedContent' => $renderedContent
));
}
/**
* @Route("/news", name="frontend_xmlsitemap")
*/
public function news($request, $page, $renderedContent)
{
$newsContentType = $this->em->getRepository(Type::class)->findOneFiltered(array(
"slug" => "news"
));
if(!$newsContentType)
throw new \Exception("News content type does not exist! Please add it to display the news.");
// Get news articles
$newsArticles = $this->paginator->paginate(
$this->em->getRepository(Item::class)->findFiltered(array(
"type" => $newsContentType
), array(
"i.created" => "DESC"
)),
$request->query->get('page', 1),
$this->configHelper->getValue("pagination_limit")
);
// Render view
return $this->render('Frontend/News/index.html.twig', array(
'newsArticles' => $newsArticles
));
}
/**
* @Route("/sitemap.xml", name="frontend_xmlsitemap")
*/
public function sitemap()
{
// Get top level pages
$allPages = $this->em->getRepository(Item::class)->findFiltered(array(
"parent" => null
));
return new Response($this->buildSitemap($allPages, true));
}
function buildSitemap($pages, $topLevel = false)
{
if($topLevel)
$string = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
else
$string = '';
foreach($pages AS $somePage)
{
$string = $string . '
<url>
<loc>https://www.apollodistributioncardiff.co.uk' . ($somePage->getSlug() == "index" ? '' : $somePage->getFriendlyPath()) . '</loc>
<priority>1.00</priority>
</url>
';
$string = $string . $this->buildSitemap($somePage->getChildren());
}
if($topLevel)
$string = $string . '</urlset>';
return $string;
}
}