src/Controller/Frontend/PageController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\Content\Item;
  4. use App\Entity\Content\Type;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Knp\Component\Pager\PaginatorInterface;
  11. use App\Helpers\Content\ContentHelper;
  12. use App\Helpers\System\ConfigHelper;
  13. class PageController extends AbstractController
  14. {
  15.     public function __construct(EntityManagerInterface $entityManagerContentHelper $contentHelperPaginatorInterface $paginatorConfigHelper $configHelper)
  16.     {
  17.         $this->em $entityManager;
  18.         $this->contentHelper $contentHelper;
  19.         $this->paginator $paginator;
  20.         $this->configHelper $configHelper;
  21.     }
  22.     /**
  23.     * @Route("/{path}", name="frontend_viewpage")
  24.     */
  25.     public function viewPage(Request $request$path '')
  26.     {
  27.         // No active language?
  28.         if(!$this->contentHelper->getActiveLanguage())
  29.             throw $this->createNotFoundException("No active language was found!");
  30.         // Url path is index?
  31.         if((strtolower(trim($request->getPathInfo(), "/")) == "index"))
  32.         {
  33.             return $this->redirect(str_replace("index""/"$this->contentHelper->cleanPath($path)));
  34.         }
  35.         else
  36.         {
  37.             // get the url into regments
  38.             $urlParts explode("/"trim($request->getPathInfo(), '/'));
  39.             // More than 2 url parts
  40.             if(count($urlParts) >= 2)
  41.             {
  42.                 // Locale and index? We need to redirect...
  43.                 if((strlen($urlParts[0]) == 2) && ($urlParts[1] == "index"))
  44.                     return $this->redirect(str_replace("index"""$request->getPathInfo()));
  45.             }
  46.         }
  47.         // Find the page
  48.         $page $this->em->getRepository(Item::class)->findOneFiltered(array(
  49.             "path" => (strlen($this->contentHelper->cleanPath($path)) ? $this->contentHelper->cleanPath($path) : 'index'),
  50.             "languageOrNull" => $this->contentHelper->getActiveLanguage(),
  51.             "status" => "published"
  52.         ));
  53.         // Not found?
  54.         if(!$page)
  55.             throw $this->createNotFoundException("The page you're looking for was not found.");
  56.         // Check page access
  57.         $this->contentHelper->checkItemAccess($page$this->getUser());
  58.         // Grab rendered content
  59.         $renderedContent $this->contentHelper->getRenderedItemContent($page$this->getUser());
  60.         // Handle form submission
  61.         $emailSent $this->contentHelper->handleFormSubmission($request$this->getUser());
  62.         if($emailSent)
  63.         {
  64.             // Redirect to thanks page
  65.             return $this->redirect($this->generateUrl('frontend_viewpage', array(
  66.                 'path' => $path "-thanks"
  67.             )));
  68.         }
  69.         // News?
  70.         if($page->getSlug() == "news")
  71.             return $this->news($request$page$renderedContent);
  72.         // Render view
  73.         return $this->render('Frontend/Layouts/default.html.twig', array(
  74.             'page' => $page,
  75.             'renderedContent' => $renderedContent
  76.         ));
  77.     }
  78.     /**
  79.     * @Route("/news", name="frontend_xmlsitemap")
  80.     */
  81.     public function news($request$page$renderedContent)
  82.     {
  83.         $newsContentType $this->em->getRepository(Type::class)->findOneFiltered(array(
  84.             "slug" => "news"
  85.         ));
  86.         if(!$newsContentType)
  87.             throw new \Exception("News content type does not exist! Please add it to display the news.");
  88.         // Get news articles
  89.         $newsArticles $this->paginator->paginate(
  90.             $this->em->getRepository(Item::class)->findFiltered(array(
  91.                 "type" => $newsContentType
  92.             ), array(
  93.                 "i.created" => "DESC"
  94.             )),
  95.             $request->query->get('page'1),
  96.             $this->configHelper->getValue("pagination_limit")
  97.         );
  98.         // Render view
  99.         return $this->render('Frontend/News/index.html.twig', array(
  100.             'newsArticles' => $newsArticles
  101.         ));
  102.     }
  103.     /**
  104.     * @Route("/sitemap.xml", name="frontend_xmlsitemap")
  105.     */
  106.     public function sitemap()
  107.     {
  108.         // Get top level pages
  109.         $allPages $this->em->getRepository(Item::class)->findFiltered(array(
  110.             "parent" => null
  111.         ));
  112.         return new Response($this->buildSitemap($allPagestrue));
  113.     }
  114.     function buildSitemap($pages$topLevel false)
  115.     {
  116.         if($topLevel)
  117.             $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">';
  118.         else
  119.             $string '';
  120.         foreach($pages AS $somePage)
  121.         {
  122.             $string $string '
  123.                 <url>
  124.                     <loc>https://www.apollodistributioncardiff.co.uk' . ($somePage->getSlug() == "index" '' $somePage->getFriendlyPath()) . '</loc>
  125.                     <priority>1.00</priority>
  126.                 </url>
  127.             ';
  128.             $string $string $this->buildSitemap($somePage->getChildren());
  129.         }
  130.         if($topLevel)
  131.             $string $string '</urlset>';
  132.         return $string;
  133.     }
  134. }