aboutsummaryrefslogtreecommitdiffstats
path: root/mod/search_ac.php
blob: 308f42f767669457015f49ba3c1409b033220af9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

function search_ac_init(&$a){
	if(!local_user())
		return "";


	$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'];
	}


	$sql_extra = '';

	$x = array();
	$x['query'] = $search;
	$x['photos'] = array();
	$x['links'] = array();
	$x['suggestions'] = array();
	$x['data'] = array();


	// 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) . "%' ");
	}


    $r = q("SELECT abook_id as id, xchan_hash as hash, xchan_name as name, xchan_photo_s as micro, xchan_url as url, xchan_addr as nick, abook_their_perms
		FROM abook left join xchan on abook_xchan = xchan_hash
		WHERE abook_channel = %d AND not ( abook_flags & %d ) $people_sql_extra order by xchan_name asc" ,
		intval(local_user()),
		intval(ABOOK_FLAG_SELF|ABOOK_FLAG_BLOCKED|ABOOK_FLAG_PENDING|ABOOK_FLAG_ARCHIVED)
	);

	if($r) {
		foreach($r as $g) {
			$x['photos'][] = $g['micro'];
			$x['links'][] = $g['url'];
			$x['suggestions'][] = '@' . $g['name'];
			$x['data'][] = intval($g['id']);
		}
	}
	else {

		$r = q("SELECT `id`, `name`, `photo`, `url` FROM `gcontact` where 1
			$people_sql_extra
			ORDER BY `name` ASC "
		);

		if(count($r)) {
			foreach($r as $g) {
				$x['photos'][] = $g['photo'];
				$x['links'][] = $g['url'];
				$x['suggestions'][] = '@' . $g['name'];
				$x['data'][] = intval($g['id']);
			}
		}
	}

	$r = q("select tid, term, url from term where type = %d $tag_sql_extra order by term asc",
		intval(TERM_HASHTAG)
	);

	if(count($r)) {
		foreach($r as $g) {
			$x['photos'][] = $a->get_baseurl() . '/images/hashtag.png';
			$x['links'][] = $g['url'];
			$x['suggestions'][] = '#' . $g['term'];
			$x['data'][] = intval($g['tid']);
		}
	}

	header("content-type: application/json");
	echo json_encode($x);

	logger('search_ac: ' . print_r($x,true));

	killme();
}