|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
use Anax\Weather\Weather; |
|
8
|
|
|
use Anax\IpGeo\IpGeo; |
|
9
|
|
|
|
|
10
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
|
11
|
|
|
// use Anax\Route\Exception\NotFoundException; |
|
12
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A sample controller to show how a controller class can be implemented. |
|
16
|
|
|
* The controller will be injected with $di if implementing the interface |
|
17
|
|
|
* ContainerInjectableInterface, like this sample class does. |
|
18
|
|
|
* The controller is mounted on a particular route and can then handle all |
|
19
|
|
|
* requests for that mount point. |
|
20
|
|
|
* |
|
21
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
22
|
|
|
*/ |
|
23
|
|
|
class WeatherController implements ContainerInjectableInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use ContainerInjectableTrait; |
|
26
|
|
|
protected $weather; |
|
27
|
|
|
protected $ipGeo; |
|
28
|
|
|
protected $request; |
|
29
|
|
|
protected $response; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string $db a sample member variable that gets initialised |
|
35
|
|
|
*/ |
|
36
|
|
|
/** |
|
37
|
|
|
* The initialize method is optional and will always be called before the |
|
38
|
|
|
* target method/action. This is a convienient method where you could |
|
39
|
|
|
* setup internal properties that are commonly used by several methods. |
|
40
|
|
|
* |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
10 |
|
public function initialize(): void |
|
44
|
|
|
{ |
|
45
|
|
|
// Use to initialise member variables. |
|
46
|
10 |
|
$this->weather = new Weather($this->di); |
|
47
|
10 |
|
$this->ipGeo = new IpGeo(); |
|
48
|
10 |
|
$this->request = $this->di->get("request"); |
|
49
|
10 |
|
$this->response = $this->di->get("response"); |
|
50
|
10 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* This is the index method action, it handles: |
|
56
|
|
|
* ANY METHOD mountpoint |
|
57
|
|
|
* ANY METHOD mountpoint/ |
|
58
|
|
|
* ANY METHOD mountpoint/index |
|
59
|
|
|
* |
|
60
|
|
|
* @return object |
|
61
|
|
|
*/ |
|
62
|
7 |
|
public function indexAction(): object |
|
63
|
|
|
{ |
|
64
|
7 |
|
$page = $this->di->get("page"); |
|
65
|
7 |
|
$title = "Väderprognos"; |
|
66
|
7 |
|
$ipAddress = $this->request->getGet("ip"); |
|
67
|
7 |
|
$city = $this->request->getGet("city"); |
|
68
|
7 |
|
$searchType = $this->request->getGet("search_type"); |
|
69
|
|
|
// $lat = $this->request->getGet("lat"); |
|
70
|
|
|
// $long = $this->request->getGet("long"); |
|
71
|
|
|
|
|
72
|
7 |
|
if ($city) { |
|
73
|
1 |
|
$res = $this->weather->getCoords($city); |
|
74
|
1 |
|
$lat = $res["lat"]; |
|
75
|
1 |
|
$long = $res["long"]; |
|
76
|
6 |
|
} elseif ($ipAddress) { |
|
77
|
3 |
|
$ipInfo = $this->ipGeo->getLocation($ipAddress); |
|
78
|
3 |
|
$lat = $ipInfo["lat"]; |
|
79
|
3 |
|
$long = $ipInfo["long"]; |
|
80
|
|
|
} else { |
|
81
|
3 |
|
$req = $this->di->get("request"); |
|
82
|
3 |
|
if (!empty($req->getServer("HTTP_CLIENT_IP"))) { |
|
83
|
1 |
|
$ipAddress = $req->getServer("HTTP_CLIENT_IP"); |
|
84
|
2 |
|
} elseif (!empty($req->getServer("HTTP_X_FORWARDED_FOR"))) { |
|
85
|
1 |
|
$ipAddress = $req->getServer("HTTP_X_FORWARDED_FOR"); |
|
86
|
|
|
} else { |
|
87
|
1 |
|
$ipAddress = $req->getServer("REMOTE_ADDR"); |
|
88
|
|
|
} |
|
89
|
3 |
|
$ipInfo = $this->ipGeo->getLocation($ipAddress); |
|
90
|
3 |
|
$lat = $ipInfo["lat"]; |
|
91
|
3 |
|
$long = $ipInfo["long"]; |
|
92
|
|
|
} |
|
93
|
7 |
|
if ($lat == "Missing") { |
|
94
|
4 |
|
$page->add("weather/index", [ |
|
95
|
4 |
|
"err" => "Oops, platsinformation saknas", |
|
96
|
4 |
|
"title" => $title, |
|
97
|
4 |
|
"ip" => $ipAddress, |
|
98
|
4 |
|
"weather" => "", |
|
99
|
|
|
]); |
|
100
|
|
|
|
|
101
|
4 |
|
return $page->render(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
3 |
|
$weatherInfo = $this->weather->getWeather($lat, $long, $searchType); |
|
105
|
|
|
|
|
106
|
3 |
|
$page->add("weather/index", [ |
|
107
|
3 |
|
"weather" => $weatherInfo, |
|
108
|
3 |
|
"ip" => $ipAddress, |
|
109
|
3 |
|
"title" => $title, |
|
110
|
|
|
]); |
|
111
|
3 |
|
if ($searchType == "history") { |
|
112
|
1 |
|
$page->add("weather/history", [ |
|
113
|
1 |
|
"weather" => $weatherInfo["history"], |
|
114
|
1 |
|
"city" => $weatherInfo["city"], |
|
115
|
|
|
]); |
|
116
|
2 |
|
} elseif ($searchType == "forecast") { |
|
117
|
1 |
|
$page->add("weather/weather", [ |
|
118
|
1 |
|
"weather" => $weatherInfo["daily"]["data"], |
|
119
|
1 |
|
"city" => $weatherInfo["city"], |
|
120
|
|
|
]); |
|
121
|
1 |
|
} elseif ($searchType == "currently") { |
|
122
|
1 |
|
$page->add("weather/current", [ |
|
123
|
1 |
|
"weather" => $weatherInfo, |
|
124
|
|
|
]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
3 |
|
return $page->render(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* This sample method action it the handler for route: |
|
135
|
|
|
* POST mountpoint/create |
|
136
|
|
|
* |
|
137
|
|
|
* @return object |
|
138
|
|
|
*/ |
|
139
|
2 |
|
public function indexActionPost(): object |
|
140
|
|
|
{ |
|
141
|
2 |
|
$searchType = $this->request->getPost("search_type"); |
|
142
|
2 |
|
$city = $this->request->getPost("city"); |
|
143
|
2 |
|
if ($city) { |
|
144
|
1 |
|
return $this->response->redirect("weather?city=$city&search_type=$searchType"); |
|
145
|
|
|
} |
|
146
|
1 |
|
$ipAddress = $this->request->getPost("ipAddress"); |
|
147
|
|
|
|
|
148
|
1 |
|
return $this->response->redirect("weather?ip=$ipAddress&search_type=$searchType"); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* This sample method action it the handler for route: |
|
153
|
|
|
* GET mountpoint/create |
|
154
|
|
|
* |
|
155
|
|
|
* @return object |
|
156
|
|
|
*/ |
|
157
|
1 |
|
public function docActionGet(): object |
|
158
|
|
|
{ |
|
159
|
1 |
|
$page = $this->di->get("page"); |
|
160
|
1 |
|
$title = "Väder API dokumentation"; |
|
161
|
1 |
|
$page->add("weather/doc", [ |
|
162
|
1 |
|
"title" => $title, |
|
163
|
|
|
]); |
|
164
|
1 |
|
return $page->render(); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|