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';
  require(ROOT.'/solarium/vendor/autoload.php');
require $basePath."lib/amon-php/amon.php"; require(ROOT.'/solarium/library/Solarium/Autoloader.php');
Amon::config(array('address'=> 'http://127.0.0.1:2464', Solarium\Autoloader::register();
'protocol' => 'http', // check solarium version available
'secret_key' => "JBcSUdFOi5lK0vCjLjbHDpQamcBnRA4iV7QLaTADeDQ")); //echo 'Solarium library version: ' . Solarium\Client::VERSION . ' - ';
Amon::setup_exception_handler(); $config = array(
} 'endpoint' => array(
  'localhost' => array(
require ROOT . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'openid.php'; 'host' => '127.0.0.1',
$openid = new LightOpenID($_SERVER['HTTP_HOST']); 'port' => 8983,
// you have to open the session to be able to modify or remove it 'path' => '/solr/contracts/',
session_start(); )
function login() { )
global $openid; );
if (!$openid->mode) { // create a client instance
$openid->required = array('contact/email'); $solr_client = new Solarium\Client($config);
$openid->identity = 'https://www.google.com/accounts/o8/id';  
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 auth() { function login() {
global $openid; global $openid;
if ($_SESSION['authed'] == true) { if (!$openid->mode) {
return true; $openid->required = array('contact/email');
} $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') {  
die('Access Denied'); function auth() {
} else { global $openid;
$_SESSION['authed'] = true; if ($_SESSION['authed'] == true) {
} return true;
} else { }
login();  
} if ($openid->mode) {
} $attr = $openid->getAttributes();
  if ($attr['contact/email'] != 'maxious@gmail.com') {
// $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); die('Access Denied');
function databaseError($errMsg) { } else {
if ($errMsg[2] != "") { $_SESSION['authed'] = true;
echo '<div class="alert-message error">'; }
Amon::log(print_r($errMsg, true).print_r($_REQUEST, true).print_r($_SERVER, true), array('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) 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_callback("/(?<=(?<!:|’s)\W)
  (A|An|And|At|For|In|Of|On|Or|The|To|With)
function percent($num_amount, $num_total) { (?=\W)/",
$count1 = $num_amount / $num_total; function($matches) { return strtolower($matches[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;
  $totale = 0;
$supplier = filter_var($_REQUEST['supplier'], FILTER_SANITIZE_STRING); foreach ($a as $key => $value)
if ($supplier != "") { $totale += array_sum_all($value);
$supplierParts = explode("-", $supplier); return $totale;
$supplierName = "%" . $supplierParts[1] . "%"; }
$supplierABN = $supplierParts[0];  
if ($supplierParts[0] > 0) // magic query modifiers
$supplierQ = ' "supplierABN" = :supplierABN AND '; $agency = filter_var($_REQUEST['agency'], FILTER_SANITIZE_STRING);
else if ($agency != "")
$supplierQ = ' "supplierName" LIKE :supplierName AND '; $agencyQ = "agencyName = '" . $agency . "' AND ";
}  
  $supplier = filter_var($_REQUEST['supplier'], FILTER_SANITIZE_STRING);
$startYear = 2007; if ($supplier != "") {
$year = filter_var($_REQUEST['year'], FILTER_SANITIZE_NUMBER_INT); $supplierParts = explode("-", $supplier);
if ($year != "") { $supplierName = "%" . $supplierParts[1] . "%";
$yearQ = "YEAR(publishDate) = " . $year . " AND "; $supplierABN = $supplierParts[0];
} if ($supplierParts[0] > 0)
$standardQ = ' "childCN" is null '; // AND YEAR(contractStart) >= 2007 AND YEAR(contractStart) <= 2010'; $supplierQ = ' "supplierABN" = :supplierABN AND ';
$start = 0.0; else
  $supplierQ = ' "supplierName" ILIKE :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);
function include_header($title) { if ($year != "") {
global $start; $yearQ = 'extract(year from "contractStart") = ' . $year . " AND ";
?> }
<!DOCTYPE html> $standardQ = ' "childCN" is null '; // AND YEAR(contractStart) >= 2007 AND YEAR(contractStart) <= 2010';
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" version="XHTML+RDFa 1.1" $start = 0.0;
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"  
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" function local_url() {
xmlns:gr="http://purl.org/goodrelations/v1#" return "http://" . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . "/";
xmlns:dc="http://purl.org/dc/terms/" }
xmlns:pc="http://purl.org/procurement#"  
xmlns:unspsc="http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml#" function include_header($title) {
xmlns:xsd="http://www.w3.org/2001/XMLSchema#" global $start;
xmlns:pcdt="http://purl.org/procurement/public-contracts-datatypes#" ?>
prefix="rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns# <!DOCTYPE html>
rdfs: http://www.w3.org/2000/01/rdf-schema# <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" version="XHTML+RDFa 1.1"
gr: http://purl.org/goodrelations/v1# xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
dcterms: http://purl.org/dc/terms/ xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
pc: http://purl.org/procurement/public-contracts# xmlns:gr="http://purl.org/goodrelations/v1#"
cpv: http://purl.org/weso/pscs/cpv/2008/resource/ xmlns:dc="http://purl.org/dc/terms/"
unspsc: http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml# xmlns:pc="http://purl.org/procurement#"
v: http://www.w3.org/2006/vcard/ns# xmlns:unspsc="http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml#"
payment: http://reference.data.gov.uk/def/payment# xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
br: http://purl.org/business-register# xmlns:pcdt="http://purl.org/procurement/public-contracts-datatypes#"
xsd: http://www.w3.org/2001/XMLSchema# prefix="rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
pcdt: http://purl.org/procurement/public-contracts-datatypes#"> rdfs: http://www.w3.org/2000/01/rdf-schema#
<head> gr: http://purl.org/goodrelations/v1#
<title><?php echo $title; ?> - Contract Dashboard</title> dcterms: http://purl.org/dc/terms/
<link rel="stylesheet" type="text/css" href="bootstrap.min.css"> pc: http://purl.org/procurement/public-contracts#
<link rel="stylesheet" type="text/css" href="bootstrap-responsive.css"> cpv: http://purl.org/weso/pscs/cpv/2008/resource/
<!-- Le HTML5 shim, for IE6-8 support of HTML elements --> unspsc: http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml#
<!--[if lt IE 9]> v: http://www.w3.org/2006/vcard/ns#
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> payment: http://reference.data.gov.uk/def/payment#
<![endif]--> br: http://purl.org/business-register#
<script type="text/javascript" src="lib/bsn.AutoSuggest_2.1.3_comp.js" charset="utf-8"></script> xsd: http://www.w3.org/2001/XMLSchema#
<link rel="stylesheet" href="autosuggest_inquisitor.css" type="text/css" media="screen" charset="utf-8" /> pcdt: http://purl.org/procurement/public-contracts-datatypes#">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <head>
<script type="text/javascript"> <title><?php echo $title; ?> - Contract Dashboard</title>
$(document).ready(function() <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
{ <link rel="stylesheet" type="text/css" href="bootstrap-responsive.css">
//hide the all of the element with class msg_body <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
$(".msg_body").hide(); <!--[if lt IE 9]>
//toggle the componenet with class msg_body <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
$(".msg_head").click(function() <![endif]-->
{ <script type="text/javascript" src="lib/bsn.AutoSuggest_2.1.3_comp.js" charset="utf-8"></script>
$(this).next(".msg_body").slideToggle(600); <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>
}); <script type="text/javascript">
</script> $(document).ready(function()
  {
<style type="text/css" title="currentStyle"> //hide the all of the element with class msg_body
@import "media/css/demo_table.css"; $(".msg_body").hide();
</style> //toggle the componenet with class msg_body
<script type="text/javascript" language="javascript" src="media/js/jquery.dataTables.js"></script> $(".msg_head").click(function()
<script type="text/javascript" language="javascript" src="lib/bootstrap-dropdown.js"></script> {
<script type="text/javascript" charset="utf-8"> $(this).next(".msg_body").slideToggle(600);
jQuery.fn.dataTableExt.aTypes.unshift( });
function ( sData ) });
{ </script>
var sValidChars = "0123456789.-,";  
var Char; <style type="text/css" title="currentStyle">
  @import "media/css/demo_table.css";
/* Check the numeric part */ </style>
for ( i=1 ; i<sData.length ; i++ ) <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>
Char = sData.charAt(i); <script type="text/javascript" charset="utf-8">
if (sValidChars.indexOf(Char) == -1) jQuery.fn.dataTableExt.aTypes.unshift(
{ function ( sData )
return null; {
} var sValidChars = "0123456789.-,";
} var Char;
   
/* Check prefixed by currency */ /* Check the numeric part */
if ( sData.charAt(0) == '$' || sData.charAt(0) == '£' ) for ( i=1 ; i<sData.length ; i++ )
{ {
return 'currency'; Char = sData.charAt(i);
} 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) */  
var x = a == "-" ? 0 : a.replace( /,/g, "" ); /* Check prefixed by currency */
var y = b == "-" ? 0 : b.replace( /,/g, "" ); if ( sData.charAt(0) == '$' || sData.charAt(0) == '£' )
  {
/* Remove the currency sign */ return 'currency';
x = x.substring( 1 ); }
y = y.substring( 1 ); return null;
  }
/* Parse and return */ );
x = parseFloat( x ); jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
y = parseFloat( y ); /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
return x - y; var x = a == "-" ? 0 : a.replace( /,/g, "" );
}; var y = b == "-" ? 0 : b.replace( /,/g, "" );
   
jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) { /* Remove the currency sign */
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */ x = x.substring( 1 );
var x = a == "-" ? 0 : a.replace( /,/g, "" ); y = y.substring( 1 );
var y = b == "-" ? 0 : b.replace( /,/g, "" );  
  /* Parse and return */
/* Remove the currency sign */ x = parseFloat( x );
x = x.substring( 1 ); y = parseFloat( y );
y = y.substring( 1 ); return x - y;
  };
/* Parse and return */  
x = parseFloat( x ); jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
y = parseFloat( y ); /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
return y - x; var x = a == "-" ? 0 : a.replace( /,/g, "" );
}; var y = b == "-" ? 0 : b.replace( /,/g, "" );
$(document).ready(function() {  
$('table').dataTable(); /* Remove the currency sign */
} ); x = x.substring( 1 );
</script> y = y.substring( 1 );
<link type="text/css" rel="stylesheet" href="style.css">  
</head> /* Parse and return */
<body> x = parseFloat( x );
<div class="navbar"> y = parseFloat( y );
<div class="navbar-inner"> return y - x;
<div class="container-fluid"> };
<a class="brand" href="#">contract dashboard</a> $(document).ready(function() {
<ul class="nav"> $('table').dataTable();
<li><a href="displayAgency.php">agencies</a></li> } );
<li><a href="displaySupplier.php">suppliers</a></li> </script>
<li><a href="displayCategory.php">categories</a></li> <link type="text/css" rel="stylesheet" href="style.css">
<li><a href="displayCalendar.php">time periods</a></li> </head>
<!-- <li class="dropdown"> <body>
<a href="#" class="dropdown-toggle">metrics</a> <div class="navbar">
<ul class="dropdown-menu">--> <div class="navbar-inner">
<li><a href="displayProcurementMethod.php">tenderm</a></li> <div class="container-fluid">
<li><a href="displayConfidentialities.php">confidentiality</a></li> <a class="brand" href="#">contract dashboard</a>
<li><a href="displayConsultancies.php">consultancies</a></li> <ul class="nav">
<li><a href="displayAmendments.php">amendments</a></li> <li><a href="displayAgency.php">agencies</a></li>
<li><a href="displayMap.php">geo</a></li> <li><a href="displaySupplier.php">suppliers</a></li>
</ul> <li><a href="displaySON.php">standing offers</a></li>
  <li><a href="displayCategory.php">categories</a></li>
  <li><a href="displayCalendar.php">dates</a></li>
<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="get" 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/ja