1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Weather Controller |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Hepa19\Weather; |
8
|
|
|
|
9
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
10
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Controller for Weather data |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class WeatherController implements ContainerInjectableInterface |
17
|
|
|
{ |
18
|
|
|
use ContainerInjectableTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Index page |
22
|
|
|
* |
23
|
|
|
* @return object |
24
|
|
|
*/ |
25
|
3 |
|
public function indexActionGet() : object |
26
|
|
|
{ |
27
|
3 |
|
$page = $this->di->get("page"); |
28
|
3 |
|
$title = "Väder"; |
29
|
3 |
|
$request = $this->di->get("request"); |
30
|
3 |
|
$location = $request->getGet("location"); |
31
|
3 |
|
$type = $request->getGet("type"); |
32
|
|
|
|
33
|
3 |
|
if ($type == "past") { |
34
|
1 |
|
$past = "checked"; |
35
|
|
|
} else { |
36
|
2 |
|
$future = "checked"; |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
if (empty($location)) { |
40
|
1 |
|
$ipgetcurrent = $this->di->get("ipgetcurrent"); |
41
|
1 |
|
$location = $ipgetcurrent->getIP(); |
42
|
1 |
|
$empty = "no"; |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
$ipstackRes = $this->getIPData($location); |
46
|
|
|
|
47
|
3 |
|
$lon = $ipstackRes["longitude"] ?? null; |
48
|
3 |
|
$lat = $ipstackRes["latitude"] ?? null; |
49
|
|
|
|
50
|
3 |
|
if (!empty($lon) && !empty($lat) && $type == "past") { |
51
|
1 |
|
$weather = $this->di->get("weather"); |
52
|
1 |
|
$weather->setUrls($lat, $lon); |
53
|
1 |
|
$weatherInfo = $this->getMultiData($weather, $lat, $lon); |
54
|
2 |
|
} else if (!empty($lon) && !empty($lat)) { |
55
|
2 |
|
$weather = $this->di->get("weather_prog"); |
56
|
2 |
|
$weather->setUrl($lat, $lon); |
57
|
2 |
|
$weatherInfo = $this->getData($weather, $lat, $lon); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$data = [ |
61
|
3 |
|
"location" => $location ?? null, |
62
|
3 |
|
"longitude" => $lon ?? null, |
63
|
3 |
|
"latitude" => $lat ?? null, |
64
|
3 |
|
"weather" => $weatherInfo ?? null, |
65
|
3 |
|
"checked1" => $future ?? null, |
66
|
3 |
|
"checked2" => $past ?? null, |
67
|
3 |
|
"empty" => $empty ?? null |
68
|
|
|
]; |
69
|
|
|
|
70
|
3 |
|
$page->add("weather/form", $data, "columns-above"); |
71
|
3 |
|
$page->add("", $data, "main"); |
72
|
|
|
|
73
|
3 |
|
if ($location) { |
74
|
3 |
|
$page->add("weather/result", $data, "columns-above"); |
75
|
3 |
|
$page->add("weather/map", $data, "columns-above"); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
return $page->render([ |
79
|
3 |
|
"title" => $title |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get ip data |
87
|
|
|
* |
88
|
|
|
*/ |
89
|
|
|
public function getIPData($location) |
90
|
|
|
{ |
91
|
|
|
$ipstack = $this->di->get("ipstack"); |
92
|
|
|
$ipstack->setUrl($location); |
93
|
|
|
$ipstackRes = $ipstack->getData(); |
94
|
|
|
|
95
|
|
|
return $ipstackRes; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get weather data |
102
|
|
|
* |
103
|
|
|
*/ |
104
|
|
|
public function getData($weather, $lat, $lon) |
105
|
|
|
{ |
106
|
|
|
$weatherInfo = $weather->getData($lat, $lon); |
107
|
|
|
$weatherInfo = $weather->filterFuture($weatherInfo); |
108
|
|
|
|
109
|
|
|
return $weatherInfo; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get weather data multi |
116
|
|
|
* |
117
|
|
|
*/ |
118
|
|
|
public function getMultiData($weather, $lat, $lon) |
119
|
|
|
{ |
120
|
|
|
$weatherInfo = $weather->getMultiData($lat, $lon); |
121
|
|
|
$weatherInfo = $weather->filterPast($weatherInfo); |
122
|
|
|
|
123
|
|
|
return $weatherInfo; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|