Update import for better PDO statement reuse and only store name/abn/status for agencies
Update import for better PDO statement reuse and only store name/abn/status for agencies

<?php <?php
   
include_once ("../lib/common.inc.php"); include_once ("../lib/common.inc.php");
function processFile($fpath, $tablename)  
{ $contractNoticeFields = array(
global $conn; "importFile",
$row = 1; "agencyName",
$handle = fopen($fpath, "r"); "parentCN",
//"t" mode string translates windows line breaks to unix "CNID",
$datamapping0711 = array( "publishDate",
"Agency" => "agencyName", "amendDate",
"Parent CN ID" => "parentCN", "contractStart",
"CN ID" => "CNID", "contractEnd",
"Publish Date" => "publishDate", "value",
"Amendment Date" => "amendDate", "description",
"Status" => "", "agencyID",
"StartDate" => "contractStart", "category",
"EndDate" => "contractEnd", "procurementMethod",
"Value" => "value", "atmID",
"Description" => "description", "SONID",
"Agency Ref Id" => "agencyID", "confidentialityContract",
"Category" => "category", "confidentialityContractReason",
"Procurement Method" => "procurementMethod", "confidentialityOutputs",
"ATM ID" => "atmID", "confidentialityOutputsReason",
"SON ID" => "SONID", "consultancy",
"Confidentiality - Contract" => "confidentialityContract", "consultancyReason",
"Confidentiality - Contract Reason(s)" => "confidentialityContractReason", "amendmentReason",
"Confidentiality - Outputs" => "confidentialityOutputs", "supplierName",
"Confidentiality - Outputs Reason(s)" => "confidentialityOutputsReason", "supplierAddress",
"Consultancy" => "consultancy", "supplierCity",
"Consultancy Reason(s)" => "consultancyReason", "supplierPostcode",
"Amendment Reason" => "amendmentReason", "supplierCountry",
"Supplier Name" => "supplierName", "supplierABNExempt",
"Supplier Address" => "supplierAddress", "supplierABN",
"Supplier City" => "supplierCity", "contactBranch",
"Supplier Postcode" => "supplierPostcode", "contactDivision",
"Supplier Country" => "supplierCountry", "contactPostcode"
"Supplier ABNExempt" => "supplierABNExempt", );
"Supplier ABN" => "supplierABN", $agencyFields = array(
"Agency Branch" => "contactBranch", "agencyName"
"Agency Divison" => "contactDivision", );
"Agency Postcode" => "contactPostcode", $supplierFields = array(
"" => "" "supplierName",
); "supplierAddress",
$headers; "supplierCity",
$contractNoticeFields = array( "supplierPostcode",
"importFile", "supplierCountry",
"agencyName", "supplierABNExempt",
"parentCN", "supplierABN"
"CNID", );
"publishDate", $contractNoticeInsertQ = 'INSERT INTO contractnotice ("' . implode('" , "', $contractNoticeFields) . '") VALUES ( ';
"amendDate", foreach ($contractNoticeFields as $key => $f) {
"contractStart", $contractNoticeInsertQ.= ($key == 0 ? "" : ", ") . "?";
"contractEnd", }
"value", $contractNoticeInsertQ.= ");";
"description", $contractNoticeInsertQ = $conn->prepare($contractNoticeInsertQ);
"agencyID",  
"category", $supplierInsertQ = 'INSERT INTO supplierdetails ("' . implode('" , "', $supplierFields) . '") VALUES ( ';
"procurementMethod", foreach ($supplierFields as $key => $f) {
"atmID", $supplierInsertQ.= ($key == 0 ? "" : ", ") . "?";
"SONID", }
"confidentialityContract", $supplierInsertQ.= ");";
"confidentialityContractReason", $supplierInsertQ = $conn->prepare($supplierInsertQ);
"confidentialityOutputs",  
"confidentialityOutputsReason", $agencyInsertQ = 'INSERT INTO agency_nametoabn ("' . implode('" , "', $agencyFields) . '") VALUES ( ';
"consultancy", foreach ($agencyFields as $key => $f) {
"consultancyReason", $agencyInsertQ.= ($key == 0 ? "" : ", ") . "?";
"amendmentReason", }
"supplierName", $agencyInsertQ.= ");";
"supplierAddress", $agencyInsertQ = $conn->prepare($agencyInsertQ);
"supplierCity",  
"supplierPostcode", function processFile($fpath, $tablename) {
"supplierCountry", global $conn, $contractNoticeQ, $supplierInsertQ, $agencyInsertQ;
"supplierABNExempt", $row = 1;
"supplierABN", $handle = fopen($fpath, "r");
"contactBranch", //"t" mode string translates windows line breaks to unix
"contactDivision", $datamapping0711 = array(
"contactPostcode" "Agency" => "agencyName",
); "Parent CN ID" => "parentCN",
$agencyFields = array( "CN ID" => "CNID",
"agencyName" "Publish Date" => "publishDate",
); "Amendment Date" => "amendDate",
$supplierFields = array( "Status" => "",
"supplierName", "StartDate" => "contractStart",
"supplierAddress", "EndDate" => "contractEnd",
"supplierCity", "Value" => "value",
"supplierPostcode", "Description" => "description",
"supplierCountry", "Agency Ref Id" => "agencyID",
"supplierABNExempt", "Category" => "category",
"supplierABN" "Procurement Method" => "procurementMethod",
); "ATM ID" => "atmID",
if ($tablename == "contractnotice") { "SON ID" => "SONID",
$contractNoticeInsertQ = 'INSERT INTO contractnotice ("' . implode('" , "', $contractNoticeFields) . '") VALUES ( '; "Confidentiality - Contract" => "confidentialityContract",
foreach ($contractNoticeFields as $key => $f) { "Confidentiality - Contract Reason(s)" => "confidentialityContractReason",
$contractNoticeInsertQ.= ($key == 0 ? "" : ", ") . "?"; "Confidentiality - Outputs" => "confidentialityOutputs",
} "Confidentiality - Outputs Reason(s)" => "confidentialityOutputsReason",
$contractNoticeInsertQ.= ");"; "Consultancy" => "consultancy",
$contractNoticeInsertQ = $conn->prepare($contractNoticeInsertQ); "Consultancy Reason(s)" => "consultancyReason",
} "Amendment Reason" => "amendmentReason",
else if ($tablename == "supplierdetails") { "Supplier Name" => "supplierName",
$supplierInsertQ = 'INSERT INTO supplierdetails ("' . implode('" , "', $supplierFields) . '") VALUES ( '; "Supplier Address" => "supplierAddress",
foreach ($supplierFields as $key => $f) { "Supplier City" => "supplierCity",
$supplierInsertQ.= ($key == 0 ? "" : ", ") . "?"; "Supplier Postcode" => "supplierPostcode",
} "Supplier Country" => "supplierCountry",
$supplierInsertQ.= ");"; "Supplier ABNExempt" => "supplierABNExempt",
$supplierInsertQ = $conn->prepare($supplierInsertQ); "Supplier ABN" => "supplierABN",
} "Agency Branch" => "contactBranch",
else if ($tablename == "agency") { "Agency Divison" => "contactDivision",
$agencyInsertQ = 'INSERT INTO agency ("' . implode('" , "', $agencyFields) . '") VALUES ( '; "Agency Postcode" => "contactPostcode",
foreach ($agencyFields as $key => $f) { "" => ""
$agencyInsertQ.= ($key == 0 ? "" : ", ") . "?"; );
} $headers;
$agencyInsertQ.= ");";  
$agencyInsertQ = $conn->prepare($agencyInsertQ); while (($data = fgetcsv($handle, 1000, "\t")) !== false) {
} $num = count($data);
while (($data = fgetcsv($handle, 1000, "\t")) !== false) { if ($row == 3) {
$num = count($data); $headers = $data;
if ($row == 3) { } elseif ($row > 3) {
$headers = $data; if ($num > count($datamapping0711)) {
} die("<font color=red>Error in data import; data mapping fields out of bounds or changed</font><br>" . $fname . print_r($data));
elseif ($row > 3) { }
if ($num > count($datamapping0711)) { $contractNoticeInsert = Array();
die("<font color=red>Error in data import; data mapping fields out of bounds or changed</font><br>" . $fname . print_r($data)); $supplierInsert = Array();
} $agencyInsert = Array();
$contractNoticeInsert = Array(); $contractNoticeInsert[] = $fpath;
$supplierInsert = Array(); $keys = array_keys($datamapping0711);
$agencyInsert = Array(); for ($c = 0; $c < $num; $c++) {
$contractNoticeInsert[] = $fpath; $data[$c] = trim($data[$c], "=");
$keys = array_keys($datamapping0711); $data[$c] = trim($data[$c], "\"");
for ($c = 0; $c < $num; $c++) { if ($tablename == "contractnotice") {
$data[$c] = trim($data[$c], "="); if (in_array(($datamapping0711[$headers[$c]]), $contractNoticeFields)) {
$data[$c] = trim($data[$c], "\""); if (($datamapping0711[$headers[$c]]) == "parentCN" || ($datamapping0711[$headers[$c]]) == "CNID") {
if ($tablename == "contractnotice") { $data[$c] = substr($data[$c], 2); // take off the "CN" prefix
if (in_array(($datamapping0711[$headers[$c]]) , $contractNoticeFields)) { if ($data[$c] > 0) {
if (($datamapping0711[$headers[$c]]) == "parentCN" || ($datamapping0711[$headers[$c]]) == "CNID") { $contractNoticeInsert[] = $data[$c];
$data[$c] = substr($data[$c], 2); // take off the "CN" prefix } else {
if ($data[$c] > 0) { $contractNoticeInsert[] = 0;
$contractNoticeInsert[] = $data[$c]; }
} } elseif (($datamapping0711[$headers[$c]]) == "supplierABN") {
else { if ($data[$c] > 0) {
$contractNoticeInsert[] = 0; $contractNoticeInsert[] = $data[$c];
} } else {
} $contractNoticeInsert[] = null;
elseif (($datamapping0711[$headers[$c]]) == "supplierABN") { }
if ($data[$c] > 0) { } elseif (($datamapping0711[$headers[$c]]) == "amendDate" || ($datamapping0711[$headers[$c]]) == "publishDate" || ($datamapping0711[$headers[$c]]) == "contractStart" || ($datamapping0711[$headers[$c]]) == "contractEnd") {
$contractNoticeInsert[] = $data[$c]; $contractNoticeInsert[] = date('Y-m-d H:i:s', strtotime($data[$c]));
} } else {
else { if (strstr("\" =", $data[$c] > 0)) {
$contractNoticeInsert[] = null; die("Invalid Description field" . $contractNoticeInsert);
} }
} $colvalue = preg_replace('/[^[:print:]]/', '', utf8_encode($data[$c]));
elseif (($datamapping0711[$headers[$c]]) == "amendDate" || ($datamapping0711[$headers[$c]]) == "publishDate" || ($datamapping0711[$headers[$c]]) == "contractStart" || ($datamapping0711[$headers[$c]]) == "contractEnd") {  
$contractNoticeInsert[] = date('Y-m-d H:i:s', strtotime($data[$c])); $contractNoticeInsert[] = $colvalue;
} }
else { }
if (strstr("\" =", $data[$c] > 0)) { } else if ($tablename == "supplierdetails") {
die("Invalid Description field" . $contractNoticeInsert); if (in_array(($datamapping0711[$headers[$c]]), $supplierFields)) {
} if (($datamapping0711[$headers[$c]]) == "supplierABN") {
$colvalue = preg_replace( '/[^[:print:]]/', '',utf8_encode( $data[$c])); if ($data[$c] > 0) {
  $contractNoticeInsert[] = $data[$c];
$contractNoticeInsert[] = $colvalue; } else {
} $contractNoticeInsert[] = 0;
} }
} } else {
else if ($tablename == "supplierdetails") { $supplierInsert[] = $data[$c];
if (in_array(($datamapping0711[$headers[$c]]) , $supplierFields)) { }
if (($datamapping0711[$headers[$c]]) == "supplierABN") { }
if ($data[$c] > 0) { } else if ($tablename == "agency") {
$contractNoticeInsert[] = $data[$c]; if (in_array(($datamapping0711[$headers[$c]]), $agencyFields)) {
} $agencyInsert[] = $data[$c];
else { }
$contractNoticeInsert[] = 0; }
} }
} flush();
else { if ($tablename == "contractnotice") {
$supplierInsert[] = $data[$c]; $contractNoticeInsertQ->execute($contractNoticeInsert);
} $errors = $conn->errorInfo();
} if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) {
}  
else if ($tablename == "agency") { } elseif ($errors[1] == 0) {
if (in_array(($datamapping0711[$headers[$c]]) , $agencyFields)) { $success++;
$agencyInsert[] = $data[$c]; } else {
} foreach ($contractNoticeFields as $key => $cnf) {
} echo var_dump($contractNoticeInsert[$key]) . $cnf . "<br>";
} }
flush(); echo $data[2] . " failed CN insert.<br>" . print_r($errors, true) . "<br> row $row <br><br>\n";
if ($tablename == "contractnotice") { }
$contractNoticeInsertQ->execute($contractNoticeInsert); } else if ($tablename == "supplierdetails") {
$errors = $conn->errorInfo(); $supplierInsertQ->execute($supplierInsert);
if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) { $errors = $conn->errorInfo();
} if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) {
elseif ($errors[1] == 0) {  
$success++; } elseif ($errors[1] == 0) {
} $success++;
else { } else {
foreach ($contractNoticeFields as $key => $cnf) { echo $data[2] . " failed supplier insert.<br>" . print_r($errors, true) . " <br> " . print_r($supplierInsert, true) . "<br> $row <br><br>\n";
echo var_dump($contractNoticeInsert[$key]) . $cnf . "<br>"; }
} } else if ($tablename == "agency") {
echo $data[2] . " failed CN insert.<br>" . print_r($errors, true) . "<br> row $row <br><br>\n"; $agencyInsertQ->execute($agencyInsert);
} $errors = $conn->errorInfo();
} if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) {
else if ($tablename == "supplierdetails") {  
$supplierInsertQ->execute($supplierInsert); } elseif ($errors[1] == 0) {
$errors = $conn->errorInfo(); $success++;
if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) { } else {
} echo $data[2] . " failed agency insert.<br>" . print_r($errors, true) . " <br> " . print_r($agencyInsert, true) . "<br> $row <br><br>\n";
elseif ($errors[1] == 0) { }
$success++; }
} flush();
else { //echo "<hr>\n";
echo $data[2] . " failed supplier insert.<br>" . print_r($errors, true) . " <br> " . print_r($supplierInsert, true) . "<br> $row <br><br>\n"; }
} $row++;
} }
else if ($tablename == "agency") { fclose($handle);
$agencyInsertQ->execute($agencyInsert); $contractNoticeInsertQ->closeCursor();
$errors = $conn->errorInfo(); $supplierInsertQ->closeCursor();
if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) { $agencyInsertQ->closeCursor();
}  
elseif ($errors[1] == 0) { return $success;
$success++; }
}  
else { $path = 'data/';
echo $data[2] . " failed agency insert.<br>" . print_r($errors, true) . " <br> " . print_r($agencyInsert, true) . "<br> $row <br><br>\n"; if ($_REQUEST["fname"] == "") {
} echo "Get files from: https://www.tenders.gov.au/?event=public.reports.list<br>";
} $dhandle = opendir($path);
flush(); // define an array to hold the files
//echo "<hr>\n"; $files = array();
  if ($dhandle) {
} // loop through all of the files
$row++; while (false !== ($fname = readdir($dhandle))) {
} if (($fname != '.') && ($fname != '..')) {
fclose($handle); echo "<a href=\"import.php?fname=$fname\">$fname</a>&nbsp;" . filesize($path . $fname) . "&nbsp;" . date("c", filemtime($path . $fname)) . "<br/>";
// run post import data processing }
  }
  }
  } else {
  $success = 0;
  $fname = $_REQUEST["fname"];
  echo " ============== $fname ============== <br>";
  flush();
  $success+= processFile($path . $fname, "contractnotice");
  $success+= processFile($path . $fname, "agency");
  $success+= processFile($path . $fname, "supplier");
  echo "<br> $success records successfully created";
   
  flush();
  // run post import data processing
// cn // cn
echo "link amend<br>"; echo "link amend<br>";
include ("linkAmendments.php"); include ("linkAmendments.php");
echo "update UNSPSC<br>"; echo "update UNSPSC<br>";
include ("updateUNSPSC.php"); include ("updateUNSPSC.php");
// agency // agency
//include ("setAgencyStatus.php"); //include ("setAgencyStatus.php");
//include ("setAgencyURLABN.php"); //include ("setAgencyURLABN.php");
return $success; }
}  
$path = 'data/';  
if ($_REQUEST["fname"] == "") {  
echo "Get files from: https://www.tenders.gov.au/?event=public.reports.list<br>";  
$dhandle = opendir($path);  
// define an array to hold the files  
$files = array();  
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/>";  
}  
}  
}  
}  
else {  
$success = 0;  
$fname = $_REQUEST["fname"];  
echo " ============== $fname ============== <br>";  
flush();  
$success+= processFile($path . $fname, "contractnotice");  
$success+= processFile($path . $fname, "agency");  
$success+= processFile($path . $fname, "supplier");  
echo "<br> $success records successfully created";  
flush();  
}  
   
?> ?>