streams/Zotlabs/Module/Search_ac.php

101 lines
2.2 KiB
PHP
Raw Normal View History

2016-04-19 03:38:38 +00:00
<?php
namespace Zotlabs\Module;
// Autocomplete for saved searches. Should probably be put in the same place as the other autocompletes
2019-05-23 23:37:56 +00:00
use Zotlabs\Web\Controller;
2016-04-19 03:38:38 +00:00
2019-05-23 23:37:56 +00:00
class Search_ac extends Controller {
function init() {
if (! local_channel()) {
2016-04-19 03:38:38 +00:00
killme();
2019-05-23 23:37:56 +00:00
}
2016-04-19 03:38:38 +00:00
2019-05-23 23:37:56 +00:00
$start = (x($_REQUEST,'start') ? $_REQUEST['start'] : 0);
$count = (x($_REQUEST,'count') ? $_REQUEST['count'] : 100);
$search = (x($_REQUEST,'search') ? $_REQUEST['search'] : EMPTY_STR);
2016-04-19 03:38:38 +00:00
2019-05-23 23:37:56 +00:00
if (x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
2016-04-19 03:38:38 +00:00
$search = $_REQUEST['query'];
}
$do_people = true;
2019-05-23 23:37:56 +00:00
$do_tags = true;
2019-05-23 23:37:56 +00:00
if (substr($search,0,1) === '@') {
$do_tags = false;
$search = substr($search,1);
}
2019-05-23 23:37:56 +00:00
if (substr($search,0,1) === '#') {
$do_people = false;
$search = substr($search,1);
}
2016-04-19 03:38:38 +00:00
// Priority to people searches
if ($search) {
$people_sql_extra = protect_sprintf(" AND xchan_name LIKE '%" . dbesc($search) . "%' ");
$tag_sql_extra = protect_sprintf(" AND term LIKE '%" . dbesc($search) . "%' ");
2016-04-19 03:38:38 +00:00
}
$results = [];
2019-05-23 23:37:56 +00:00
if ($do_people) {
$r = q("SELECT abook_id, xchan_name, xchan_photo_s, xchan_url, xchan_addr FROM abook
left join xchan on abook_xchan = xchan_hash WHERE abook_channel = %d
$people_sql_extra
ORDER BY xchan_name ASC ",
intval(local_channel())
);
2019-05-23 23:37:56 +00:00
if ($r) {
foreach ($r as $g) {
$results[] = [
'photo' => $g['xchan_photo_s'],
'name' => '@' . $g['xchan_name'],
'id' => $g['abook_id'],
'link' => $g['xchan_url'],
'label' => '',
'nick' => '',
];
}
2016-04-19 03:38:38 +00:00
}
}
2019-05-23 23:37:56 +00:00
if ($do_tags) {
$r = q("select distinct term, tid, url from term
where ttype in ( %d, %d ) $tag_sql_extra group by term order by term asc",
intval(TERM_HASHTAG),
intval(TERM_COMMUNITYTAG)
);
2019-05-23 23:37:56 +00:00
if ($r) {
foreach ($r as $g) {
$results[] = [
'photo' => z_root() . '/images/hashtag.png',
'name' => '#' . $g['term'],
'id' => $g['tid'],
'link' => $g['url'],
'label' => '',
'nick' => '',
];
}
2016-04-19 03:38:38 +00:00
}
}
2019-05-23 23:37:56 +00:00
json_return_and_die( [
2016-04-19 03:38:38 +00:00
'start' => $start,
'count' => $count,
'items' => $results,
2019-05-23 23:37:56 +00:00
]);
2016-04-19 03:38:38 +00:00
}
}