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