more ausbudget work
[disclosr.git] / admin / importAusbudget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
 
require_once '../include/common.inc.php';
 
$db = $server->get_db('disclosr-agencies');
$rows = $db->get_view("app", "byName")->rows;
$nametoid = Array();
$accounts = Array();
foreach ($rows as $row) {
    $nametoid[trim($row->key)] = $row->value;
}
$nameField = "Agency";
$elevenField = "2011-2012";
$twelveField = "2012-2013";
//  $request = Requests::get($url);
//echo $url;
//    $Data = str_getcsv($request->body, "\n"); //parse the rows 
$Data = str_getcsv(file_get_contents("ausbudget.csv"), "\n"); //parse the rows
$headers = Array();
foreach ($Data as $num => $line) {
    $Row = str_getcsv($line, ",");
    if ($num == 0) {
        $headers = $Row;
        print_r($headers);
    } else {
        if (isset($Row[array_search($nameField, $headers)])) {
            $agencyName = $Row[array_search($nameField, $headers)];
            if (!in_array(trim($agencyName), array_keys($nametoid))) {
                echo trim($agencyName) . " missing" . PHP_EOL;
            } else {
                //echo $Row[array_search($nameField, $headers)] . PHP_EOL;
//                              print_r($Row);
                if (!is_array($accounts[$nametoid[trim($agencyName)]]["budget"]['2011-2012'])) $accounts[$nametoid[trim($agencyName)]]["budget"]['2011-2012'] = Array();
                if (!is_array($accounts[$nametoid[trim($agencyName)]]["budget"]['2012-2013'])) $accounts[$nametoid[trim($agencyName)]]["budget"]['2012-2013'] = Array();
 
                if (!isset($accounts[$nametoid[trim($agencyName)]]["budget"]['2011-2012']['value'])) $accounts[$nametoid[trim($agencyName)]]["budget"]['2011-2012']['value'] = 0;
                if (!isset($accounts[$nametoid[trim($agencyName)]]["budget"]['2012-2013']['value'])) $accounts[$nametoid[trim($agencyName)]]["budget"]['2012-2013']['value'] = 0;
                $accounts[$nametoid[trim($agencyName)]]["budget"]['2011-2012']['value'] += $Row[array_search($elevenField, $headers)];
                $accounts[$nametoid[trim($agencyName)]]["budget"]['2012-2013']['value'] += $Row[array_search($twelveField, $headers)];
 
                $accounts[$nametoid[trim($agencyName)]]["budget"]['2011-2012']['source'] = 'http://budgetaus.net/';
                $accounts[$nametoid[trim($agencyName)]]["budget"]['2012-2013']['source'] = 'http://budgetaus.net/';
            }
        } else {
            echo "error finding any agency" . $line . PHP_EOL;
        }
    }
}
 
 
 
//print_r($accounts);
foreach ($accounts as $id => $allvalues) {
    echo $id . "<br>" . PHP_EOL;
    $doc = object_to_array($db->get($id));
    // print_r($doc);
 
    foreach ($allvalues as $valueType => $values) {
        if (!isset($doc['statistics'][$valueType]) || !is_array($doc['statistics'][$valueType])) {
 
            $doc['statistics'][$valueType] = Array();
        }
        $doc['statistics'][$valueType] = array_unique(array_merge($doc['statistics'][$valueType], $values));
    }
 
    $db->save($doc);
}
?>