From: Raphaƫl Gertz Date: Tue, 12 Dec 2023 17:42:51 +0000 (+0100) Subject: Use new prev and next links X-Git-Tag: 0.2~4 X-Git-Url: https://git.rapsys.eu/blogbundle/commitdiff_plain/654b7d88a927d5ec5aabdaf6adc1594335f61877?hp=7711361c419c68c35f8e59b93cbbcc9c15a33ed1 Use new prev and next links --- diff --git a/Controller/KeywordController.php b/Controller/KeywordController.php index d765404..d4649a0 100644 --- a/Controller/KeywordController.php +++ b/Controller/KeywordController.php @@ -30,39 +30,18 @@ class KeywordController extends AbstractController { * @return Response The rendered view */ public function index(Request $request): Response { - //With keywords - if ($count = $this->doctrine->getRepository(Keyword::class)->findCountAsInt()) { - //Negative page or over page - if (($page = (int) $request->get('page', 0)) < 0 || $page > $count / $this->limit) { - //Throw 404 - throw $this->createNotFoundException($this->translator->trans('Unable to find keywords (page: %page%)', ['%page%' => $page])); - } - - //Without keywords - if (empty($this->context['keywords'] = $this->doctrine->getRepository(Keyword::class)->findAllAsArray($page, $this->limit))) { - //Throw 404 - throw $this->createNotFoundException($this->translator->trans('Unable to find keywords')); - } - - //With prev link - if ($page > 0) { - //Set keywords older - $this->context['head']['prev'] = $this->context['keywords_prev'] = $this->generateUrl($request->attributes->get('_route'), ['page' => $page - 1]+$request->attributes->get('_route_params')); - } - - //With next link - if ($count > ($page + 1) * $this->limit) { - //Set keywords newer - $this->context['head']['next'] = $this->context['keywords_next'] = $this->generateUrl($request->attributes->get('_route'), ['page' => $page + 1]+$request->attributes->get('_route_params')); - } + //With not enough keywords + if (($this->count = $this->doctrine->getRepository(Keyword::class)->findCountAsInt()) < $this->page * $this->limit) { + //Throw 404 + throw $this->createNotFoundException($this->translator->trans('Unable to find keywords')); + } + //Get keywords + if ($this->context['keywords'] = $this->doctrine->getRepository(Keyword::class)->findAllAsArray($this->page, $this->limit)) { //Set modified $this->modified = max(array_map(function ($v) { return $v['modified']; }, $this->context['keywords'])); //Without keywords } else { - //Set empty keywords - $this->context['keywords'] = []; - //Set empty modified $this->modified = new \DateTime('-1 year'); }