Redid direct agency info import
[disclosr.git] / import.php
blob:a/import.php -> blob:b/import.php
--- a/import.php
+++ b/import.php
@@ -1,31 +1,22 @@
 <?php
-require_once 'sag/src/Sag.php';
-$sag = new Sag();
+require_once 'common.inc.php';
+
 $sag->setDatabase("disclosr-agencies", true);
-$file = "agencies.csv";
-$handle = fopen($file, "r");
-$fieldNames = Array();
-$line = 0;
-while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
-	if ($line == 0) {
-		$fieldNames = $data;
-	}
-	else {
-		$agency = Array();
-		$valueCount = 0;
-		foreach ($data as $value) {
-			if ($value != "") $agency[$fieldNames[$valueCount]] = $value;
-			$valueCount++;
-		}
+//createAgencyDesignDoc();
+ $conn = new PDO("pgsql:dbname=contractDashboard;user=postgres;password=snmc;host=localhost");
+ $namesQ = 'select agency.abn, string_agg("agencyName",\',\') as names from agency inner join agency_nametoabn on agency.abn::text = agency_nametoabn.abn group by agency.abn;';
+ $abntonames = Array(); 
+ foreach ($conn->query($namesQ) as $row) {
+      $abntonames[$row['abn']] = explode(",",$row['names']);
+  }
+  $result = $conn->query("select * from agency");
+  while ($agency = $result->fetch(PDO::FETCH_ASSOC)) {
+		
+		$agency['otherNames'] = $abntonames[$agency['abn']];
+                if (sizeof($abntonames[$agency['abn']]) == 1) $agency['name'] = $abntonames[$agency['abn']][0];
 		$agency["lastScraped"] = "1/1/1970";
 		$agency["scrapeDepth"] = 1;
-		var_dump($agency);
                 $sag->post($agency);
-	}
-	$line++;
-	if ($line % 10000 == 0) echo "$line records... \n";
-}
-fclose($handle);
-echo "Found a total of $line records in $file.\n";
+  }
 ?>