1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\WeatherService; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
class WeatherController implements ContainerInjectableInterface |
9
|
|
|
{ |
10
|
|
|
use ContainerInjectableTrait; |
11
|
|
|
|
12
|
1 |
|
public function indexActionGet() |
13
|
|
|
{ |
14
|
1 |
|
$title = "Forecast from IP"; |
15
|
|
|
|
16
|
1 |
|
$request = $this->di->get("request"); |
17
|
1 |
|
$page = $this->di->get("page"); |
18
|
|
|
|
19
|
1 |
|
$validIp = $request->getGet("ip") ?? "true"; |
20
|
|
|
|
21
|
|
|
// Get users IP-address from SERVER |
22
|
1 |
|
$ip = $request->getServer("HTTP_X_FORWARDED_FOR"); |
23
|
1 |
|
$page->add("weather/index", [ |
24
|
1 |
|
"ip" => $ip, |
25
|
1 |
|
"valid" => $validIp |
26
|
|
|
]); |
27
|
|
|
|
28
|
1 |
|
return $page->render([ |
29
|
1 |
|
"title" => $title, |
30
|
|
|
]); |
31
|
|
|
} |
32
|
|
|
|
33
|
3 |
|
public function ipActionGet() |
34
|
|
|
{ |
35
|
3 |
|
$di = $this->di; |
36
|
3 |
|
$request = $di->get("request"); |
37
|
3 |
|
$response = $di->get("response"); |
38
|
3 |
|
$page = $di->get("page"); |
39
|
3 |
|
$weather = $di->get("weather"); |
40
|
|
|
|
41
|
3 |
|
$ip = $request->getGet("ip"); |
42
|
3 |
|
$history = $request->getGet("history") ?? null; |
43
|
|
|
|
44
|
3 |
|
if ($history) { |
45
|
1 |
|
return $response->redirect("weather/history?ip=" . $ip); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
$weather->setLocation($ip); |
49
|
|
|
|
50
|
2 |
|
$res = $weather->getForecast(); |
51
|
|
|
|
52
|
2 |
|
if (isset($res["error"])) { |
53
|
1 |
|
return $response->redirect("weather?ip=false"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$data = [ |
57
|
1 |
|
"res" => $res |
58
|
|
|
]; |
59
|
|
|
|
60
|
1 |
|
$page->add("weather/weather", $data); |
61
|
|
|
|
62
|
1 |
|
return $page->render([ |
63
|
1 |
|
"title" => "Local weather" |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function historyActionGet() |
68
|
|
|
{ |
69
|
2 |
|
$di = $this->di; |
70
|
2 |
|
$request = $di->get("request"); |
71
|
2 |
|
$response = $di->get("response"); |
72
|
2 |
|
$page = $di->get("page"); |
73
|
2 |
|
$ip = $request->getGet("ip"); |
74
|
2 |
|
$weather = $di->get("weather"); |
75
|
|
|
|
76
|
2 |
|
$weather->setLocation($ip); |
77
|
2 |
|
$res = $weather->getOldCast(); |
78
|
|
|
|
79
|
2 |
|
if (isset($res["error"])) { |
80
|
1 |
|
return $response->redirect("weather?ip=false"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$data = [ |
84
|
1 |
|
"res" => $res |
85
|
|
|
]; |
86
|
|
|
|
87
|
1 |
|
$page->add("weather/history", $data); |
88
|
|
|
|
89
|
1 |
|
return $page->render([ |
90
|
1 |
|
"title" => "Local weather" |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|