File: /home/api/public_html/nashville/get-search-results.php
<?php
//ini_set( "display_errors", true );
//ini_set( "display_startup_errors", true );
header( "Cache-Control: no-cache, must-revalidate" );
header( "Expires: Sat, 26 Jul 1997 05:00:00 GMT" );
ini_set( "memory_limit", "1G" );
require( "php/config.php" );
$oRes = new Result( "SearchResults" );
$oSearch = new Search();
$oSearch->setActive( true );
if ( isset( $_GET['DrivingTime'] ) )
$_GET['Distance'] = $_GET['DrivingTime'];
foreach ( $_GET as $k => $v )
{
switch( strtolower( $k ) )
{
case "page":
$oRes->setPage( $v ); break;
case "sortby":
$oSearch->setVal( $k, $v ); $oRes->setSortField( $oSearch->sSortBy ); break;
case "https":
$oRes->bHttps = true; break;
case "sortdir":
$oSearch->setVal( $k, $v ); $oRes->setSortDir( $oSearch->sSortDir ); break;
case "limit":
$oRes->setLimit( $v ); break;
// case "listingagencycode":
// $oSearch->setVal( $k, 'RTC' . $v ); break;
case "propertystatus":
if ( strtolower( $v ) == "off-market" )
{
$oRes->aSearchReturnFields[] = "ClosedDate";
$oRes->aSearchReturnFields[] = "SalePrice";
}
default:
$oSearch->setVal( $k, $v );
}
}
$iBaseLimit = false;
if ( isset( $_GET['DrivingTime'] ) && is_numeric( $_GET['DrivingTime'] ) &&
isset( $_GET['StartLocation'] ) && ( strlen( $_GET['StartLocation'] ) > 1 ) )
{
$oRes->aPassThrough = array( "Duration", "DurationValue" );
$oRes->aSearchReturnFields[] = "Duration";
$oRes->aSearchReturnFields[] = "DurationValue";
if ( strlen( $oRes->iLimit ) > 0 )
{
$iBaseLimit = $oRes->iLimit;
$oRes->setLimit( $oRes->iLimit * 5 );
}
else
{
$oRes->setLimit( 100 );
$iBaseLimit = 20;
}
}
$aResult = $oSearch->doQuery();
if ( isset( $_GET['DrivingTime'] ) && is_numeric( $_GET['DrivingTime'] ) &&
isset( $aResult['Data'] ) && is_array( $aResult['Data'] ) )
{
uasort( $aResult['Data'], "sortDist" );
$aResult['Data'] = array_values( $aResult['Data'] );
$aKeepDests = $aResult['Data'];
$iDestOffset = 0;
while ( count( $aKeepDests ) > 0 )
{
$destinations = "";
$aThisDest = array();
foreach ( $aKeepDests as $kKD => $aKD )
{
if ( count( $aThisDest ) > 20 ) continue;
else
{
$aThisDest[ $kKD ] = $aKD;
unset( $aKeepDests[ $kKD ] );
}
}
foreach ( $aThisDest as $oD )
$destinations .= $oD['Latitude'] . "," . $oD['Longitude'] . "|";
$destinations = substr( $destinations, 0, -1 );
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" . $_GET['StartLocation'] .
"&destinations=" . $destinations . "&key=" . GOOGLEAPIKEY;
$c = curl_init( $url );
curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
$j = curl_exec( $c );
curl_close( $c );
$oResult = json_decode( $j );
if ( $oResult->status == "OK" )
{
foreach ( $oResult->rows[0]->elements as $kDest => $oDest )
{
if ( $oDest->duration->value > ( $_GET['DrivingTime'] * 60 ) ) {}
else
{
$aResult['Data'][$kDest + $iDestOffset]['Duration'] = $oDest->duration->text;
$aResult['Data'][$kDest + $iDestOffset]['DurationValue'] = $oDest->duration->value;
}
}
}
$iDestOffset += count( $aThisDest );
}
foreach ( $aResult['Data'] as $kR => $oR )
{
if ( ! ( strlen( $oR['Duration'] ) > 0 ) )
unset( $aResult['Data'][ $kR ] );
}
$aResult['TotalRecords'] = count( $aResult['Data'] );
$aResult['Data'] = array_values( $aResult['Data'] );
if ( $_GET['SortBy'] == "Duration" )
uasort( $aResult['Data'], "sortDuration" );
if ( $iBaseLimit )
$aResult['Data'] = array_slice( $aResult['Data'], 0, $iBaseLimit );
$aResult['RecordsReturned'] = count( $aResult['Data'] );
}
if ( isset( $aResult["Data"] ) )
{
$oRes->addData( $aResult["Data"], "Data" );
$oRes->addData( $aResult["TotalRecords"], "TotalRecords" );
$oRes->addData( $aResult["RecordsReturned"], "RecordsReturned" );
}
else
$oRes->addData( 0, "TotalRecords" );
$oRes->addData( $oSearch->aSQLStatements, "sql" );
//print "here2";
$oMLS->finish( "nashville", "search-results", json_encode( $_GET ) );
print $oRes->getJSON();
function sortDist( $a, $b )
{
if ( $a['distance'] == $b['distance'] ) return 0;
else return ( $a['distance'] < $b['distance'] ) ? -1 : 1;
exit;
}
function sortDuration( $a, $b )
{
if ( $_GET['SortDir'] == "asc" )
{
if ( $a['DurationValue'] == $b['DurationValue'] ) return 0;
else return ( $a['DurationValue'] < $b['DurationValue'] ) ? -1 : 1;
}
else
{
if ( $a['DurationValue'] == $b['DurationValue'] ) return 0;
else return ( $a['DurationValue'] < $b['DurationValue'] ) ? 1 : -1;
}
exit;
}