Add city delete and interface improvements
parent
a70098e142
commit
cc92ebc7b4
|
@ -38,6 +38,15 @@ class CityController extends Controller {
|
||||||
return new TemplateResponse($this->appName, 'main');
|
return new TemplateResponse($this->appName, 'main');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @NoAdminRequired
|
||||||
|
* @NoCSRFRequired
|
||||||
|
*/
|
||||||
|
public function getAll() {
|
||||||
|
$cities = $this->mapper->getAll($this->userId);
|
||||||
|
return new JSONResponse(array("cities" => $cities, "userid" => $this->userId));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @NoAdminRequired
|
* @NoAdminRequired
|
||||||
* @NoCSRFRequired
|
* @NoCSRFRequired
|
||||||
|
@ -48,6 +57,7 @@ class CityController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
// @TODO: check city is not already registered
|
// @TODO: check city is not already registered
|
||||||
|
// @TODO: check there is an answer for the city http://api.openweathermap.org/data/2.5/forecast?q=Orsay&mode=xml
|
||||||
|
|
||||||
if ($id = $this->mapper->create($this->userId, $name)) {
|
if ($id = $this->mapper->create($this->userId, $name)) {
|
||||||
return new JSONResponse(array("id" => $id));
|
return new JSONResponse(array("id" => $id));
|
||||||
|
@ -55,5 +65,24 @@ class CityController extends Controller {
|
||||||
|
|
||||||
return new JSONResponse(array());
|
return new JSONResponse(array());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @NoAdminRequired
|
||||||
|
* @NoCSRFRequired
|
||||||
|
*/
|
||||||
|
public function delete ($id) {
|
||||||
|
if (!$id || !is_numeric($id)) {
|
||||||
|
return new JSONResponse(array(), Http::STATUS_BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
$city = $this->mapper->load($id);
|
||||||
|
if ($city['user_id'] != $this->userId) {
|
||||||
|
return new JSONResponse(array(), 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->mapper->delete($id);
|
||||||
|
|
||||||
|
return new JSONResponse(array("deleted" => true));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -43,6 +43,21 @@
|
||||||
color: #333;
|
color: #333;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#city-list-left li .icon-delete {
|
||||||
|
width: 30px;
|
||||||
|
height: 18px;
|
||||||
|
top: 0;
|
||||||
|
right: 0;
|
||||||
|
position: absolute;
|
||||||
|
cursor: pointer;
|
||||||
|
height: 100%;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
#city-list-left li:hover .icon-delete {
|
||||||
|
display: inline;
|
||||||
|
}
|
||||||
|
|
||||||
#city-right {
|
#city-right {
|
||||||
padding: 15px;
|
padding: 15px;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
|
|
|
@ -20,6 +20,31 @@ class CityMapper extends Mapper {
|
||||||
parent::__construct($db, 'weather_city');
|
parent::__construct($db, 'weather_city');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function load ($id) {
|
||||||
|
$sql = 'SELECT id, name, user_id FROM ' .
|
||||||
|
'*PREFIX*weather_city WHERE id = ?';
|
||||||
|
$query = \OCP\DB::prepare($sql);
|
||||||
|
$result = $query->execute(array($id));
|
||||||
|
|
||||||
|
if ($row = $result->fetchRow()) {
|
||||||
|
return $row;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAll ($userId) {
|
||||||
|
$sql = 'SELECT id, name FROM ' .
|
||||||
|
'*PREFIX*weather_city WHERE user_id = ?';
|
||||||
|
$query = \OCP\DB::prepare($sql);
|
||||||
|
$result = $query->execute(array($userId));
|
||||||
|
|
||||||
|
$cities = array();
|
||||||
|
while ($row = $result->fetchRow()) {
|
||||||
|
$cities[] = $row;
|
||||||
|
}
|
||||||
|
return $cities;
|
||||||
|
}
|
||||||
|
|
||||||
public function create ($userId, $name) {
|
public function create ($userId, $name) {
|
||||||
\OCP\DB::beginTransaction();
|
\OCP\DB::beginTransaction();
|
||||||
$query = \OCP\DB::prepare('INSERT INTO *PREFIX*weather_city ' .
|
$query = \OCP\DB::prepare('INSERT INTO *PREFIX*weather_city ' .
|
||||||
|
@ -37,5 +62,13 @@ class CityMapper extends Mapper {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function delete ($id) {
|
||||||
|
\OCP\DB::beginTransaction();
|
||||||
|
$query = \OCP\DB::prepare('DELETE FROM *PREFIX*weather_city ' .
|
||||||
|
'WHERE id = ?');
|
||||||
|
$query->execute(array($id));
|
||||||
|
\OCP\DB::commit();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -58,7 +58,7 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
|
||||||
$http.post(OC.generateUrl('/apps/weather/city/add'), {'name': city.name}).
|
$http.post(OC.generateUrl('/apps/weather/city/add'), {'name': city.name}).
|
||||||
success(function (data, status, headers, config) {
|
success(function (data, status, headers, config) {
|
||||||
if (data != null && !undef(data['id'])) {
|
if (data != null && !undef(data['id'])) {
|
||||||
$scope.boards.push({"name": city.name, "id": data['id']})
|
$scope.cities.push({"name": city.name, "id": data['id']})
|
||||||
$scope.showAddCity = false;
|
$scope.showAddCity = false;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -69,5 +69,34 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
|
||||||
$scope.addCityError = g_error500;
|
$scope.addCityError = g_error500;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
$scope.deleteCity = function(city) {
|
||||||
|
if (undef(city)) {
|
||||||
|
alert(g_error500);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$http.post(OC.generateUrl('/apps/weather/city/delete'), {'id': city.id}).
|
||||||
|
success(function (data, status, headers, config) {
|
||||||
|
if (data != null && !undef(data['deleted'])) {
|
||||||
|
for (var i = 0; i < $scope.cities.length; i++) {
|
||||||
|
if ($scope.cities[i].id === city.id) {
|
||||||
|
$scope.cities.splice(i, 1);
|
||||||
|
// If current city is the removed city, close it
|
||||||
|
if ($scope.selectedCityId === city.id) {
|
||||||
|
$scope.selectedCityId = 0;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
alert('Failed to remove city. Please contact your administrator');
|
||||||
|
}
|
||||||
|
}).
|
||||||
|
fail(function (data, status, headers, config) {
|
||||||
|
alert(g_error500);
|
||||||
|
});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
<div class="ng-scope" id="app" ng-app="Weather" ng-controller="WeatherController">
|
<div class="ng-scope" id="app" ng-app="Weather" ng-controller="WeatherController">
|
||||||
<div id="city-list-left">
|
<div id="city-list-left">
|
||||||
<ul class="city-list">
|
<ul class="city-list">
|
||||||
<li class=city-list-item" ng-repeat="city in citys" class="{{ city.id == selectedCityId ? 'selected' : ''}}">
|
<li class=city-list-item" ng-repeat="city in cities" class="{{ city.id == selectedCityId ? 'selected' : ''}}">
|
||||||
<a href="#" ng-click="loadCity(city.id);">{{ city.name }}</a>
|
<a href="#" ng-click="loadCity(city.id);">{{ city.name }}</a>
|
||||||
<div class="icon-delete svn delete action" ng-click="deleteCity(city);"></div>
|
<div class="icon-delete svn delete action" ng-click="deleteCity(city);"></div>
|
||||||
</li>
|
</li>
|
||||||
|
|
Loading…
Reference in New Issue