1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Controller; |
4
|
|
|
|
5
|
|
|
namespace artes\Weather; |
6
|
|
|
|
7
|
|
|
// use Anax\Commons\AppInjectableInterface; |
8
|
|
|
// use Anax\Commons\AppInjectableTrait; |
9
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
10
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
11
|
|
|
use Anax\Route\Exception\NotFoundException; |
12
|
|
|
use artes\IP\IPGeotag; |
13
|
|
|
use artes\IP\RealIP; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A sample controller to show how a controller class can be implemented. |
17
|
|
|
* The controller will be injected with $app if implementing the interface |
18
|
|
|
* AppInjectableInterface, like this sample class does. |
19
|
|
|
* The controller is mounted on a particular route and can then handle all |
20
|
|
|
* requests for that mount point. |
21
|
|
|
* |
22
|
|
|
* @SuppressWarnings(PHPMD) |
23
|
|
|
*/ |
24
|
|
|
class WeatherController implements ContainerInjectableInterface |
25
|
|
|
{ |
26
|
|
|
use ContainerInjectableTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* This is the index method action, it handles: |
30
|
|
|
* ANY METHOD mountpoint |
31
|
|
|
* ANY METHOD mountpoint/ |
32
|
|
|
* ANY METHOD mountpoint/index |
33
|
|
|
* |
34
|
|
|
* @return object |
35
|
|
|
*/ |
36
|
1 |
|
public function indexActionGet() : object |
37
|
|
|
{ |
38
|
1 |
|
$page = $this->di->get("page"); |
39
|
1 |
|
$session = $this->di->get("session"); |
40
|
1 |
|
$realip = new RealIP(); |
41
|
1 |
|
$ipaddress = $realip->getRealIpAddr(); |
42
|
|
|
$data = [ |
43
|
1 |
|
"warning" => $session->get("warning"), |
44
|
1 |
|
"ip" => $ipaddress |
45
|
|
|
]; |
46
|
1 |
|
$session->destroy(); |
47
|
|
|
|
48
|
1 |
|
$page->add( |
49
|
1 |
|
"weather/index", |
50
|
|
|
$data |
51
|
|
|
); |
52
|
|
|
|
53
|
1 |
|
return $page->render([ |
54
|
1 |
|
"title" => "Weather", |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* This is the index method action, it handles: |
60
|
|
|
* ANY METHOD mountpoint |
61
|
|
|
* ANY METHOD mountpoint/ |
62
|
|
|
* ANY METHOD mountpoint/index |
63
|
|
|
* |
64
|
|
|
* @return object |
65
|
|
|
*/ |
66
|
1 |
|
public function indexActionPost() : object |
67
|
|
|
{ |
68
|
1 |
|
$ip = $this->di->get("ip"); |
69
|
1 |
|
$request = $this->di->get("request"); |
70
|
1 |
|
$response = $this->di->get("response"); |
71
|
1 |
|
$session = $this->di->get("session"); |
72
|
1 |
|
$validator = new ValidWeather($request, $ip); |
73
|
1 |
|
if ($validator->errormsg()) { |
74
|
1 |
|
$session->set("warning", $validator->errormsg()); |
75
|
1 |
|
return $response->redirect("weather"); |
76
|
|
|
} |
77
|
1 |
|
$page = $this->di->get("page"); |
78
|
1 |
|
$lat = $request->getPost("latitud"); |
79
|
1 |
|
$long = $request->getPost("longitud"); |
80
|
1 |
|
$type = $request->getPost("infotyp"); |
81
|
1 |
|
$userip = $request->getPost("userip"); |
82
|
1 |
|
$alldata = $this->generateData($userip, $lat, $long, $type); |
83
|
1 |
|
$data = $alldata[0]; |
84
|
1 |
|
$page->add("weather/info", $data); |
85
|
1 |
|
if (!(($lat|| $alldata[1]) && ($long || $alldata[2]))) { |
86
|
1 |
|
$msg = "<p class='warning'>No geodata could be detected.</p>"; |
87
|
1 |
|
$session->set("warning", $msg); |
88
|
1 |
|
return $response->redirect("weather"); |
89
|
|
|
} |
90
|
|
|
return $page->render(["title" => "Weather"]); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function generateData($userip, $lat, $long, $type) |
94
|
|
|
{ |
95
|
1 |
|
$ipkey = ""; |
96
|
1 |
|
$weatherkey = ""; |
97
|
|
|
// this loads $ipkey and $weatherkey |
98
|
1 |
|
include(ANAX_INSTALL_PATH . '/config/api/apikeys.php'); |
99
|
1 |
|
$geotag = new IPGeotag($ipkey); |
100
|
1 |
|
$geoinfo = ""; |
101
|
1 |
|
if ($userip) { |
102
|
1 |
|
$geoinfo = $geotag->checkdefaultip($userip); |
103
|
1 |
|
$lat = $geoinfo["latitude"] ?? ""; |
104
|
1 |
|
$long = $geoinfo["longitude"] ?? ""; |
105
|
1 |
|
$geoinfo = $geotag->checkinputip($userip); |
106
|
|
|
} |
107
|
1 |
|
$map = $geotag->printmap($lat, $long); |
108
|
1 |
|
$data = $this->getWeather($weatherkey, $lat, $long, $type); |
109
|
1 |
|
$data["map"] = $map; |
110
|
1 |
|
$data["geoinfo"] = $geoinfo; |
111
|
1 |
|
return [$data, $lat, $long]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* This is the index method action, it handles: |
116
|
|
|
* ANY METHOD mountpoint |
117
|
|
|
* ANY METHOD mountpoint/ |
118
|
|
|
* ANY METHOD mountpoint/index |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
2 |
|
public function getWeather($weatherkey, $lat, $long, $type) : array |
123
|
|
|
{ |
124
|
2 |
|
$openweather = new OpenWeather($weatherkey, $lat, $long); |
125
|
2 |
|
$weatherinfo = $openweather->currentweather(); |
126
|
2 |
|
if ($type === "historik") { |
127
|
2 |
|
$historic = $openweather->historicweather(); |
128
|
2 |
|
$forecast = ""; |
129
|
|
|
} else { |
130
|
2 |
|
$forecast = $openweather->forecast(); |
131
|
2 |
|
$historic = ""; |
132
|
|
|
} |
133
|
|
|
$data = [ |
134
|
2 |
|
"weatherinfo" => $weatherinfo, |
135
|
2 |
|
"forecast" => $forecast, |
136
|
2 |
|
"historic" => $historic |
137
|
|
|
]; |
138
|
2 |
|
return $data; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|