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