<?php
namespace App\Controller\Web\Sites;
use App\Component\ViewLayer\Views\WebDocView;
use App\Doctrine\Repository\Feed\FeedSearch;
use App\Entity\Feed\AbstractEntry;
use App\Entity\Feed\Entry\AbstractContentEntry;
use App\Entity\System\School;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
final class FeedController extends AbstractSiteController
{
public const ROUTES__LANDING__ROOT = 'app.web.sites.feed.landing.root';
public const ROUTES__LANDING__SUB = 'app.web.sites.feed.landing.sub';
public const ROUTES__VIEW__ROOT = 'app.web.sites.feed.view.root';
public const ROUTES__VIEW__SUB = 'app.web.sites.feed.view.sub';
private const PAGE_SIZE = 12;
/**
* @return WebDocView
*
* @Route(
* "/feed",
* name = self::ROUTES__LANDING__ROOT,
* )
* @Route(
* "/{path}/feed",
* name = self::ROUTES__LANDING__SUB,
* requirements = {
* "path" = ".+",
* },
* )
*/
public function landingAction(): WebDocView
{
// get the context
$context = $this->contextualize();
// set title for page
$context->getDom()->getTitle()
->setTitle('Feed')
->setSection($context->getDepartment()->getName())
->setProduct(null)
->setBrand($context->getTenant()->getName());
// set crumbs
$context->getVars()->set('crumbs', [
'module' => [
'key' => 'feed',
'name' => 'Feed',
],
'text' => null,
]);
// get feed entries
$results = $this->getEntityManager()->getRepository(AbstractContentEntry::class)->findBySearch(
$search = (new FeedSearch())
->setRoot($context->getRootDepartment())
->setDistrict(false)
->setSchools(($context->getSchool() instanceof School && ! $context->getSchool()->isTypeDistrict()) ? $context->getSchool() : null)
->setBoosted(true)
->setDeep(false)
->setLimit(self::PAGE_SIZE + 1)
->setOffset(self::PAGE_SIZE * ($page = $this->getRequest()->get('page', 0))),
)->getIterator()->getArrayCopy();
return (new WebDocView([
'vars' => $context,
'search' => $search,
// TODO: this pagination stuff needs done better as there is a side-effect of the function that is not clear...
'pagination' => $this->pagination($results, $page),
'results' => $results,
]))->setRenderContext($context);
}
/**
* @param string $id
* @param string $slug
* @return WebDocView|RedirectResponse
*
* @Route(
* "/feed/{id}/{slug}",
* name = self::ROUTES__VIEW__ROOT,
* requirements = {
* "id" = "[^/]+",
* "slug" = "[^/]+",
* },
* )
* @Route(
* "/{path}/feed/{id}/{slug}",
* name = self::ROUTES__VIEW__SUB,
* requirements = {
* "path" = ".+",
* "id" = "[^/]+",
* "slug" = "[^/]+",
* },
* )
*/
public function viewAction(string $id, string $slug)
{
// get the context
$context = $this->contextualize();
// try to find based on the id
$entry = $this->getEntityManager()->getRepository(AbstractEntry::class)->find(
$id,
);
if ( ! $entry) {
throw new NotFoundHttpException();
}
// if the slug is not the same, need to redirect
if ($entry->getSlug() && $entry->getSlug() !== strtolower($slug)) {
return $this->redirectToRoute(
$this->getRequest()->attributes->get('_route'),
array_merge(
$this->getRequest()->attributes->get('_route_params'),
[
'slug' => $entry->getSlug(),
],
),
);
}
// set title for page
$context->getDom()->getTitle()
->setTitle($entry->getLabel())
->setSection($context->getDepartment()->getName())
->setProduct(null)
->setBrand($context->getTenant()->getName());
// set crumbs
$context->getVars()->set('crumbs', [
'module' => [
'key' => 'feed',
'name' => 'Feed',
],
'text' => $entry->getLabel(),
]);
return (new WebDocView([
'vars' => $context,
'entry' => $entry,
]))->setRenderContext($context);
}
/**
* @param array $items
* @param int $page
* @return array|null
*/
private function pagination(array &$items, int $page = 0): ?array
{
$pagination = array(
'previous' => ($page > 0) ? ($page - 1) : null,
'current' => $page,
'next' => null,
);
if (count($items) > self::PAGE_SIZE) {
$pagination['next'] = $page + 1;
array_pop($items);
}
if (empty($pagination['previous']) && empty($pagination['next'])) {
$pagination = null;
}
if ($page > 0 && count($items) === 0) {
throw new NotFoundHttpException();
}
return $pagination;
}
}