add rtk ausbudget import
[disclosr.git] / admin / exportAusbudget.php
blob:a/admin/exportAusbudget.php -> blob:b/admin/exportAusbudget.php
--- a/admin/exportAusbudget.php
+++ b/admin/exportAusbudget.php
@@ -1,1 +1,68 @@
+<?php
 
+include_once("../include/common.inc.php");
+require($basePath . 'lib/phpquery/phpQuery/phpQuery.php');
+
+setlocale(LC_CTYPE, 'C');
+
+$dir = "./ausbudget/";
+$dhandle = opendir("./ausbudget/");
+$headers = Array("Table ID", "Portfolio", "Agency", "Program", "Scheme", "2011-2012", "2012-2013", "Difference", "Source");
+
+$fp = fopen('php://output', 'w');
+if ($fp) {
+    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);
+    if ($dhandle) {
+        // loop through all of the files
+        while (false !== ($fname = readdir($dhandle))) {
+            if (($fname != '.') && ($fname != '..')) {
+                //echo "$fname <br>";
+                $html = phpQuery::newDocumentHTML(file_get_contents($dir . $fname));
+                phpQuery::selectDocument($html);
+                foreach (pq('table')->elements as $table) {
+                    $data = Array();
+                    ////echo "loltable";
+                    //echo $table->ownerDocument->saveXML($table);
+                    foreach (pq('tr',$table)->elements as $row) {
+                        //echo "lolrow";
+                        $rowText = pq($row)->text();
+                        if (strpos($rowText, "Twitter") === false) {
+
+                            $key = trim(pq("td:first", $row)->text());
+                            //echo "<b>$key</b><br>";
+                            $value = trim(str_replace(pq("td:first", $row)->text(), "", pq("td", $row)->text()));
+                            if ($key == "2011-2012" || $key == "2012-2013") {
+                                $eValue = explode("-", $value);
+                                $value = trim(str_replace(Array("$", ",", "\n"), "", $eValue[0]));
+                            }
+                            //echo "$value <br>";
+                            $data[$key] = trim(str_replace(Array("\n"), "", $value));
+                            if ($key == "Source") {
+                               
+                                foreach ($headers as $fieldName) {
+                                    if (isset($data[$fieldName])) {
+
+                                        $csvrow[] = $data[$fieldName];
+                                    } else {
+                                        $csvrow[] = "";
+                                    }
+                                }
+
+                                fputcsv($fp, array_values($csvrow));
+                                $data = Array();
+                                 $csvrow = Array();
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+    die;
+}
+?>
+