add ZURB foundation html5 framework
[disclosr.git] / exportAgencies.csv.php
blob:a/exportAgencies.csv.php -> blob:b/exportAgencies.csv.php
<?php <?php
  // use https://github.com/okfn/publicbodies/blob/master/data/nz.csv format
  include_once("include/common.inc.php");
   
include_once("./lib/common.inc.php");  
setlocale(LC_CTYPE, 'C'); setlocale(LC_CTYPE, 'C');
// source: http://stackoverflow.com/questions/81934/easy-way-to-export-a-sql-table-without-access-to-the-server-or-phpmyadmin#81951  
   
$unspsc = Array(); $headers = Array("title","abbr","key","category","parent","parent_key","description","url","jurisdiction","jurisdiction_code","source","source_url","address","contact","email","tags","created_at","updated_at");
$unspscresult = $conn->prepare('select * from "UNSPSCcategories" where "UNSPSC"::text like \'%00000\';');  
$unspscresult->execute(); $db = $server->get_db('disclosr-agencies');
foreach ($unspscresult->fetchAll() as $row) {  
$unspsc[$row['UNSPSC']] = $row['Title'];  
  $foiEmail = Array();
  try {
  $rows = $db->get_view("app", "foiEmails", null, true)->rows;
  //print_r($rows);
  foreach ($rows as $row) {
  $foiEmail[$row->key] = $row->value;
  }
  } catch (SetteeRestClientException $e) {
  setteErrorHandler($e);
  die();
} }
   
$query = $conn->prepare('  
SELECT "CNID",contractnotice."agencyName",agency_nametoabn.abn as "agencyABN",  
EXTRACT(EPOCH FROM "publishDate") as "publishDate",  
EXTRACT(EPOCH FROM "contractStart") as "contractStart",  
EXTRACT(EPOCH FROM "contractEnd") as "contractEnd",  
value,description,category,  
"supplierName",(case when "supplierABN" != 0 THEN "supplierABN"::text ELSE "supplierName" END) as supplierID,  
(\'https://www.tenders.gov.au/?event=public.advancedsearch.keyword&keyword=CN\'::text || "CNID"::text) as sourceURL  
FROM contractnotice join agency_nametoabn on contractnotice."agencyName"=agency_nametoabn."agencyName"  
where "childCN" is null'  
, array(PDO::ATTR_CURSOR => PDO::FETCH_ORI_NEXT));  
$query->execute();  
$errors = $conn->errorInfo();  
if ($errors[2] != "") {  
die("Export terminated, db error" . print_r($errors, true));  
}  
   
$num_fields = $query->columnCount();  
$headers = Array();  
for ($i = 0; $i < $num_fields; $i++) { // for each column in query, make a CSV header  
$meta = $query->getColumnMeta($i);  
$headers[] = $meta['name'];  
}  
$fp = fopen('php://output', 'w'); $fp = fopen('php://output', 'w');
if ($fp && $query) { if ($fp && $db) {
header('Content-Type: text/csv'); header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="export.' . date("c") . '.csv"'); header('Content-Disposition: attachment; filename="export.' . date("c") . '.csv"');
header('Pragma: no-cache'); header('Pragma: no-cache');
header('Expires: 0'); header('Expires: 0');
fputcsv($fp, $headers); fputcsv($fp, $headers);
while ($row = $query->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) { try {
foreach ($row as $key => &$colvalue) { $agencies = $db->get_view("app", "byCanonicalName", null, true)->rows;
  //print_r($rows);
  foreach ($agencies as $agency) {
  // print_r($agency);
   
$colvalue = preg_replace('/[^[:print:]]/', '', utf8_encode($colvalue)); if (isset($agency->value->foiEmail) && $agency->value->foiEmail != "null" && !isset($agency->value->status)) {
if ($headers[$key] == "publishDate" || $headers[$key] == "contractStart" $row = Array();
|| $headers[$key] == "contractEnd") { $row["title"] = trim($agency->value->name);
$colvalue = date("Y-m-d", $colvalue); $row["abbr"] = (isset($agency->value->shortName) ? $agency->value->shortName : "");
} $row["key"] = (isset($agency->value->shortName) ? "au/".strtolower($agency->value->shortName) : "");
/* if ($headers[$key] == "CNID") { $row["category"] ="";
$colvalue = str_replace("A","", $colvalue); $row["parent"] ="";
}*/ $row["parentkey"] ="";
if ($headers[$key] == "cat1" || $headers[$key] == "cat2" $row["description"] = (isset($agency->value->description) ? $agency->value->description : "");
|| $headers[$key] == "cat3") { $row["url"] = (isset($agency->value->website) ? $agency->value->website : "");
$colvalue = $unspsc[$colvalue]; $row["jurisdiction"] = "Australia";
  $row["jurisdiction_code"] = "au";
   
  $row["source"] ="";
  $row["source_url"] ="";
  $row["address"] ="";
  $row["contact"] ="";
   
  $row["email"] = (isset($agency->value->foiEmail) ? $agency->value->foiEmail : "");
  $row["tags"] ="";
  $row["created_at"] ="";
  $row["updated_at"] ="";
   
   
  $otherBodies = Array();
  if (isset($agency->value->foiBodies)) {
  $otherBodies = array_merge($otherBodies, $agency->value->foiBodies);
  }
  if (isset($agency->value->positions)) {
  $positions = Array();
  foreach ($agency->value->positions as $position) {
  $positions[] = "Office of the ".$position;
  }
  $otherBodies = array_merge($otherBodies, $positions);
  }
  sort($otherBodies);
  if (count($otherBodies) > 0) {
  $row["description"] .= "<br/> This department also responds to requests for information held by " . implode(", ", $otherBodies);
  }
   
   
  fputcsv($fp, array_values($row));
} }
} }
fputcsv($fp, array_values($row)); } catch (SetteeRestClientException $e) {
  setteErrorHandler($e);
} }
   
die; die;
} }
?> ?>