2015-07-08 21:40:17 +00:00
|
|
|
<?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;
|
|
|
|
|
2017-07-03 21:12:50 +00:00
|
|
|
use \OCP\IConfig;
|
2015-07-08 21:40:17 +00:00
|
|
|
use \OCP\IRequest;
|
|
|
|
use \OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use \OCP\AppFramework\Controller;
|
|
|
|
use \OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use \OCP\AppFramework\Http;
|
|
|
|
|
2016-04-24 20:16:29 +00:00
|
|
|
use \OCA\Weather\Db\CityEntity;
|
2015-07-08 21:40:17 +00:00
|
|
|
use \OCA\Weather\Db\CityMapper;
|
2016-04-24 20:16:29 +00:00
|
|
|
use \OCA\Weather\Db\SettingsMapper;
|
2015-11-21 08:41:12 +00:00
|
|
|
use \OCA\Weather\Controller\IntermediateController;
|
2015-07-08 21:40:17 +00:00
|
|
|
|
2015-11-21 08:41:12 +00:00
|
|
|
class CityController extends IntermediateController {
|
2015-07-08 21:40:17 +00:00
|
|
|
|
|
|
|
private $userId;
|
|
|
|
private $mapper;
|
2015-07-18 13:58:51 +00:00
|
|
|
private $settingsMapper;
|
2017-07-03 21:12:50 +00:00
|
|
|
private $config;
|
2015-07-08 21:40:17 +00:00
|
|
|
|
2017-07-03 21:12:50 +00:00
|
|
|
public function __construct ($appName, IConfig $config, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper) {
|
2015-07-08 21:40:17 +00:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
$this->userId = $userId;
|
|
|
|
$this->mapper = $mapper;
|
2015-07-18 13:58:51 +00:00
|
|
|
$this->settingsMapper = $settingsMapper;
|
2017-07-03 21:12:50 +00:00
|
|
|
$this->config = $config;
|
2015-07-08 21:40:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
|
|
|
public function index () {
|
2019-11-06 14:20:21 +00:00
|
|
|
$response = new TemplateResponse($this->appName, 'main'); // templates/main.php
|
|
|
|
|
|
|
|
$csp = new StrictContentSecurityPolicy();
|
|
|
|
$csp->allowEvalScript();
|
|
|
|
$csp->allowInlineStyle();
|
|
|
|
|
|
|
|
$response->setContentSecurityPolicy($csp);
|
|
|
|
|
|
|
|
return $response;
|
2015-07-08 21:40:17 +00:00
|
|
|
}
|
2015-07-08 22:05:57 +00:00
|
|
|
|
2015-07-09 19:05:43 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
|
|
|
public function getAll() {
|
|
|
|
$cities = $this->mapper->getAll($this->userId);
|
2015-07-18 13:58:51 +00:00
|
|
|
$home = $this->settingsMapper->getHome($this->userId);
|
|
|
|
return new JSONResponse(array(
|
|
|
|
"cities" => $cities,
|
|
|
|
"userid" => $this->userId,
|
|
|
|
"home" => $home
|
|
|
|
));
|
2015-07-09 19:05:43 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 22:05:57 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
|
|
|
public function add ($name) {
|
|
|
|
if (!$name) {
|
|
|
|
return new JSONResponse(array(), Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
2015-07-09 19:35:17 +00:00
|
|
|
// Trim city name to remove unneeded spaces
|
|
|
|
$name = trim($name);
|
|
|
|
|
|
|
|
$cities = $this->mapper->getAll($this->userId);
|
|
|
|
for ($i = 0; $i < count($cities); $i++) {
|
|
|
|
if (strtolower($cities[$i]['name']) == strtolower($name)) {
|
|
|
|
return new JSONResponse(array(), 409);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 10:14:35 +00:00
|
|
|
$cityInfos = $this->getCityInformations($name);
|
|
|
|
|
|
|
|
if (!$cityInfos["response"]) {
|
|
|
|
return new JSONResponse($cityInfos, $cityInfos["code"]);
|
2015-07-09 19:35:17 +00:00
|
|
|
}
|
2015-07-08 22:05:57 +00:00
|
|
|
|
|
|
|
if ($id = $this->mapper->create($this->userId, $name)) {
|
2017-05-02 14:37:49 +00:00
|
|
|
// Load parameter is set to true if we don't found previous cities.
|
|
|
|
// This permit to trigger loading of the first city in UI
|
|
|
|
return new JSONResponse(array(
|
|
|
|
"id" => $id,
|
|
|
|
"load" => count($cities) == 0)
|
|
|
|
);
|
2015-07-08 22:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new JSONResponse(array());
|
|
|
|
}
|
2015-07-09 19:05:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2016-04-24 20:16:29 +00:00
|
|
|
$entity = new CityEntity();
|
|
|
|
$entity->setId($id);
|
|
|
|
$entity->setUser_id($this->userId);
|
|
|
|
|
|
|
|
$this->mapper->delete($entity);
|
2015-07-09 19:05:43 +00:00
|
|
|
|
|
|
|
return new JSONResponse(array("deleted" => true));
|
|
|
|
}
|
2015-07-09 19:35:17 +00:00
|
|
|
|
|
|
|
private function getCityInformations ($name) {
|
2017-07-03 21:12:50 +00:00
|
|
|
$apiKey = $this->config->getAppValue($this->appName, 'openweathermap_api_key');
|
2017-05-02 10:14:35 +00:00
|
|
|
$cityDatas = json_decode($this->curlGET(
|
2017-07-03 21:12:50 +00:00
|
|
|
"http://api.openweathermap.org/data/2.5/forecast?q=".urlencode($name)."&mode=json&APPID=".urlencode($apiKey))[1],
|
2017-05-02 10:14:35 +00:00
|
|
|
true);
|
|
|
|
if (in_array('cod', $cityDatas)) {
|
|
|
|
return array("code" => 502, "response" => null);
|
|
|
|
}
|
|
|
|
|
2015-07-09 19:35:17 +00:00
|
|
|
if ($cityDatas['cod'] != '200') {
|
2017-07-03 21:12:50 +00:00
|
|
|
return array("code" => $cityDatas['cod'], "response" => null, "apikey" => $apiKey);
|
2015-07-09 19:35:17 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 10:52:18 +00:00
|
|
|
return array("code" => 200, "response" => $cityDatas);
|
2015-07-09 19:35:17 +00:00
|
|
|
}
|
2015-07-08 21:40:17 +00:00
|
|
|
};
|
|
|
|
?>
|