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