fix search
fix search

--- a/displayAgency.php
+++ b/displayAgency.php
@@ -7,24 +7,10 @@
     include_header($agency);
 echo '<center><h1>'.$agency.'</h1></center>';
     agencySuppliersGraph($agency);
-//    MethodCountGraph($agency);
- //   CnCGraph($agency);
- //   MethodValueGraph($agency);
-    /* biggest contracts
-      spending by year
-      spending by industry/category
-      spending by supplier
-
-      spread procurement methods (stacked bar graph)
-      + percent consultancies + percent confidential (bar graph)
-
-      Average value by procurement type
-
-      --- info
-      website, procurement plan, annual reports
-      Breakdown of divisions/branches
-      Breakdown percentage,number,value by procurement type
-      Histograph, overlaying number value reported per week over X years
+/*
+ *     * Common categories and uncommon categories
+    * Procurement method in text, seperate Open via SON from Open
+    * Consultancies, Confidentialities percentage
       Compliance statistics: amendments, delay in reporting average and number completely late */
 
     $query = 'SELECT "CNID", "description", "value", "agencyName", "category",

--- a/displaySupplier.php
+++ b/displaySupplier.php
@@ -2,27 +2,26 @@
 
 include_once ("./lib/common.inc.php");
 if ($_REQUEST['supplier']) {
-
     $supplierS = htmlentities(strip_tags($_REQUEST['supplier']));
-    include_header(str_replace("%","",$supplierName));
-echo '<center><h1>'.str_replace("%","",$supplierName).'</h1></center>';
+$title = $supplierName;
+if ($supplierABN) {
+  $query = 'SELECT text_mode("supplierName") as "supplierName" from contractnotice where "supplierABN" = :supplierABN group by "supplierABN"';
+$query = $conn->prepare($query);
+$query->bindParam(":supplierABN", $supplierABN);
+        $query->execute();
+                databaseError($conn->errorInfo());
+        $title = reset($query->fetchAll())['supplierName'];
+}
+    include_header(str_replace("%","",$title));
+echo '<center><h1>'.str_replace("%","",$title).'</h1></center>';
     agenciesGraph();
-   // MethodCountGraph($supplierS);
-  //  CnCGraph($supplierS);
-   // MethodValueGraph($supplierS);
-    /* lobbyist ties
-
-      links to ABR/ASIC/Google News/ASX/Court records
-
-      total value to various agencies (bar graph)
-
-      spread procurement methods + percent consultancies + percent confidential (bar graph)
-
-      spread of contract values
-
-      spread of industries (textual?) */
-   // echo '<img src="graphs/displayMethodCountGraph.php?month=' . stripslashes($supplier) . '">';
-   // echo '<img src="graphs/displayCnCGraph.php?month=' . stripslashes($supplier) . '">';
+   /*
+    * Make supplier name the most common name for this ABN
+    * Common names and uncommon names
+    * Common categories and uncommon categories
+    * Procurement method in text, seperate Open via SON from Open
+    * Consultancies, Confidentialities percentage
+    */
 $query = 'select contractnotice."SONID", min(title) as title, count(*), sum(value) as value from contractnotice inner join standingoffers on contractnotice."SONID" = standingoffers."SONID" WHERE ' .$yearQ . ' ' .
             $supplierQ . ' ' . $standardQ
             . ' and contractnotice."SONID" != \'\' group by contractnotice."SONID"';
@@ -36,7 +35,7 @@
   }
     $query->execute();
     databaseError($conn->errorInfo());
-echo "<table>  <thead>
+echo "<h3>Standing Offers/Panels</h3><table>  <thead>
     <tr>
       <th>Standing Offer</th>
       <th>Contracts Count</th>
@@ -52,7 +51,7 @@
     <td>\$$value</td>
     </tr>");
     }
-    echo "</table><table>  <thead>
+    echo "</table><table><h3>Contracts</h3>  <thead>
     <tr>
       <th>Contract Notice Number</th>
       <th>Contract Description</th>
@@ -96,7 +95,7 @@
      */
     include_header("Suppliers");
     suppliersGraph();
-    $query = 'SELECT SUM("value") as val, mode("supplierName") as supplierName, "supplierABN",(
+    $query = 'SELECT SUM("value") as val, text_mode("supplierName") as supplierName, "supplierABN",(
  case when "supplierABN" != 0 THEN lower("supplierABN"::text) ELSE lower("supplierName") END) as supplierID 
 FROM contractnotice
 WHERE ' .$yearQ . ' "childCN" is null

--- a/install.sql
+++ b/install.sql
@@ -11,10 +11,15 @@
 LANGUAGE 'sql' IMMUTABLE;
  
 -- Tell Postgres how to use our aggregate
-CREATE AGGREGATE mode(anyelement) (
+CREATE AGGREGATE text_mode(anyelement) (
   SFUNC=array_append, --Function to call for each row. Just builds the array
   STYPE=anyarray,
   FINALFUNC=_final_mode, --Function to call after everything has been added to array
   INITCOND='{}' --Initialize an empty array when starting
 );
 
+CREATE VIEW suppliers AS
+  select distinct on ("supplierABN") "supplierABN", "supplierName", count("supplierName") from contractnotice
+  where "supplierABN" is not null
+  group by "supplierABN", "supplierName"
+  order by "supplierABN" asc, count("supplierName") desc;

--- a/lib/graphs.inc.php
+++ b/lib/graphs.inc.php
@@ -229,7 +229,7 @@
     includeFlot();
 
     $topX = 20;
-    $query = 'SELECT SUM(value) as val, mode("supplierName") "supplierName" FROM contractnotice WHERE ' .$yearQ . ' extract ("YEAR" from "contractStart") >= :startYear AND "childCN" is null AND "agencyName" like :agency
+    $query = 'SELECT SUM(value) as val, text_mode("supplierName") "supplierName" FROM contractnotice WHERE ' .$yearQ . ' extract ("YEAR" from "contractStart") >= :startYear AND "childCN" is null AND "agencyName" like :agency
 GROUP BY lower("supplierName") ORDER BY val DESC limit '.$topX;
     $query = $conn->prepare($query);
     $query->bindParam(":startYear", $startYear);
@@ -244,7 +244,7 @@
     }
 
 
-    $query = 'SELECT sum(a.val) as value, count(1) as count from (SELECT SUM(value) as val, mode("supplierName") FROM contractnotice WHERE ' .$yearQ . '(extract ("YEAR" from "contractStart") >= :startYear) AND "childCN" is null and "agencyName" like :agency
+    $query = 'SELECT sum(a.val) as value, count(1) as count from (SELECT SUM(value) as val, text_mode("supplierName") FROM contractnotice WHERE ' .$yearQ . '(extract ("YEAR" from "contractStart") >= :startYear) AND "childCN" is null and "agencyName" like :agency
 GROUP BY lower("supplierName") ORDER BY val DESC LIMIT 184467440 OFFSET '.$topX.') as a';
     $query = $conn->prepare($query);
     $query->bindParam(":startYear", $startYear);
@@ -516,7 +516,7 @@
     $values = Array();
 
 
-    $query = 'SELECT SUM("value") as value, mode("supplierName") as supplierName, (
+    $query = 'SELECT SUM("value") as value, text_mode("supplierName") as supplierName, (
  case when "supplierABN" != 0 THEN "supplierABN"::text ELSE "supplierName" END) as supplierID 
 FROM contractnotice
 WHERE ' .$yearQ . ' "childCN" is null

--- a/search_autosuggest.php
+++ b/search_autosuggest.php
@@ -6,10 +6,10 @@
 $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
 $aResults = array();
 $count = 0;
-if ($len) {
-    $query = 'SELECT mode("supplierName"), MAX("supplierABN"), count(*) as count
+if ($len > 2) {
+    $query = 'SELECT text_mode("supplierName"), MAX("supplierABN"), count(*) as count
 FROM contractnotice
-WHERE "supplierName" ILIKE :supplierName
+WHERE "supplierName" ILIKE :supplierName or "supplierABN"::text = :supplierABN
 GROUP BY "supplierName"
 ORDER BY count(*) DESC
 LIMIT 4;
@@ -17,12 +17,14 @@
     $query = $conn->prepare($query);
     $supplierName = $input . '%';
     $query->bindParam(":supplierName", $supplierName);
+    $supplierABN = $input ;
+    $query->bindParam(":supplierABN", $supplierABN);
     $query->execute();
     databaseError($conn->errorInfo());
     foreach ($query->fetchAll() as $row) {
         $count++;
         $aResults[] = array(
-            "id" => "supplier-" . $row[1] . '-' . $row[0],
+            "id" => "supplier-" . $row[1] . '-' . htmlspecialchars($row[0]),
             "value" => htmlspecialchars($row[0]),
             "info" => htmlspecialchars("Supplier - " . $row['count'] . " records")
         );