Add home setting to have a default city to show at application startup
parent
55f806fa1a
commit
170014edab
|
@ -16,9 +16,11 @@ use \OCP\AppFramework\App;
|
||||||
use \OCP\IContainer;
|
use \OCP\IContainer;
|
||||||
|
|
||||||
use \OCA\Weather\Controller\CityController;
|
use \OCA\Weather\Controller\CityController;
|
||||||
|
use \OCA\Weather\Controller\SettingsController;
|
||||||
use \OCA\Weather\Controller\WeatherController;
|
use \OCA\Weather\Controller\WeatherController;
|
||||||
|
|
||||||
use \OCA\Weather\Db\CityMapper;
|
use \OCA\Weather\Db\CityMapper;
|
||||||
|
use \OCA\Weather\Db\SettingsMapper;
|
||||||
|
|
||||||
class Application extends App {
|
class Application extends App {
|
||||||
|
|
||||||
|
@ -41,6 +43,10 @@ class Application extends App {
|
||||||
return new CityMapper($c->query('ServerContainer')->getDb());
|
return new CityMapper($c->query('ServerContainer')->getDb());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$container->registerService('SettingsMapper', function(IContainer $c) {
|
||||||
|
return new SettingsMapper($c->query('ServerContainer')->getDb());
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controllers
|
* Controllers
|
||||||
*/
|
*/
|
||||||
|
@ -49,6 +55,17 @@ class Application extends App {
|
||||||
$c->query('AppName'),
|
$c->query('AppName'),
|
||||||
$c->query('Request'),
|
$c->query('Request'),
|
||||||
$c->query('UserId'),
|
$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')
|
$c->query('CityMapper')
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
@ -29,4 +29,27 @@
|
||||||
</field>
|
</field>
|
||||||
</declaration>
|
</declaration>
|
||||||
</table>
|
</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>
|
</database>
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
<description>Watch the weather directly on your ownCloud</description>
|
<description>Watch the weather directly on your ownCloud</description>
|
||||||
<licence>AGPL</licence>
|
<licence>AGPL</licence>
|
||||||
<author>Loic Blot</author>
|
<author>Loic Blot</author>
|
||||||
<version>1.1.0</version>
|
<version>1.1.3</version>
|
||||||
<requiremin>7</requiremin>
|
<requiremin>7</requiremin>
|
||||||
|
|
||||||
<ocsid>170605</ocsid>
|
<ocsid>170605</ocsid>
|
||||||
|
|
|
@ -21,5 +21,7 @@ $application->registerRoutes($this, array('routes' => array(
|
||||||
array('name' => 'city#delete', 'url' => '/city/delete', 'verb' => 'POST'),
|
array('name' => 'city#delete', 'url' => '/city/delete', 'verb' => 'POST'),
|
||||||
|
|
||||||
array('name' => 'weather#get', 'url' => '/weather/get', 'verb' => 'GET'),
|
array('name' => 'weather#get', 'url' => '/weather/get', 'verb' => 'GET'),
|
||||||
|
|
||||||
|
array('name' => 'settings#homeset', 'url' => '/settings/home/set', 'verb' => 'POST'),
|
||||||
)));
|
)));
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -23,11 +23,13 @@ class CityController extends Controller {
|
||||||
|
|
||||||
private $userId;
|
private $userId;
|
||||||
private $mapper;
|
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);
|
parent::__construct($appName, $request);
|
||||||
$this->userId = $userId;
|
$this->userId = $userId;
|
||||||
$this->mapper = $mapper;
|
$this->mapper = $mapper;
|
||||||
|
$this->settingsMapper = $settingsMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -44,7 +46,12 @@ class CityController extends Controller {
|
||||||
*/
|
*/
|
||||||
public function getAll() {
|
public function getAll() {
|
||||||
$cities = $this->mapper->getAll($this->userId);
|
$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
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
?>
|
|
@ -32,6 +32,10 @@ class CityMapper extends Mapper {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function exists ($id) {
|
||||||
|
return ($this->load($id));
|
||||||
|
}
|
||||||
|
|
||||||
public function getAll ($userId) {
|
public function getAll ($userId) {
|
||||||
$sql = 'SELECT id, name FROM ' .
|
$sql = 'SELECT id, name FROM ' .
|
||||||
'*PREFIX*weather_city WHERE user_id = ?';
|
'*PREFIX*weather_city WHERE user_id = ?';
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
?>
|
|
@ -68,6 +68,18 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
|
||||||
if (!undef(data['userid'])) {
|
if (!undef(data['userid'])) {
|
||||||
$scope.userId = 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) {
|
fail(function (data, status, headers, config) {
|
||||||
$scope.fatalError();
|
$scope.fatalError();
|
||||||
|
@ -189,5 +201,25 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
|
||||||
alert(g_error500);
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|
Loading…
Reference in New Issue