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