2012-07-17 01:14:45 +00:00
< ? php
2015-01-08 16:16:08 +00:00
// Autocomplete for saved searches. Should probably be put in the same place as the other autocompletes
2012-07-17 01:14:45 +00:00
function search_ac_init ( & $a ){
2015-01-29 04:56:04 +00:00
if ( ! local_channel ())
2015-01-08 16:16:08 +00:00
killme ();
2012-07-17 01:14:45 +00:00
$start = ( x ( $_REQUEST , 'start' ) ? $_REQUEST [ 'start' ] : 0 );
$count = ( x ( $_REQUEST , 'count' ) ? $_REQUEST [ 'count' ] : 100 );
$search = ( x ( $_REQUEST , 'search' ) ? $_REQUEST [ 'search' ] : " " );
if ( x ( $_REQUEST , 'query' ) && strlen ( $_REQUEST [ 'query' ])) {
$search = $_REQUEST [ 'query' ];
}
// Priority to people searches
if ( $search ) {
2013-10-02 02:54:22 +00:00
$people_sql_extra = protect_sprintf ( " AND `xchan_name` LIKE '% " . dbesc ( $search ) . " %' " );
2012-07-17 01:14:45 +00:00
$tag_sql_extra = protect_sprintf ( " AND term LIKE '% " . dbesc ( $search ) . " %' " );
}
2013-10-02 02:54:22 +00:00
2015-01-08 16:16:08 +00:00
$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
2013-10-02 02:54:22 +00:00
$people_sql_extra
ORDER BY `xchan_name` ASC " ,
2015-01-29 04:56:04 +00:00
intval ( local_channel ())
2013-10-02 02:54:22 +00:00
);
2015-01-08 16:16:08 +00:00
$results = array ();
2013-10-02 02:54:22 +00:00
if ( $r ) {
foreach ( $r as $g ) {
2015-01-08 16:16:08 +00:00
$results [] = array (
" photo " => $g [ 'xchan_photo_s' ],
" name " => '@' . $g [ 'xchan_name' ],
" id " => $g [ 'abook_id' ],
" link " => $g [ 'xchan_url' ],
" label " => '' ,
" nick " => '' ,
);
2013-10-02 02:54:22 +00:00
}
}
2012-07-17 01:14:45 +00:00
2015-11-20 08:15:48 +00:00
$r = q ( " select distinct term, tid, url from term where type in ( %d, %d ) $tag_sql_extra group by term order by term asc " ,
intval ( TERM_HASHTAG ),
intval ( TERM_COMMUNITYTAG )
2012-07-17 01:14:45 +00:00
);
if ( count ( $r )) {
foreach ( $r as $g ) {
2015-01-08 16:16:08 +00:00
$results [] = array (
" photo " => $a -> get_baseurl () . '/images/hashtag.png' ,
" name " => '#' . $g [ 'term' ],
" id " => $g [ 'tid' ],
" link " => $g [ 'url' ],
" label " => '' ,
" nick " => '' ,
);
2012-07-17 01:14:45 +00:00
}
}
header ( " content-type: application/json " );
2015-01-08 16:16:08 +00:00
$o = array (
'start' => $start ,
'count' => $count ,
'items' => $results ,
);
echo json_encode ( $o );
2012-07-17 01:14:45 +00:00
logger ( 'search_ac: ' . print_r ( $x , true ));
killme ();
}