Add home setting to have a default city to show at application startup

master
Loic Blot 2015-07-18 13:58:51 +00:00
parent 55f806fa1a
commit 170014edab
9 changed files with 187 additions and 3 deletions

View File

@ -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')
);
});

View File

@ -29,4 +29,27 @@
</field>
</declaration>
</table>
<table>
<name>*dbprefix*weather_config</name>
<declaration>
<field>
<name>user</name>
<type>text</type>
<notnull>true</notnull>
<length>255</length>
</field>
<field>
<name>key</name>
<type>text</type>
<notnull>true</notnull>
<length>255</length>
</field>
<field>
<name>value</name>
<type>text</type>
<notnull>false</notnull>
<length>10240</length>
</field>
</declaration>
</table>
</database>

View File

@ -5,7 +5,7 @@
<description>Watch the weather directly on your ownCloud</description>
<licence>AGPL</licence>
<author>Loic Blot</author>
<version>1.1.0</version>
<version>1.1.3</version>
<requiremin>7</requiremin>
<ocsid>170605</ocsid>

View File

@ -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'),
)));
?>

View File

@ -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
));
}
/**

View File

@ -0,0 +1,52 @@
<?php
/**
* ownCloud - Weather
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Loic Blot <loic.blot@unix-experience.fr>
* @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));
}
};
?>

View File

@ -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 = ?';

View File

@ -0,0 +1,47 @@
<?php
/**
* ownCloud - weather
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Loic Blot <loic.blot@unix-experience.fr>
* @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;
}
};
?>

View File

@ -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);
});
}
}
]);