aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/front/controllers/SessionFilterController.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-05-22 11:02:56 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commitaf290059d10319e76d1e7d78b592cab99c26d91a (patch)
treeb088526052182d4b4f3c0af20db89f7d28fc3d9a /application/front/controllers/SessionFilterController.php
parent893f5159c64e5bcff505c8367e6dc22cc2a7b14d (diff)
downloadShaarli-af290059d10319e76d1e7d78b592cab99c26d91a.tar.gz
Shaarli-af290059d10319e76d1e7d78b592cab99c26d91a.tar.zst
Shaarli-af290059d10319e76d1e7d78b592cab99c26d91a.zip
Process session filters through Slim controllers
Including: - visibility - links per page - untagged only
Diffstat (limited to 'application/front/controllers/SessionFilterController.php')
-rw-r--r--application/front/controllers/SessionFilterController.php81
1 files changed, 81 insertions, 0 deletions
diff --git a/application/front/controllers/SessionFilterController.php b/application/front/controllers/SessionFilterController.php
new file mode 100644
index 00000000..a021dc37
--- /dev/null
+++ b/application/front/controllers/SessionFilterController.php
@@ -0,0 +1,81 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Shaarli\Bookmark\BookmarkFilter;
8use Shaarli\Security\SessionManager;
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12/**
13 * Class SessionFilterController
14 *
15 * Slim controller used to handle filters stored in the user session, such as visibility, links per page, etc.
16 *
17 * @package Shaarli\Front\Controller
18 */
19class SessionFilterController extends ShaarliController
20{
21 /**
22 * GET /links-per-page: set the number of bookmarks to display per page in homepage
23 */
24 public function linksPerPage(Request $request, Response $response): Response
25 {
26 $linksPerPage = $request->getParam('nb') ?? null;
27 if (null === $linksPerPage || false === is_numeric($linksPerPage)) {
28 $linksPerPage = $this->container->conf->get('general.links_per_page', 20);
29 }
30
31 $this->container->sessionManager->setSessionParameter(
32 SessionManager::KEY_LINKS_PER_PAGE,
33 abs(intval($linksPerPage))
34 );
35
36 return $this->redirectFromReferer($response, ['linksperpage'], ['nb']);
37 }
38
39 /**
40 * GET /visibility: allows to display only public or only private bookmarks in linklist
41 */
42 public function visibility(Request $request, Response $response, array $args): Response
43 {
44 if (false === $this->container->loginManager->isLoggedIn()) {
45 return $this->redirectFromReferer($response, ['visibility']);
46 }
47
48 $newVisibility = $args['visibility'] ?? null;
49 if (false === in_array($newVisibility, [BookmarkFilter::$PRIVATE, BookmarkFilter::$PUBLIC], true)) {
50 $newVisibility = null;
51 }
52
53 $currentVisibility = $this->container->sessionManager->getSessionParameter(SessionManager::KEY_VISIBILITY);
54
55 // Visibility not set or not already expected value, set expected value, otherwise reset it
56 if ($newVisibility !== null && (null === $currentVisibility || $currentVisibility !== $newVisibility)) {
57 // See only public bookmarks
58 $this->container->sessionManager->setSessionParameter(
59 SessionManager::KEY_VISIBILITY,
60 $newVisibility
61 );
62 } else {
63 $this->container->sessionManager->deleteSessionParameter(SessionManager::KEY_VISIBILITY);
64 }
65
66 return $this->redirectFromReferer($response, ['visibility']);
67 }
68
69 /**
70 * GET /untagged-only: allows to display only bookmarks without any tag
71 */
72 public function untaggedOnly(Request $request, Response $response): Response
73 {
74 $this->container->sessionManager->setSessionParameter(
75 SessionManager::KEY_UNTAGGED_ONLY,
76 empty($this->container->sessionManager->getSessionParameter(SessionManager::KEY_UNTAGGED_ONLY))
77 );
78
79 return $this->redirectFromReferer($response, ['untaggedonly', 'untagged-only']);
80 }
81}