|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mahw17\Weather; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Controller to handle user login. |
|
10
|
|
|
*/ |
|
11
|
|
|
class WeatherController implements ContainerInjectableInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use ContainerInjectableTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Index page with user form. |
|
17
|
|
|
* |
|
18
|
|
|
* @return object |
|
19
|
|
|
*/ |
|
20
|
1 |
|
public function indexActionGet() : object |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
// Load framework services |
|
24
|
1 |
|
$page = $this->di->get("page"); |
|
25
|
1 |
|
$session = $this->di->get("session"); |
|
26
|
|
|
|
|
27
|
|
|
// Get IP-object from weather service |
|
28
|
1 |
|
$ipObj = $this->di->get("weather")->ipObj; |
|
29
|
|
|
|
|
30
|
|
|
// Set navbar active |
|
31
|
1 |
|
$session->set('navbar', 'weather'); |
|
32
|
|
|
|
|
33
|
|
|
// Collect data |
|
34
|
1 |
|
$currentIp = $ipObj->getIpAddress(); |
|
35
|
|
|
|
|
36
|
|
|
$data = [ |
|
37
|
1 |
|
"title" => "Väder | ramverk1", |
|
38
|
1 |
|
"intro_mount" => 'Väder', |
|
39
|
1 |
|
"intro_path" => 'formulär', |
|
40
|
1 |
|
"currentIp" => $currentIp |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
// Add and render views |
|
44
|
1 |
|
$page->add("mahw17/intro/subintro", $data, "subintro"); |
|
45
|
1 |
|
$page->add("mahw17/weather/form", $data, "main"); |
|
46
|
|
|
|
|
47
|
1 |
|
return $page->render($data); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Retrive posted form. Check input. Based on input render either forecast or weather history |
|
52
|
|
|
* |
|
53
|
|
|
*/ |
|
54
|
3 |
|
public function indexActionPost($test = false) |
|
55
|
|
|
{ |
|
56
|
|
|
// Load framework services |
|
57
|
3 |
|
$page = $this->di->get("page"); |
|
58
|
3 |
|
$request = $this->di->get("request"); |
|
59
|
3 |
|
$response = $this->di->get("response"); |
|
60
|
3 |
|
$session = $this->di->get("session"); |
|
61
|
3 |
|
$weather = $this->di->get("weather"); |
|
62
|
|
|
|
|
63
|
|
|
// Get Ip-object from weather service |
|
64
|
3 |
|
$ipValidation = $weather->ipObj; |
|
65
|
|
|
|
|
66
|
|
|
// Validate coord (from posted) |
|
67
|
3 |
|
$coord = $request->getPost('coord', null); |
|
68
|
3 |
|
$coord = $weather->validateCoord($coord); |
|
69
|
|
|
|
|
70
|
|
|
// If none valide coords by post check posted ip-address for coords |
|
71
|
3 |
|
if (!$coord) { |
|
72
|
1 |
|
$ipAddress = $request->getPost('ip', null); |
|
73
|
1 |
|
$coord = $ipValidation->validateCoord($ipAddress); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// If neither coords by post or thru the ip is valid |
|
77
|
3 |
|
if (!$coord) { |
|
78
|
|
|
// Error message! |
|
79
|
1 |
|
return $response->redirect("error/weather"); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Type of data (forecast/history) |
|
83
|
2 |
|
$weaterType = $request->getPost('weather', null); |
|
84
|
|
|
|
|
85
|
|
|
// Fetch API-resultset |
|
86
|
2 |
|
$weatherInfo = null; |
|
87
|
2 |
|
$day = time(); |
|
88
|
2 |
|
if ($weaterType === "forecast") { |
|
89
|
1 |
|
$weatherInfo = $weather->weatherForecast($coord); |
|
90
|
|
|
} else { |
|
91
|
1 |
|
$weatherInfo = $weather->weatherHistory($day, $coord); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Set navbar active |
|
95
|
2 |
|
$session->set('navbar', 'weather'); |
|
96
|
|
|
|
|
97
|
|
|
// Collect data |
|
98
|
|
|
$data = [ |
|
99
|
2 |
|
"title" => "Väder | ramverk1", |
|
100
|
2 |
|
"intro_mount" => 'Prognos', |
|
101
|
2 |
|
"intro_path" => 'resultat', |
|
102
|
2 |
|
"weatherType" => $weaterType, |
|
103
|
2 |
|
"weatherInfo" => $weatherInfo |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
|
|
// Add and render views |
|
107
|
2 |
|
$page->add("mahw17/intro/subintro", $data, "subintro"); |
|
108
|
2 |
|
$page->add("mahw17/weather/result", $data, "main"); |
|
109
|
|
|
|
|
110
|
2 |
|
return $test ? $data : $page->render($data); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* This is the index method action, it handles: |
|
115
|
|
|
* GET METHOD mountpoint |
|
116
|
|
|
* GET METHOD mountpoint/ |
|
117
|
|
|
* GET METHOD mountpoint/index |
|
118
|
|
|
* |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function forecastActionGet($source = 'ip', $param1 = '194.103.20.10', $param2 = null) : array |
|
122
|
|
|
{ |
|
123
|
|
|
// Load framework services |
|
124
|
1 |
|
$weather = $this->di->get("weather"); |
|
125
|
|
|
|
|
126
|
|
|
// Input as IP-address => convert to coordinates |
|
127
|
1 |
|
if (strtolower($source) === "ip") { |
|
128
|
|
|
// Load framework services |
|
129
|
1 |
|
$ipValidation = $weather->ipObj; |
|
130
|
1 |
|
$results = $ipValidation->validateIp($param1); |
|
131
|
|
|
|
|
132
|
1 |
|
if ($results['valid']) { |
|
133
|
|
|
$info = $ipValidation->ipInfo($param1); |
|
134
|
|
|
$param1 = $info->latitude; |
|
135
|
|
|
$param2 = $info->longitude; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$coordinates = [ |
|
140
|
1 |
|
"lat" => $param1, |
|
141
|
1 |
|
"lon" => $param2 |
|
142
|
|
|
]; |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
1 |
|
$results = $weather->weatherForecast($coordinates); |
|
146
|
|
|
|
|
147
|
|
|
// Deal with the action and return a response. |
|
148
|
|
|
$json = [ |
|
149
|
1 |
|
'data' => $results |
|
150
|
|
|
]; |
|
151
|
1 |
|
return [$json]; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* This is the index method action, it handles: |
|
156
|
|
|
* GET METHOD mountpoint |
|
157
|
|
|
* GET METHOD mountpoint/ |
|
158
|
|
|
* GET METHOD mountpoint/index |
|
159
|
|
|
* |
|
160
|
|
|
* @return array |
|
161
|
|
|
*/ |
|
162
|
1 |
|
public function historyActionGet($day = 1542818124, $source = 'ip', $param1 = '194.103.20.10', $param2 = null) : array |
|
163
|
|
|
{ |
|
164
|
|
|
// Load framework services |
|
165
|
1 |
|
$weather = $this->di->get("weather"); |
|
166
|
|
|
|
|
167
|
|
|
// Collect data |
|
168
|
|
|
// Input as IP-address => convert to coordinates |
|
169
|
1 |
|
if (strtolower($source) === "ip") { |
|
170
|
|
|
// Load framework services |
|
171
|
1 |
|
$ipValidation = $weather->ipObj; |
|
172
|
1 |
|
$results = $ipValidation->validateIp($param1); |
|
173
|
|
|
|
|
174
|
1 |
|
if ($results['valid']) { |
|
175
|
|
|
$info = $ipValidation->ipInfo($param1); |
|
176
|
|
|
$param1 = $info->latitude; |
|
177
|
|
|
$param2 = $info->longitude; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$coordinates = [ |
|
182
|
1 |
|
"lat" => $param1, |
|
183
|
1 |
|
"lon" => $param2 |
|
184
|
|
|
]; |
|
185
|
|
|
|
|
186
|
|
|
|
|
187
|
1 |
|
$results = $weather->weatherHistory($day, $coordinates); |
|
188
|
|
|
|
|
189
|
|
|
// Deal with the action and return a response. |
|
190
|
|
|
$json = [ |
|
191
|
1 |
|
'data' => $results |
|
192
|
|
|
]; |
|
193
|
1 |
|
return [$json]; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|