add datagov importer
[contractdashboard.git] / lib / common.inc.php
blob:a/lib/common.inc.php -> blob:b/lib/common.inc.php
<?php <?php
date_default_timezone_set("Australia/ACT"); date_default_timezone_set("Australia/ACT");
   
error_reporting(E_ALL ^ E_NOTICE); error_reporting(E_ALL ^ E_NOTICE);
   
   
$conn = new PDO("pgsql:dbname=contractDashboard;user=postgres;password=snmc;host=localhost"); $conn = new PDO("pgsql:dbname=contractDashboard;user=postgres;password=snmc;host=localhost");
   
if (!$conn) { if (!$conn) {
die("A database error occurred.\n"); die("A database error occurred.\n");
} }
   
define('ROOT', pathinfo(__FILE__, PATHINFO_DIRNAME)); define('ROOT', pathinfo(__FILE__, PATHINFO_DIRNAME));
if (strstr($_SERVER['PHP_SELF'], "labs/") || strstr($_SERVER['PHP_SELF'], "admin/") || strstr($_SERVER['PHP_SELF'], "heuristics/")) { if (strstr($_SERVER['PHP_SELF'], "labs/") || strstr($_SERVER['PHP_SELF'], "admin/") || strstr($_SERVER['PHP_SELF'], "heuristics/")) {
$basePath = "../"; $basePath = "../";
} }
if (php_sapi_name() != "cli") { require ROOT . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'openid.php';
  $openid = new LightOpenID($_SERVER['HTTP_HOST']);
require $basePath."lib/amon-php/amon.php"; // you have to open the session to be able to modify or remove it
Amon::config(array('address'=> 'http://127.0.0.1:2464', session_start();
'protocol' => 'http', function login() {
'secret_key' => "JBcSUdFOi5lK0vCjLjbHDpQamcBnRA4iV7QLaTADeDQ")); global $openid;
Amon::setup_exception_handler(); if (!$openid->mode) {
} $openid->required = array('contact/email');
  $openid->identity = 'https://www.google.com/accounts/o8/id';
require ROOT . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'openid.php'; header('Location: ' . $openid->authUrl());
$openid = new LightOpenID($_SERVER['HTTP_HOST']); }
// you have to open the session to be able to modify or remove it }
session_start();  
function login() { function auth() {
global $openid; global $openid;
if (!$openid->mode) { if ($_SESSION['authed'] == true) {
$openid->required = array('contact/email'); return true;
$openid->identity = 'https://www.google.com/accounts/o8/id'; }
header('Location: ' . $openid->authUrl());  
} if ($openid->mode) {
} $attr = $openid->getAttributes();
  if ($attr['contact/email'] != 'maxious@gmail.com') {
function auth() { die('Access Denied');
global $openid; } else {
if ($_SESSION['authed'] == true) { $_SESSION['authed'] = true;
return true; }
} } else {
  login();
if ($openid->mode) { }
$attr = $openid->getAttributes(); }
if ($attr['contact/email'] != 'maxious@gmail.com') {  
die('Access Denied'); // $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} else { function databaseError($errMsg) {
$_SESSION['authed'] = true; if ($errMsg[2] != "") {
} echo '<div class="alert-message error">';
} else { die(print_r($errMsg, true));
login(); echo "</div>";
} }
} }
   
// $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); function ucsmart($str) {
function databaseError($errMsg) { $shortWords = Array("The", "Pty", "Ltd", "Inc", "Red", "Oil", "A", "An", "And", "At", "For", "In"
if ($errMsg[2] != "") { , "Of", "On", "Or", "The", "To", "With");
echo '<div class="alert-message error">'; $strArray = explode(" ", preg_replace("/(?<=(?<!:|’s)\W)
Amon::log(print_r($errMsg, true).print_r($_REQUEST, true).print_r($_SERVER, true), array('error')); (A|An|And|At|For|In|Of|On|Or|The|To|With)
die(print_r($errMsg, true)); (?=\W)/e", 'strtolower("$1")', ucwords(strtolower($str))));
echo "</div>"; foreach ($strArray as &$word) {
} if (strlen($word) <= 4 && !in_array($word, $shortWords))
} $word = strtoupper($word);
  }
function ucsmart($str) { return implode(" ", $strArray);
$shortWords = Array("The", "Pty", "Ltd", "Inc", "Red", "Oil", "A", "An", "And", "At", "For", "In" }
, "Of", "On", "Or", "The", "To", "With");  
$strArray = explode(" ", preg_replace("/(?<=(?<!:|’s)\W) function percent($num_amount, $num_total) {
(A|An|And|At|For|In|Of|On|Or|The|To|With) $count1 = $num_amount / $num_total;
(?=\W)/e", 'strtolower("$1")', ucwords(strtolower($str)))); $count2 = $count1 * 100;
foreach ($strArray as &$word) { $count = number_format($count2, 2);
if (strlen($word) <= 4 && !in_array($word, $shortWords)) return $count;
$word = strtoupper($word); }
}  
return implode(" ", $strArray); function array_sum_all($a) {
} if (!is_array($a))
  return $a;
function percent($num_amount, $num_total) { foreach ($a as $key => $value)
$count1 = $num_amount / $num_total; $totale += array_sum_all($value);
$count2 = $count1 * 100; return $totale;
$count = number_format($count2, 2); }
return $count;  
} // magic query modifiers
  $agency = filter_var($_REQUEST['agency'], FILTER_SANITIZE_STRING);
function array_sum_all($a) { if ($agency != "")
if (!is_array($a)) $agencyQ = "agencyName = '" . $agency . "' AND ";
return $a;  
foreach ($a as $key => $value) $supplier = filter_var($_REQUEST['supplier'], FILTER_SANITIZE_STRING);
$totale += array_sum_all($value); if ($supplier != "") {
return $totale; $supplierParts = explode("-", $supplier);
} $supplierName = "%" . $supplierParts[1] . "%";
  $supplierABN = $supplierParts[0];
// magic query modifiers if ($supplierParts[0] > 0)
$agency = filter_var($_REQUEST['agency'], FILTER_SANITIZE_STRING); $supplierQ = ' "supplierABN" = :supplierABN AND ';
if ($agency != "") else
$agencyQ = "agencyName = '" . $agency . "' AND "; $supplierQ = ' "supplierName" ILIKE :supplierName AND ';
  }
$supplier = filter_var($_REQUEST['supplier'], FILTER_SANITIZE_STRING);  
if ($supplier != "") { $startYear = 2007;
$supplierParts = explode("-", $supplier); $year = filter_var($_REQUEST['year'], FILTER_SANITIZE_NUMBER_INT);
$supplierName = "%" . $supplierParts[1] . "%"; if ($year != "") {
$supplierABN = $supplierParts[0]; $yearQ = 'extract(year from "contractStart") = ' . $year . " AND ";
if ($supplierParts[0] > 0) }
$supplierQ = ' "supplierABN" = :supplierABN AND '; $standardQ = ' "childCN" is null '; // AND YEAR(contractStart) >= 2007 AND YEAR(contractStart) <= 2010';
else $start = 0.0;
$supplierQ = ' "supplierName" LIKE :supplierName AND ';  
} function local_url() {
  return "http://" . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . "/";
$startYear = 2007; }
$year = filter_var($_REQUEST['year'], FILTER_SANITIZE_NUMBER_INT);  
if ($year != "") { function include_header($title) {
$yearQ = "YEAR(publishDate) = " . $year . " AND "; global $start;
} ?>
$standardQ = ' "childCN" is null '; // AND YEAR(contractStart) >= 2007 AND YEAR(contractStart) <= 2010'; <!DOCTYPE html>
$start = 0.0; <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" version="XHTML+RDFa 1.1"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
function local_url() { xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
return "http://" . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . "/"; xmlns:gr="http://purl.org/goodrelations/v1#"
} xmlns:dc="http://purl.org/dc/terms/"
  xmlns:pc="http://purl.org/procurement#"
function include_header($title) { xmlns:unspsc="http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml#"
global $start; xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
?> xmlns:pcdt="http://purl.org/procurement/public-contracts-datatypes#"
<!DOCTYPE html> prefix="rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" version="XHTML+RDFa 1.1" rdfs: http://www.w3.org/2000/01/rdf-schema#
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" gr: http://purl.org/goodrelations/v1#
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" dcterms: http://purl.org/dc/terms/
xmlns:gr="http://purl.org/goodrelations/v1#" pc: http://purl.org/procurement/public-contracts#
xmlns:dc="http://purl.org/dc/terms/" cpv: http://purl.org/weso/pscs/cpv/2008/resource/
xmlns:pc="http://purl.org/procurement#" unspsc: http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml#
xmlns:unspsc="http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml#" v: http://www.w3.org/2006/vcard/ns#
xmlns:xsd="http://www.w3.org/2001/XMLSchema#" payment: http://reference.data.gov.uk/def/payment#
xmlns:pcdt="http://purl.org/procurement/public-contracts-datatypes#" br: http://purl.org/business-register#
prefix="rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns# xsd: http://www.w3.org/2001/XMLSchema#
rdfs: http://www.w3.org/2000/01/rdf-schema# pcdt: http://purl.org/procurement/public-contracts-datatypes#">
gr: http://purl.org/goodrelations/v1# <head>
dcterms: http://purl.org/dc/terms/ <title><?php echo $title; ?> - Contract Dashboard</title>
pc: http://purl.org/procurement/public-contracts# <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
cpv: http://purl.org/weso/pscs/cpv/2008/resource/ <link rel="stylesheet" type="text/css" href="bootstrap-responsive.css">
unspsc: http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml# <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
v: http://www.w3.org/2006/vcard/ns# <!--[if lt IE 9]>
payment: http://reference.data.gov.uk/def/payment# <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
br: http://purl.org/business-register# <![endif]-->
xsd: http://www.w3.org/2001/XMLSchema# <script type="text/javascript" src="lib/bsn.AutoSuggest_2.1.3_comp.js" charset="utf-8"></script>
pcdt: http://purl.org/procurement/public-contracts-datatypes#"> <link rel="stylesheet" href="autosuggest_inquisitor.css" type="text/css" media="screen" charset="utf-8" />
<head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<title><?php echo $title; ?> - Contract Dashboard</title> <script type="text/javascript">
<link rel="stylesheet" type="text/css" href="bootstrap.min.css"> $(document).ready(function()
<link rel="stylesheet" type="text/css" href="bootstrap-responsive.css"> {
<!-- Le HTML5 shim, for IE6-8 support of HTML elements --> //hide the all of the element with class msg_body
<!--[if lt IE 9]> $(".msg_body").hide();
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> //toggle the componenet with class msg_body
<![endif]--> $(".msg_head").click(function()
<script type="text/javascript" src="lib/bsn.AutoSuggest_2.1.3_comp.js" charset="utf-8"></script> {
<link rel="stylesheet" href="autosuggest_inquisitor.css" type="text/css" media="screen" charset="utf-8" /> $(this).next(".msg_body").slideToggle(600);
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> });
<script type="text/javascript"> });
$(document).ready(function() </script>
{  
//hide the all of the element with class msg_body <style type="text/css" title="currentStyle">
$(".msg_body").hide(); @import "media/css/demo_table.css";
//toggle the componenet with class msg_body </style>
$(".msg_head").click(function() <script type="text/javascript" language="javascript" src="media/js/jquery.dataTables.js"></script>
{ <script type="text/javascript" language="javascript" src="lib/bootstrap-dropdown.js"></script>
$(this).next(".msg_body").slideToggle(600); <script type="text/javascript" charset="utf-8">
}); jQuery.fn.dataTableExt.aTypes.unshift(
}); function ( sData )
</script> {
  var sValidChars = "0123456789.-,";
<style type="text/css" title="currentStyle"> var Char;
@import "media/css/demo_table.css";  
</style> /* Check the numeric part */
<script type="text/javascript" language="javascript" src="media/js/jquery.dataTables.js"></script> for ( i=1 ; i<sData.length ; i++ )
<script type="text/javascript" language="javascript" src="lib/bootstrap-dropdown.js"></script> {
<script type="text/javascript" charset="utf-8"> Char = sData.charAt(i);
jQuery.fn.dataTableExt.aTypes.unshift( if (sValidChars.indexOf(Char) == -1)
function ( sData ) {
{ return null;
var sValidChars = "0123456789.-,"; }
var Char; }
   
/* Check the numeric part */ /* Check prefixed by currency */
for ( i=1 ; i<sData.length ; i++ ) if ( sData.charAt(0) == '$' || sData.charAt(0) == '£' )
{ {
Char = sData.charAt(i); return 'currency';
if (sValidChars.indexOf(Char) == -1) }
{ return null;
return null; }
} );
} jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
  /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
/* Check prefixed by currency */ var x = a == "-" ? 0 : a.replace( /,/g, "" );
if ( sData.charAt(0) == '$' || sData.charAt(0) == '£' ) var y = b == "-" ? 0 : b.replace( /,/g, "" );
{  
return 'currency'; /* Remove the currency sign */
} x = x.substring( 1 );
return null; y = y.substring( 1 );
}  
); /* Parse and return */
jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) { x = parseFloat( x );
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */ y = parseFloat( y );
var x = a == "-" ? 0 : a.replace( /,/g, "" ); return x - y;
var y = b == "-" ? 0 : b.replace( /,/g, "" ); };
   
/* Remove the currency sign */ jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
x = x.substring( 1 ); /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
y = y.substring( 1 ); var x = a == "-" ? 0 : a.replace( /,/g, "" );
  var y = b == "-" ? 0 : b.replace( /,/g, "" );
/* Parse and return */  
x = parseFloat( x ); /* Remove the currency sign */
y = parseFloat( y ); x = x.substring( 1 );
return x - y; y = y.substring( 1 );
};  
  /* Parse and return */
jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) { x = parseFloat( x );
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */ y = parseFloat( y );
var x = a == "-" ? 0 : a.replace( /,/g, "" ); return y - x;
var y = b == "-" ? 0 : b.replace( /,/g, "" ); };
  $(document).ready(function() {
/* Remove the currency sign */ $('table').dataTable();
x = x.substring( 1 ); } );
y = y.substring( 1 ); </script>
  <link type="text/css" rel="stylesheet" href="style.css">
/* Parse and return */ </head>
x = parseFloat( x ); <body>
y = parseFloat( y ); <div class="navbar">
return y - x; <div class="navbar-inner">
}; <div class="container-fluid">
$(document).ready(function() { <a class="brand" href="#">contract dashboard</a>
$('table').dataTable(); <ul class="nav">
} ); <li><a href="displayAgency.php">agencies</a></li>
</script> <li><a href="displaySupplier.php">suppliers</a></li>
<link type="text/css" rel="stylesheet" href="style.css"> <li><a href="displayCategory.php">categories</a></li>
</head> <li><a href="displayCalendar.php">time periods</a></li>
<body> <!-- <li class="dropdown">
<div class="navbar"> <a href="#" class="dropdown-toggle">metrics</a>
<div class="navbar-inner"> <ul class="dropdown-menu">-->
<div class="container-fluid"> <li><a href="displayProcurementMethod.php">tenderm</a></li>
<a class="brand" href="#">contract dashboard</a> <li><a href="displayConfidentialities.php">confidentiality</a></li>
<ul class="nav"> <li><a href="displayConsultancies.php">consultancies</a></li>
<li><a href="displayAgency.php">agencies</a></li> <li><a href="displayAmendments.php">amendments</a></li>
<li><a href="displaySupplier.php">suppliers</a></li> <li><a href="displayMap.php">geo</a></li>
<li><a href="displayCategory.php">categories</a></li> </ul>
<li><a href="displayCalendar.php">time periods</a></li>  
<!-- <li class="dropdown">  
<a href="#" class="dropdown-toggle">metrics</a> <form method="post" action="search.php" class="pull-right">
<ul class="dropdown-menu">--> <input type="text" id="searchKeyword" name="searchKeyword" value="" placeholder="Search" />
<li><a href="displayProcurementMethod.php">tenderm</a></li> <input type="hidden" id="searchID" name="searchID" value=""/>
<li><a href="displayConfidentialities.php">confidentiality</a></li> </form>
<li><a href="displayConsultancies.php">consultancies</a></li>  
<li><a href="displayAmendments.php">amendments</a></li> </div>
<li><a href="displayMap.php">geo</a></li> </div><!-- /topbar-inner -->
</ul> </div><!-- /topbar -->
  </div><!-- /topbar-wrapper -->
  <script type="text/javascript">
<form method="post" action="search.php" class="pull-right">  
<input type="text" id="searchKeyword" name="searchKeyword" value="" placeholder="Search" /> var options_xml = {
<input type="hidden" id="searchID" name="searchID" value=""/> script: function (input) { return "search_autosuggest.php?input="+input; },
</form> varname:"input",
  callback: function (obj) { document.getElementById('searchID').value = obj.id; }
</div> };
</div><!-- /topbar-inner --> var as_xml = new bsn.AutoSuggest('searchKeyword', options_xml);
</div><!-- /topbar --> </script>
</div><!-- /topbar-wrapper --> <div class="container-fluid">
<script type="text/javascript"> <div class="row-fluid">
  <div class="span3">
var options_xml = { <h3> Filter by: </h3>
script: function (input) { return "search_autosuggest.php?input="+input; }, <div class="well sidebar-nav">
varname:"input", <li class="nav-header">Year</li>
callback: function (obj) { document.getElementById('searchID').value = obj.id; } <?php
}; foreach (range(2008,2015) as $year) {
var as_xml = new bsn.