From 170014edabda7f064597c913ed9e8f69aa5141d9 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 18 Jul 2015 13:58:51 +0000 Subject: [PATCH] Add home setting to have a default city to show at application startup --- appinfo/application.php | 17 ++++++++++ appinfo/database.xml | 23 ++++++++++++++ appinfo/info.xml | 2 +- appinfo/routes.php | 2 ++ controller/citycontroller.php | 11 +++++-- controller/settingscontroller.php | 52 +++++++++++++++++++++++++++++++ db/citymapper.php | 4 +++ db/settingsmapper.php | 47 ++++++++++++++++++++++++++++ js/public/app.js | 32 +++++++++++++++++++ 9 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 controller/settingscontroller.php create mode 100644 db/settingsmapper.php diff --git a/appinfo/application.php b/appinfo/application.php index c6fc6f3..79320d3 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -16,9 +16,11 @@ use \OCP\AppFramework\App; use \OCP\IContainer; use \OCA\Weather\Controller\CityController; +use \OCA\Weather\Controller\SettingsController; use \OCA\Weather\Controller\WeatherController; use \OCA\Weather\Db\CityMapper; +use \OCA\Weather\Db\SettingsMapper; class Application extends App { @@ -41,6 +43,10 @@ class Application extends App { return new CityMapper($c->query('ServerContainer')->getDb()); }); + $container->registerService('SettingsMapper', function(IContainer $c) { + return new SettingsMapper($c->query('ServerContainer')->getDb()); + }); + /** * Controllers */ @@ -49,6 +55,17 @@ class Application extends App { $c->query('AppName'), $c->query('Request'), $c->query('UserId'), + $c->query('CityMapper'), + $c->query('SettingsMapper') + ); + }); + + $container->registerService('SettingsController', function(IContainer $c) { + return new SettingsController( + $c->query('AppName'), + $c->query('Request'), + $c->query('UserId'), + $c->query('SettingsMapper'), $c->query('CityMapper') ); }); diff --git a/appinfo/database.xml b/appinfo/database.xml index 1489b6b..33c2458 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -29,4 +29,27 @@ + + *dbprefix*weather_config + + + user + text + true + 255 + + + key + text + true + 255 + + + value + text + false + 10240 + + +
diff --git a/appinfo/info.xml b/appinfo/info.xml index d8892a7..771d18b 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,7 +5,7 @@ Watch the weather directly on your ownCloud AGPL Loic Blot - 1.1.0 + 1.1.3 7 170605 diff --git a/appinfo/routes.php b/appinfo/routes.php index 15cb2d8..fb1b290 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -21,5 +21,7 @@ $application->registerRoutes($this, array('routes' => array( array('name' => 'city#delete', 'url' => '/city/delete', 'verb' => 'POST'), array('name' => 'weather#get', 'url' => '/weather/get', 'verb' => 'GET'), + + array('name' => 'settings#homeset', 'url' => '/settings/home/set', 'verb' => 'POST'), ))); ?> diff --git a/controller/citycontroller.php b/controller/citycontroller.php index 0becb2d..68e34e1 100644 --- a/controller/citycontroller.php +++ b/controller/citycontroller.php @@ -23,11 +23,13 @@ class CityController extends Controller { private $userId; private $mapper; + private $settingsMapper; - public function __construct ($appName, IRequest $request, $userId, CityMapper $mapper) { + public function __construct ($appName, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper) { parent::__construct($appName, $request); $this->userId = $userId; $this->mapper = $mapper; + $this->settingsMapper = $settingsMapper; } /** @@ -44,7 +46,12 @@ class CityController extends Controller { */ public function getAll() { $cities = $this->mapper->getAll($this->userId); - return new JSONResponse(array("cities" => $cities, "userid" => $this->userId)); + $home = $this->settingsMapper->getHome($this->userId); + return new JSONResponse(array( + "cities" => $cities, + "userid" => $this->userId, + "home" => $home + )); } /** diff --git a/controller/settingscontroller.php b/controller/settingscontroller.php new file mode 100644 index 0000000..eb3e205 --- /dev/null +++ b/controller/settingscontroller.php @@ -0,0 +1,52 @@ + + * @copyright Loic Blot 2015 + */ + +namespace OCA\Weather\Controller; + +use \OCP\IRequest; +use \OCP\AppFramework\Http\TemplateResponse; +use \OCP\AppFramework\Controller; +use \OCP\AppFramework\Http\JSONResponse; +use \OCP\AppFramework\Http; + +use \OCA\Weather\Db\SettingsMapper; +use \OCA\Weather\Db\CityMapper; + +class SettingsController extends Controller { + + private $userId; + private $mapper; + + public function __construct ($appName, IRequest $request, $userId, SettingsMapper $mapper, CityMapper $cityMapper) { + parent::__construct($appName, $request); + $this->userId = $userId; + $this->mapper = $mapper; + $this->cityMapper = $cityMapper; + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function homeSet ($city) { + if (!$city || !is_numeric($city)) { + return new JSONResponse(array(), Http::STATUS_BAD_REQUEST); + } + + if (!$this->cityMapper->exists($city)) { + return new JSONResponse(array(), Http::STATUS_NOT_FOUND); + } + + $this->mapper->setHome($this->userId, $city); + return new JSONResponse(array("set" => true)); + } +}; +?> diff --git a/db/citymapper.php b/db/citymapper.php index 9472d15..a839316 100644 --- a/db/citymapper.php +++ b/db/citymapper.php @@ -32,6 +32,10 @@ class CityMapper extends Mapper { return null; } + public function exists ($id) { + return ($this->load($id)); + } + public function getAll ($userId) { $sql = 'SELECT id, name FROM ' . '*PREFIX*weather_city WHERE user_id = ?'; diff --git a/db/settingsmapper.php b/db/settingsmapper.php new file mode 100644 index 0000000..d64dbe3 --- /dev/null +++ b/db/settingsmapper.php @@ -0,0 +1,47 @@ + + * @copyright Loic Blot 2015 + */ + +namespace OCA\Weather\Db; + +use \OCP\IDb; + +use \OCP\AppFramework\Db\Mapper; + +class SettingsMapper extends Mapper { + public function __construct (IDb $db) { + parent::__construct($db, 'weather_city'); + } + + public function setHome ($userId, $cityId) { + \OCP\DB::beginTransaction(); + $query = \OCP\DB::prepare('DELETE FROM *PREFIX*weather_config ' . + 'WHERE `user` = ? and `key` = ?'); + $query->execute(array($userId, "home")); + + $query = \OCP\DB::prepare('INSERT INTO *PREFIX*weather_config ' . + '(`user`,`key`,`value`) VALUES (?,?,?)'); + $query->execute(array($userId, "home", $cityId)); + \OCP\DB::commit(); + } + + public function getHome ($userId) { + $sql = 'SELECT value FROM ' . + '*PREFIX*weather_config WHERE `user` = ? and `key` = ?'; + $query = \OCP\DB::prepare($sql); + $result = $query->execute(array($userId, "home")); + + if ($row = $result->fetchRow()) { + return $row["value"]; + } + return 0; + } +}; +?> diff --git a/js/public/app.js b/js/public/app.js index bec52a2..3b81289 100644 --- a/js/public/app.js +++ b/js/public/app.js @@ -68,6 +68,18 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil if (!undef(data['userid'])) { $scope.userId = data['userid']; } + + if (!undef(data['home'])) { + $scope.homeCity = data['home']; + if ($scope.homeCity) { + for (i = 0; i < $scope.cities.length; i++) { + if ($scope.cities[i].id == $scope.homeCity) { + $scope.loadCity($scope.cities[i]); + return; + } + } + } + } }). fail(function (data, status, headers, config) { $scope.fatalError(); @@ -189,5 +201,25 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil alert(g_error500); }); }; + + $scope.setHome = function(cityId) { + if (undef(cityId)) { + alert(g_error500); + return; + } + + $http.post(OC.generateUrl('/apps/weather/settings/home/set'), {'city': cityId}). + success(function (data, status, headers, config) { + if (data != null && !undef(data['set'])) { + $scope.homeCity = cityId; + } + else { + alert('Failed to set home. Please contact your administrator'); + } + }). + fail(function (data, status, headers, config) { + alert(g_error500); + }); + } } ]);