beginnings rss scraper
[disclosr.git] / admin / importAPSCEmployees.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
<?php
 
require_once '../include/common.inc.php';
 
$db = $server->get_db('disclosr-agencies');
$rows = $db->get_view("app", "byName")->rows;
$nametoid = Array();
$sums = Array();
foreach ($rows as $row) {
    $nametoid[trim($row->key)] = $row->value;
}
$employeeCSVs = Array("2002-2003" => "0203apsemployees.csv",
    "2003-2004" => "0304apsemployees.csv",
    "2004-2005" => "0405apsemployees.csv",
    "2005-2006" => "0506apsemployees.csv",
    "2006-2007" => "0607apsemployees.csv",
    "2007-2008" => "0708apsemployees.csv",
    "2008-2009" => "0809apsemployees.csv",
    "2009-2010" => "0910apsemployees.csv",
    "2010-2011" => "1011apsemployees.csv"
);
foreach ($employeeCSVs as $timePeriod => $employeeCSV) {
    echo $employeeCSV . "<br>" . PHP_EOL;
    $row = 1;
    if (($handle = fopen($employeeCSV, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            //print_r($data);
            $name = trim($data[0]);
            if (isset($nametoid[$name])) {
                $id = $nametoid[$name];
                //echo $id . "<br>" . PHP_EOL;
                @$sums[$id][$timePeriod] += $data[1];
            } else {
                echo "<br>ERROR NAME MISSING FROM ID LIST<br><bR>" . PHP_EOL;
 
                die();
            }
        }
        fclose($handle);
    }
}
foreach ($sums as $id => $sum) {
    echo $id . "<br>" . PHP_EOL;
    $doc = $db->get($id);
     echo $doc->name . "<br>" . PHP_EOL;
    // print_r($doc);
    $changed = false;
    if (!isset($doc->statistics)) {
        $changed = true;
        $doc->statistics = Array();
    }
    foreach ($sum as $timePeriod => $value) {
        if (!isset($doc->statistics->employees->$timePeriod->value) 
                || $doc->statistics->employees->$timePeriod->value != $value) {
            $changed = true;
            $doc->statistics["employees"][$timePeriod] = Array("value" => $value, "source" => "http://apsc.gov.au/stateoftheservice/");
        }
    }
    if ($changed) {
        $db->save($doc);
    } else {
        echo "not changed" . "<br>" . PHP_EOL;
    }
}
// employees: timeperiod, source = apsc state of service, value 
?>