|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace librarianphp\Web; |
| 4 | + |
| 5 | +use Librarian\Provider\TwigServiceProvider; |
| 6 | +use Librarian\Response; |
| 7 | +use Librarian\Provider\ContentServiceProvider; |
| 8 | +use Librarian\WebController; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class StaticController |
| 12 | + * Renders content from the data dirs |
| 13 | + * @package App\Command\Web |
| 14 | + */ |
| 15 | +class ContentController extends WebController |
| 16 | +{ |
| 17 | + public function handle(): int |
| 18 | + { |
| 19 | + /** @var TwigServiceProvider $twig */ |
| 20 | + $twig = $this->getApp()->twig; |
| 21 | + |
| 22 | + /** @var ContentServiceProvider $content_provider */ |
| 23 | + $content_provider = $this->getApp()->content; |
| 24 | + |
| 25 | + $request = $this->getRequest(); |
| 26 | + |
| 27 | + try { |
| 28 | + $content = $content_provider->fetch($request->getRoute() . '/' . $request->getSlug()); |
| 29 | + |
| 30 | + if ($content === null) { |
| 31 | + $page = 1; |
| 32 | + $limit = $this->getApp()->config->posts_per_page ?? 10; |
| 33 | + $params = $this->getRequest()->getParams(); |
| 34 | + |
| 35 | + if (key_exists('page', $params)) { |
| 36 | + $page = $params['page']; |
| 37 | + } |
| 38 | + |
| 39 | + $start = ($page * $limit) - $limit; |
| 40 | + |
| 41 | + $content_list = $content_provider->fetchFrom($request->getRoute(), $start, $limit); |
| 42 | + $response = new Response($twig->render('content/listing.html.twig', [ |
| 43 | + 'content_list' => $content_list, |
| 44 | + 'total_pages' => $content_provider->fetchTotalPages($limit), |
| 45 | + 'current_page' => $page, |
| 46 | + 'base_url' => $request->getRoute() |
| 47 | + ])); |
| 48 | + |
| 49 | + $response->output(); |
| 50 | + return 0; |
| 51 | + } |
| 52 | + } catch (\Exception $e) { |
| 53 | + Response::redirect('/notfound'); |
| 54 | + } |
| 55 | + |
| 56 | + $output = $twig->render('content/single.html.twig', [ |
| 57 | + 'content' => $content |
| 58 | + ]); |
| 59 | + |
| 60 | + |
| 61 | + $response = new Response($output); |
| 62 | + $response->output(); |
| 63 | + return 0; |
| 64 | + } |
| 65 | +} |
0 commit comments