nc-weather/lib/Controller/CityController.php

151 lines
3.7 KiB
PHP
Raw Normal View History

<?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;
use \OCP\IRequest;
use \OCP\AppFramework\Http\TemplateResponse;
use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http\JSONResponse;
use \OCP\AppFramework\Http;
2019-11-06 14:52:47 +00:00
use \OCP\AppFramework\Http\StrictContentSecurityPolicy;
2016-04-24 20:16:29 +00:00
use \OCA\Weather\Db\CityEntity;
use \OCA\Weather\Db\CityMapper;
2016-04-24 20:16:29 +00:00
use \OCA\Weather\Db\SettingsMapper;
use \OCA\Weather\Controller\IntermediateController;
class CityController extends IntermediateController {
private $userId;
private $mapper;
private $settingsMapper;
2017-07-03 21:12:50 +00:00
private $config;
2017-07-03 21:12:50 +00:00
public function __construct ($appName, IConfig $config, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper) {
parent::__construct($appName, $request);
$this->userId = $userId;
$this->mapper = $mapper;
$this->settingsMapper = $settingsMapper;
2017-07-03 21:12:50 +00:00
$this->config = $config;
}
/**
* @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;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getAll() {
$cities = $this->mapper->getAll($this->userId);
$home = $this->settingsMapper->getHome($this->userId);
return new JSONResponse(array(
"cities" => $cities,
"userid" => $this->userId,
"home" => $home
));
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function add ($name) {
if (!$name) {
return new JSONResponse(array(), Http::STATUS_BAD_REQUEST);
}
// 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);
}
}
$cityInfos = $this->getCityInformations($name);
if (!$cityInfos["response"]) {
return new JSONResponse($cityInfos, $cityInfos["code"]);
}
if ($id = $this->mapper->create($this->userId, $name)) {
// 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)
);
}
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);
}
2016-04-24 20:16:29 +00:00
$entity = new CityEntity();
$entity->setId($id);
$entity->setUser_id($this->userId);
$this->mapper->delete($entity);
return new JSONResponse(array("deleted" => true));
}
private function getCityInformations ($name) {
2017-07-03 21:12:50 +00:00
$apiKey = $this->config->getAppValue($this->appName, 'openweathermap_api_key');
$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],
true);
2019-11-08 18:29:02 +00:00
// If no cod we just return a 502 as the API is not responding properly
2022-12-25 20:15:27 +00:00
if (!array_key_exists('cod', $cityDatas)) {
2019-11-08 18:29:02 +00:00
return array("code" => 502, "response" => null);
}
if ($cityDatas['cod'] != '200') {
2017-07-03 21:12:50 +00:00
return array("code" => $cityDatas['cod'], "response" => null, "apikey" => $apiKey);
}
2017-05-02 10:52:18 +00:00
return array("code" => 200, "response" => $cityDatas);
}
};
?>