streams/Code/Widget/Savedsearch.php

102 lines
3.4 KiB
PHP
Raw Normal View History

2017-03-16 01:48:27 +00:00
<?php
2022-02-16 04:08:28 +00:00
namespace Code\Widget;
2017-03-16 01:48:27 +00:00
2021-12-02 22:33:36 +00:00
use App;
2022-02-16 04:08:28 +00:00
use Code\Lib\Features;
use Code\Render\Theme;
2022-02-12 20:43:29 +00:00
2021-12-02 22:33:36 +00:00
2022-10-23 21:18:44 +00:00
class Savedsearch implements WidgetInterface
2021-12-02 23:02:31 +00:00
{
2022-10-24 03:39:49 +00:00
public function widget(array $arguments): string
2021-12-02 23:02:31 +00:00
{
2022-01-25 23:20:02 +00:00
if ((!local_channel()) || (!Features::enabled(local_channel(), 'savedsearch'))) {
2021-12-02 23:02:31 +00:00
return '';
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$search = ((x($_GET, 'netsearch')) ? $_GET['netsearch'] : '');
2021-12-03 03:01:39 +00:00
if (!$search) {
2021-12-02 23:02:31 +00:00
$search = ((x($_GET, 'search')) ? $_GET['search'] : '');
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
if (x($_GET, 'searchsave') && $search) {
2021-12-03 03:01:39 +00:00
$r = q(
"select * from term where uid = %d and ttype = %d and term = '%s' limit 1",
2021-12-02 23:02:31 +00:00
intval(local_channel()),
intval(TERM_SAVEDSEARCH),
dbesc($search)
);
if (!$r) {
2021-12-03 03:01:39 +00:00
q(
"insert into term ( uid,ttype,term ) values ( %d, %d, '%s') ",
2021-12-02 23:02:31 +00:00
intval(local_channel()),
intval(TERM_SAVEDSEARCH),
dbesc($search)
);
}
}
if (x($_GET, 'searchremove') && $search) {
2021-12-03 03:01:39 +00:00
q(
"delete from term where uid = %d and ttype = %d and term = '%s'",
2021-12-02 23:02:31 +00:00
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), '&');
2022-11-20 05:28:50 +00:00
$srchurl = str_replace(['?f=', '&f='], ['', ''], $srchurl);
2021-12-02 23:02:31 +00:00
2022-11-20 05:28:50 +00:00
$hasq = str_contains($srchurl, '?');
$hasamp = str_contains($srchurl, '&');
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
if (($hasamp) && (!$hasq)) {
2021-12-02 23:02:31 +00:00
$srchurl = substr($srchurl, 0, strpos($srchurl, '&')) . '?f=&' . substr($srchurl, strpos($srchurl, '&') + 1);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
$r = q(
"select tid,term from term WHERE uid = %d and ttype = %d ",
2021-12-02 23:02:31 +00:00
intval(local_channel()),
intval(TERM_SAVEDSEARCH)
);
$saved = [];
if (count($r)) {
foreach ($r as $rr) {
2022-11-20 05:28:50 +00:00
$saved[] = [
2021-12-02 23:02:31 +00:00
'id' => $rr['tid'],
'term' => $rr['term'],
'dellink' => z_root() . '/' . $srchurl . (($hasq || $hasamp) ? '' : '?f=') . '&amp;searchremove=1&amp;search=' . urlencode($rr['term']),
'srchlink' => z_root() . '/' . $srchurl . (($hasq || $hasamp) ? '' : '?f=') . '&amp;search=' . urlencode($rr['term']),
'displayterm' => htmlspecialchars($rr['term'], ENT_COMPAT, 'UTF-8'),
'encodedterm' => urlencode($rr['term']),
'delete' => t('Remove term'),
'selected' => ($search == $rr['term']),
2022-11-20 05:28:50 +00:00
];
2021-12-02 23:02:31 +00:00
}
}
2022-02-12 20:43:29 +00:00
$tpl = Theme::get_template("saved_searches.tpl");
2022-11-20 05:28:50 +00:00
return replace_macros($tpl, [
2021-12-02 23:02:31 +00:00
'$title' => t('Saved Searches'),
'$add' => t('add'),
'$searchbox' => searchbox($search, 'netsearch-box', $srchurl . (($hasq) ? '' : '?f='), true),
'$saved' => $saved,
2022-11-20 05:28:50 +00:00
]);
2021-12-02 23:02:31 +00:00
}
2017-03-16 01:48:27 +00:00
}