diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-01-26 11:15:15 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | 485b168a9677d160b0c0426e4f282b9bd0c632c1 (patch) | |
tree | c7c21ea01096bfe28622b3a23c5c76d1cb2435fa /application/front | |
parent | bee33239ed444f9724422fe5234cd79997500519 (diff) | |
download | Shaarli-485b168a9677d160b0c0426e4f282b9bd0c632c1.tar.gz Shaarli-485b168a9677d160b0c0426e4f282b9bd0c632c1.tar.zst Shaarli-485b168a9677d160b0c0426e4f282b9bd0c632c1.zip |
Process picwall rendering through Slim controller + UT
Diffstat (limited to 'application/front')
-rw-r--r-- | application/front/controllers/PictureWallController.php | 72 | ||||
-rw-r--r-- | application/front/exceptions/ThumbnailsDisabledException.php | 15 |
2 files changed, 87 insertions, 0 deletions
diff --git a/application/front/controllers/PictureWallController.php b/application/front/controllers/PictureWallController.php new file mode 100644 index 00000000..08d31b29 --- /dev/null +++ b/application/front/controllers/PictureWallController.php | |||
@@ -0,0 +1,72 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller; | ||
6 | |||
7 | use Shaarli\Front\Exception\ThumbnailsDisabledException; | ||
8 | use Shaarli\Thumbnailer; | ||
9 | use Slim\Http\Request; | ||
10 | use Slim\Http\Response; | ||
11 | |||
12 | /** | ||
13 | * Class PicturesWallController | ||
14 | * | ||
15 | * Slim controller used to render the pictures wall page. | ||
16 | * If thumbnails mode is set to NONE, we just render the template without any image. | ||
17 | * | ||
18 | * @package Front\Controller | ||
19 | */ | ||
20 | class PictureWallController extends ShaarliController | ||
21 | { | ||
22 | public function index(Request $request, Response $response): Response | ||
23 | { | ||
24 | if ($this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) === Thumbnailer::MODE_NONE) { | ||
25 | throw new ThumbnailsDisabledException(); | ||
26 | } | ||
27 | |||
28 | $this->assignView( | ||
29 | 'pagetitle', | ||
30 | t('Picture wall') .' - '. $this->container->conf->get('general.title', 'Shaarli') | ||
31 | ); | ||
32 | |||
33 | // Optionally filter the results: | ||
34 | $links = $this->container->bookmarkService->search($request->getQueryParams()); | ||
35 | $linksToDisplay = []; | ||
36 | |||
37 | // Get only bookmarks which have a thumbnail. | ||
38 | // Note: we do not retrieve thumbnails here, the request is too heavy. | ||
39 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | ||
40 | foreach ($links as $key => $link) { | ||
41 | if (!empty($link->getThumbnail())) { | ||
42 | $linksToDisplay[] = $formatter->format($link); | ||
43 | } | ||
44 | } | ||
45 | |||
46 | $data = $this->executeHooks($linksToDisplay); | ||
47 | foreach ($data as $key => $value) { | ||
48 | $this->assignView($key, $value); | ||
49 | } | ||
50 | |||
51 | return $response->write($this->render('picwall')); | ||
52 | } | ||
53 | |||
54 | /** | ||
55 | * @param mixed[] $linksToDisplay List of formatted bookmarks | ||
56 | * | ||
57 | * @return mixed[] Template data after active plugins render_picwall hook execution. | ||
58 | */ | ||
59 | protected function executeHooks(array $linksToDisplay): array | ||
60 | { | ||
61 | $data = [ | ||
62 | 'linksToDisplay' => $linksToDisplay, | ||
63 | ]; | ||
64 | $this->container->pluginManager->executeHooks( | ||
65 | 'render_picwall', | ||
66 | $data, | ||
67 | ['loggedin' => $this->container->loginManager->isLoggedIn()] | ||
68 | ); | ||
69 | |||
70 | return $data; | ||
71 | } | ||
72 | } | ||
diff --git a/application/front/exceptions/ThumbnailsDisabledException.php b/application/front/exceptions/ThumbnailsDisabledException.php new file mode 100644 index 00000000..1b9cf5b7 --- /dev/null +++ b/application/front/exceptions/ThumbnailsDisabledException.php | |||
@@ -0,0 +1,15 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Exception; | ||
6 | |||
7 | class ThumbnailsDisabledException extends ShaarliException | ||
8 | { | ||
9 | public function __construct() | ||
10 | { | ||
11 | $message = t('Picture wall unavailable (thumbnails are disabled).'); | ||
12 | |||
13 | parent::__construct($message, 400); | ||
14 | } | ||
15 | } | ||