]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/front/controller/admin/ThumbnailsController.php
Process thumbnail synchronize page through Slim controllers
[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;
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11/**
12 * Class ToolsController
13 *
14 * Slim controller used to handle thumbnails update.
15 */
16class ThumbnailsController extends ShaarliAdminController
17{
18 /**
19 * GET /admin/thumbnails - Display thumbnails update page
20 */
21 public function index(Request $request, Response $response): Response
22 {
23 $ids = [];
24 foreach ($this->container->bookmarkService->search() as $bookmark) {
25 // A note or not HTTP(S)
26 if ($bookmark->isNote() || !startsWith(strtolower($bookmark->getUrl()), 'http')) {
27 continue;
28 }
29
30 $ids[] = $bookmark->getId();
31 }
32
33 $this->assignView('ids', $ids);
34 $this->assignView(
35 'pagetitle',
36 t('Thumbnails update') .' - '. $this->container->conf->get('general.title', 'Shaarli')
37 );
38
39 return $response->write($this->render('thumbnails'));
40 }
41
42 /**
43 * PATCH /admin/shaare/{id}/thumbnail-update - Route for AJAX calls
44 */
45 public function ajaxUpdate(Request $request, Response $response, array $args): Response
46 {
47 $id = $args['id'] ?? null;
48
49 if (false === ctype_digit($id)) {
50 return $response->withStatus(400);
51 }
52
53 try {
54 $bookmark = $this->container->bookmarkService->get($id);
55 } catch (BookmarkNotFoundException $e) {
56 return $response->withStatus(404);
57 }
58
59 $bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl()));
60 $this->container->bookmarkService->set($bookmark);
61
62 return $response->withJson($this->container->formatterFactory->getFormatter('raw')->format($bookmark));
63 }
64
65 /**
66 * @param mixed[] $data Variables passed to the template engine
67 *
68 * @return mixed[] Template data after active plugins render_picwall hook execution.
69 */
70 protected function executeHooks(array $data): array
71 {
72 $this->container->pluginManager->executeHooks(
73 'render_tools',
74 $data
75 );
76
77 return $data;
78 }
79}