src/Controller/ContentController.php line 103

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Content;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. /**
  10.  * Content
  11.  *
  12.  * @author "Cornell University, Student Services IT"
  13.  */
  14. class ContentController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/faq", methods={"GET"})
  18.      *
  19.      * @return RedirectResponse
  20.      */
  21.     public function shortUrl1Action()
  22.     {
  23.         // get the current default roster
  24.         // in the event that no default=Y/available=Y/authRequired=N roster, an exception is thrown
  25.         $rosterSlug $this->getSystemSettings()->getRosterPrintDefault()->getSlug();
  26.         // intentionally use 302 here, because when new roster becomes available, the default will change.
  27.         return $this->redirectToRoute('app_content_view', ['rosterSlug' => $rosterSlug'contentSlug' => 'faq'], 302);
  28.     }
  29.     /**
  30.      * @Route("/using-scheduler", methods={"GET"})
  31.      *
  32.      * @return RedirectResponse
  33.      */
  34.     public function shortUrl2Action()
  35.     {
  36.         // get the current default roster
  37.         // in the event that no default=Y/available=Y/authRequired=N roster, an exception is thrown
  38.         $rosterSlug $this->getSystemSettings()->getRosterPrintDefault()->getSlug();
  39.         // intentionally use 302 here, because when new roster becomes available, the default will change.
  40.         return $this->redirectToRoute('app_content_view', ['rosterSlug' => $rosterSlug'contentSlug' => 'using-scheduler'], 302);
  41.     }
  42.     /**
  43.      * @Route("/using-syllabi-faculty", methods={"GET"})
  44.      *
  45.      * @return RedirectResponse
  46.      */
  47.     public function shortUrl3Action()
  48.     {
  49.         // get the current default roster
  50.         // in the event that no default=Y/available=Y/authRequired=N roster, an exception is thrown
  51.         $rosterSlug $this->getSystemSettings()->getRosterPrintDefault()->getSlug();
  52.         // intentionally use 302 here, because when new roster becomes available, the default will change.
  53.         return $this->redirectToRoute('app_content_view', ['rosterSlug' => $rosterSlug'contentSlug' => 'using-syllabi-faculty'], 302);
  54.     }
  55.     /**
  56.      * @Route("/using-syllabi-students", methods={"GET"})
  57.      *
  58.      * @return RedirectResponse
  59.      */
  60.     public function shortUrl4Action()
  61.     {
  62.         // get the current default roster
  63.         // in the event that no default=Y/available=Y/authRequired=N roster, an exception is thrown
  64.         $rosterSlug $this->getSystemSettings()->getRosterPrintDefault()->getSlug();
  65.         // intentionally use 302 here, because when new roster becomes available, the default will change.
  66.         return $this->redirectToRoute('app_content_view', ['rosterSlug' => $rosterSlug'contentSlug' => 'using-syllabi-students'], 302);
  67.     }
  68.     /**
  69.      * @Route("/api-details", methods={"GET"})
  70.      *
  71.      * @return RedirectResponse
  72.      */
  73.     public function shortUrl5Action()
  74.     {
  75.         // get the current default roster
  76.         // in the event that no default=Y/available=Y/authRequired=N roster, an exception is thrown
  77.         $rosterSlug $this->getSystemSettings()->getRosterPrintDefault()->getSlug();
  78.         // intentionally use 302 here, because when new roster becomes available, the default will change.
  79.         return $this->redirectToRoute('app_content_view', ['rosterSlug' => $rosterSlug'contentSlug' => 'api-details'], 302);
  80.     }
  81.     /**
  82.      * Display a content page. Always viewed in context of a roster.
  83.      *
  84.      * @Route("/content/{rosterSlug}/{contentSlug}", name="app_content_view", defaults={"rosterSlug" = "", "contentSlug" = ""}, methods={"GET"})
  85.      *
  86.      * @param string $rosterSlug
  87.      * @param string $contentSlug
  88.      * @return \Symfony\Component\HttpFoundation\Response
  89.      */
  90.     public function viewAction(Request $request$rosterSlug$contentSlug)
  91.     {
  92.         list($roster$availRosters) = $this->getSpecifiedAndAvailRosters($rosterSlug);
  93.         $em $this->getDoctrine()->getManager();
  94.         $repository $em->getRepository(Content::class);
  95.         /* @var $content \App\Entity\Content */
  96.         $content $repository->findOneBy(['slug' => $contentSlug]);
  97.         // if not found or embedded only - do not disclose that this content exists
  98.         if (!$content || $content->isEmbeddedOnly()) {
  99.             throw new NotFoundHttpException('Content could not be found.');
  100.         }
  101.         $response = new Response();
  102.         // Only non auth required rosters can be public and cached.
  103.         if (!$roster->isAuthRequired()) {
  104.             if ($this->isGatewayCacheEnabled()) {
  105.                 // Set response as public. Otherwise it will be private by default.
  106.                 $response->setPublic();
  107.                 $response->setMaxAge($this->getGatewayCacheLifetime());
  108.                 $response->setSharedMaxAge($this->getGatewayCacheLifetime());
  109.                 // we're not using validation model, but set our last modified anyway.
  110.                 if ($content->getUpdatedDttm() > $roster->getLastModified()) {
  111.                     $response->setLastModified($content->getUpdatedDttm());
  112.                 } else {
  113.                     $response->setLastModified($roster->getLastModified());
  114.                 }
  115.             }
  116.         } else {
  117.             $response->setPrivate();
  118.             $response->setMaxAge(0);
  119.             $response->headers->addCacheControlDirective('no-store'true);
  120.             $response->headers->addCacheControlDirective('no-cache'true);
  121.         }
  122.         $canonicalUrl $this->generateCanonicalUrl($request->getRequestUri());
  123.         return $this->render('Content/view.html.twig', ['roster' => $roster,
  124.             'content' => $content,
  125.             'faqNavItems' => $this->getFaqNavItems($rosterSlug),
  126.             'availRosters' => $availRosters,
  127.             'canonicalUrl' => $canonicalUrl
  128.         ], $response);
  129.     }
  130. }