From a9a5bea1c773729a31e932faf8373e9bacf17970 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 9 Aug 2014 12:15:03 +0200 Subject: [PATCH] Changed last search query tracking to server-side Recent changes to last search query handling still haven't fixed all possible routes, so I've simplified the whole thing at expense of clean URLs. --- public_html/media/js/core.js | 20 -------------------- src/Controllers/PostController.php | 12 +++++------- src/Router.php | 3 ++- src/Views/post/post-small.phtml | 6 +++++- src/Views/post/post-view.phtml | 16 +++++++++++----- 5 files changed, 23 insertions(+), 34 deletions(-) diff --git a/public_html/media/js/core.js b/public_html/media/js/core.js index bcb62303..987d80b5 100644 --- a/public_html/media/js/core.js +++ b/public_html/media/js/core.js @@ -25,12 +25,6 @@ function getCookie(name) return unescape(value.substring(start, end)); } -function rememberLastSearchQuery() -{ - var lastSearchQuery = $('#settings').attr('data-last-search-query'); - setCookie('last-search-query', lastSearchQuery); -} - //core functionalities, prototypes function getJSON(data) { @@ -52,7 +46,6 @@ function postJSON(data) function getHtml(data) { - rememberLastSearchQuery(); return $.get(data); } @@ -77,19 +70,6 @@ $.fn.bindOnce = function(name, eventName, callback) //basic event listeners $(function() { - $(window).on('beforeunload', function() - { - rememberLastSearchQuery(); - }); - if (window.history && window.history.pushState) - { - $(window).on('popstate', function() - { - rememberLastSearchQuery(); - }); - } - rememberLastSearchQuery(); - $('body').bind('dom-update', function() { //event confirmations diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index a6a48679..fd3b700a 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -11,7 +11,6 @@ class PostController extends AbstractController { $query = trim($query); $context->transport->searchQuery = $query; - $context->transport->lastSearchQuery = $query; if ($source == 'mass-tag') { Access::assert(new Privilege(Privilege::MassTag)); @@ -49,7 +48,6 @@ class PostController extends AbstractController $additionalInfo = trim(InputHelper::get('tag')); $context->transport->searchQuery = $query; - $context->transport->lastSearchQuery = $query; if (strpos($query, '/') !== false) throw new SimpleException('Search query contains invalid characters'); @@ -284,7 +282,7 @@ class PostController extends AbstractController $this->redirectToGenericView($identifier); } - public function genericView($identifier) + public function genericView($identifier, $query = null) { $context = Core::getContext(); @@ -294,17 +292,17 @@ class PostController extends AbstractController try { - $context->transport->lastSearchQuery = InputHelper::get('last-search-query'); + $context->transport->searchQuery = $query; list ($prevPostId, $nextPostId) = PostSearchService::getPostIdsAround( - $context->transport->lastSearchQuery, $identifier); + $context->transport->searchQuery, $identifier); } #search for some reason was invalid, e.g. tag was deleted in the meantime catch (Exception $e) { - $context->transport->lastSearchQuery = ''; + $context->transport->searchQuery = ''; list ($prevPostId, $nextPostId) = - PostSearchService::getPostIdsAround($context->transport->lastSearchQuery, $identifier); + PostSearchService::getPostIdsAround($context->transport->searchQuery, $identifier); } $isUserFavorite = Auth::getCurrentUser()->hasFavorited($post); diff --git a/src/Router.php b/src/Router.php index 3d3e37f2..fc775cf5 100644 --- a/src/Router.php +++ b/src/Router.php @@ -63,8 +63,9 @@ class Router extends \Chibi\Routing\Router $this->register(['PostController', 'listView'], 'GET', '/{source}/{query}/{additionalInfo}/{page}', $postValidation); $this->register(['PostController', 'listRedirectAction'], 'POST', '/{source}-redirect', $postValidation); - $this->register(['PostController', 'genericView'], 'GET', '/post/{identifier}', $postValidation); $this->register(['PostController', 'fileView'], 'GET', '/post/{name}/retrieve', $postValidation); + $this->register(['PostController', 'genericView'], 'GET', '/post/{identifier}', $postValidation); + $this->register(['PostController', 'genericView'], 'GET', '/post/{identifier}/search={query}', $postValidation); $this->register(['PostController', 'thumbnailView'], 'GET', '/post/{name}/thumb', $postValidation); $this->register(['PostController', 'toggleTagAction'], null, '/post/{identifier}/toggle-tag/{tag}/{enable}', $postValidation); diff --git a/src/Views/post/post-small.phtml b/src/Views/post/post-small.phtml index a0567955..7440f01f 100644 --- a/src/Views/post/post-small.phtml +++ b/src/Views/post/post-small.phtml @@ -45,7 +45,11 @@ if ($masstag) getSettings()->hasEnabledPostTagTitles()): ?> title="context->post->getTags()) ?>" - href="linkTo(['PostController', 'genericView'], ['identifier' => $this->context->post->getId()]) ?>"> + href="linkTo(['PostController', 'genericView'], + empty($this->context->transport->searchQuery) + ? ['identifier' => $this->context->post->getId()] + : ['identifier' => $this->context->post->getId(), + 'query' => $this->context->transport->searchQuery]) ?>"> 0; context->transport->nextPostId): ?> + empty($this->context->transport->searchQuery) + ? ['identifier' => $this->context->transport->nextPostId] + : ['identifier' => $this->context->transport->nextPostId, + 'query' => $this->context->transport->searchQuery]) ?>"> @@ -46,7 +49,10 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0; context->transport->prevPostId): ?> + empty($this->context->transport->searchQuery) + ? ['identifier' => $this->context->transport->prevPostId] + : ['identifier' => $this->context->transport->prevPostId, + 'query' => $this->context->transport->searchQuery]) ?>"> @@ -57,13 +63,13 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
- context->transport->lastSearchQuery)): ?> + context->transport->searchQuery)): ?>
Current search:
- context->transport->lastSearchQuery ?> + ['query' => $this->context->transport->searchQuery]) ?>"> + context->transport->searchQuery ?>