diff --git a/appinfo/routes.php b/appinfo/routes.php index d6e7a98..0e9f168 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -25,5 +25,7 @@ $application->registerRoutes($this, array('routes' => array( array('name' => 'settings#homeset', 'url' => '/settings/home/set', 'verb' => 'POST'), array('name' => 'settings#apikeyset', 'url' => '/settings/apikey/set','verb' => 'POST'), array('name' => 'settings#apikeyget', 'url' => '/settings/apikey/get','verb' => 'GET'), + array('name' => 'settings#metricset', 'url' => '/settings/metric/set','verb' => 'POST'), + array('name' => 'settings#metricget', 'url' => '/settings/metric/get','verb' => 'GET'), ))); ?> diff --git a/controller/settingscontroller.php b/controller/settingscontroller.php index 76d6d81..09651f3 100644 --- a/controller/settingscontroller.php +++ b/controller/settingscontroller.php @@ -66,5 +66,27 @@ class SettingsController extends Controller { return new JSONResponse(array("apikey" => $this->mapper->getApiKey($this->userId))); } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function metricSet ($metric) { + $this->mapper->setMetric($this->userId, $metric); + return new JSONResponse(array("set" => true)); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function metricGet () { + $metric = $this->mapper->getMetric($this->userId); + if ($metric === 0) { + $this->mapper->setMetric($this->userId, "metric"); + $metric = "metric"; + } + return new JSONResponse(array("metric" => $metric)); + } }; ?> diff --git a/controller/weathercontroller.php b/controller/weathercontroller.php index d84ec5c..3f87b81 100644 --- a/controller/weathercontroller.php +++ b/controller/weathercontroller.php @@ -26,8 +26,9 @@ class WeatherController extends Controller { private $mapper; private $settingsMapper; private $apiKey; - private static $apiWeatherURL = "http://api.openweathermap.org/data/2.5/weather?mode=json&units=metric&q="; - private static $apiForecastURL = "http://api.openweathermap.org/data/2.5/forecast?mode=json&units=metric&q="; + private $metric; + 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="; public function __construct ($appName, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper) { parent::__construct($appName, $request); @@ -35,6 +36,7 @@ class WeatherController extends Controller { $this->mapper = $mapper; $this->settingsMapper = $settingsMapper; $this->apiKey = $settingsMapper->getApiKey($this->userId); + $this->metric = $settingsMapper->getMetric($this->userId); } /** @@ -51,13 +53,13 @@ class WeatherController extends Controller { private function getCityInformations ($name) { // @TODO setting for metric - $cityDatas = json_decode(file_get_contents(WeatherController::$apiWeatherURL.$name."&APPID=".$this->apiKey), true); + $cityDatas = json_decode(file_get_contents(WeatherController::$apiWeatherURL.$name."&APPID=".$this->apiKey."&units=".$this->metric), true); if ($cityDatas['cod'] != '200') { return null; } $cityDatas["forecast"] = array(); - $forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$this->apiKey), true); + $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'])) { // Show only 8 values max // @TODO: setting ? diff --git a/db/settingsmapper.php b/db/settingsmapper.php index 25d12e5..8054c89 100644 --- a/db/settingsmapper.php +++ b/db/settingsmapper.php @@ -32,10 +32,18 @@ class SettingsMapper extends Mapper { $this->setSetting("apikey", $userId, $apiKey); } - public function getApiKey($userId) { + public function getApiKey ($userId) { return $this->getSetting($userId, "apikey"); } + public function setMetric ($userId, $metric) { + $this->setSetting("metric", $userId, $metric); + } + + public function getMetric ($userId) { + return $this->getSetting($userId, "metric"); + } + public function setSetting ($settingName, $userId, $settingValue) { \OCP\DB::beginTransaction(); $query = \OCP\DB::prepare('DELETE FROM *PREFIX*weather_config ' . diff --git a/js/public/app.js b/js/public/app.js index 5d841ca..c89b1e7 100644 --- a/js/public/app.js +++ b/js/public/app.js @@ -26,6 +26,8 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil $scope.owncloudAppImgPath = ''; $scope.apiKey = ''; $scope.userId = ''; + $scope.metric = 'metric'; + $scope.metricRepresentation = '°C'; $scope.cities = []; $scope.showAddCity = false; $scope.addCityError = ''; @@ -60,27 +62,26 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil $scope.loadCities(); }); - $timeout(function () { - $scope.loadApiKey(); - }); + $timeout(function () { $scope.loadApiKey(); }); + $timeout(function () { $scope.loadMetric(); }); $scope.modifyAPIKey = function () { $http.post(OC.generateUrl('/apps/weather/settings/apikey/set'), {'apikey': $scope.apiKey}). success(function (data, status, headers, config) { if (data != null && !undef(data['set'])) { - // @TODO + $scope.loadCity($scope.domCity); } else { - $scope.setApiKeyError = 'Failed to set API key. Please contact your administrator'; + $scope.settingError = 'Failed to set API key. Please contact your administrator'; } }). error(function (data, status, headers, config) { if (status == 403) { - $scope.setApiKeyError = "This key doesn't work. Please provide a valid OpenWeatherMap API key"; + $scope.settingError = "This key doesn't work. Please provide a valid OpenWeatherMap API key"; } }). fail(function (data, status, headers, config) { - $scope.setApiKeyError = g_error500; + $scope.settingError = g_error500; }); } @@ -96,6 +97,53 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil }); }; + $scope.mapMetric = function () { + if ($scope.metric == 'kelvin') { + $scope.metricRepresentation = '°K'; + } + else if ($scope.metric == 'imperial') { + $scope.metricRepresentation = '°F'; + } + else { + $scope.metric = 'metric'; + $scope.metricRepresentation = '°C'; + } + }; + + $scope.modifyMetric = function () { + $http.post(OC.generateUrl('/apps/weather/settings/metric/set'), {'metric': $scope.metric}). + success(function (data, status, headers, config) { + if (data != null && !undef(data['set'])) { + $scope.mapMetric(); + $scope.loadCity($scope.domCity); + } + else { + $scope.settingError = 'Failed to set metric. Please contact your administrator'; + } + }). + error(function (data, status, headers, config) { + if (status == 404) { + $scope.settingError = "This metric is not known."; + } + }). + fail(function (data, status, headers, config) { + $scope.settingError = g_error500; + }); + } + + $scope.loadMetric = function () { + $http.get(OC.generateUrl('/apps/weather/settings/metric/get')). + success(function (data, status, headers, config) { + if (!undef(data['metric'])) { + $scope.metric = data['metric']; + $scope.mapMetric(); + } + }). + fail(function (data, status, headers, config) { + $scope.fatalError(); + }); + }; + $scope.loadCities = function () { $http.get(OC.generateUrl('/apps/weather/city/getall')). success(function (data, status, headers, config) { diff --git a/templates/main.php b/templates/main.php index 34a4470..ada6691 100644 --- a/templates/main.php +++ b/templates/main.php @@ -31,8 +31,14 @@
@@ -46,7 +52,7 @@ -