Add new module and widget for managing saved searches

This commit is contained in:
Hypolite Petovan 2019-10-07 14:13:31 -04:00
parent 11ad0acd28
commit cdefa7f32a
11 changed files with 140 additions and 152 deletions

View file

@ -40,22 +40,6 @@ function network_init(App $a)
Hook::add('head', __FILE__, 'network_infinite_scroll_head'); 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; $is_a_date_query = false;
$group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0); $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'] .= ForumManager::widget(local_user(), $cid);
$a->page['aside'] .= Widget::postedByYear('network', local_user(), false); $a->page['aside'] .= Widget::postedByYear('network', local_user(), false);
$a->page['aside'] .= Widget::networks('network', defaults($_GET, 'nets', '') ); $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', '') ); $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 * Return selected tab from query
* *

View file

@ -18,64 +18,16 @@ use Friendica\Model\Item;
use Friendica\Module\BaseSearchModule; use Friendica\Module\BaseSearchModule;
use Friendica\Util\Strings; 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) { function search_init(App $a) {
$search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : ''); $search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : '');
if (local_user()) { 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 /// @todo Check if there is a case at all that "aside" is prefilled here
if (!isset($a->page['aside'])) { if (!isset($a->page['aside'])) {
$a->page['aside'] = ''; $a->page['aside'] = '';
} }
$a->page['aside'] .= search_saved_searches(); $a->page['aside'] .= \Friendica\Content\Widget\SavedSearches::getHTML('search?q=' . $search, $search);
} }
} }

View file

@ -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;
}
}

View file

@ -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);
}
}

View file

@ -190,6 +190,8 @@ return [
'/search' => [ '/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' => [ '/settings' => [

View file

@ -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>

View file

@ -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>

View file

@ -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}}

View file

@ -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}}

View file

@ -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>

View file

@ -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>