<?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", |
$headers; |
"supplierABN" |
|
); |
while (($data = fgetcsv($handle, 1000, "\t")) !== false) { |
if ($tablename == "contractnotice") { |
$num = count($data); |
$contractNoticeInsertQ = 'INSERT INTO contractnotice ("' . implode('" , "', $contractNoticeFields) . '") VALUES ( '; |
if ($row == 3) { |
foreach ($contractNoticeFields as $key => $f) { |
$headers = $data; |
$contractNoticeInsertQ.= ($key == 0 ? "" : ", ") . "?"; |
} elseif ($row > 3) { |
} |
if ($num > count($datamapping0712)) { |
$contractNoticeInsertQ.= ");"; |
die("<font color=red>Error in data import; data mapping fields out of bounds or changed</font><br>" . $fname . print_r($data)); |
$contractNoticeInsertQ = $conn->prepare($contractNoticeInsertQ); |
} |
} |
$contractNoticeInsert = Array(); |
else if ($tablename == "supplierdetails") { |
$supplierInsert = Array(); |
$supplierInsertQ = 'INSERT INTO supplierdetails ("' . implode('" , "', $supplierFields) . '") VALUES ( '; |
$agencyInsert = Array(); |
foreach ($supplierFields as $key => $f) { |
$contractNoticeInsert[] = $fpath; |
$supplierInsertQ.= ($key == 0 ? "" : ", ") . "?"; |
$keys = array_keys($datamapping0712); |
} |
for ($c = 0; $c < $num; $c++) { |
$supplierInsertQ.= ");"; |
$data[$c] = trim($data[$c], "="); |
$supplierInsertQ = $conn->prepare($supplierInsertQ); |
$data[$c] = trim($data[$c], "\""); |
} |
if (in_array(($datamapping0712[$headers[$c]]), $contractNoticeFields)) { |
else if ($tablename == "agency") { |
if (($datamapping0712[$headers[$c]]) == "parentCN" || ($datamapping0712[$headers[$c]]) == "CNID") { |
$agencyInsertQ = 'INSERT INTO agency ("' . implode('" , "', $agencyFields) . '") VALUES ( '; |
$data[$c] = substr($data[$c], 2); // take off the "CN" prefix |
foreach ($agencyFields as $key => $f) { |
if ($data[$c] > 0 && $data[$c] != '0') { |
$agencyInsertQ.= ($key == 0 ? "" : ", ") . "?"; |
$contractNoticeInsert[] = $data[$c]; |
} |
} else { |
$agencyInsertQ.= ");"; |
$contractNoticeInsert[] = null; |
$agencyInsertQ = $conn->prepare($agencyInsertQ); |
} |
} |
} elseif (($datamapping0712[$headers[$c]]) == "supplierABN") { |
while (($data = fgetcsv($handle, 1000, "\t")) !== false) { |
if ($data[$c] > 0 && $data[$c] != '0') { |
$num = count($data); |
$contractNoticeInsert[] = $data[$c]; |
if ($row == 3) { |
} else { |
$headers = $data; |
$contractNoticeInsert[] = null; |
} |
} |
elseif ($row > 3) { |
} elseif (($datamapping0712[$headers[$c]]) == "amendDate" || ($datamapping0712[$headers[$c]]) == "publishDate" || ($datamapping0712[$headers[$c]]) == "contractStart" || ($datamapping0712[$headers[$c]]) == "contractEnd") { |
if ($num > count($datamapping0711)) { |
$contractNoticeInsert[] = date('Y-m-d H:i:s', strtotime($data[$c])); |
die("<font color=red>Error in data import; data mapping fields out of bounds or changed</font><br>" . $fname . print_r($data)); |
} else { |
} |
if (strstr("\" =", $data[$c] > 0)) { |
$contractNoticeInsert = Array(); |
die("Invalid Description field" . $contractNoticeInsert); |
$supplierInsert = Array(); |
} |
$agencyInsert = Array(); |
$colvalue = preg_replace('/[^[:print:]]/', '', utf8_encode($data[$c])); |
$contractNoticeInsert[] = $fpath; |
|
$keys = array_keys($datamapping0711); |
$contractNoticeInsert[] = $colvalue; |
for ($c = 0; $c < $num; $c++) { |
} |
$data[$c] = trim($data[$c], "="); |
} |
$data[$c] = trim($data[$c], "\""); |
} |
if ($tablename == "contractnotice") { |
flush(); |
if (in_array(($datamapping0711[$headers[$c]]) , $contractNoticeFields)) { |
$contractNoticeInsertQ->execute($contractNoticeInsert); |
if (($datamapping0711[$headers[$c]]) == "parentCN" || ($datamapping0711[$headers[$c]]) == "CNID") { |
$errors = $conn->errorInfo(); |
$data[$c] = substr($data[$c], 2); // take off the "CN" prefix |
if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) { |
$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"); |
} elseif ($errors[1] == 0) { |
if ($data[$c] > 0) { |
$success++; |
$contractNoticeInsert[] = $data[$c]; |
} else { |
} |
foreach ($contractNoticeFields as $key => $cnf) { |
else { |
echo var_dump($contractNoticeInsert[$key]) . $cnf . "<br>"; |
$contractNoticeInsert[] = 0; |
} |
} |
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) { |
flush(); |
$contractNoticeInsert[] = $data[$c]; |
//echo "<hr>\n"; |
} |
} |
else { |
$row++; |
$contractNoticeInsert[] = null; |
} |
} |
fclose($handle); |
} |
$contractNoticeInsertQ->closeCursor(); |
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])); |
return $success; |
} |
} |
else { |
|
if (strstr("\" =", $data[$c] > 0)) { |
|
die("Invalid Description field" . $contractNoticeInsert); |
|
} |
|
$colvalue = preg_replace( '/[^[:print:]]/', '',utf8_encode( $data[$c])); |
|
|
|
$contractNoticeInsert[] = $colvalue; |
|
} |
|
} |
|
} |
|
else if ($tablename == "supplierdetails") { |
|
if (in_array(($datamapping0711[$headers[$c]]) , $supplierFields)) { |
|
if (($datamapping0711[$headers[$c]]) == "supplierABN") { |
|
if ($data[$c] > 0) { |
|
$contractNoticeInsert[] = $data[$c]; |
|
} |
|
else { |
|
$contractNoticeInsert[] = 0; |
|
} |
|
} |
|
else { |
|
$supplierInsert[] = $data[$c]; |
|
} |
|
} |
|
} |
|
else if ($tablename == "agency") { |
|
if (in_array(($datamapping0711[$headers[$c]]) , $agencyFields)) { |
|
$agencyInsert[] = $data[$c]; |
|
} |
|
} |
|
} |
|
flush(); |
|
if ($tablename == "contractnotice") { |
|
$contractNoticeInsertQ->execute($contractNoticeInsert); |
|
$errors = $conn->errorInfo(); |
|
if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) { |
|
} |
|
elseif ($errors[1] == 0) { |
|
$success++; |
|
} |
|
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); |
|
return $success; |
|
} |
|
$path = 'data/'; |
$path = 'data/'; |
if ($_REQUEST["fname"] == "") { |
if ($_REQUEST["fname"] == "") { |
echo "Get files from: https://www.tenders.gov.au/?event=public.reports.list<br>"; |
echo "Get files from: https://www.tenders.gov.au/?event=public.reports.list<br>"; |
$dhandle = opendir($path); |
$dhandle = opendir($path); |
// define an array to hold the files |
// define an array to hold the files |
$files = array(); |
$files = array(); |
if ($dhandle) { |
if ($dhandle) { |
// loop through all of the files |
// loop through all of the files |
while (false !== ($fname = readdir($dhandle))) { |
while (false !== ($fname = readdir($dhandle))) { |
if (($fname != '.') && ($fname != '..')) { |
if (($fname != '.') && ($fname != '..')) { |
echo "<a href=\"import.php?fname=$fname\">$fname</a> " . filesize($path . $fname) . " " . date("c", filemtime($path . $fname)) . "<br/>"; |
$files[date("c", filemtime($path . $fname)) . md5($fname)] = $fname; |
} |
} |
} |
} |
} |
} |
} |
ksort($files); |
else { |
foreach ($files as $date => $fname) { |
$success = 0; |
echo "<a href=\"import.php?fname=$fname\">$fname</a> " . filesize($path . $fname) . " " . $date . "<br/>"; |
$fname = $_REQUEST["fname"]; |
} |
echo " ============== $fname ============== <br>"; |
} else { |
flush(); |
$success = 0; |
$success+= processFile($path . $fname, "contractnotice"); |
$fname = $_REQUEST["fname"]; |
$success+= processFile($path . $fname, "agency"); |
echo " ============== $fname ============== <br>"; |
$success+= processFile($path . $fname, "supplier"); |
flush(); |
echo "<br> $success records successfully created"; |
$success+= processFile($path . $fname, "contractnotice"); |
flush(); |
$success+= processFile($path . $fname, "agency"); |
} |
$success+= processFile($path . $fname, "supplier"); |
// run post import data processing |
echo "<br> $success records successfully created"; |
|
|
|
flush(); |
|
// run post import data processing |
// cn |
// cn |
include ("linkAmendments.php"); |
echo "link amend<br>"; |
include ("updateUNSPSC.php"); |
include ("linkAmendments.php"); |
|
echo "update UNSPSC<br>"; |
|
include ("updateUNSPSC.php"); |
// agency |
// agency |
//include ("setAgencyStatus.php"); |
//include ("setAgencyStatus.php"); |
//include ("setAgencyURLABN.php"); |
//include ("setAgencyURLABN.php"); |
|
} |
|
} |
?> |
?> |
|
|