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