Handling of minister/secretary names in FOI export
[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,21 @@
 <?php
 
 include_once("../include/common.inc.php");
+
+function shortName($name) {
+    $name = trim($name);
+    if (strstr($name,"Minister ") || strstr($name,"Treasurer") || strstr($name,"Parliamentary Secretary")) {
+        $badWords = Array ("Assisting the Prime Minister on","Assisting on"," the "," of "," for "," on "," and "," to ",","," ","'","`");
+        return str_replace($badWords,"",$name);
+    }
+            
+    else {
+    $out = Array();
+    preg_match_all('/[A-Z]/', $name, $out);
+    return implode("", $out[0]);
+    }
+}
+
 setlocale(LC_CTYPE, 'C');
 
 $headers = Array("#id", "name", "request_email", "short_name", "notes", "publication_scheme", "home_page", "tag_string");
@@ -16,6 +31,7 @@
     }
 } catch (SetteeRestClientException $e) {
     setteErrorHandler($e);
+    die();
 }
 
 $foiEmail = Array();
@@ -27,11 +43,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,8 +57,8 @@
         $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;
@@ -58,9 +75,7 @@
                 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]);
+                    $row["short_name"] = shortName($agency->value->name);
                 }
                 $row["notes"] = "";
                 $row["publication_scheme"] = (isset($agency->value->infoPublicationSchemeURL) ? $agency->value->infoPublicationSchemeURL : "");
@@ -75,8 +90,8 @@
 
                 if (isset($agency->value->foiBodies)) {
                     foreach ($agency->value->foiBodies as $foiBody) {
-                        $row['name'] = $foiBody;
-                        $row['short_name'] = "";
+                        $row['name'] = iconv("UTF-8", "ASCII//TRANSLIT",$foiBody);
+                        $row["short_name"] = shortName($foiBody);
                         fputcsv($fp, array_values($row));
                     }
                 }