diff --git a/RX2.php b/RX2.php new file mode 100644 index 0000000..eb34050 --- /dev/null +++ b/RX2.php @@ -0,0 +1,183 @@ + 'ERROR', + 'code' => '101', + 'description' => 'No Address present.', + ); + $jsonOut = json_encode($aOutput); + break; + } // if ($address === '') + $geo = new geocode(); + $latlon = $geo->getLatLong($address); + $jsonOut = json_encode($latlon); + $geo = NULL; + break; + case 'DIST': + if ($address === '') + { + $aOutput = array + ( + 'status' => 'ERROR', + 'code' => '101', + 'description' => 'No Address present.', + ); + $jsonOut = json_encode($aOutput); + break; + } // if ($address === '') + $geo = new geocode(); + $db = new PDO('sqlite:assets/db/RX.db'); + $latlon = $geo->getLatLong($address); + if ($latlon['status'] === 'OK') + { + $shortest = 999999.99; // <-- Sets to a very high value to find shortest distance below. + $pharmacies = $db->query('SELECT rowid,* FROM pharmacies'); + $SKIP = 0; // Part of test mode to skip all but one record in DB + foreach ($pharmacies as $pharmacy) + { + $destination = $pharmacy['Address'] . ' ' . $pharmacy['City'] . ' ' . $pharmacy['State'] . ' ' . $pharmacy['Zip']; + if ($SKIP === 0) $result = $geo->getDistance($address, $destination); + if ($TEST == 1) $SKIP = 1; + if ($result['status'] === 'OK') + { + $distance = floatval(preg_replace('/[^0-9.]/', '', $result['distance'])); + if ($distance < $shortest) + { + $aOutput = array + ( + 'pharmacy' => $pharmacy['Pharmacy'], + 'address' => $pharmacy['Address'], + 'city' => $pharmacy['City'], + 'state' => $pharmacy['State'], + 'zip' => $pharmacy['Zip'], + 'latitude' => $pharmacy['Latitude'], + 'longitude' => $pharmacy['Longitude'], + 'home_lat' => $latlon['lat'], + 'home_lon' => $latlon['lon'], + 'status' => $result['status'], + 'distance' => $result['distance'], + 'duration' => $result['duration'], + ); + $shortest = $distance; + } // if ($result['distance'] < $shortest) + } // if ($result['status'] === 'OK') + else + { + $aOutput = array + ( + 'status' => 'ERROR', + 'code' => '104', + 'description' => "Problem returning distance between address and pharmacy.\nAddress: " . $address . "\nPharmacy( ROWID: " . $pharmacy['rowid'] . " Name: " . $pharmacy['Pharmacy'] . ') ', + ); + $jsonOut = json_encode($aOutput); + break; + } //else ($result['status'] === 'OK') + } // foreach ($pharmacies as $pharmacy) + } // if ($latlon['status'] === 'OK') + else + { + $aOutput = array + ( + 'status' => 'ERROR', + 'code' => '103', + 'description' => "Problem returning Lat / Lon.\nAddress: " . $address, + ); + $jsonOut = json_encode($aOutput); + break; + + } // else ($latlon['status'] === 'OK') + $jsonOut = json_encode($aOutput); + $db = NULL; + $geo = NULL; + break; + + default: + $aOutput = array + ( + 'status' => 'ERROR', + 'code' => '102', + 'description' => "Invalid Command sent - " . $command, + ); + $jsonOut = json_encode($aOutput); + break; + + } // switch ($command) + break; + +// case 'POST': +// break; +// +// case 'PUT': +// break; +// +// case 'DELETE': +// break; +// + default: + // Invalid Request Method + header("HTTP/1.0 405 Method Not Allowed"); + exit; + + } // switch ($request_method) + } // else ($key !== 'RLH4321') + echo $jsonOut; + +// ##### END OF PROGRAM ################################################################################################ + + /** + * GetParam - Returns the GET or POST values, and allows for defaults if not present. + * @param $param_name + * @param null $default + * + * @return string|null + */ + function GetParam($param_name, $default=NULL) + { + global $_POST; + global $_GET; + $param_value = ""; + + if(isset($_POST[$param_name])) + $param_value = $_POST[$param_name]; + else if(isset($_GET[$param_name])) + $param_value = $_GET[$param_name]; + else if($param_value === '') + $param_value = $default; + + return $param_value; + } diff --git a/assets/classes/geocode.php b/assets/classes/geocode.php new file mode 100644 index 0000000..25263ef --- /dev/null +++ b/assets/classes/geocode.php @@ -0,0 +1,112 @@ +api_key.'&origins='.rawurlencode($origin).'&destinations='.rawurlencode($destination).'&mode=driving&units=imperial&sensor=false + * + */ + public function getDistance($origin, $destination, $mode='driving', $unit='imperial') + { + // Set defaults to test for + $modes = array('driving', 'walking', 'bicycling'); + $units = array('metric', 'imperial'); + + // Test that addresses are present + if (!is_string($origin) || !is_string($destination))die("All Addresses must be passed as a string"); + + // Test that Mode is present and of correct value + $mode = strtolower($mode); + if (!in_array($mode, $modes))die("Mode not correct"); + + // Test that Unit is present and of correct value + $unit = strtolower($unit); + if (!in_array($unit, $units))die("Unit not correct"); + + // Prep string for send ie(remove spaces and other objects) + $_origin = rawurlencode($origin); + $_destination = rawurlencode($destination); + $_mode = rawurlencode($mode); + $_unit = rawurlencode($unit); + + $_url = sprintf('https://maps.googleapis.com/maps/api/distancematrix/json?%s&origins=%s&destinations=%s&mode=%s&units=%s&sensor=false',API_KEY, $_origin, $_destination, $_mode, $_unit); + if($_result = file_get_contents($_url)) + { + $decode = json_decode($_result,true); + $_dist['status'] = $decode['status']; + $_dist['distance'] = $decode['rows'][0]['elements'][0]['distance']['text']; + $_dist['duration'] = $decode['rows'][0]['elements'][0]['duration']['text']; + return $_dist; + }else die('Nothing was returned'); + } + +} // End Class GEOCODE + + + + diff --git a/assets/data/pharmacies.csv b/assets/data/pharmacies.csv new file mode 100644 index 0000000..9cdc845 --- /dev/null +++ b/assets/data/pharmacies.csv @@ -0,0 +1,31 @@ +"name","address","city","state","zip","latitude","longitude" +"WALGREENS","3696 SW TOPEKA BLVD","TOPEKA","KS",66611,39.00142300,-95.68695000 +"KMART PHARMACY ","1740 SW WANAMAKER ROAD","TOPEKA","KS",66604,39.03504000,-95.75870000 +"CONTINENTAL PHARMACY LLC","821 SW 6TH AVE","TOPEKA","KS",66603,39.05433000,-95.68453000 +"STORMONT-VAIL RETAIL PHARMACY","2252 SW 10TH AVE.","TOPEKA","KS",66604,39.05167000,-95.70534000 +"DILLON PHARMACY","2010 SE 29TH ST","TOPEKA","KS",66605,39.01638400,-95.65065000 +"WAL-MART PHARMACY ","1501 S.W. WANAMAKER ROAD","TOPEKA","KS",66604,39.03955000,-95.76459000 +"KING PHARMACY","4033 SW 10TH AVE","TOPEKA","KS",66604,39.05121000,-95.72700000 +"HY-VEE PHARMACY ","12122 STATE LINE RD","LEAWOOD","KS",66209,38.90775300,-94.60801000 +"JAYHAWK PHARMACY AND PATIENT SUPPLY","2860 SW MISSION WOODS DR","TOPEKA","KS",66614,39.01505300,-95.77866000 +"PRICE CHOPPER PHARMACY","3700 W 95TH ST","LEAWOOD","KS",66206,38.95792000,-94.62881500 +"AUBURN PHARMACY","13351 MISSION RD","LEAWOOD","KS",66209,38.88534500,-94.62800000 +"CVS PHARMACY","5001 WEST 135 ST","LEAWOOD","KS",66224,38.88323000,-94.64518000 +"SAMS PHARMACY ","1401 SW WANAMAKER ROAD","TOPEKA","KS",66604,39.04160300,-95.76462600 +"CVS PHARMACY","2835 SW WANAMAKER RD","TOPEKA","KS",66614,39.01550300,-95.76434000 +"HY-VEE PHARMACY ","2951 SW WANAMAKER RD","TOPEKA","KS",66614,39.01215700,-95.76394000 +"TALLGRASS PHARMACY","601 SW CORPORATE VIEW","TOPEKA","KS",66615,39.05716000,-95.76692000 +"HUNTERS RIDGE PHARMACY","3405 NW HUNTERS RIDGE TER STE 200","TOPEKA","KS",66618,39.12984500,-95.71265400 +"ASSURED PHARMACY ","11100 ASH ST STE 200","LEAWOOD","KS",66211,38.92663200,-94.64744000 +"WALGREENS","4701 TOWN CENTER DR","LEAWOOD","KS",66211,38.91619000,-94.64036600 +"PRICE CHOPPER PHARMACY","1100 SOUTH 7 HWY","BLUE SPRINGS","MO",64015,39.02931000,-94.27175000 +"CVS PHARMACY","1901 WEST KANSAS STREET","LIBERTY","MO",64068,39.24385000,-94.44961000 +"MARRS PHARMACY","205 RD MIZE ROAD","BLUE SPRINGS","MO",64014,39.02353000,-94.26060500 +"WAL-MART PHARMACY ","600 NE CORONADO DRIVE","BLUE SPRINGS","MO",64014,39.02419300,-94.25503000 +"WAL-MART PHARMACY ","10300 E HWY 350","RAYTOWN","MO",64133,38.98376500,-94.45930500 +"HY-VEE PHARMACY ","9400 E. 350 HIGHWAY","RAYTOWN","MO",64133,38.99300000,-94.47083000 +"HY-VEE PHARMACY ","625 W 40 HWY","BLUE SPRINGS","MO",64014,39.01070400,-94.27108000 +"HY-VEE PHARMACY ","109 NORTH BLUE JAY DRIVE","LIBERTY","MO",64068,39.24575800,-94.44702000 +"WALGREENS ","1701 NW HIGHWAY 7","BLUE SPRINGS","MO",64015,39.03754800,-94.27153000 +"WALGREENS ","9300 E GREGORY BLVD","RAYTOWN","MO",64133,38.99534200,-94.47340000 +"WALGREENS ","1191 W KANSAS AVE","LIBERTY","MO",64068,39.24387000,-94.44186400 diff --git a/assets/db/RX.db b/assets/db/RX.db new file mode 100644 index 0000000..fff580b Binary files /dev/null and b/assets/db/RX.db differ diff --git a/assets/js/system.js b/assets/js/system.js new file mode 100644 index 0000000..36487a3 --- /dev/null +++ b/assets/js/system.js @@ -0,0 +1,85 @@ +function btnFind() +{ + // Reset Rows + $("#alertBox").hide(); + $(".alert").html(""); + $('.alert').removeClass('alert-warning').removeClass('alert-danger').addClass('alert-warning'); + $("#spinnerBox").hide(); + $("#txtOutput").hide(); + + // Set Vars + let txtAddress = $("#txtAddress").val() + ""; + // Quick test to make sure address was entered. + if (txtAddress === "") + { + $(".alert").html("You have to enter an address first."); + $("#alertBox").show(); + return; + } + + // Show Spinner + $("#spinnerBox").show(); + + // API - Call geocode API + // K = (Required) Key, allows access to API + // C = (Required) Command, + // LL - Will return the Lat/Lon of a given address. + // DIST - Will return the distance information. + // T = (Optional) Test, Test Mode = 1 will only pull one record from the DB + // ADDRESS = Address you want to return the Lat/Lon or Distance from. + $.ajax( + { + url : 'https://rhays.us/Examples/RX_API_Two/RX2.php', + type : 'GET', + dataType: 'json', + data : + { + 'K' : 'RLH4321', + 'C' : 'DIST', + 'T' : '1', + 'address' : txtAddress, + }, + success : function(data) + { + switch(data.status.toUpperCase()) + { + case 'OK': + sOutput = '

The Pharmacy closest to the address location you entered is:

'; + sOutput = sOutput + 'Pharmacy: ' + data.pharmacy + '
'; + sOutput = sOutput + 'Address: ' + data.address + '
'; + sOutput = sOutput + 'City: ' + data.city + '
'; + sOutput = sOutput + 'State: ' + data.state + '
'; + sOutput = sOutput + 'Zip: ' + data.zip + '

'; + sOutput = sOutput + '

Approx. Drive time and distance:

'; + sOutput = sOutput + 'Distance: ' + data.distance + '
'; + sOutput = sOutput + 'Duration: ' + data.duration + '
'; + + $("#txtOutput").html(sOutput); + $("#spinnerBox").hide(); + $("#txtOutput").show(); + break; + case 'ERROR': + $("#spinnerBox").hide(); + $('.alert').removeClass('alert-warning').removeClass('alert-danger').addClass('alert-danger'); + $(".alert").html(JSON.stringify('Error code: ' + data.code + ' - ' + data.description)); + $("#alertBox").show(); + break; + } + }, + error : function(request,error) + { + $("#spinnerBox").hide(); + $('.alert').removeClass('alert-warning').removeClass('alert-danger').addClass('alert-danger'); + $(".alert").html('SYSTEM.JS
' + JSON.stringify(request)); + $("#alertBox").show(); + return; + }, + // TODO: Hook to put in status codes + // statusCode: + // { + // 404: function() { + // alert( "page not found" ); + // } + // }, + }); +} diff --git a/index.html b/index.html index cf24251..3c5aa8a 100644 --- a/index.html +++ b/index.html @@ -1,55 +1,58 @@ - + + + + + RX_API_Two + + + + + - - - - RX_API_Two - - - - - - -
- - -
-
-
-
-

RX API Two

-
-
-
-
Please enter your address to find the closest pharmacy near you.
-
-
-
- - -
-
-
-
-
-
-
-
-
-
- - -
-
-
-
-
-
-
- - - - +
+

 

+
+
+

RX API Two

+
+
+

 

+
+
Enter your address to find the closest pharmacy near you.
+
+
+
+

 

+
+
+ +
+

 

+
+
+
+
+

 

+
+
+
+
+
+

 

+
+
+ +
+
+
+
+
+
+ + + \ No newline at end of file