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