mirror of
https://codeberg.org/streams/streams.git
synced 2024-09-21 00:15:57 +00:00
101 lines
3.4 KiB
PHP
101 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Code\Widget;
|
|
|
|
use App;
|
|
use Code\Lib\Features;
|
|
use Code\Render\Theme;
|
|
|
|
|
|
class Savedsearch implements WidgetInterface
|
|
{
|
|
|
|
public function widget(array $arguments): string
|
|
{
|
|
|
|
if ((!local_channel()) || (!Features::enabled(local_channel(), 'savedsearch'))) {
|
|
return '';
|
|
}
|
|
|
|
$search = ((x($_GET, 'netsearch')) ? $_GET['netsearch'] : '');
|
|
if (!$search) {
|
|
$search = ((x($_GET, 'search')) ? $_GET['search'] : '');
|
|
}
|
|
|
|
if (x($_GET, 'searchsave') && $search) {
|
|
$r = q(
|
|
"select * from term where uid = %d and ttype = %d and term = '%s' limit 1",
|
|
intval(local_channel()),
|
|
intval(TERM_SAVEDSEARCH),
|
|
dbesc($search)
|
|
);
|
|
if (!$r) {
|
|
q(
|
|
"insert into term ( uid,ttype,term ) values ( %d, %d, '%s') ",
|
|
intval(local_channel()),
|
|
intval(TERM_SAVEDSEARCH),
|
|
dbesc($search)
|
|
);
|
|
}
|
|
}
|
|
|
|
if (x($_GET, 'searchremove') && $search) {
|
|
q(
|
|
"delete from term where uid = %d and ttype = %d and term = '%s'",
|
|
intval(local_channel()),
|
|
intval(TERM_SAVEDSEARCH),
|
|
dbesc($search)
|
|
);
|
|
$search = '';
|
|
}
|
|
|
|
$srchurl = App::$query_string;
|
|
|
|
$srchurl = rtrim(preg_replace('/searchsave\=[^\&].*?(\&|$)/is', '', $srchurl), '&');
|
|
$srchurl = rtrim(preg_replace('/searchremove\=[^\&].*?(\&|$)/is', '', $srchurl), '&');
|
|
|
|
$srchurl = rtrim(preg_replace('/search\=[^\&].*?(\&|$)/is', '', $srchurl), '&');
|
|
$srchurl = rtrim(preg_replace('/submit\=[^\&].*?(\&|$)/is', '', $srchurl), '&');
|
|
$srchurl = str_replace(['?f=', '&f='], ['', ''], $srchurl);
|
|
|
|
|
|
$hasq = str_contains($srchurl, '?');
|
|
$hasamp = str_contains($srchurl, '&');
|
|
|
|
if (($hasamp) && (!$hasq)) {
|
|
$srchurl = substr($srchurl, 0, strpos($srchurl, '&')) . '?f=&' . substr($srchurl, strpos($srchurl, '&') + 1);
|
|
}
|
|
|
|
$r = q(
|
|
"select tid,term from term WHERE uid = %d and ttype = %d ",
|
|
intval(local_channel()),
|
|
intval(TERM_SAVEDSEARCH)
|
|
);
|
|
|
|
$saved = [];
|
|
|
|
if (count($r)) {
|
|
foreach ($r as $rr) {
|
|
$saved[] = [
|
|
'id' => $rr['tid'],
|
|
'term' => $rr['term'],
|
|
'dellink' => z_root() . '/' . $srchurl . (($hasq || $hasamp) ? '' : '?f=') . '&searchremove=1&search=' . urlencode($rr['term']),
|
|
'srchlink' => z_root() . '/' . $srchurl . (($hasq || $hasamp) ? '' : '?f=') . '&search=' . urlencode($rr['term']),
|
|
'displayterm' => htmlspecialchars($rr['term'], ENT_COMPAT, 'UTF-8'),
|
|
'encodedterm' => urlencode($rr['term']),
|
|
'delete' => t('Remove term'),
|
|
'selected' => ($search == $rr['term']),
|
|
];
|
|
}
|
|
}
|
|
|
|
$tpl = Theme::get_template("saved_searches.tpl");
|
|
return replace_macros($tpl, [
|
|
'$title' => t('Saved Searches'),
|
|
'$add' => t('add'),
|
|
'$searchbox' => searchbox($search, 'netsearch-box', $srchurl . (($hasq) ? '' : '?f='), true),
|
|
'$saved' => $saved,
|
|
]);
|
|
|
|
}
|
|
}
|