More MySQL purging
[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 = "../";
  }
// $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); /*require ROOT . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'openid.php';
function databaseError($errMsg) require(ROOT.'/solarium/vendor/autoload.php');
{ require(ROOT.'/solarium/library/Solarium/Autoloader.php');
if ($errMsg[2] != "") { Solarium\Autoloader::register();
die(print_r($errMsg,true)); // check solarium version available
} //echo 'Solarium library version: ' . Solarium\Client::VERSION . ' - ';
} $config = array(
  'endpoint' => array(
function ucsmart($str) { 'localhost' => array(
$shortWords = Array("The","Pty","Ltd","Inc","Red","Oil","A","An","And","At","For","In" 'host' => '127.0.0.1',
,"Of","On","Or","The","To","With"); 'port' => 8983,
$strArray = explode(" ",preg_replace("/(?<=(?<!:|’s)\W) 'path' => '/solr/contracts/',
(A|An|And|At|For|In|Of|On|Or|The|To|With) )
(?=\W)/e", 'strtolower("$1")', ucwords(strtolower($str)))); )
foreach($strArray as &$word) { );
if (strlen($word) <= 4 && !in_array($word,$shortWords)) $word = strtoupper($word); // create a client instance
} $solr_client = new Solarium\Client($config);
return implode(" ",$strArray);  
}  
  $openid = new LightOpenID($_SERVER['HTTP_HOST']);
function percent($num_amount, $num_total) { // you have to open the session to be able to modify or remove it
$count1 = $num_amount / $num_total; session_start();
$count2 = $count1 * 100; function login() {
$count = number_format($count2, 2); global $openid;
return $count; if (!$openid->mode) {
} $openid->required = array('contact/email');
function array_sum_all($a) { $openid->identity = 'https://www.google.com/accounts/o8/id';
if(!is_array($a)) return $a; header('Location: ' . $openid->authUrl());
foreach($a as $key=>$value) }
$totale += array_sum_all($value); }
return $totale;  
} function auth() {
// magic query modifiers global $openid;
$agency = filter_var($_REQUEST['agency'], FILTER_SANITIZE_STRING); if ($_SESSION['authed'] == true) {
if ($agency != "") $agencyQ = "agencyName = '" . $agency . "' AND "; return true;
  }
$supplier = filter_var($_REQUEST['supplier'], FILTER_SANITIZE_STRING);  
if ($supplier != "") { if ($openid->mode) {
$supplierParts = explode("-", $supplier); $attr = $openid->getAttributes();
$supplierName = "%".$supplierParts[1]."%"; if ($attr['contact/email'] != 'maxious@gmail.com') {
$supplierABN = $supplierParts[0]; die('Access Denied');
if ($supplierParts[0] > 0) $supplierQ = ' "supplierABN" = :supplierABN AND '; } else {
else $supplierQ = ' "supplierName" LIKE :supplierName AND '; $_SESSION['authed'] = true;
} }
  } else {
$startYear = 2007; login();
$year = filter_var($_REQUEST['year'], FILTER_SANITIZE_NUMBER_INT); }
if ($year != "") $yearQ = "YEAR(publishDate) = " . $year . " AND "; }*/
   
$standardQ = ' "childCN" is null '; // AND YEAR(contractStart) >= 2007 AND YEAR(contractStart) <= 2010'; // $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$start =0.0; function databaseError($errMsg) {
  if ($errMsg[2] != "") {
function include_header($title) { echo '<div class="alert-message error">';
global $start; die(print_r($errMsg, true));
?> echo "</div>";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" }
"http://www.w3.org/TR/html4/strict.dtd"> }
<html> function cleanseName($input) {
<head>  
<title>Contract Dashboard - <?php echo $title; ?></title> return trim(join(" ",array_filter(explode(" ",strtolower($input)), function($v, $k) {
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.0r4/build/reset-fonts-grids/reset-fonts-grids.css"> $cleanseNamesCorp = Array(
  "ltd",
<script type="text/javascript" src="lib/bsn.AutoSuggest_2.1.3_comp.js" charset="utf-8"></script> "limited",
<link rel="stylesheet" href="autosuggest_inquisitor.css" type="text/css" media="screen" charset="utf-8" /> "australia",
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> "australian",
<script type="text/javascript"> "australasia",
$(document).ready(function() "(aus)",
{ "(aust)",
//hide the all of the element with class msg_body "(australia)",
$(".msg_body").hide(); "the",
//toggle the componenet with class msg_body "pty",
$(".msg_head").click(function() "contractors",
{ "p/l",
$(this).next(".msg_body").slideToggle(600); "inc.",
}); "inc",
}); "incorporated"
</script> );
  return !in_array($v,$cleanseNamesCorp) ;
<style type="text/css" title="currentStyle"> }, ARRAY_FILTER_USE_BOTH)));
@import "media/css/demo_table.css"; }
</style> function ucsmart($str) {
<script type="text/javascript" language="javascript" src="media/js/jquery.dataTables.js"></script> $shortWords = Array("The", "Pty", "Ltd", "Inc", "Red", "Oil", "A", "An", "And", "At", "For", "In"
<script type="text/javascript" charset="utf-8"> , "Of", "On", "Or", "The", "To", "With", "Use", "No", "Yes");
jQuery.fn.dataTableExt.aTypes.unshift( $strArray = explode(" ", preg_replace_callback("/(?<=(?<!:|’s)\W)
function ( sData ) (A|An|And|At|For|In|Of|On|Or|The|To|With)
{ (?=\W)/",
var sValidChars = "0123456789.-,"; function($matches) { return strtolower($matches[1]); } , ucwords(strtolower($str))));
var Char; foreach ($strArray as &$word) {
  if (strlen($word) <= 4 && !in_array($word, $shortWords))
/* Check the numeric part */ $word = strtoupper($word);
for ( i=1 ; i<sData.length ; i++ ) }
{ return implode(" ", $strArray);
Char = sData.charAt(i); }
if (sValidChars.indexOf(Char) == -1)  
{ function percent($num_amount, $num_total) {
return null; $count1 = $num_amount / $num_total;
} $count2 = $count1 * 100;
} $count = number_format($count2, 2);
  return $count;
/* Check prefixed by currency */ }
if ( sData.charAt(0) == '$' || sData.charAt(0) == '£' )  
{ function array_sum_all($a) {
return 'currency'; if (!is_array($a))
} return $a;
return null; $totale = 0;
} foreach ($a as $key => $value)
); $totale += array_sum_all($value);
jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) { return $totale;
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */ }
var x = a == "-" ? 0 : a.replace( /,/g, "" );  
var y = b == "-" ? 0 : b.replace( /,/g, "" ); function show_stats($stats) {
  setlocale(LC_MONETARY, 'en_US');
/* Remove the currency sign */ $value = number_format(doubleval($stats['total_value']) , 2);
x = x.substring( 1 ); echo "<br/> {$stats['total']} contracts, total value $$value <br/>";
y = y.substring( 1 ); if ($stats['consultancy'] > 0) {
  echo "<b>Consultancies:</b> {$stats['consultancy']} contracts, ".percent($stats['consultancy'],$stats['total']);
/* Parse and return */ echo "%<br/>";
x = parseFloat( x ); }
y = parseFloat( y ); if ($stats['confidentialityContract']+$stats['confidentialityOutputs'] > 0) {
return x - y; $confidentialities = $stats['confidentialityContract']+$stats['confidentialityOutputs'];
}; echo "<b>Confidentialities:</b> $confidentialities contracts, ".percent( $confidentialities,$stats['total'])."%";
  echo "( {$stats['confidentialityContract']} on the contract, {$stats['confidentialityOutputs']} on the outputs)";
jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) { echo "<br/>";
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */ }
var x = a == "-" ? 0 : a.replace( /,/g, "" ); echo "<b>Procurement methods:</b> ";
var y = b == "-" ? 0 : b.replace( /,/g, "" ); if ($stats['open'] > 0) {
  echo "Open {$stats['open']} contracts, ".percent($stats['open'],$stats['total']);
/* Remove the currency sign */ echo "%, ";
x = x.substring( 1 ); }
y = y.substring( 1 ); if ($stats['openSON'] > 0) {
  echo "Open via SON {$stats['openSON']} contracts, ".percent($stats['openSON'],$stats['total']);
/* Parse and return */ echo "%, ";
x = parseFloat( x ); }
y = parseFloat( y ); if ($stats['prequalified'] > 0) {
return y - x; echo "Prequalified {$stats['prequalified']} contracts, ".percent($stats['prequalified'],$stats['total']);
}; echo "%, ";
$(document).ready(function() { }
$('table').dataTable(); if ($stats['limited'] > 0) {
} ); echo "limited {$stats['limited']} contracts, ".percent($stats['limited'],$stats['total']);
</script> echo "%, ";
<link type="text/css" rel="stylesheet" href="style.css"> }
</head> }
<body><div id="doc3" class="yui-t4">  
<div id="hd"> // magic query modifiers
<h1> contract dashboard</h1> $agency = filter_var($_REQUEST['agency'], FILTER_SANITIZE_STRING);
</div> if ($agency != "")
<div id="sitenav"> $agencyQ = "agencyName = '" . $agency . "' AND ";
<a href="displayAgency.php">agencies</a>  
<a href="displaySupplier.php">suppliers</a> $supplier = filter_var($_REQUEST['supplier'], FILTER_SANITIZE_STRING);
<a href="displayCategory.php">product/service categories</a> if ($supplier != "") {
<a href="displayCalendar.php">time periods</a> if ($supplier[0] !== '-') {
| metrics | $supplierParts = explode("-", $supplier);
<a href="displayProcurementMethod.php">procurement method</a>  
<a href="displayConfidentialities.php">contractual confidentiality</a> $supplierABN = array_shift($supplierParts);
<a href="displayConsultancies.php">consultancies</a> $supplierName = "%" . implode('-',$supplierParts) . "%";
<a href="displayAmendments.php">amendments</a>  
<a href="displayMap.php">postcodes</a> $supplierQ = ' "supplierABN" = :supplierABN AND ';
<div id="search"><form method="post" action="search.php" class="asholder"> } else {
<label for="searchKeyword">search</label> $supplierName = "%" . substr($supplier,1) . "%";
<input type="text" id="searchKeyword" name="searchKeyword" value="" style="width:150px" /> $supplierQ = ' "supplierName" ILIKE :supplierName AND ';
<input type="hidden" id="searchID" name="searchID" value=""/><input type="submit" value="submit" /> }
</form></div> }
<script type="text/javascript">  
  $startYear = 2007;
var options_xml = { $year = filter_var($_REQUEST['year'], FILTER_SANITIZE_NUMBER_INT);
script: function (input) { return "search_autosuggest.php?input="+input; }, if ($year != "") {
varname:"input", $yearQ = 'extract(year from "contractStart") = ' . $year . " AND ";
callback: function (obj) { document.getElementById('searchID').value = obj.id; } }
}; $standardQ = ' "childCN" is null '; // AND YEAR(contractStart) >= 2007 AND YEAR(contractStart) <= 2010';
var as_xml = new bsn.AutoSuggest('searchKeyword', options_xml); $start = 0.0;
</script>  
  function local_url() {
</div> return "http://" . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . "/";
<div id="bd" role="main"> }
<div id="yui-main">  
<div class="yui-b"><div class="yui-g"> function include_header($title) {
<?php global $start;
$start = (float) array_sum(explode(' ',microtime())); ?>
} <!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" version="XHTML+RDFa 1.1"
function include_footer() { xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
global $start; xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
$end = (float) array_sum(explode(' ',microtime())); xmlns:gr="http://purl.org/goodrelations/v1#"
?> xmlns:dc="http://purl.org/dc/terms/"
</div> xmlns:pc="http://purl.org/procurement#"
</div> xmlns:unspsc="http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml#"
</div> xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
<div class="yui-b"> xmlns:pcdt="http://purl.org/procurement/public-contracts-datatypes#"
Filter by:<ul> prefix="rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
<li>year rdfs: http://www.w3.org/2000/01/rdf-schema#
<ul><li>2008</li> gr: http://purl.org/goodrelations/v1#
</ul> dcterms: http://purl.org/dc/terms/
</li> pc: http://purl.org/procurement/public-contracts#
</ul> <br> </div> </div> cpv: http://purl.org/weso/pscs/cpv/2008/resource/
  unspsc: http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml#
<?php v: http://www.w3.org/2006/vcard/ns#
echo '<div id="ft"><p>'."Processing time: ". sprintf("%.4f", ($end-$start))." seconds".'</p></div>'; payment: http://reference.data.gov.uk/def/payment#
echo '</div> </body> </html>'; br: http://purl.org/business-register#
} xsd: http://www.w3.org/2001/XMLSchema#
include ("graphs.inc.php"); pcdt: http://purl.org/procurement/public-contracts-datatypes#">
?> <head>
  <title><?php echo $title; ?> - Contract Dashboard</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="bootstrap-responsive.css">
  <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
  <!--[if lt IE 9]>
  <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>
  <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">
  $(document).ready(function()
  {
  //hide the all of the element with class msg_body
  $(".msg_body").hide();
  //toggle the componenet with class msg_body
  $(".msg_head").click(function()
  {
  $(this).next(".msg_body").slideToggle(600);
  });
  });
  </script>
   
  <style type="text/css" title="currentStyle">
  @import "media/css/demo_table.css";
  </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 type="text/javascript" charset="utf-8">
  jQuery.fn.dataTableExt.aTypes.unshift(
  function ( sData )
  {
  var sValidChars = "0123456789.-,";
  var Char;
   
  /* Check the numeric part */
  for ( i=1 ; i<sData.length ; i++ )
  {
  Char = sData.charAt(i);
  if (sValidChars.indexOf(Char) == -1)
  {
  return null;
  }
  }
   
  /* Check prefixed by currency */
  if ( sData.charAt(0) == '$' || sData.charAt(0) == '£' )
  {
  return 'currency';
  }
  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, "" );
  var y = b == "-" ? 0 : b.replace( /,/g, "" );
   
  /* Remove the currency sign */
  x = x.substring( 1 );
  y = y.substring( 1 );
   
  /* Parse and return */
  x = parseFloat( x );
  y = parseFloat( y );
  return x - y;
  };
   
  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) */
  var x = a == "-" ? 0 : a.replace( /,/g, "" );
  var y = b == "-" ? 0 : b.replace( /,/g, "" );
   
  /* Remove the currency sign */
  x = x.substring( 1 );
  y = y.substring( 1 );
   
  /* Parse and return */
  x = parseFloat( x );
  y = parseFloat( y );
  return y - x;
  };
  $(document).ready(function() {
  $('table').dataTable();
  } );
  </script>
  <link type="text/css" rel="stylesheet" href="style.css">
  </head>
  <body>
  <div class="navbar">
  <div class="navbar-inner">
  <div class=&qu