From c67f48eb28a8aa3fafa161104fc360babf3ed038 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Thu, 21 Jun 2018 18:33:32 -0700 Subject: [PATCH] set timezone of itinerary legs based on airport location --- controllers/controllers.php | 20 ++++++++++++++++++++ views/new-itinerary.php | 26 ++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/controllers/controllers.php b/controllers/controllers.php index bc13dd3..9a49305 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -1005,3 +1005,23 @@ $app->get('/edit', function() use($app) { $app->redirect($url . '?edit=' . $params['url'], 302); } }); + +$app->get('/airport-info', function() use($app){ + if($user=require_login($app)) { + $params = $app->request()->params(); + if(!isset($params['code'])) return; + + $ch = curl_init('https://atlas.p3k.io/api/timezone?airport='.urlencode($params['code'])); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $data = json_decode(curl_exec($ch), true); + + if(!$data) + $response = ['error' => 'unknown']; + else { + $response = $data; + } + + $app->response()['Content-type'] = 'application/json'; + $app->response()->body(json_encode($response)); + } +}); diff --git a/views/new-itinerary.php b/views/new-itinerary.php index c3176f9..14deb65 100644 --- a/views/new-itinerary.php +++ b/views/new-itinerary.php @@ -44,7 +44,7 @@
- +
@@ -58,7 +58,7 @@
- +
@@ -127,6 +127,27 @@ $(function(){ }); } + function timezone_for_airport(code, callback) { + $.getJSON("/airport-info?code="+code, function(data){ + callback(data.offset); + }); + } + + function bind_leg_timezone() { + $(".itinerary-leg .leg-origin").unbind("change").change(function(el){ + timezone_for_airport($(this).val(), function(offset){ + $(el.target).parents(".itinerary-leg").find(".leg-departure-tz").val(offset); + $(el.target).parents(".itinerary-leg").find(".leg-departure-tz").parent().addClass("has-success"); + }); + }); + $(".itinerary-leg .leg-destination").unbind("change").change(function(el){ + timezone_for_airport($(this).val(), function(offset){ + $(el.target).parents(".itinerary-leg").find(".leg-arrival-tz").val(offset); + $(el.target).parents(".itinerary-leg").find(".leg-arrival-tz").parent().addClass("has-success"); + }); + }); + } + function add_leg() { $("#itinerary-legs-container").append($("#leg-template").html()); @@ -152,6 +173,7 @@ $(function(){ */ bind_leg_x(); + bind_leg_timezone(); } add_leg();