From cdefa7f32a224501a14bcacbe0f0a3792631df93 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 7 Oct 2019 14:13:31 -0400 Subject: [PATCH] Add new module and widget for managing saved searches --- mod/network.php | 55 +------------------ mod/search.php | 50 +---------------- src/Content/Widget/SavedSearches.php | 47 ++++++++++++++++ src/Module/Search/Saved.php | 40 ++++++++++++++ static/routes.config.php | 4 +- view/templates/saved_searches_aside.tpl | 15 ----- view/templates/widget/saved_searches.tpl | 15 +++++ .../frio/templates/saved_searches_aside.tpl | 18 ------ .../frio/templates/widget/saved_searches.tpl | 18 ++++++ .../templates/saved_searches_aside.tpl | 15 ----- .../templates/widget/saved_searches.tpl | 15 +++++ 11 files changed, 140 insertions(+), 152 deletions(-) create mode 100644 src/Content/Widget/SavedSearches.php create mode 100644 src/Module/Search/Saved.php delete mode 100644 view/templates/saved_searches_aside.tpl create mode 100644 view/templates/widget/saved_searches.tpl delete mode 100644 view/theme/frio/templates/saved_searches_aside.tpl create mode 100644 view/theme/frio/templates/widget/saved_searches.tpl delete mode 100644 view/theme/quattro/templates/saved_searches_aside.tpl create mode 100644 view/theme/quattro/templates/widget/saved_searches.tpl diff --git a/mod/network.php b/mod/network.php index fddec60c8..515219827 100644 --- a/mod/network.php +++ b/mod/network.php @@ -40,22 +40,6 @@ function network_init(App $a) Hook::add('head', __FILE__, 'network_infinite_scroll_head'); - $search = (!empty($_GET['search']) ? Strings::escapeHtml($_GET['search']) : ''); - - if (($search != '') && !empty($_GET['submit'])) { - $a->internalRedirect('search?search=' . urlencode($search)); - } - - if (!empty($_GET['save'])) { - $exists = DBA::exists('search', ['uid' => local_user(), 'term' => $search]); - if (!$exists) { - DBA::insert('search', ['uid' => local_user(), 'term' => $search]); - } - } - if (!empty($_GET['remove'])) { - DBA::delete('search', ['uid' => local_user(), 'term' => $search]); - } - $is_a_date_query = false; $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0); @@ -155,47 +139,10 @@ function network_init(App $a) $a->page['aside'] .= ForumManager::widget(local_user(), $cid); $a->page['aside'] .= Widget::postedByYear('network', local_user(), false); $a->page['aside'] .= Widget::networks('network', defaults($_GET, 'nets', '') ); - $a->page['aside'] .= saved_searches($search); + $a->page['aside'] .= Widget\SavedSearches::getHTML($a->query_string); $a->page['aside'] .= Widget::fileAs('network', defaults($_GET, 'file', '') ); } -function saved_searches($search) -{ - $srchurl = '/network?f=' - . (!empty($_GET['cid']) ? '&cid=' . rawurlencode($_GET['cid']) : '') - . (!empty($_GET['star']) ? '&star=' . rawurlencode($_GET['star']) : '') - . (!empty($_GET['bmark']) ? '&bmark=' . rawurlencode($_GET['bmark']) : '') - . (!empty($_GET['conv']) ? '&conv=' . rawurlencode($_GET['conv']) : '') - . (!empty($_GET['nets']) ? '&nets=' . rawurlencode($_GET['nets']) : '') - . (!empty($_GET['cmin']) ? '&cmin=' . rawurlencode($_GET['cmin']) : '') - . (!empty($_GET['cmax']) ? '&cmax=' . rawurlencode($_GET['cmax']) : '') - . (!empty($_GET['file']) ? '&file=' . rawurlencode($_GET['file']) : ''); - ; - - $terms = DBA::select('search', ['id', 'term'], ['uid' => local_user()]); - $saved = []; - - while ($rr = DBA::fetch($terms)) { - $saved[] = [ - 'id' => $rr['id'], - 'term' => $rr['term'], - 'encodedterm' => urlencode($rr['term']), - 'delete' => L10n::t('Remove term'), - 'selected' => ($search == $rr['term']), - ]; - } - - $tpl = Renderer::getMarkupTemplate('saved_searches_aside.tpl'); - $o = Renderer::replaceMacros($tpl, [ - '$title' => L10n::t('Saved Searches'), - '$add' => L10n::t('add'), - '$searchbox' => HTML::search($search, 'netsearch-box', $srchurl), - '$saved' => $saved, - ]); - - return $o; -} - /** * Return selected tab from query * diff --git a/mod/search.php b/mod/search.php index b28ad7b0e..9651d5c9b 100644 --- a/mod/search.php +++ b/mod/search.php @@ -18,64 +18,16 @@ use Friendica\Model\Item; use Friendica\Module\BaseSearchModule; use Friendica\Util\Strings; -function search_saved_searches() { - $o = ''; - $search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : ''); - - $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d", - intval(local_user()) - ); - - if (DBA::isResult($r)) { - $saved = []; - foreach ($r as $rr) { - $saved[] = [ - 'id' => $rr['id'], - 'term' => $rr['term'], - 'encodedterm' => urlencode($rr['term']), - 'delete' => L10n::t('Remove term'), - 'selected' => ($search==$rr['term']), - ]; - } - - - $tpl = Renderer::getMarkupTemplate("saved_searches_aside.tpl"); - - $o .= Renderer::replaceMacros($tpl, [ - '$title' => L10n::t('Saved Searches'), - '$add' => '', - '$searchbox' => '', - '$saved' => $saved, - ]); - } - - return $o; -} - - function search_init(App $a) { $search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : ''); if (local_user()) { - if (!empty($_GET['save']) && $search) { - $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1", - intval(local_user()), - DBA::escape($search) - ); - if (!DBA::isResult($r)) { - DBA::insert('search', ['uid' => local_user(), 'term' => $search]); - } - } - if (!empty($_GET['remove']) && $search) { - DBA::delete('search', ['uid' => local_user(), 'term' => $search]); - } - /// @todo Check if there is a case at all that "aside" is prefilled here if (!isset($a->page['aside'])) { $a->page['aside'] = ''; } - $a->page['aside'] .= search_saved_searches(); + $a->page['aside'] .= \Friendica\Content\Widget\SavedSearches::getHTML('search?q=' . $search, $search); } } diff --git a/src/Content/Widget/SavedSearches.php b/src/Content/Widget/SavedSearches.php new file mode 100644 index 000000000..7f7c171e0 --- /dev/null +++ b/src/Content/Widget/SavedSearches.php @@ -0,0 +1,47 @@ + local_user()]); + if (DBA::isResult($saved_searches)) { + $saved = []; + foreach ($saved_searches as $saved_search) { + $saved[] = [ + 'id' => $saved_search['id'], + 'term' => $saved_search['term'], + 'encodedterm' => urlencode($saved_search['term']), + 'delete' => L10n::t('Remove term'), + 'selected' => $search == $saved_search['term'], + ]; + } + + $tpl = Renderer::getMarkupTemplate('widget/saved_searches.tpl'); + + $o = Renderer::replaceMacros($tpl, [ + '$title' => L10n::t('Saved Searches'), + '$add' => '', + '$searchbox' => '', + '$saved' => $saved, + '$return_url' => urlencode($return_url), + ]); + } + + return $o; + } +} diff --git a/src/Module/Search/Saved.php b/src/Module/Search/Saved.php new file mode 100644 index 000000000..abd9cfa79 --- /dev/null +++ b/src/Module/Search/Saved.php @@ -0,0 +1,40 @@ +get(2, 'none'); + $search = Strings::escapeTags(trim(rawurldecode(self::getArgs()->get(3, '')))); + + $return_url = $_GET['return_url'] ?? 'search?q=' . urlencode($search); + + if (local_user()) { + switch ($action) { + case 'add': + $fields = ['uid' => local_user(), 'term' => $search]; + if (!DBA::exists('search', $fields)) { + DBA::insert('search', $fields); + info(L10n::t('Search term successfully saved.')); + } else { + info(L10n::t('Search term already saved.')); + } + break; + + case 'remove': + DBA::delete('search', ['uid' => local_user(), 'term' => $search]); + info(L10n::t('Search term successfully removed.')); + break; + } + } + + self::getApp()->internalRedirect($return_url); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 73ac54991..18144d799 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -189,7 +189,9 @@ return [ ], '/search' => [ - '/acl' => [Module\Search\Acl::class, [R::GET, R::POST]], + '/acl' => [Module\Search\Acl::class, [R::GET, R::POST]], + '/saved/add/{term}' => [Module\Search\Saved::class, [R::GET]], + '/saved/remove/{term}' => [Module\Search\Saved::class, [R::GET]], ], '/settings' => [ diff --git a/view/templates/saved_searches_aside.tpl b/view/templates/saved_searches_aside.tpl deleted file mode 100644 index caf60cc0b..000000000 --- a/view/templates/saved_searches_aside.tpl +++ /dev/null @@ -1,15 +0,0 @@ - -
- - {{$searchbox nofilter}} - - -
-
diff --git a/view/templates/widget/saved_searches.tpl b/view/templates/widget/saved_searches.tpl new file mode 100644 index 000000000..858e921f7 --- /dev/null +++ b/view/templates/widget/saved_searches.tpl @@ -0,0 +1,15 @@ + +
+ + {{$searchbox nofilter}} + + +
+
diff --git a/view/theme/frio/templates/saved_searches_aside.tpl b/view/theme/frio/templates/saved_searches_aside.tpl deleted file mode 100644 index 48b07bcde..000000000 --- a/view/theme/frio/templates/saved_searches_aside.tpl +++ /dev/null @@ -1,18 +0,0 @@ - -{{if $saved}} -
- - - -
-
-{{/if}} diff --git a/view/theme/frio/templates/widget/saved_searches.tpl b/view/theme/frio/templates/widget/saved_searches.tpl new file mode 100644 index 000000000..1553961e1 --- /dev/null +++ b/view/theme/frio/templates/widget/saved_searches.tpl @@ -0,0 +1,18 @@ + +{{if $saved}} +
+ + + +
+
+{{/if}} diff --git a/view/theme/quattro/templates/saved_searches_aside.tpl b/view/theme/quattro/templates/saved_searches_aside.tpl deleted file mode 100644 index 4b2b00148..000000000 --- a/view/theme/quattro/templates/saved_searches_aside.tpl +++ /dev/null @@ -1,15 +0,0 @@ -
-

{{$title}}

- - - - {{$searchbox nofilter}} - -
diff --git a/view/theme/quattro/templates/widget/saved_searches.tpl b/view/theme/quattro/templates/widget/saved_searches.tpl new file mode 100644 index 000000000..69e21c786 --- /dev/null +++ b/view/theme/quattro/templates/widget/saved_searches.tpl @@ -0,0 +1,15 @@ +
+

{{$title}}

+ + + + {{$searchbox nofilter}} + +