aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/front
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2021-02-04 10:57:44 +0100
committerGitHub <noreply@github.com>2021-02-04 10:57:44 +0100
commit8997ae6c8e24286f7d47981eaf905e80d2481c10 (patch)
tree9906b122998ca4420af68b1bb110033b99f7d8bf /application/front
parent11edc143b42a7be09c0c9dc02730c83e8cbb73c2 (diff)
parent9b8c0a4560fa1d87cab1529099b1b4677e92e265 (diff)
downloadShaarli-8997ae6c8e24286f7d47981eaf905e80d2481c10.tar.gz
Shaarli-8997ae6c8e24286f7d47981eaf905e80d2481c10.tar.zst
Shaarli-8997ae6c8e24286f7d47981eaf905e80d2481c10.zip
Merge pull request #1697 from ArthurHoaro/feature/pagination
Handle pagination through BookmarkService
Diffstat (limited to 'application/front')
-rw-r--r--application/front/controller/admin/ManageTagController.php11
-rw-r--r--application/front/controller/admin/ThumbnailsController.php2
-rw-r--r--application/front/controller/visitor/BookmarkListController.php63
-rw-r--r--application/front/controller/visitor/DailyController.php2
-rw-r--r--application/front/controller/visitor/PictureWallController.php12
5 files changed, 37 insertions, 53 deletions
diff --git a/application/front/controller/admin/ManageTagController.php b/application/front/controller/admin/ManageTagController.php
index 8675a0c5..1333cce7 100644
--- a/application/front/controller/admin/ManageTagController.php
+++ b/application/front/controller/admin/ManageTagController.php
@@ -57,9 +57,12 @@ class ManageTagController extends ShaarliAdminController
57 } 57 }
58 58
59 // TODO: move this to bookmark service 59 // TODO: move this to bookmark service
60 $count = 0; 60 $searchResult = $this->container->bookmarkService->search(
61 $bookmarks = $this->container->bookmarkService->search(['searchtags' => $fromTag], BookmarkFilter::$ALL, true); 61 ['searchtags' => $fromTag],
62 foreach ($bookmarks as $bookmark) { 62 BookmarkFilter::$ALL,
63 true
64 );
65 foreach ($searchResult->getBookmarks() as $bookmark) {
63 if (false === $isDelete) { 66 if (false === $isDelete) {
64 $bookmark->renameTag($fromTag, $toTag); 67 $bookmark->renameTag($fromTag, $toTag);
65 } else { 68 } else {
@@ -68,11 +71,11 @@ class ManageTagController extends ShaarliAdminController
68 71
69 $this->container->bookmarkService->set($bookmark, false); 72 $this->container->bookmarkService->set($bookmark, false);
70 $this->container->history->updateLink($bookmark); 73 $this->container->history->updateLink($bookmark);
71 $count++;
72 } 74 }
73 75
74 $this->container->bookmarkService->save(); 76 $this->container->bookmarkService->save();
75 77
78 $count = $searchResult->getResultCount();
76 if (true === $isDelete) { 79 if (true === $isDelete) {
77 $alert = sprintf( 80 $alert = sprintf(
78 t('The tag was removed from %d bookmark.', 'The tag was removed from %d bookmarks.', $count), 81 t('The tag was removed from %d bookmark.', 'The tag was removed from %d bookmarks.', $count),
diff --git a/application/front/controller/admin/ThumbnailsController.php b/application/front/controller/admin/ThumbnailsController.php
index 94d97d4b..5dfea096 100644
--- a/application/front/controller/admin/ThumbnailsController.php
+++ b/application/front/controller/admin/ThumbnailsController.php
@@ -22,7 +22,7 @@ class ThumbnailsController extends ShaarliAdminController
22 public function index(Request $request, Response $response): Response 22 public function index(Request $request, Response $response): Response
23 { 23 {
24 $ids = []; 24 $ids = [];
25 foreach ($this->container->bookmarkService->search() as $bookmark) { 25 foreach ($this->container->bookmarkService->search()->getBookmarks() as $bookmark) {
26 // A note or not HTTP(S) 26 // A note or not HTTP(S)
27 if ($bookmark->isNote() || !startsWith(strtolower($bookmark->getUrl()), 'http')) { 27 if ($bookmark->isNote() || !startsWith(strtolower($bookmark->getUrl()), 'http')) {
28 continue; 28 continue;
diff --git a/application/front/controller/visitor/BookmarkListController.php b/application/front/controller/visitor/BookmarkListController.php
index 106440b6..4aae2652 100644
--- a/application/front/controller/visitor/BookmarkListController.php
+++ b/application/front/controller/visitor/BookmarkListController.php
@@ -37,7 +37,6 @@ class BookmarkListController extends ShaarliVisitorController
37 37
38 $searchTags = normalize_spaces($request->getParam('searchtags') ?? ''); 38 $searchTags = normalize_spaces($request->getParam('searchtags') ?? '');
39 $searchTerm = escape(normalize_spaces($request->getParam('searchterm') ?? '')); 39 $searchTerm = escape(normalize_spaces($request->getParam('searchterm') ?? ''));
40 ;
41 40
42 // Filter bookmarks according search parameters. 41 // Filter bookmarks according search parameters.
43 $visibility = $this->container->sessionManager->getSessionParameter('visibility'); 42 $visibility = $this->container->sessionManager->getSessionParameter('visibility');
@@ -45,39 +44,26 @@ class BookmarkListController extends ShaarliVisitorController
45 'searchtags' => $searchTags, 44 'searchtags' => $searchTags,
46 'searchterm' => $searchTerm, 45 'searchterm' => $searchTerm,
47 ]; 46 ];
48 $linksToDisplay = $this->container->bookmarkService->search(
49 $search,
50 $visibility,
51 false,
52 !!$this->container->sessionManager->getSessionParameter('untaggedonly')
53 ) ?? [];
54
55 // ---- Handle paging.
56 $keys = [];
57 foreach ($linksToDisplay as $key => $value) {
58 $keys[] = $key;
59 }
60
61 $linksPerPage = $this->container->sessionManager->getSessionParameter('LINKS_PER_PAGE', 20) ?: 20;
62 47
63 // Select articles according to paging. 48 // Select articles according to paging.
64 $pageCount = (int) ceil(count($keys) / $linksPerPage) ?: 1; 49 $page = (int) ($request->getParam('page') ?? 1);
65 $page = (int) $request->getParam('page') ?? 1;
66 $page = $page < 1 ? 1 : $page; 50 $page = $page < 1 ? 1 : $page;
67 $page = $page > $pageCount ? $pageCount : $page; 51 $linksPerPage = $this->container->sessionManager->getSessionParameter('LINKS_PER_PAGE', 20) ?: 20;
68 52
69 // Start index. 53 $searchResult = $this->container->bookmarkService->search(
70 $i = ($page - 1) * $linksPerPage; 54 $search,
71 $end = $i + $linksPerPage; 55 $visibility,
56 false,
57 !!$this->container->sessionManager->getSessionParameter('untaggedonly'),
58 false,
59 ['offset' => $linksPerPage * ($page - 1), 'limit' => $linksPerPage]
60 ) ?? [];
72 61
73 $linkDisp = [];
74 $save = false; 62 $save = false;
75 while ($i < $end && $i < count($keys)) { 63 $links = [];
76 $save = $this->updateThumbnail($linksToDisplay[$keys[$i]], false) || $save; 64 foreach ($searchResult->getBookmarks() as $key => $bookmark) {
77 $link = $formatter->format($linksToDisplay[$keys[$i]]); 65 $save = $this->updateThumbnail($bookmark, false) || $save;
78 66 $links[$key] = $formatter->format($bookmark);
79 $linkDisp[$keys[$i]] = $link;
80 $i++;
81 } 67 }
82 68
83 if ($save) { 69 if ($save) {
@@ -87,15 +73,10 @@ class BookmarkListController extends ShaarliVisitorController
87 // Compute paging navigation 73 // Compute paging navigation
88 $searchtagsUrl = $searchTags === '' ? '' : '&searchtags=' . urlencode($searchTags); 74 $searchtagsUrl = $searchTags === '' ? '' : '&searchtags=' . urlencode($searchTags);
89 $searchtermUrl = $searchTerm === '' ? '' : '&searchterm=' . urlencode($searchTerm); 75 $searchtermUrl = $searchTerm === '' ? '' : '&searchterm=' . urlencode($searchTerm);
76 $page = $searchResult->getPage();
90 77
91 $previous_page_url = ''; 78 $previousPageUrl = !$searchResult->isLastPage() ? '?page=' . ($page + 1) . $searchtermUrl . $searchtagsUrl : '';
92 if ($i !== count($keys)) { 79 $nextPageUrl = !$searchResult->isFirstPage() ? '?page=' . ($page - 1) . $searchtermUrl . $searchtagsUrl : '';
93 $previous_page_url = '?page=' . ($page + 1) . $searchtermUrl . $searchtagsUrl;
94 }
95 $next_page_url = '';
96 if ($page > 1) {
97 $next_page_url = '?page=' . ($page - 1) . $searchtermUrl . $searchtagsUrl;
98 }
99 80
100 $tagsSeparator = $this->container->conf->get('general.tags_separator', ' '); 81 $tagsSeparator = $this->container->conf->get('general.tags_separator', ' ');
101 $searchTagsUrlEncoded = array_map('urlencode', tags_str2array($searchTags, $tagsSeparator)); 82 $searchTagsUrlEncoded = array_map('urlencode', tags_str2array($searchTags, $tagsSeparator));
@@ -105,16 +86,16 @@ class BookmarkListController extends ShaarliVisitorController
105 $data = array_merge( 86 $data = array_merge(
106 $this->initializeTemplateVars(), 87 $this->initializeTemplateVars(),
107 [ 88 [
108 'previous_page_url' => $previous_page_url, 89 'previous_page_url' => $previousPageUrl,
109 'next_page_url' => $next_page_url, 90 'next_page_url' => $nextPageUrl,
110 'page_current' => $page, 91 'page_current' => $page,
111 'page_max' => $pageCount, 92 'page_max' => $searchResult->getLastPage(),
112 'result_count' => count($linksToDisplay), 93 'result_count' => $searchResult->getTotalCount(),
113 'search_term' => escape($searchTerm), 94 'search_term' => escape($searchTerm),
114 'search_tags' => escape($searchTags), 95 'search_tags' => escape($searchTags),
115 'search_tags_url' => $searchTagsUrlEncoded, 96 'search_tags_url' => $searchTagsUrlEncoded,
116 'visibility' => $visibility, 97 'visibility' => $visibility,
117 'links' => $linkDisp, 98 'links' => $links,
118 ] 99 ]
119 ); 100 );
120 101
diff --git a/application/front/controller/visitor/DailyController.php b/application/front/controller/visitor/DailyController.php
index 29492a5f..3739ec16 100644
--- a/application/front/controller/visitor/DailyController.php
+++ b/application/front/controller/visitor/DailyController.php
@@ -100,7 +100,7 @@ class DailyController extends ShaarliVisitorController
100 $days = []; 100 $days = [];
101 $format = DailyPageHelper::getFormatByType($type); 101 $format = DailyPageHelper::getFormatByType($type);
102 $length = DailyPageHelper::getRssLengthByType($type); 102 $length = DailyPageHelper::getRssLengthByType($type);
103 foreach ($this->container->bookmarkService->search() as $bookmark) { 103 foreach ($this->container->bookmarkService->search()->getBookmarks() as $bookmark) {
104 $day = $bookmark->getCreated()->format($format); 104 $day = $bookmark->getCreated()->format($format);
105 105
106 // Stop iterating after DAILY_RSS_NB_DAYS entries 106 // Stop iterating after DAILY_RSS_NB_DAYS entries
diff --git a/application/front/controller/visitor/PictureWallController.php b/application/front/controller/visitor/PictureWallController.php
index 23553ee6..9c8f07d7 100644
--- a/application/front/controller/visitor/PictureWallController.php
+++ b/application/front/controller/visitor/PictureWallController.php
@@ -30,19 +30,19 @@ class PictureWallController extends ShaarliVisitorController
30 ); 30 );
31 31
32 // Optionally filter the results: 32 // Optionally filter the results:
33 $links = $this->container->bookmarkService->search($request->getQueryParams()); 33 $bookmarks = $this->container->bookmarkService->search($request->getQueryParams())->getBookmarks();
34 $linksToDisplay = []; 34 $links = [];
35 35
36 // Get only bookmarks which have a thumbnail. 36 // Get only bookmarks which have a thumbnail.
37 // Note: we do not retrieve thumbnails here, the request is too heavy. 37 // Note: we do not retrieve thumbnails here, the request is too heavy.
38 $formatter = $this->container->formatterFactory->getFormatter('raw'); 38 $formatter = $this->container->formatterFactory->getFormatter('raw');
39 foreach ($links as $key => $link) { 39 foreach ($bookmarks as $key => $bookmark) {
40 if (!empty($link->getThumbnail())) { 40 if (!empty($bookmark->getThumbnail())) {
41 $linksToDisplay[] = $formatter->format($link); 41 $links[] = $formatter->format($bookmark);
42 } 42 }
43 } 43 }
44 44
45 $data = ['linksToDisplay' => $linksToDisplay]; 45 $data = ['linksToDisplay' => $links];
46 $this->executePageHooks('render_picwall', $data, TemplatePage::PICTURE_WALL); 46 $this->executePageHooks('render_picwall', $data, TemplatePage::PICTURE_WALL);
47 47
48 foreach ($data as $key => $value) { 48 foreach ($data as $key => $value) {