From 11ad0acd289bbbf87081206e564da0874910a45e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 7 Oct 2019 14:10:30 -0400 Subject: [PATCH 1/6] Add accessor for App->args in App and BaseModule --- src/App.php | 10 ++++++++++ src/BaseModule.php | 10 +++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index c6ed818dd..8a36c3bfe 100644 --- a/src/App.php +++ b/src/App.php @@ -132,6 +132,16 @@ class App */ private $process; + /** + * Returns the arguments of the query + * + * @return Arguments + */ + public function getArgs() + { + return $this->args; + } + /** * Returns the current config cache of this node * diff --git a/src/BaseModule.php b/src/BaseModule.php index 0383487ab..76ed90111 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -120,7 +120,7 @@ abstract class BaseModule extends BaseObject $a = \get_app(); $x = explode('.', $hash); - if (time() > (IntVal($x[0]) + $max_livetime)) { + if (time() > (intval($x[0]) + $max_livetime)) { return false; } @@ -155,4 +155,12 @@ abstract class BaseModule extends BaseObject throw new \Friendica\Network\HTTPException\ForbiddenException(); } } + + /** + * @return App\Arguments + */ + protected static function getArgs() + { + return self::getApp()->getArgs(); + } } From cdefa7f32a224501a14bcacbe0f0a3792631df93 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 7 Oct 2019 14:13:31 -0400 Subject: [PATCH 2/6] 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}} + +
From 22598fc7e8a2833ecddcafe8ac052d5d9055f71f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 7 Oct 2019 14:19:50 -0400 Subject: [PATCH 3/6] Improve search box - Use new saved search module - Use dynamic search option loop - Use "q" instead of "search" for search query string parameter --- mod/search.php | 6 ++--- src/Content/Text/HTML.php | 33 +++++++++++++------------ view/templates/searchbox.tpl | 30 +++++++++++----------- view/theme/frio/templates/nav.tpl | 2 +- view/theme/frio/templates/searchbox.tpl | 26 +++++++++---------- view/theme/vier/templates/nav.tpl | 2 +- 6 files changed, 48 insertions(+), 51 deletions(-) diff --git a/mod/search.php b/mod/search.php index 9651d5c9b..7cd5233bf 100644 --- a/mod/search.php +++ b/mod/search.php @@ -19,7 +19,7 @@ use Friendica\Module\BaseSearchModule; use Friendica\Util\Strings; function search_init(App $a) { - $search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : ''); + $search = (!empty($_GET['q']) ? Strings::escapeTags(trim(rawurldecode($_GET['q']))) : ''); if (local_user()) { /// @todo Check if there is a case at all that "aside" is prefilled here @@ -69,7 +69,7 @@ function search_content(App $a) { Nav::setSelected('search'); - $search = (!empty($_REQUEST['search']) ? Strings::escapeTags(trim(rawurldecode($_REQUEST['search']))) : ''); + $search = (!empty($_REQUEST['q']) ? Strings::escapeTags(trim(rawurldecode($_REQUEST['q']))) : ''); $tag = false; if (!empty($_GET['tag'])) { @@ -82,7 +82,7 @@ function search_content(App $a) { 'name' => "search-header", '$title' => L10n::t("Search"), '$title_size' => 3, - '$content' => HTML::search($search,'search-box','search', false) + '$content' => HTML::search($search,'search-box',false) ]); if (strpos($search,'#') === 0) { diff --git a/src/Content/Text/HTML.php b/src/Content/Text/HTML.php index eee443a8f..ea9a4737c 100644 --- a/src/Content/Text/HTML.php +++ b/src/Content/Text/HTML.php @@ -893,9 +893,9 @@ class HTML * @param bool $aside Display the search widgit aside. * * @return string Formatted HTML. - * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \Exception */ - public static function search($s, $id = 'search-box', $url = 'search', $aside = true) + public static function search($s, $id = 'search-box', $aside = true) { $mode = 'text'; @@ -905,24 +905,25 @@ class HTML $save_label = $mode === 'text' ? L10n::t('Save') : L10n::t('Follow'); $values = [ - '$s' => $s, - '$id' => $id, - '$action_url' => $url, - '$search_label' => L10n::t('Search'), - '$save_label' => $save_label, - '$savedsearch' => 'savedsearch', - '$search_hint' => L10n::t('@name, !forum, #tags, content'), - '$mode' => $mode - ]; + '$s' => $s, + '$q' => urlencode($s), + '$id' => $id, + '$search_label' => L10n::t('Search'), + '$save_label' => $save_label, + '$search_hint' => L10n::t('@name, !forum, #tags, content'), + '$mode' => $mode, + '$return_url' => urlencode('search?q=' . $s), + ]; if (!$aside) { - $values['$searchoption'] = [ - L10n::t("Full Text"), - L10n::t("Tags"), - L10n::t("Contacts")]; + $values['$search_options'] = [ + 'fulltext' => L10n::t('Full Text'), + 'tags' => L10n::t('Tags'), + 'contacts' => L10n::t('Contacts') + ]; if (Config::get('system', 'poco_local_search')) { - $values['$searchoption'][] = L10n::t("Forums"); + $values['$searchoption']['forums'] = L10n::t('Forums'); } } diff --git a/view/templates/searchbox.tpl b/view/templates/searchbox.tpl index 4b1a51f87..d566befba 100644 --- a/view/templates/searchbox.tpl +++ b/view/templates/searchbox.tpl @@ -1,20 +1,18 @@
-
- {{strip}} - - {{if $searchoption}} + +{{strip}} + + {{if $search_options}} - {{/if}} - - - {{if $savedsearch}} - - {{/if}} - {{/strip}} -
+ {{/if}} + + {{if $s}} + {{$save_label}} + {{/if}} +{{/strip}} +
diff --git a/view/theme/frio/templates/nav.tpl b/view/theme/frio/templates/nav.tpl index a3bcdc976..4698fb657 100644 --- a/view/theme/frio/templates/nav.tpl +++ b/view/theme/frio/templates/nav.tpl @@ -265,7 +265,7 @@ diff --git a/view/theme/frio/templates/searchbox.tpl b/view/theme/frio/templates/searchbox.tpl index c7d06d107..bf2ee5304 100644 --- a/view/theme/frio/templates/searchbox.tpl +++ b/view/theme/frio/templates/searchbox.tpl @@ -4,34 +4,32 @@ Some parts of this template will be moved by js to other places (see theme.js) -
-
+
{{* The button to save searches *}} - {{if $savedsearch}} - + {{if $s}} + {{$save_label}} {{/if}} {{* The select popup menu to select what kind of results the user would like to search for *}} - {{if $searchoption}} + {{if $search_options}}
-
{{/if}} @@ -47,10 +45,10 @@ Some parts of this template will be moved by js to other places (see theme.js) -
-{{if $savedsearch}} -
- -