]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/front/controller/visitor/BookmarkListController.php
Merge pull request #1697 from ArthurHoaro/feature/pagination
[github/shaarli/Shaarli.git] / application / front / controller / visitor / BookmarkListController.php
index 5267c8f5bd14d77830800d4192232534084c8933..4aae26528430cf1e106b2cfde0d2f76e9a9dc7b7 100644 (file)
@@ -33,9 +33,10 @@ class BookmarkListController extends ShaarliVisitorController
 
         $formatter = $this->container->formatterFactory->getFormatter();
         $formatter->addContextData('base_path', $this->container->basePath);
+        $formatter->addContextData('index_url', index_url($this->container->environment));
 
         $searchTags = normalize_spaces($request->getParam('searchtags') ?? '');
-        $searchTerm = escape(normalize_spaces($request->getParam('searchterm') ?? ''));;
+        $searchTerm = escape(normalize_spaces($request->getParam('searchterm') ?? ''));
 
         // Filter bookmarks according search parameters.
         $visibility = $this->container->sessionManager->getSessionParameter('visibility');
@@ -43,39 +44,26 @@ class BookmarkListController extends ShaarliVisitorController
             'searchtags' => $searchTags,
             'searchterm' => $searchTerm,
         ];
-        $linksToDisplay = $this->container->bookmarkService->search(
-            $search,
-            $visibility,
-            false,
-            !!$this->container->sessionManager->getSessionParameter('untaggedonly')
-        ) ?? [];
-
-        // ---- Handle paging.
-        $keys = [];
-        foreach ($linksToDisplay as $key => $value) {
-            $keys[] = $key;
-        }
-
-        $linksPerPage = $this->container->sessionManager->getSessionParameter('LINKS_PER_PAGE', 20) ?: 20;
 
         // Select articles according to paging.
-        $pageCount = (int) ceil(count($keys) / $linksPerPage) ?: 1;
-        $page = (int) $request->getParam('page') ?? 1;
+        $page = (int) ($request->getParam('page') ?? 1);
         $page = $page < 1 ? 1 : $page;
-        $page = $page > $pageCount ? $pageCount : $page;
+        $linksPerPage = $this->container->sessionManager->getSessionParameter('LINKS_PER_PAGE', 20) ?: 20;
 
-        // Start index.
-        $i = ($page - 1) * $linksPerPage;
-        $end = $i + $linksPerPage;
+        $searchResult = $this->container->bookmarkService->search(
+            $search,
+            $visibility,
+            false,
+            !!$this->container->sessionManager->getSessionParameter('untaggedonly'),
+            false,
+            ['offset' => $linksPerPage * ($page - 1), 'limit' => $linksPerPage]
+        ) ?? [];
 
-        $linkDisp = [];
         $save = false;
-        while ($i < $end && $i < count($keys)) {
-            $save = $this->updateThumbnail($linksToDisplay[$keys[$i]], false) || $save;
-            $link = $formatter->format($linksToDisplay[$keys[$i]]);
-
-            $linkDisp[$keys[$i]] = $link;
-            $i++;
+        $links = [];
+        foreach ($searchResult->getBookmarks() as $key => $bookmark) {
+            $save = $this->updateThumbnail($bookmark, false) || $save;
+            $links[$key] = $formatter->format($bookmark);
         }
 
         if ($save) {
@@ -85,30 +73,29 @@ class BookmarkListController extends ShaarliVisitorController
         // Compute paging navigation
         $searchtagsUrl = $searchTags === '' ? '' : '&searchtags=' . urlencode($searchTags);
         $searchtermUrl = $searchTerm === '' ? '' : '&searchterm=' . urlencode($searchTerm);
+        $page = $searchResult->getPage();
 
-        $previous_page_url = '';
-        if ($i !== count($keys)) {
-            $previous_page_url = '?page=' . ($page + 1) . $searchtermUrl . $searchtagsUrl;
-        }
-        $next_page_url = '';
-        if ($page > 1) {
-            $next_page_url = '?page=' . ($page - 1) . $searchtermUrl . $searchtagsUrl;
-        }
+        $previousPageUrl = !$searchResult->isLastPage() ? '?page=' . ($page + 1) . $searchtermUrl . $searchtagsUrl : '';
+        $nextPageUrl = !$searchResult->isFirstPage() ? '?page=' . ($page - 1) . $searchtermUrl . $searchtagsUrl : '';
+
+        $tagsSeparator = $this->container->conf->get('general.tags_separator', ' ');
+        $searchTagsUrlEncoded = array_map('urlencode', tags_str2array($searchTags, $tagsSeparator));
+        $searchTags = !empty($searchTags) ? trim($searchTags, $tagsSeparator) . $tagsSeparator : '';
 
         // Fill all template fields.
         $data = array_merge(
             $this->initializeTemplateVars(),
             [
-                'previous_page_url' => $previous_page_url,
-                'next_page_url' => $next_page_url,
+                'previous_page_url' => $previousPageUrl,
+                'next_page_url' => $nextPageUrl,
                 'page_current' => $page,
-                'page_max' => $pageCount,
-                'result_count' => count($linksToDisplay),
+                'page_max' => $searchResult->getLastPage(),
+                'result_count' => $searchResult->getTotalCount(),
                 'search_term' => escape($searchTerm),
                 'search_tags' => escape($searchTags),
-                'search_tags_url' => array_map('urlencode', explode(' ', $searchTags)),
+                'search_tags_url' => $searchTagsUrlEncoded,
                 'visibility' => $visibility,
-                'links' => $linkDisp,
+                'links' => $links,
             ]
         );
 
@@ -119,8 +106,9 @@ class BookmarkListController extends ShaarliVisitorController
                 return '[' . $tag . ']';
             };
             $data['pagetitle'] .= ! empty($searchTags)
-                ? implode(' ', array_map($bracketWrap, preg_split('/\s+/', $searchTags))) . ' '
-                : '';
+                ? implode(' ', array_map($bracketWrap, tags_str2array($searchTags, $tagsSeparator))) . ' '
+                : ''
+            ;
             $data['pagetitle'] .= '- ';
         }
 
@@ -137,8 +125,10 @@ class BookmarkListController extends ShaarliVisitorController
      */
     public function permalink(Request $request, Response $response, array $args): Response
     {
+        $privateKey = $request->getParam('key');
+
         try {
-            $bookmark = $this->container->bookmarkService->findByHash($args['hash']);
+            $bookmark = $this->container->bookmarkService->findByHash($args['hash'], $privateKey);
         } catch (BookmarkNotFoundException $e) {
             $this->assignView('error_message', $e->getMessage());
 
@@ -149,11 +139,12 @@ class BookmarkListController extends ShaarliVisitorController
 
         $formatter = $this->container->formatterFactory->getFormatter();
         $formatter->addContextData('base_path', $this->container->basePath);
+        $formatter->addContextData('index_url', index_url($this->container->environment));
 
         $data = array_merge(
             $this->initializeTemplateVars(),
             [
-                'pagetitle' => $bookmark->getTitle() .' - '. $this->container->conf->get('general.title', 'Shaarli'),
+                'pagetitle' => $bookmark->getTitle() . ' - ' . $this->container->conf->get('general.title', 'Shaarli'),
                 'links' => [$formatter->format($bookmark)],
             ]
         );
@@ -178,7 +169,8 @@ class BookmarkListController extends ShaarliVisitorController
             $bookmark->setThumbnail(null);
 
             // Requires an update, not async retrieval, thumbnails enabled
-            if ($bookmark->shouldUpdateThumbnail()
+            if (
+                $bookmark->shouldUpdateThumbnail()
                 && true !== $this->container->conf->get('general.enable_async_metadata', true)
                 && $this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
             ) {