]> git.mxchange.org Git - friendica.git/commitdiff
Add new module and widget for managing saved searches
authorHypolite Petovan <hypolite@mrpetovan.com>
Mon, 7 Oct 2019 18:13:31 +0000 (14:13 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 7 Oct 2019 18:27:46 +0000 (14:27 -0400)
mod/network.php
mod/search.php
src/Content/Widget/SavedSearches.php [new file with mode: 0644]
src/Module/Search/Saved.php [new file with mode: 0644]
static/routes.config.php
view/templates/saved_searches_aside.tpl [deleted file]
view/templates/widget/saved_searches.tpl [new file with mode: 0644]
view/theme/frio/templates/saved_searches_aside.tpl [deleted file]
view/theme/frio/templates/widget/saved_searches.tpl [new file with mode: 0644]
view/theme/quattro/templates/saved_searches_aside.tpl [deleted file]
view/theme/quattro/templates/widget/saved_searches.tpl [new file with mode: 0644]

index fddec60c8dc86457e1273d551adb2d766e63babb..515219827adea97c87a97b0fec33325541795ae4 100644 (file)
@@ -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
  *
index b28ad7b0e9f58a5efbe18ec8b1a3b6a27384c6f4..9651d5c9b4f1c4085b66873eea81ad1038c0bc80 100644 (file)
@@ -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 (file)
index 0000000..7f7c171
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+
+namespace Friendica\Content\Widget;
+
+use Friendica\Core\L10n;
+use Friendica\Core\Renderer;
+use Friendica\Database\DBA;
+
+class SavedSearches
+{
+       /**
+        * @param string $return_url
+        * @param string $search
+        * @return string
+        * @throws \Exception
+        */
+       public static function getHTML($return_url, $search = '')
+       {
+               $o = '';
+
+               $saved_searches = DBA::select('search', ['id', 'term'], ['uid' => 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 (file)
index 0000000..abd9cfa
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+namespace Friendica\Module\Search;
+
+use Friendica\BaseModule;
+use Friendica\Core\L10n;
+use Friendica\Database\DBA;
+use Friendica\Util\Strings;
+
+class Saved extends BaseModule
+{
+       public static function rawContent()
+       {
+               $action = self::getArgs()->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);
+       }
+}
index 73ac54991f08109efd0c623e2a7fbdb7aaade643..18144d79900a388b9236d9926b64c5aa409da5a6 100644 (file)
@@ -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 (file)
index caf60cc..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-
-<div class="widget" id="saved-search-list">
-       <h3 id="search">{{$title}}</h3>
-       {{$searchbox nofilter}}
-       
-       <ul role="menu" id="saved-search-ul">
-               {{foreach $saved as $search}}
-               <li role="menuitem" class="saved-search-li clear">
-                       <a title="{{$search.delete}}" onclick="return confirmDelete();" id="drop-saved-search-term-{{$search.id}}" class="iconspacer savedsearchdrop " href="network?remove=1&amp;search={{$search.encodedterm}}"></a>
-                       <a id="saved-search-term-{{$search.id}}" class="savedsearchterm" href="search?search={{$search.encodedterm}}">{{$search.term}}</a>
-               </li>
-               {{/foreach}}
-       </ul>
-       <div class="clear"></div>
-</div>
diff --git a/view/templates/widget/saved_searches.tpl b/view/templates/widget/saved_searches.tpl
new file mode 100644 (file)
index 0000000..858e921
--- /dev/null
@@ -0,0 +1,15 @@
+
+<div class="widget" id="saved-search-list">
+       <h3 id="search">{{$title}}</h3>
+       {{$searchbox nofilter}}
+       
+       <ul role="menu" id="saved-search-ul">
+               {{foreach $saved as $search}}
+               <li role="menuitem" class="saved-search-li clear">
+                       <a href="search/saved/remove/{{$search.encodedterm}}?return_url={{$return_url}}" title="{{$search.delete}}" onclick="return confirmDelete();" id="drop-saved-search-term-{{$search.id}}" class="iconspacer savedsearchdrop"></a>
+                       <a href="search?q={{$search.encodedterm}}" id="saved-search-term-{{$search.id}}" class="savedsearchterm">{{$search.term}}</a>
+               </li>
+               {{/foreach}}
+       </ul>
+       <div class="clear"></div>
+</div>
diff --git a/view/theme/frio/templates/saved_searches_aside.tpl b/view/theme/frio/templates/saved_searches_aside.tpl
deleted file mode 100644 (file)
index 48b07bc..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-
-{{if $saved}}
-<div class="widget" id="saved-search-list">
-       <h3 id="search">{{$title}}</h3>
-
-       <ul role="menu" id="saved-search-ul">
-               {{foreach $saved as $search}}
-               <li role="menuitem" class="saved-search-li clear">
-                       <a title="{{$search.delete}}" onclick="return confirmDelete();" id="drop-saved-search-term-{{$search.id}}" class="savedsearchdrop pull-right widget-action faded-icon" href="network?remove=1&amp;search={{$search.encodedterm}}">
-                               <i class="fa fa-trash" aria-hidden="true"></i>
-                       </a>
-                       <a id="saved-search-term-{{$search.id}}" class="savedsearchterm" href="search?search={{$search.encodedterm}}">{{$search.term}}</a>
-               </li>
-               {{/foreach}}
-       </ul>
-       <div class="clearfix"></div>
-</div>
-{{/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 (file)
index 0000000..1553961
--- /dev/null
@@ -0,0 +1,18 @@
+
+{{if $saved}}
+<div class="widget" id="saved-search-list">
+       <h3 id="search">{{$title}}</h3>
+
+       <ul role="menu" id="saved-search-ul">
+               {{foreach $saved as $search}}
+               <li role="menuitem" class="saved-search-li clear">
+                       <a href="search/saved/remove/{{$search.encodedterm}}?return_url={{$return_url}}" title="{{$search.delete}}" onclick="return confirmDelete();" id="drop-saved-search-term-{{$search.id}}" class="savedsearchdrop pull-right widget-action faded-icon">
+                               <i class="fa fa-trash" aria-hidden="true"></i>
+                       </a>
+                       <a href="search?q={{$search.encodedterm}}" id="saved-search-term-{{$search.id}}" class="savedsearchterm">{{$search.term}}</a>
+               </li>
+               {{/foreach}}
+       </ul>
+       <div class="clearfix"></div>
+</div>
+{{/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 (file)
index 4b2b001..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-<div id="saved-search-list" class="widget">
-       <h3 class="title">{{$title}}</h3>
-
-       <ul id="saved-search-ul">
-               {{foreach $saved as $search}}
-                       <li class="tool {{if $search.selected}}selected{{/if}}">
-                                       <a href="search?search={{$search.encodedterm}}" class="label" >{{$search.term}}</a>
-                                       <a href="network?remove=1&search={{$search.encodedterm}}" class="action icon s10 delete" title="{{$search.delete}}" onclick="return confirmDelete();"></a>
-                       </li>
-               {{/foreach}}
-       </ul>
-       
-       {{$searchbox nofilter}}
-       
-</div>
diff --git a/view/theme/quattro/templates/widget/saved_searches.tpl b/view/theme/quattro/templates/widget/saved_searches.tpl
new file mode 100644 (file)
index 0000000..69e21c7
--- /dev/null
@@ -0,0 +1,15 @@
+<div id="saved-search-list" class="widget">
+       <h3 class="title">{{$title}}</h3>
+
+       <ul id="saved-search-ul">
+        {{foreach $saved as $search}}
+                       <li class="tool {{if $search.selected}}selected{{/if}}">
+                               <a href="search?q={{$search.encodedterm}}" class="label">{{$search.term}}</a>
+                               <a href="search/saved/remove/{{$search.encodedterm}}?return_url={{$return_url}}" class="action icon s10 delete" title="{{$search.delete}}" onclick="return confirmDelete();"></a>
+                       </li>
+        {{/foreach}}
+       </ul>
+
+    {{$searchbox nofilter}}
+
+</div>