Fix autosuggest
[contractdashboard.git] / search_autosuggest.php
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
<?php
 
include_once ("./lib/common.inc.php");
$input = strtolower($_REQUEST['input']);
$len = strlen($input);
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
$aResults = array();
$count = 0;
if ($len) {
    $query = 'SELECT MAX("supplierName"), MAX("supplierABN"), count(*) as count
FROM contractnotice
WHERE "supplierName" ILIKE :supplierName
GROUP BY "supplierName"
ORDER BY count(*) DESC
LIMIT 4;
';
    $query = $conn->prepare($query);
    $supplierName = $input . '%';
    $query->bindParam(":supplierName", $supplierName);
    $query->execute();
    databaseError($conn->errorInfo());
    foreach ($query->fetchAll() as $row) {
        $count++;
        $aResults[] = array(
            "id" => "supplier-" . $row[1] . '-' . $row[0],
            "value" => htmlspecialchars($row[0]),
            "info" => htmlspecialchars("Supplier - " . $row['count'] . " records")
        );
    }
    $query = 'SELECT MAX("agencyName"), count(*) as count
FROM contractnotice
WHERE "agencyName" ILIKE :agencyName
GROUP BY "agencyName"
ORDER BY count DESC
LIMIT 4;';
    $query = $conn->prepare($query);
    $agencyName = $input . '%';
    $query->bindParam(":agencyName", $agencyName);
    $query->execute();
    databaseError($conn->errorInfo());
    foreach ($query->fetchAll() as $row) {
        $count++;
        $aResults[] = array(
            "id" => "agency-" . $row[0],
            "value" => htmlspecialchars($row[0]),
            "info" => htmlspecialchars("Government Agency - " . $row['count'] . " records")
        );
    }
        $query = "SELECT \"CNID\", description, value FROM contractnotice
WHERE to_tsvector('english', description) @@ plaintoto_tsquery('english', :input)";
    $query = $conn->prepare($query);
    $agencyName = $input . '%';
    $query->bindParam(":input", $input);
    $query->execute();
    databaseError($conn->errorInfo());
    foreach ($query->fetchAll() as $row) {
        $count++;
        $aResults[] = array(
            "id" => "cnid-" . $row[0],
            "value" => htmlspecialchars($row['description']),
            "info" => htmlspecialchars("Contract Notice - Value ".$row['value'])
        );
    }
}
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Pragma: no-cache"); // HTTP/1.0
if (isset($_REQUEST['json'])) {
    header("Content-Type: application/json");
    echo "{\"results\": [";
    $arr = array();
    for ($i = 0; $i < count($aResults); $i++) {
        $arr[] = "{\"id\": \"" . $aResults[$i]['id'] . "\", \"value\": \"" . $aResults[$i]['value'] . "\", \"info\": \"\"}";
    }
    echo implode(", ", $arr);
    echo "]}";
} else {
    header("Content-Type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>";
    for ($i = 0; $i < count($aResults); $i++) {
        echo "<rs id=\"" . $aResults[$i]['id'] . "\" info=\"" . $aResults[$i]['info'] . "\">" . $aResults[$i]['value'] . "</rs>";
    }
    echo "</results>";
}
?>