1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jodn14\Weather; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
use Jodn14\Models\Validator; |
8
|
|
|
use Jodn14\Models\Locator; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Weather controller that 1) brings the user forecasts for the next 7 days |
12
|
|
|
* 2) Shows what the weather's been like for the past 30 days |
13
|
|
|
* Does so by taking IP-address or City name and calling Dark Sky API |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class WeatherController implements ContainerInjectableInterface |
17
|
|
|
{ |
18
|
|
|
use ContainerInjectableTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string $db a sample member variable that gets initialised |
22
|
|
|
*/ |
23
|
|
|
private $db = "not active"; |
24
|
|
|
private $validator; |
25
|
|
|
private $locator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The initialize method is optional and will always be called before the |
29
|
|
|
* target method/action. This is a convienient method where you could |
30
|
|
|
* setup internal properties that are commonly used by several methods. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
4 |
|
public function initialize() : void |
35
|
|
|
{ |
36
|
|
|
// Use to initialise member variables. |
37
|
4 |
|
$this->db = "active"; |
38
|
4 |
|
$this->validator = new Validator(); |
39
|
4 |
|
$this->locator = new Locator(); |
40
|
4 |
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Display index with input form |
46
|
|
|
* |
47
|
|
|
* @return object |
48
|
|
|
*/ |
49
|
1 |
|
public function indexAction() : object |
50
|
|
|
{ |
51
|
1 |
|
$title = "Weather"; |
52
|
1 |
|
$request = $this->di->get("request"); |
53
|
|
|
|
54
|
|
|
$data = [ |
55
|
1 |
|
"userIp" => $request->getServer("REMOTE_ADDR"), |
56
|
|
|
]; |
57
|
|
|
|
58
|
1 |
|
$page = $this->di->get("page"); |
59
|
1 |
|
$page->add("anax/weather/index", $data); |
60
|
|
|
|
61
|
1 |
|
return $page->render([ |
62
|
1 |
|
"title" => $title, |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Display index with json input form |
68
|
|
|
* |
69
|
|
|
* @return object |
70
|
|
|
*/ |
71
|
1 |
|
public function jsonAction() : object |
72
|
|
|
{ |
73
|
1 |
|
$title = "JSON Weather Service"; |
74
|
1 |
|
$request = $this->di->get("request"); |
75
|
|
|
|
76
|
|
|
$data = [ |
77
|
1 |
|
"userIp" => $request->getServer("REMOTE_ADDR"), |
78
|
|
|
]; |
79
|
|
|
|
80
|
1 |
|
$page = $this->di->get("page"); |
81
|
1 |
|
$page->add("anax/weather/json", $data); |
82
|
|
|
|
83
|
1 |
|
return $page->render([ |
84
|
1 |
|
"title" => $title, |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* POST form processing, get ip and lookup weather |
90
|
|
|
* |
91
|
|
|
* @return object |
92
|
|
|
*/ |
93
|
1 |
|
public function pastActionPost() : object |
94
|
|
|
{ |
95
|
1 |
|
$title = "Weather Service"; |
96
|
1 |
|
$request = $this->di->get("request"); |
97
|
1 |
|
$page = $this->di->get("page"); |
98
|
1 |
|
$curl = $this->di->get("curl"); |
99
|
1 |
|
$userInput = $request->getPost("userInput"); |
100
|
|
|
|
101
|
1 |
|
$valid = $this->validator->validateIp($userInput); |
102
|
1 |
|
if ($valid["hostname"] == "undefined host") { |
103
|
1 |
|
$location = $curl->getCoordinates($userInput); |
104
|
|
|
|
105
|
1 |
|
if (in_array(400, $location)) { |
106
|
1 |
|
if ($location["code"] == 400) { |
107
|
1 |
|
$page->add("anax/weather/invalid", $location); |
108
|
|
|
|
109
|
1 |
|
return $page->render([ |
110
|
1 |
|
"title" => $title, |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
1 |
|
$latitude = $location["lat"]; |
115
|
1 |
|
$longitud = $location["lon"]; |
116
|
|
|
} else { |
117
|
1 |
|
$location = $this->locator->getLocation($userInput); |
118
|
1 |
|
$longitud = $location["longitud"]; |
119
|
1 |
|
$latitude = $location["latitude"]; |
120
|
1 |
|
$userInput = $location["city"]; |
121
|
|
|
} |
122
|
1 |
|
$weather = $curl->getPast($latitude, $longitud); |
123
|
|
|
|
124
|
|
|
$data = [ |
125
|
1 |
|
"city" => $userInput, |
126
|
1 |
|
"latitude" => $latitude ?? null, |
127
|
1 |
|
"longitud" => $longitud ?? null, |
128
|
1 |
|
"weather" => $weather, |
129
|
|
|
]; |
130
|
|
|
|
131
|
1 |
|
$page->add("anax/weather/past", $data); |
132
|
|
|
|
133
|
1 |
|
return $page->render([ |
134
|
1 |
|
"title" => $title, |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* POST form processing, get ip and lookup weather |
140
|
|
|
* |
141
|
|
|
* @return object |
142
|
|
|
*/ |
143
|
1 |
|
public function forecastActionPost() : object |
144
|
|
|
{ |
145
|
1 |
|
$title = "Weather Service"; |
146
|
1 |
|
$request = $this->di->get("request"); |
147
|
1 |
|
$page = $this->di->get("page"); |
148
|
1 |
|
$curl = $this->di->get("curl"); |
149
|
1 |
|
$userInput = $request->getPost("userInput"); |
150
|
|
|
|
151
|
1 |
|
$valid = $this->validator->validateIp($userInput); |
152
|
1 |
|
if ($valid["hostname"] == "undefined host") { |
153
|
1 |
|
$location = $curl->getCoordinates($userInput); |
154
|
|
|
|
155
|
1 |
|
if (in_array(400, $location)) { |
156
|
1 |
|
if ($location["code"] == 400) { |
157
|
1 |
|
$page->add("anax/weather/invalid", $location); |
158
|
|
|
|
159
|
1 |
|
return $page->render([ |
160
|
1 |
|
"title" => $title, |
161
|
|
|
]); |
162
|
|
|
} |
163
|
|
|
} |
164
|
1 |
|
$latitude = $location["lat"]; |
165
|
1 |
|
$longitud = $location["lon"]; |
166
|
|
|
} else { |
167
|
1 |
|
$location = $this->locator->getLocation($userInput); |
168
|
1 |
|
$longitud = $location["longitud"]; |
169
|
1 |
|
$latitude = $location["latitude"]; |
170
|
1 |
|
$userInput = $location["city"]; |
171
|
|
|
} |
172
|
1 |
|
$weather = $curl->getForecast($latitude, $longitud); |
173
|
|
|
|
174
|
|
|
$info = [ |
175
|
1 |
|
"city" => $userInput, |
176
|
1 |
|
"latitude" => $latitude ?? null, |
177
|
1 |
|
"longitud" => $longitud ?? null, |
178
|
|
|
]; |
179
|
|
|
|
180
|
1 |
|
$data = array_merge($valid, array_merge($weather, $info)); |
181
|
|
|
|
182
|
1 |
|
$page->add("anax/weather/forecast", $data); |
183
|
|
|
|
184
|
1 |
|
return $page->render([ |
185
|
1 |
|
"title" => $title, |
186
|
|
|
]); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|