diff --git a/api/actions/merchants.php b/api/actions/merchants.php new file mode 100644 index 0000000..e52aa15 --- /dev/null +++ b/api/actions/merchants.php @@ -0,0 +1,105 @@ +boundingCoordinates($radius, "miles"); + +ob_flush(); +$people = $database->debug()->select("merchants", [ + "merchantid", + "name", + "address", + "latitude", + "longitude" + ], [ + "AND" => [ + 'latitude[<>]' => [$searchbounds[0]->getLatitudeInDegrees(), $searchbounds[1]->getLatitudeInDegrees()], + 'longitude[<>]' => [$searchbounds[0]->getLongitudeInDegrees(), $searchbounds[1]->getLongitudeInDegrees()] + ], + "LIMIT" => 100 + ] +); +$query = ob_get_contents(); +ob_clean(); +$merchants = $database->query($query)->fetchAll(); + +// Get a list of the merchant types +for ($i = 0; $i < count($merchants); $i++) { + $types = $database->select( + "merchant_type", + ["[>]merchant_types" => "typeid"], + ["merchant_type.typeid (id)", "type (name)", "icon"], + ["merchantid" => $merchants[$i]["merchantid"]] + ); + $merchants[$i]["types"] = $types; +} + +if (!empty($VARS["format"]) && $VARS["format"] == "geojson") { + $geojson = [ + "name" => "Nearby Merchants", + "type" => "FeatureCollection", + "features" => [] + ]; + + foreach ($merchants as $merchant) { + $geojson["features"][] = [ + "type" => "Feature", + "geometry" => [ + "type" => "Point", + "coordinates" => [ + $merchant["longitude"] * 1.0, + $merchant["latitude"] * 1.0 + ] + ], + "properties" => [ + "id" => $merchant["merchantid"], + "name" => utf8_encode($merchant["name"]), + "address" => utf8_encode($merchant["address"]), + "types" => $merchant["types"] + ] + ]; + } + + exitWithJson($geojson); +} + +$nearby = []; + +foreach ($merchants as $merchant) { + $nearby[] = [ + "name" => utf8_encode($merchant["name"]), + "merchantid" => $merchant["merchantid"], + "address" => utf8_encode($merchant["address"]), + "types" => $merchant["types"], + "latitude" => $merchant["latitude"] * 1.0, + "longitude" => $merchant["longitude"] * 1.0 + ]; +} + +exitWithJson([ + "status" => "OK", + "radius" => $radius, + "bounds" => [ + 0 => [ + "latitude" => $searchbounds[0]->getLatitudeInDegrees(), + "longitude" => $searchbounds[0]->getLongitudeInDegrees() + ], + 1 => [ + "latitude" => $searchbounds[1]->getLatitudeInDegrees(), + "longitude" => $searchbounds[1]->getLongitudeInDegrees() + ], + ], + "nearby" => $nearby +]); diff --git a/api/apisettings.php b/api/apisettings.php index d4db106..34cde83 100644 --- a/api/apisettings.php +++ b/api/apisettings.php @@ -84,6 +84,16 @@ $APIS = [ "format (optional)" => "/(geojson)/" ] ], + "getmerchants" => [ + "load" => "merchants.php", + "vars" => [ + "key" => $keyregex, + "latitude" => "/-?[0-9]{2}\.[0-9]+/", + "longitude" => "/-?[0-9]{2,3}\.[0-9]+/", + "radius (optional)" => "/[0-9]*\.?[0-9]+/", + "format (optional)" => "/(geojson)/" + ] + ], "broadcast" => [ "load" => "broadcast.php", "vars" => [ diff --git a/database.mwb b/database.mwb index 393a5e6..d48e909 100644 Binary files a/database.mwb and b/database.mwb differ