<?php
namespace App\Controller;
use App\Entity\Content;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Content
*
* @author "Cornell University, Student Services IT"
*/
class ContentController extends AbstractController
{
/**
* @Route("/faq", methods={"GET"})
*
* @return RedirectResponse
*/
public function shortUrl1Action()
{
// get the current default roster
// in the event that no default=Y/available=Y/authRequired=N roster, an exception is thrown
$rosterSlug = $this->getSystemSettings()->getRosterPrintDefault()->getSlug();
// intentionally use 302 here, because when new roster becomes available, the default will change.
return $this->redirectToRoute('app_content_view', ['rosterSlug' => $rosterSlug, 'contentSlug' => 'faq'], 302);
}
/**
* @Route("/using-scheduler", methods={"GET"})
*
* @return RedirectResponse
*/
public function shortUrl2Action()
{
// get the current default roster
// in the event that no default=Y/available=Y/authRequired=N roster, an exception is thrown
$rosterSlug = $this->getSystemSettings()->getRosterPrintDefault()->getSlug();
// intentionally use 302 here, because when new roster becomes available, the default will change.
return $this->redirectToRoute('app_content_view', ['rosterSlug' => $rosterSlug, 'contentSlug' => 'using-scheduler'], 302);
}
/**
* @Route("/using-syllabi-faculty", methods={"GET"})
*
* @return RedirectResponse
*/
public function shortUrl3Action()
{
// get the current default roster
// in the event that no default=Y/available=Y/authRequired=N roster, an exception is thrown
$rosterSlug = $this->getSystemSettings()->getRosterPrintDefault()->getSlug();
// intentionally use 302 here, because when new roster becomes available, the default will change.
return $this->redirectToRoute('app_content_view', ['rosterSlug' => $rosterSlug, 'contentSlug' => 'using-syllabi-faculty'], 302);
}
/**
* @Route("/using-syllabi-students", methods={"GET"})
*
* @return RedirectResponse
*/
public function shortUrl4Action()
{
// get the current default roster
// in the event that no default=Y/available=Y/authRequired=N roster, an exception is thrown
$rosterSlug = $this->getSystemSettings()->getRosterPrintDefault()->getSlug();
// intentionally use 302 here, because when new roster becomes available, the default will change.
return $this->redirectToRoute('app_content_view', ['rosterSlug' => $rosterSlug, 'contentSlug' => 'using-syllabi-students'], 302);
}
/**
* @Route("/api-details", methods={"GET"})
*
* @return RedirectResponse
*/
public function shortUrl5Action()
{
// get the current default roster
// in the event that no default=Y/available=Y/authRequired=N roster, an exception is thrown
$rosterSlug = $this->getSystemSettings()->getRosterPrintDefault()->getSlug();
// intentionally use 302 here, because when new roster becomes available, the default will change.
return $this->redirectToRoute('app_content_view', ['rosterSlug' => $rosterSlug, 'contentSlug' => 'api-details'], 302);
}
/**
* Display a content page. Always viewed in context of a roster.
*
* @Route("/content/{rosterSlug}/{contentSlug}", name="app_content_view", defaults={"rosterSlug" = "", "contentSlug" = ""}, methods={"GET"})
*
* @param string $rosterSlug
* @param string $contentSlug
* @return \Symfony\Component\HttpFoundation\Response
*/
public function viewAction(Request $request, $rosterSlug, $contentSlug)
{
list($roster, $availRosters) = $this->getSpecifiedAndAvailRosters($rosterSlug);
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository(Content::class);
/* @var $content \App\Entity\Content */
$content = $repository->findOneBy(['slug' => $contentSlug]);
// if not found or embedded only - do not disclose that this content exists
if (!$content || $content->isEmbeddedOnly()) {
throw new NotFoundHttpException('Content could not be found.');
}
$response = new Response();
// Only non auth required rosters can be public and cached.
if (!$roster->isAuthRequired()) {
if ($this->isGatewayCacheEnabled()) {
// Set response as public. Otherwise it will be private by default.
$response->setPublic();
$response->setMaxAge($this->getGatewayCacheLifetime());
$response->setSharedMaxAge($this->getGatewayCacheLifetime());
// we're not using validation model, but set our last modified anyway.
if ($content->getUpdatedDttm() > $roster->getLastModified()) {
$response->setLastModified($content->getUpdatedDttm());
} else {
$response->setLastModified($roster->getLastModified());
}
}
} else {
$response->setPrivate();
$response->setMaxAge(0);
$response->headers->addCacheControlDirective('no-store', true);
$response->headers->addCacheControlDirective('no-cache', true);
}
$canonicalUrl = $this->generateCanonicalUrl($request->getRequestUri());
return $this->render('Content/view.html.twig', ['roster' => $roster,
'content' => $content,
'faqNavItems' => $this->getFaqNavItems($rosterSlug),
'availRosters' => $availRosters,
'canonicalUrl' => $canonicalUrl
], $response);
}
}