Skip to content

Commit

Permalink
Add home setting to have a default city to show at application startup
Browse files Browse the repository at this point in the history
  • Loading branch information
nerzhul committed Jul 18, 2015
1 parent 55f806f commit 170014e
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 3 deletions.
17 changes: 17 additions & 0 deletions appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
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 {

Expand All @@ -41,6 +43,10 @@ public function __construct (array $urlParams=array()) {
return new CityMapper($c->query('ServerContainer')->getDb());
});

$container->registerService('SettingsMapper', function(IContainer $c) {
return new SettingsMapper($c->query('ServerContainer')->getDb());
});

/**
* Controllers
*/
Expand All @@ -49,6 +55,17 @@ public function __construct (array $urlParams=array()) {
$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')
);
});
Expand Down
23 changes: 23 additions & 0 deletions appinfo/database.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@
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'),
)));
?>
11 changes: 9 additions & 2 deletions controller/citycontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -44,7 +46,12 @@ public function index () {
*/
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
));
}

/**
Expand Down
52 changes: 52 additions & 0 deletions controller/settingscontroller.php
Original file line number Diff line number Diff line change
@@ -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));
}
};
?>
4 changes: 4 additions & 0 deletions db/citymapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public function load ($id) {
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 = ?';
Expand Down
47 changes: 47 additions & 0 deletions db/settingsmapper.php
Original file line number Diff line number Diff line change
@@ -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;
}
};
?>
32 changes: 32 additions & 0 deletions js/public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});
}
}
]);

0 comments on commit 170014e

Please sign in to comment.