1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Linder\Controller; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
use Linder\Model\Coordinates; |
8
|
|
|
use Linder\Model\DarkSky; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A controller that handles a get request |
12
|
|
|
* and returns if its a valid ip or not. |
13
|
|
|
*/ |
14
|
|
|
class WeatherController implements ContainerInjectableInterface |
15
|
|
|
{ |
16
|
|
|
use ContainerInjectableTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This is the index method action, it handles: |
20
|
|
|
* |
21
|
|
|
* @return object |
22
|
|
|
*/ |
23
|
1 |
|
public function indexAction() : object |
24
|
|
|
{ |
25
|
1 |
|
$page = $this->di->get("page"); |
26
|
|
|
|
27
|
1 |
|
$search = $this->di->get("request")->getGet("search"); |
28
|
1 |
|
$default = "57.708870,11.974560"; |
29
|
|
|
|
30
|
1 |
|
if (filter_var($search, FILTER_VALIDATE_IP)) { |
31
|
1 |
|
$res = $this->di->get("ipverifier")->getJson($search); |
32
|
1 |
|
$res = ((!$res["latitude"]) || (!$res["longitude"])) ? "" : $res["latitude"] . "," . $res["longitude"]; |
33
|
|
|
} else { |
34
|
1 |
|
$geocoder = $this->di->get("geocoder"); |
35
|
1 |
|
$coords = new Coordinates($geocoder); |
36
|
1 |
|
$res = $coords->getCoordinates($search ?? $default); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
$error = ($res == "") ? 'Din sökning "' . $search . '" gav inget resultat, visar Göteborg istället.' : ""; |
40
|
1 |
|
$latlon = ($res == "") ? $default : $res; |
41
|
|
|
|
42
|
1 |
|
$darksky = $this->di->get("darksky"); |
43
|
1 |
|
$type = $this->di->get("request")->getGet("type"); |
44
|
1 |
|
if ($type == "past") { |
45
|
1 |
|
$res = $darksky->getWeatherPast($latlon); |
46
|
|
|
} else { |
47
|
1 |
|
$res = $darksky->getWeatherComing($latlon); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$data = [ |
51
|
1 |
|
"latlon" => $latlon, |
52
|
1 |
|
"res" => $res, |
53
|
1 |
|
"error" => $error |
54
|
|
|
]; |
55
|
|
|
|
56
|
1 |
|
$page->add("weather/main", $data); |
57
|
|
|
|
58
|
1 |
|
return $page->render([ |
59
|
1 |
|
"title" => "Weather", |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|