Sub-agency FOI export for alaveteli
[disclosr.git] / alaveteli / exportAgencies.csv.php
blob:a/alaveteli/exportAgencies.csv.php -> blob:b/alaveteli/exportAgencies.csv.php
<?php <?php
   
include_once("../include/common.inc.php"); include_once("../include/common.inc.php");
setlocale(LC_CTYPE, 'C'); setlocale(LC_CTYPE, 'C');
   
$headers = Array("#id", "name", "request_email", "short_name", "notes", "publication_scheme", "home_page", "tag_string"); $headers = Array("#id", "name", "request_email", "short_name", "notes", "publication_scheme", "home_page", "tag_string");
   
$db = $server->get_db('disclosr-agencies'); $db = $server->get_db('disclosr-agencies');
   
$tag = Array(); $tag = Array();
try { try {
$rows = $db->get_view("app", "byDeptStateName", null, true)->rows; $rows = $db->get_view("app", "byDeptStateName", null, true)->rows;
//print_r($rows); //print_r($rows);
foreach ($rows as $row) { foreach ($rows as $row) {
$tag[$row->value] = phrase_to_tag(dept_to_portfolio($row->key)); $tag[$row->id] = phrase_to_tag(dept_to_portfolio($row->key));
  }
  } catch (SetteeRestClientException $e) {
  setteErrorHandler($e);
  }
   
  $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) { } catch (SetteeRestClientException $e) {
setteErrorHandler($e); setteErrorHandler($e);
} }
   
$fp = fopen('php://output', 'w'); $fp = fopen('php://output', 'w');
if ($fp && $db) { if ($fp && $db) {
header('Content-Type: text/csv'); header('Content-Type: text/csv');
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);
try { try {
$agencies = $db->get_view("app", "byCanonicalName", null, true)->rows; $agencies = $db->get_view("app", "byCanonicalName", null, true)->rows;
//print_r($rows); //print_r($rows);
foreach ($agencies as $agency) { foreach ($agencies as $agency) {
// print_r($agency); // print_r($agency);
$row = Array();  
$row["#id"] = $agency->value->_id; if (isset($agency->value->foiEmail) && $agency->value->foiEmail != "null" && !isset($agency->value->status)) {
$row["name"] = $agency->value->name; $row = Array();
if (isset($agency->value->foi_email)) { $row["#id"] = $agency->id;
$row["request_email"] = $agency->value->foiEmail; $row["name"] = trim($agency->value->name);
} else { if (isset($agency->value->foiEmail)) {
$row["request_email"] = "foi@".GetDomain($agency->value->website); $row["request_email"] = $agency->value->foiEmail;
// } else {
} if ($agency->value->orgType == "FMA-DepartmentOfState") {
if (isset($agency->value->shortName)) { $row["request_email"] = "foi@" . GetDomain($agency->value->website);
$row["short_name"] = $agency->value->shortName; } else {
} else { $row["request_email"] = $foiEmail[$agency->value->parentOrg];
$out = Array(); }
preg_match_all('/[A-Z]/', $agency->value->name, $out); }
$row["short_name"] = implode("",$out[0]); if (isset($agency->value->shortName)) {
} $row["short_name"] = $agency->value->shortName;
$row["notes"] = ""; } else {
$row["publication_scheme"] = $agency->value->infoPublicationSchemeURL; $out = Array();
$row["home_page"] = $agency->value->website; preg_match_all('/[A-Z]/', trim($agency->value->name), $out);
if ($agency->value->orgType == "FMA-DepartmentOfState") { $row["short_name"] = implode("", $out[0]);
$row["tag_string"] = $tag[$agency->value->_id]; }
} else { $row["notes"] = "";
$row["tag_string"] = $tag[$agency->value->parentOrg];; $row["publication_scheme"] = (isset($agency->value->infoPublicationSchemeURL) ? $agency->value->infoPublicationSchemeURL : "");
} $row["home_page"] = (isset($agency->value->website) ? $agency->value->website : "");
  if ($agency->value->orgType == "FMA-DepartmentOfState") {
fputcsv($fp, array_values($row)); $row["tag_string"] = $tag[$agency->value->_id] . " " . $agency->value->orgType;
  } else {
if (isset($agency->value->foiBodies)) { $row["tag_string"] = $tag[$agency->value->parentOrg] . " " . $agency->value->orgType;
foreach ($agency->value->foiBodies as $foiBody) { }
$row['name'] = $foiBody;  
$row['short_name'] = ""; fputcsv($fp, array_values($row));
fputcsv($fp, array_values($row));  
  if (isset($agency->value->foiBodies)) {
  foreach ($agency->value->foiBodies as $foiBody) {
  $row['name'] = $foiBody;
  $row['short_name'] = "";
  fputcsv($fp, array_values($row));
  }
} }
} }
} }
} catch (SetteeRestClientException $e) { } catch (SetteeRestClientException $e) {
setteErrorHandler($e); setteErrorHandler($e);
} }
   
die; die;
} }
?> ?>