lotta updates
[contractdashboard.git] / admin / importSON.php
blob:a/admin/importSON.php -> blob:b/admin/importSON.php
<?php <?php
include_once ("../lib/common.inc.php"); include_once ("../lib/common.inc.php");
function fetchSON($SONID) function fetchSON($SONID)
{ {
global $conn; global $conn;
echo $SONID; echo $SONID."\n";
   
# Create a search parser object # Create a search parser object
$search = new DOMDocument(); $search = new DOMDocument();
$doc = new DOMDocument(); $doc = new DOMDocument();
   
# Parse the HTML from Google. # Parse the HTML from Google.
# The @ before the method call suppresses any warnings that # The @ before the method call suppresses any warnings that
# loadHTML might throw because of invalid HTML in the page. # loadHTML might throw because of invalid HTML in the page.
@$search->loadHTML(file_get_contents("https://www.tenders.gov.au/?event=public.advancedsearch.CNSONRedirect&type=sonSearchEvent&SONID=$SONID")); @$search->loadHTML(file_get_contents("https://www.tenders.gov.au/?event=public.advancedsearch.CNSONRedirect&type=sonSearchEvent&SONID=$SONID"));
   
# Iterate over all the <a> tags # Iterate over all the <a> tags
foreach ($search->getElementsByTagName('a') as $link) { foreach ($search->getElementsByTagName('a') as $link) {
# Show the <a href> # Show the <a href>
if (strpos($link->getAttribute('href'), 'UUID')) { if (strpos($link->getAttribute('href'), 'UUID')) {
echo $link->getAttribute('href'); echo $link->getAttribute('href')."\n";
@$doc->loadHTML(file_get_contents("https://www.tenders.gov.au".$link->getAttribute('href'))); @$doc->loadHTML(file_get_contents("https://www.tenders.gov.au".$link->getAttribute('href')));
  break;
} }
} }
   
//TODO update title from $x("//p[@class='lead']/text()") //update title from $x("//p[@class='lead']/text()")
  $xpath = new DOMXpath($doc);
  $elements = $xpath->query("//p[@class='lead']/text()");
  echo ($elements[0]->wholeText)."\n";
  if ($title[0]->wholeText != "") {
  $conn->exec('insert into standingoffers ("SONID",title) values (\''.$SONID."','".$title[0]->wholeText."') ON CONFLICT DO UPDATE;");
  }
$sql='insert into standingoffer_suppliers("SONID",name,abn,state,postcode) values(?,?,?,?,?)'; $sql='insert into standingoffer_suppliers("SONID",name,abn,state,postcode) values(?,?,?,?,?)';
$q=$conn->prepare($sql); $q=$conn->prepare($sql);
   
foreach($doc->getElementsByTagName('tr') as $row) { foreach($doc->getElementsByTagName('tr') as $row) {
$supplier = Array("SONID"=>$SONID); $supplier = Array("SONID"=>$SONID);
$i = 0; $i = 0;
foreach ($row->childNodes as $column){ foreach ($row->childNodes as $column){
//echo $i; //echo $i;
if ($i==0) $supplier['name'] = $column->textContent; if ($i==0) $supplier['name'] = $column->textContent;
if ($i==2) $supplier['abn'] = trim(str_replace(' ','',$column->textContent)); if ($i==2) $supplier['abn'] = trim(str_replace(' ','',$column->textContent));
if ($i==4) $supplier['state'] = $column->textContent; if ($i==4) $supplier['state'] = $column->textContent;
if ($i==6) $supplier['postcode'] = $column->textContent; if ($i==6) $supplier['postcode'] = $column->textContent;
//echo $column->textContent; //echo $column->textContent;
$i++; $i++;
} }
//print_r($supplier); //print_r($supplier);
$q->execute(array_values($supplier)); $q->execute(array_values($supplier));
} }
} }
if ($_REQUEST['SONID']) { if ($_REQUEST['SONID']) {
fetchSON($_REQUEST['SONID']); fetchSON($_REQUEST['SONID']);
  } else {
   
  $sons = Array(
  "SON1200402",
  "SON1209932",
  "SON98624");
   
  foreach ($sons as $son) {
  fetchSON($son);
   
} }
  }