Sub-agency FOI 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,6 +1,7 @@
 <?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");
@@ -16,6 +17,7 @@
     }
 } catch (SetteeRestClientException $e) {
     setteErrorHandler($e);
+    die();
 }
 
 $foiEmail = Array();
@@ -27,11 +29,12 @@
     }
 } catch (SetteeRestClientException $e) {
     setteErrorHandler($e);
+    die();
 }
 
 $fp = fopen('php://output', 'w');
 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('Pragma: no-cache');
     header('Expires: 0');
@@ -40,46 +43,42 @@
         $agencies = $db->get_view("app", "byCanonicalName", null, true)->rows;
         //print_r($rows);
         foreach ($agencies as $agency) {
-               // print_r($agency);
-               
+            // print_r($agency);
+
             if (isset($agency->value->foiEmail) && $agency->value->foiEmail != "null" && !isset($agency->value->status)) {
                 $row = Array();
                 $row["#id"] = $agency->id;
                 $row["name"] = trim($agency->value->name);
-                if (isset($agency->value->foiEmail)) {
-                    $row["request_email"] = $agency->value->foiEmail;
-                } else {
-                    if ($agency->value->orgType == "FMA-DepartmentOfState") {
-                        $row["request_email"] = "foi@" . GetDomain($agency->value->website);
-                    } else {
-                        $row["request_email"] = $foiEmail[$agency->value->parentOrg];
+                $row["request_email"] = (isset($agency->value->foiEmail) ? $agency->value->foiEmail : "");
+                $row["short_name"] = (isset($agency->value->shortName) ? $agency->value->shortName : "");
+                $row["notes"] = (isset($agency->value->description) ? $agency->value->description : "");
+
+                $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);
                 }
-                if (isset($agency->value->shortName)) {
-                    $row["short_name"] = $agency->value->shortName;
-                } else {
-                    $out = Array();
-                    preg_match_all('/[A-Z]/', trim($agency->value->name), $out);
-                    $row["short_name"] = implode("", $out[0]);
+                sort($otherBodies);
+                if (count($otherBodies) > 0) {
+                    $row["notes"] .= "<br/> This department also responds to requests for information held by " . implode(", ", $otherBodies);
                 }
-                $row["notes"] = "";
+
                 $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") {
-                    $row["tag_string"] = $tag[$agency->value->_id] . " " . $agency->value->orgType;
+                    $row["tag_string"] = $tag[$agency->value->_id];
                 } else {
-                    $row["tag_string"] = $tag[$agency->value->parentOrg] . " " . $agency->value->orgType;
+                    $row["tag_string"] = $tag[$agency->value->parentOrg];
                 }
-
+                $row["tag_string"] .= " " . $agency->value->orgType;
+                $row["tag_string"] .= " federal";
                 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) {