]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/front/controller/admin/ThumbnailsController.php
Handle pagination through BookmarkService
[github/shaarli/Shaarli.git] / application / front / controller / admin / ThumbnailsController.php
CommitLineData
6132d647
A
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
1a8ac737 8use Shaarli\Render\TemplatePage;
6132d647
A
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12/**
13 * Class ToolsController
14 *
15 * Slim controller used to handle thumbnails update.
16 */
17class ThumbnailsController extends ShaarliAdminController
18{
19 /**
20 * GET /admin/thumbnails - Display thumbnails update page
21 */
22 public function index(Request $request, Response $response): Response
23 {
24 $ids = [];
9b8c0a45 25 foreach ($this->container->bookmarkService->search()->getBookmarks() as $bookmark) {
6132d647
A
26 // A note or not HTTP(S)
27 if ($bookmark->isNote() || !startsWith(strtolower($bookmark->getUrl()), 'http')) {
28 continue;
29 }
30
31 $ids[] = $bookmark->getId();
32 }
33
34 $this->assignView('ids', $ids);
35 $this->assignView(
36 'pagetitle',
53054b2b 37 t('Thumbnails update') . ' - ' . $this->container->conf->get('general.title', 'Shaarli')
6132d647
A
38 );
39
1a8ac737 40 return $response->write($this->render(TemplatePage::THUMBNAILS));
6132d647
A
41 }
42
43 /**
44 * PATCH /admin/shaare/{id}/thumbnail-update - Route for AJAX calls
45 */
46 public function ajaxUpdate(Request $request, Response $response, array $args): Response
47 {
48 $id = $args['id'] ?? null;
49
50 if (false === ctype_digit($id)) {
51 return $response->withStatus(400);
52 }
53
54 try {
efb7d21b 55 $bookmark = $this->container->bookmarkService->get((int) $id);
6132d647
A
56 } catch (BookmarkNotFoundException $e) {
57 return $response->withStatus(404);
58 }
59
60 $bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl()));
61 $this->container->bookmarkService->set($bookmark);
62
63 return $response->withJson($this->container->formatterFactory->getFormatter('raw')->format($bookmark));
64 }
6132d647 65}