Link amendments made in new numbering scheme
[contractdashboard.git] / admin / partialdata / import.php
blob:a/admin/partialdata/import.php -> blob:b/admin/partialdata/import.php
--- a/admin/partialdata/import.php
+++ b/admin/partialdata/import.php
@@ -1,12 +1,18 @@
 <?php
-include_once ("../lib/common.inc.php");
+if (php_sapi_name() == "cli") {
+    include_once ("../../lib/common.inc.php");
 function processFile($fpath, $tablename)
 {
 	global $conn;
+	echo " ============== $fpath  ============== <br>";
+	flush();
 	$row = 1;
+	$success = 0;
+	$dupes = 0;
 	$handle = fopen($fpath, "r");
 	//"t" mode string translates windows line breaks to unix
 	$datamapping0507 = array(
+
 		"Agency" => "agencyName",
 		"CN ID" => "CNID",
 		"Publish Date" => "publishDate",
@@ -16,7 +22,8 @@
 		"Title" => "description",
 		"Category" => "category",
 		"ATM ID" => "atmID",
-		"LastUpdated" => "",
+		"Supplier Name" => "supplierName",
+		"LastUpdated" => "amendDate",
 		"" => ""
 	);
 	$headers;
@@ -32,7 +39,7 @@
 		"value",
 		"atmID",
 		"supplierName",
-		"LastUpdated"
+		"amendDate"
 	);
 	if ($tablename == "contractnotice") {
 		$contractNoticeInsertQ = 'INSERT INTO contractnotice ("' . implode('" , "', $contractNoticeFields) . '") VALUES ( ';
@@ -50,7 +57,7 @@
 		}
 		elseif ($row > 3) {
 			if ($num > count($datamapping0507)) {
-				die("<font color=red>Error in data import; data mapping fields out of bounds or changed</font><br>" . $fname . print_r($data));
+				die("<font color=red>Error in data import; data mapping fields out of bounds or changed</font><br>" . $fname . "data:" .$num. print_r($data ,true). "mapping:" . count($datamapping0507). print_r($datamapping0507 ,true));
 			}
 			$contractNoticeInsert = Array();
 			$contractNoticeInsert[] = $fpath;
@@ -62,7 +69,6 @@
 					if (in_array(($datamapping0507[$headers[$c]]) , $contractNoticeFields)) {
 						if (($datamapping0507[$headers[$c]]) == "parentCN" || ($datamapping0507[$headers[$c]]) == "CNID") {
 							$data[$c] = substr($data[$c], 2); // take off the "CN" prefix
-							$data[$c] = str_replace("-A", "00", $data[$c]); // make amendments really big numbers
 							if (!is_numeric($data[$c]) && $data[$c] != "") die($data[$c] . " is not numeric");
 							if ($data[$c] > 0) {
 								$contractNoticeInsert[] = $data[$c];
@@ -98,6 +104,7 @@
 				$contractNoticeInsertQ->execute($contractNoticeInsert);
 				$errors = $conn->errorInfo();
 				if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) {
+					$dupes++;
 				}
 				elseif ($errors[1] == 0) {
 					$success++;
@@ -116,6 +123,9 @@
 		$row++;
 	}
 	fclose($handle);
+	echo " $dupes duplicate records<br>";
+		echo " $success records successfully created<br>";
+	flush();
 	return $success;
 }
 $path = './';
@@ -127,8 +137,9 @@
 	if ($dhandle) {
 		// loop through all of the files
 		while (false !== ($fname = readdir($dhandle))) {
-			if (($fname != '.') && ($fname != '..')) {
-				echo "<a href=\"import.php?fname=$fname\">$fname</a>&nbsp;" . filesize($path . $fname) . "&nbsp;" . date("c", filemtime($path . $fname)) . "<br/>";
+			if (($fname != '.') && ($fname != '..') && (!isset($_REQUEST["filter"]) || strpos($fname,$_REQUEST["filter"]) != false)) {
+				echo "<a href=\"importdatagov.php?fname=$fname\">$fname</a>&nbsp;" . filesize($path . $fname) . "&nbsp;" . date("c", filemtime($path . $fname)) . "<br/>";
+				processFile($path . $fname, "contractnotice");
 			}
 		}
 	}
@@ -136,11 +147,10 @@
 else {
 	$success = 0;
 	$fname = $_REQUEST["fname"];
-	echo " ============== $fname  ============== <br>";
-	flush();
+	
 	$success+= processFile($path . $fname, "contractnotice");
-	echo "<br> $success records successfully created";
-	flush();
+
+}
 }
 ?>