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