diff --git a/controller/weathercontroller.php b/controller/weathercontroller.php index 3f87b81..445450f 100644 --- a/controller/weathercontroller.php +++ b/controller/weathercontroller.php @@ -27,6 +27,7 @@ class WeatherController extends Controller { private $settingsMapper; private $apiKey; private $metric; + private $curl; private static $apiWeatherURL = "http://api.openweathermap.org/data/2.5/weather?mode=json&q="; private static $apiForecastURL = "http://api.openweathermap.org/data/2.5/forecast?mode=json&q="; @@ -37,6 +38,11 @@ class WeatherController extends Controller { $this->settingsMapper = $settingsMapper; $this->apiKey = $settingsMapper->getApiKey($this->userId); $this->metric = $settingsMapper->getMetric($this->userId); + $this->curl = curl_init(); + } + + public function __destruct () { + curl_close($this->curl); } /** @@ -46,18 +52,19 @@ class WeatherController extends Controller { public function get($name) { $cityInfos = $this->getCityInformations($name); if (!$cityInfos) { - return new JSONResponse(array(), Http::STATUS_NOT_FOUND); + return new JSONResponse(array(), $this->errorCode); } return new JSONResponse($cityInfos); } private function getCityInformations ($name) { - // @TODO setting for metric - $cityDatas = json_decode(file_get_contents(WeatherController::$apiWeatherURL.$name."&APPID=".$this->apiKey."&units=".$this->metric), true); - if ($cityDatas['cod'] != '200') { + $reqContent = $this->curlGET(WeatherController::$apiWeatherURL.$name."&APPID=".$this->apiKey."&units=".$this->metric); + if ($reqContent[0] != Http::STATUS_OK) { + $this->errorCode = $reqContent[0]; return null; } + $cityDatas = json_decode($reqContent[1], true); $cityDatas["forecast"] = array(); $forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$this->apiKey."&units=".$this->metric), true); if ($forecast['cod'] == '200' && isset($forecast['cnt']) && is_numeric($forecast['cnt'])) { @@ -82,6 +89,13 @@ class WeatherController extends Controller { return $cityDatas; } + private function curlGET ($URL) { + curl_setopt($this->curl, CURLOPT_URL, $URL); + curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); + $output = curl_exec($this->curl); + return array(curl_getinfo($this->curl, CURLINFO_HTTP_CODE), $output); + } + private function windDegToString($deg) { if ($deg > 0 && $deg < 23 || $deg > 333) { diff --git a/css/style.css b/css/style.css index 09384c0..12835ec 100644 --- a/css/style.css +++ b/css/style.css @@ -146,6 +146,11 @@ font-size: 1.5em; } +.city-load-error a { + color: #FF3333; + text-align: center; +} + #city-weather-panel, #city-forecast-panel { border-radius: 3px; padding: 30px; diff --git a/js/public/app.js b/js/public/app.js index d6c12ba..e4d4177 100644 --- a/js/public/app.js +++ b/js/public/app.js @@ -33,6 +33,7 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil $scope.addCityError = ''; $scope.cityLoadError = ''; + $scope.cityLoadNeedsAPIKey = false; $scope.currentCity = null; $scope.selectedCityId = 0; $scope.domCity = null; @@ -216,17 +217,25 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil else { $scope.cityLoadError = 'Failed to get city weather informations. Please contact your administrator'; } + $scope.cityLoadNeedsAPIKey = false; }). error(function (data, status, headers, config) { if (status == 404) { $scope.cityLoadError = "No city with this name found."; + $scope.cityLoadNeedsAPIKey = false; + } + else if (status == 401) { + $scope.cityLoadError = "Your OpenWeatherMap API key is invalid. Please provide a working API Key."; + $scope.cityLoadNeedsAPIKey = true; } else if (status == 500) { $scope.cityLoadError = g_error500; + $scope.cityLoadNeedsAPIKey = false; } }). fail(function (data, status, headers, config) { $scope.cityLoadError = g_error500; + $scope.cityLoadNeedsAPIKey = false; }); } diff --git a/templates/main.php b/templates/main.php index ba2a052..d497495 100644 --- a/templates/main.php +++ b/templates/main.php @@ -43,7 +43,10 @@