src/App/Controller/Web/Sites/FeedController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Web\Sites;
  3. use App\Component\ViewLayer\Views\WebDocView;
  4. use App\Doctrine\Repository\Feed\FeedSearch;
  5. use App\Entity\Feed\AbstractEntry;
  6. use App\Entity\Feed\Entry\AbstractContentEntry;
  7. use App\Entity\System\School;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. final class FeedController extends AbstractSiteController
  12. {
  13.     public const ROUTES__LANDING__ROOT 'app.web.sites.feed.landing.root';
  14.     public const ROUTES__LANDING__SUB 'app.web.sites.feed.landing.sub';
  15.     public const ROUTES__VIEW__ROOT 'app.web.sites.feed.view.root';
  16.     public const ROUTES__VIEW__SUB 'app.web.sites.feed.view.sub';
  17.     private const PAGE_SIZE 12;
  18.     /**
  19.      * @return WebDocView
  20.      *
  21.      * @Route(
  22.      *     "/feed",
  23.      *     name = self::ROUTES__LANDING__ROOT,
  24.      * )
  25.      * @Route(
  26.      *     "/{path}/feed",
  27.      *     name = self::ROUTES__LANDING__SUB,
  28.      *     requirements = {
  29.      *         "path" = ".+",
  30.      *     },
  31.      * )
  32.      */
  33.     public function landingAction(): WebDocView
  34.     {
  35.         // get the context
  36.         $context $this->contextualize();
  37.         // set title for page
  38.         $context->getDom()->getTitle()
  39.             ->setTitle('Feed')
  40.             ->setSection($context->getDepartment()->getName())
  41.             ->setProduct(null)
  42.             ->setBrand($context->getTenant()->getName());
  43.         // set crumbs
  44.         $context->getVars()->set('crumbs', [
  45.             'module' => [
  46.                 'key' => 'feed',
  47.                 'name' => 'Feed',
  48.             ],
  49.             'text' => null,
  50.         ]);
  51.         // get feed entries
  52.         $results $this->getEntityManager()->getRepository(AbstractContentEntry::class)->findBySearch(
  53.             $search = (new FeedSearch())
  54.                 ->setRoot($context->getRootDepartment())
  55.                 ->setDistrict(false)
  56.                 ->setSchools(($context->getSchool() instanceof School && ! $context->getSchool()->isTypeDistrict()) ? $context->getSchool() : null)
  57.                 ->setBoosted(true)
  58.                 ->setDeep(false)
  59.                 ->setLimit(self::PAGE_SIZE 1)
  60.                 ->setOffset(self::PAGE_SIZE * ($page $this->getRequest()->get('page'0))),
  61.         )->getIterator()->getArrayCopy();
  62.         return (new WebDocView([
  63.             'vars' => $context,
  64.             'search' => $search,
  65.             // TODO: this pagination stuff needs done better as there is a side-effect of the function that is not clear...
  66.             'pagination' => $this->pagination($results$page),
  67.             'results' => $results,
  68.         ]))->setRenderContext($context);
  69.     }
  70.     /**
  71.      * @param string $id
  72.      * @param string $slug
  73.      * @return WebDocView|RedirectResponse
  74.      *
  75.      * @Route(
  76.      *     "/feed/{id}/{slug}",
  77.      *     name = self::ROUTES__VIEW__ROOT,
  78.      *     requirements = {
  79.      *         "id" = "[^/]+",
  80.      *         "slug" = "[^/]+",
  81.      *     },
  82.      * )
  83.      * @Route(
  84.      *     "/{path}/feed/{id}/{slug}",
  85.      *     name = self::ROUTES__VIEW__SUB,
  86.      *     requirements = {
  87.      *         "path" = ".+",
  88.      *         "id" = "[^/]+",
  89.      *         "slug" = "[^/]+",
  90.      *     },
  91.      * )
  92.      */
  93.     public function viewAction(string $idstring $slug)
  94.     {
  95.         // get the context
  96.         $context $this->contextualize();
  97.         // try to find based on the id
  98.         $entry $this->getEntityManager()->getRepository(AbstractEntry::class)->find(
  99.             $id,
  100.         );
  101.         if ( ! $entry) {
  102.             throw new NotFoundHttpException();
  103.         }
  104.         // if the slug is not the same, need to redirect
  105.         if ($entry->getSlug() && $entry->getSlug() !== strtolower($slug)) {
  106.             return $this->redirectToRoute(
  107.                 $this->getRequest()->attributes->get('_route'),
  108.                 array_merge(
  109.                     $this->getRequest()->attributes->get('_route_params'),
  110.                     [
  111.                         'slug' => $entry->getSlug(),
  112.                     ],
  113.                 ),
  114.             );
  115.         }
  116.         // set title for page
  117.         $context->getDom()->getTitle()
  118.             ->setTitle($entry->getLabel())
  119.             ->setSection($context->getDepartment()->getName())
  120.             ->setProduct(null)
  121.             ->setBrand($context->getTenant()->getName());
  122.         // set crumbs
  123.         $context->getVars()->set('crumbs', [
  124.             'module' => [
  125.                 'key' => 'feed',
  126.                 'name' => 'Feed',
  127.             ],
  128.             'text' => $entry->getLabel(),
  129.         ]);
  130.         return (new WebDocView([
  131.             'vars' => $context,
  132.             'entry' => $entry,
  133.         ]))->setRenderContext($context);
  134.     }
  135.     /**
  136.      * @param array $items
  137.      * @param int $page
  138.      * @return array|null
  139.      */
  140.     private function pagination(array &$itemsint $page 0): ?array
  141.     {
  142.         $pagination = array(
  143.             'previous' => ($page 0) ? ($page 1) : null,
  144.             'current' => $page,
  145.             'next' => null,
  146.         );
  147.         if (count($items) > self::PAGE_SIZE) {
  148.             $pagination['next'] = $page 1;
  149.             array_pop($items);
  150.         }
  151.         if (empty($pagination['previous']) && empty($pagination['next'])) {
  152.             $pagination null;
  153.         }
  154.         if ($page && count($items) === 0) {
  155.             throw new NotFoundHttpException();
  156.         }
  157.         return $pagination;
  158.     }
  159. }