<?php
// use https://github.com/okfn/publicbodies/blob/master/data/nz.csv format
include_once("include/common.inc.php");

setlocale(LC_CTYPE, 'C');

$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");

$db = $server->get_db('disclosr-agencies');


$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();
}

$fp = fopen('php://output', 'w');
if ($fp && $db) {
    header('Content-Type: text/csv; charset=utf-8');
    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);

            if (isset($agency->value->foiEmail) && $agency->value->foiEmail != "null" && !isset($agency->value->status)) {
                $row = Array();
                $row["title"] = trim($agency->value->name);
                $row["abbr"] = (isset($agency->value->shortName) ? $agency->value->shortName : "");
                $row["key"] = (isset($agency->value->shortName) ? "au/".strtolower($agency->value->shortName) : "");
                $row["category"] ="";
                 $row["parent"] ="";
                 $row["parentkey"] ="";
                $row["description"] = (isset($agency->value->description) ? $agency->value->description : "");
                $row["url"] = (isset($agency->value->website) ? $agency->value->website : "");
                $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));
            }
        }
    } catch (SetteeRestClientException $e) {
        setteErrorHandler($e);
    }

    die;
}
?>

