summaryrefslogtreecommitdiff
path: root/includes/lookup_contact.php
blob: 1be1658d4fc6c14685870e23cfcaee0a48fef51f (plain)
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
<?php
/**
 * JSON autocomplete endpoint — returns contacts matching ?q= by title or SCREF short name.
 *
 * @package contact
 */

namespace Bitweaver\Contact;

require_once '../../kernel/includes/setup_inc.php';

global $gBitDb, $gBitUser;

if( !$gBitUser->hasPermission( 'p_contact_view' ) ) {
	header( 'Content-Type: application/json' );
	echo '[]';
	exit;
}

$q = trim( $_GET['q'] ?? '' );
if( strlen( $q ) < 2 ) {
	header( 'Content-Type: application/json' );
	echo '[]';
	exit;
}

$like = '%'.strtolower( $q ).'%';

$rows = $gBitDb->getArray(
	"SELECT FIRST 30 lc.content_id, lc.title,
		(SELECT FIRST 1 sx.data FROM ".BIT_DB_PREFIX."liberty_xref sx
		 WHERE sx.content_id=lc.content_id AND sx.item='SCREF') AS scref
	 FROM ".BIT_DB_PREFIX."liberty_content lc
	 WHERE lc.content_type_guid='contact'
	   AND (LOWER(lc.title) LIKE ? OR EXISTS (
		SELECT 1 FROM ".BIT_DB_PREFIX."liberty_xref sx
		WHERE sx.content_id=lc.content_id AND sx.item='SCREF' AND LOWER(sx.data) LIKE ?
	   ))
	 ORDER BY lc.title",
	[ $like, $like ]
);

header( 'Content-Type: application/json' );
echo json_encode( array_values( $rows ?? [] ) );
exit;