more tables for categories
[contractdashboard.git] / lib / common.inc.php
blob:a/lib/common.inc.php -> blob:b/lib/common.inc.php
--- a/lib/common.inc.php
+++ b/lib/common.inc.php
@@ -4,11 +4,7 @@
 error_reporting(E_ALL ^ E_NOTICE);
 
 
-$conn = new PDO("pgsql:dbname=contractdashboard;user=alex;host=localhost");
-
-if (!$conn) {
-    die("A database error occurred.\n");
-}
+include('db.inc.php');
 
 define('ROOT', pathinfo(__FILE__, PATHINFO_DIRNAME));
 if (strstr($_SERVER['PHP_SELF'], "labs/") || strstr($_SERVER['PHP_SELF'], "admin/") || strstr($_SERVER['PHP_SELF'], "heuristics/")) {
@@ -71,10 +67,32 @@
         echo "</div>";
     }
 }
-
+function cleanseName($input) {
+
+    return trim(join(" ",array_filter(explode(" ",strtolower($input)), function($v, $k) {
+          $cleanseNamesCorp = Array(
+        "ltd",
+        "limited",
+        "australia",
+        "australian",
+        "australasia",
+        "(aus)",
+        "(aust)",
+        "(australia)",
+        "the",
+        "pty",
+        "contractors",
+        "p/l",
+        "inc.",
+        "inc",
+        "incorporated"
+    );
+    return !in_array($v,$cleanseNamesCorp) ;
+}, ARRAY_FILTER_USE_BOTH)));
+}
 function ucsmart($str) {
     $shortWords = Array("The", "Pty", "Ltd", "Inc", "Red", "Oil", "A", "An", "And", "At", "For", "In"
-        , "Of", "On", "Or", "The", "To", "With");
+        , "Of", "On", "Or", "The", "To", "With", "Use", "No", "Yes");
     $strArray = explode(" ",  preg_replace_callback("/(?<=(?<!:|’s)\W)
             (A|An|And|At|For|In|Of|On|Or|The|To|With)
             (?=\W)/",
@@ -102,6 +120,39 @@
     return $totale;
 }
 
+function show_stats($stats) {
+    setlocale(LC_MONETARY, 'en_US');
+  $value = number_format(doubleval($stats['total_value']) , 2);
+  echo "<br/> {$stats['total']} contracts, total value $$value <br/>";
+      if ($stats['consultancy'] > 0) {
+        echo "<b>Consultancies:</b> {$stats['consultancy']} contracts, ".percent($stats['consultancy'],$stats['total']);
+        echo "%<br/>";
+    }
+    if ($stats['confidentialityContract']+$stats['confidentialityOutputs'] > 0) {
+        $confidentialities = $stats['confidentialityContract']+$stats['confidentialityOutputs'];
+        echo "<b>Confidentialities:</b> $confidentialities contracts, ".percent( $confidentialities,$stats['total'])."%";
+        echo "( {$stats['confidentialityContract']} on the contract, {$stats['confidentialityOutputs']} on the outputs)";
+        echo "<br/>";
+    }
+    echo "<b>Procurement methods:</b> ";
+    if ($stats['open'] > 0) {
+        echo "Open {$stats['open']} contracts, ".percent($stats['open'],$stats['total']);
+        echo "%, ";
+    }
+    if ($stats['openSON'] > 0) {
+        echo "Open via SON {$stats['openSON']} contracts, ".percent($stats['openSON'],$stats['total']);
+        echo "%, ";
+    }
+    if ($stats['prequalified'] > 0) {
+        echo "Prequalified {$stats['prequalified']} contracts, ".percent($stats['prequalified'],$stats['total']);
+        echo "%, ";
+    }
+    if ($stats['limited'] > 0) {
+        echo "limited {$stats['limited']} contracts, ".percent($stats['limited'],$stats['total']);
+        echo "%, ";
+    }
+}
+
 // magic query modifiers
 $agency = filter_var($_REQUEST['agency'], FILTER_SANITIZE_STRING);
 if ($agency != "")
@@ -109,13 +160,17 @@
 
 $supplier = filter_var($_REQUEST['supplier'], FILTER_SANITIZE_STRING);
 if ($supplier != "") {
+  if ($supplier[0] !== '-') {
     $supplierParts = explode("-", $supplier);
-    $supplierName = "%" . $supplierParts[1] . "%";
-    $supplierABN = $supplierParts[0];
-    if ($supplierParts[0] > 0)
-        $supplierQ = ' "supplierABN" = :supplierABN AND ';
-    else
-        $supplierQ = ' "supplierName" ILIKE :supplierName AND ';
+
+    $supplierABN = array_shift($supplierParts);
+    $supplierName = "%" . implode('-',$supplierParts) . "%";
+
+    $supplierQ = ' "supplierABN" = :supplierABN AND ';
+   } else {
+    $supplierName = "%" . substr($supplier,1) . "%";
+    $supplierQ = ' "supplierName" ILIKE :supplierName AND ';
+  }
 }
 
 $startYear = 2007;
@@ -157,6 +212,7 @@
               pcdt: http://purl.org/procurement/public-contracts-datatypes#">
         <head>
             <title><?php echo $title; ?> - Contract Dashboard</title>
+            <meta name="viewport" content="width=device-width, initial-scale=1">
             <link rel="stylesheet" type="text/css" href="bootstrap.min.css"> 
             <link rel="stylesheet" type="text/css" href="bootstrap-responsive.css"> 
             <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
@@ -288,7 +344,7 @@
                 	<div class="well sidebar-nav">
                     		<li class="nav-header">Year</li>
 <?php
-foreach (range(2007,2016) as $year) {
+foreach (range(2007,2017) as $year) {
 $get = $_GET;
 $get['year'] = $year;
 $query = http_build_query($get);
@@ -307,10 +363,15 @@
             }
 
             function include_footer() {
-                global $start;
+                global $start, $conn;
                 $end = (float) array_sum(explode(' ', microtime()));
 
-                echo '</div>  <footer>' . "Processing time: " . sprintf("%.4f", ($end - $start)) . " seconds" . '  <footer>';
+        $query = 'SELECT DATE("importDate") FROM contractnotice ORDER BY "importDate" DESC limit 1';
+        $query = $conn->prepare($query);
+        $query->execute();
+        databaseError($conn->errorInfo());
+        $last_update = reset($query->fetchAll())[0];
+                echo '</div>  <footer>' . "Last updated: ".$last_update." Processing time: " . sprintf("%.4f", ($end - $start)) . " seconds" . '  <footer>';
 
                     if (strpos($_SERVER['SERVER_NAME'], ".gs")) {
                     ?>
@@ -326,6 +387,160 @@
                             var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                         })();
 
+                        /**
+                        * https://gist.github.com/remy/370590
+ * Note that this script is intended to be included at the *end* of the document, before </body>
+ */
+(function (window, document) {
+if ('open' in document.createElement('details')) return;
+
+// made global by myself to be reused elsewhere
+var addEvent = (function () {
+  if (document.addEventListener) {
+    return function (el, type, fn) {
+      if (el && el.nodeName || el === window) {
+        el.addEventListener(type, fn, false);
+      } else if (el && el.length) {
+        for (var i = 0; i < el.length; i++) {
+          addEvent(el[i], type, fn);
+        }
+      }
+    };
+  } else {
+    return function (el, type, fn) {
+      if (el && el.nodeName || el === window) {
+        el.attachEvent('on' + type, function () { return fn.call(el, window.event); });
+      } else if (el && el.length) {
+        for (var i = 0; i < el.length; i++) {
+          addEvent(el[i], type, fn);
+        }
+      }
+    };
+  }
+})();
+
+
+/** details support - typically in it's own script */
+// find the first /real/ node
+function firstNode(source) {
+  var node = null;
+  if (source.firstChild.nodeName != "#text") {
+    return source.firstChild;
+  } else {
+    source = source.firstChild;
+    do {
+      source = source.nextSibling;
+    } while (source && source.nodeName == '#text');
+
+    return source || null;
+  }
+}
+
+function isSummary(el) {
+  var nn = el.nodeName.toUpperCase();
+  if (nn == 'DETAILS') {
+    return false;
+  } else if (nn == 'SUMMARY') {
+    return true;
+  } else {
+    return isSummary(el.parentNode);
+  }
+}
+
+function toggleDetails(event) {
+  // more sigh - need to check the clicked object
+  var keypress = event.type == 'keypress',
+      target = event.target || event.srcElement;
+  if (keypress || isSummary(target)) {
+    if (keypress) {
+      // if it's a keypress, make sure it was enter or space
+      keypress = event.which || event.keyCode;
+      if (keypress == 32 || keypress == 13) {
+        // all's good, go ahead and toggle
+      } else {
+        return;
+      }
+    }
+
+    var open = this.getAttribute('open');
+    if (open === null) {
+      this.setAttribute('open', 'open');
+    } else {
+      this.removeAttribute('open');
+    }
+
+    // this.className = open ? 'open' : ''; // Lame
+    // trigger reflow (required in IE - sometimes in Safari too)
+    setTimeout(function () {
+      document.body.className = document.body.className;
+    }, 13);
+
+    if (keypress) {
+      event.preventDefault && event.preventDefault();
+      return false;
+    }
+  }
+}
+
+function addStyle() {
+  var style = document.createElement('style'),
+      head = document.getElementsByTagName('head')[0],
+      key = style.innerText === undefined ? 'textContent' : 'innerText';
+
+  var rules = ['details{display: block;}','details > *{display: none;}','details.open > *{display: block;}','details[open] > *{display: block;}','details > summary:first-child{display: block;cursor: pointer;}','details[open]{display: block;}'];
+      i = rules.length;
+
+  style[key] = rules.join("\n");
+  head.insertBefore(style, head.firstChild);
+}
+
+var details = document.getElementsByTagName('details'),
+    wrapper,
+    i = details.length,
+    j,
+    first = null,
+    label = document.createElement('summary');
+
+label.appendChild(document.createTextNode('Details'));
+
+while (i--) {
+  first = firstNode(details[i]);
+
+  if (first != null && first.nodeName.toUpperCase() == 'SUMMARY') {
+    // we've found that there's a details label already
+  } else {
+    // first = label.cloneNode(true); // cloned nodes weren't picking up styles in IE - random
+    first = document.createElement('summary');
+    first.appendChild(document.createTextNode('Details'));
+    if (details[i].firstChild) {
+      details[i].insertBefore(first, details[i].firstChild);
+    } else {
+      details[i].appendChild(first);
+    }
+  }
+
+  // this feels *really* nasty, but we can't target details :text in css :(
+  j = details[i].childNodes.length;
+  while (j--) {
+    if (details[i].childNodes[j].nodeName === '#text' && (details[i].childNodes[j].nodeValue||'').replace(/\s/g, '').length) {
+      wrapper = document.createElement('text');
+      wrapper.appendChild(details[i].childNodes[j]);
+      details[i].insertBefore(wrapper, details[i].childNodes[j]);
+    }
+  }
+
+  first.legend = true;
+  first.tabIndex = 0;
+}
+
+// trigger details in case this being used on it's own
+document.createElement('details');
+addEvent(details, 'click', toggleDetails);
+addEvent(details, 'keypress', toggleDetails);
+addStyle();
+
+})(window, document);
+
                     </script>
                     <?php
                 }