|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Blixter\Weather; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
|
|
8
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
|
9
|
|
|
// use Anax\Route\Exception\NotFoundException; |
|
10
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A sample controller to show how a controller class can be implemented. |
|
14
|
|
|
* The controller will be injected with $di if implementing the interface |
|
15
|
|
|
* ContainerInjectableInterface, like this sample class does. |
|
16
|
|
|
* The controller is mounted on a particular route and can then handle all |
|
17
|
|
|
* requests for that mount point. |
|
18
|
|
|
* |
|
19
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
20
|
|
|
*/ |
|
21
|
|
|
class WeatherController implements ContainerInjectableInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use ContainerInjectableTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This is the index method action, it handles: |
|
27
|
|
|
* GET METHOD mountpoint |
|
28
|
|
|
* GET METHOD mountpoint/ |
|
29
|
|
|
* GET METHOD mountpoint/index |
|
30
|
|
|
* |
|
31
|
|
|
* @return object |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function indexActionGet(): object |
|
34
|
|
|
{ |
|
35
|
|
|
// Add content as a view and then render the page |
|
36
|
1 |
|
$page = $this->di->get("page"); |
|
37
|
1 |
|
$title = "Väder"; |
|
38
|
|
|
|
|
39
|
|
|
$data = [ |
|
40
|
1 |
|
"title" => $title, |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
1 |
|
$page->add("blixter/weather/header", $data); |
|
44
|
1 |
|
$page->add("blixter/weather/search-location", $data); |
|
45
|
|
|
|
|
46
|
|
|
// Deal with the action and return a response. |
|
47
|
1 |
|
return $page->render([ |
|
48
|
1 |
|
"title" => $title, |
|
49
|
|
|
]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* This is the index method action, it handles: |
|
54
|
|
|
* POST METHOD mountpoint |
|
55
|
|
|
* POST METHOD mountpoint/ |
|
56
|
|
|
* POST METHOD mountpoint/index |
|
57
|
|
|
* |
|
58
|
|
|
* @return object |
|
59
|
|
|
*/ |
|
60
|
4 |
|
public function indexActionPost(): object |
|
61
|
|
|
{ |
|
62
|
|
|
// Add content as a view and then render the page |
|
63
|
4 |
|
$page = $this->di->get("page"); |
|
64
|
4 |
|
$request = $this->di->get("request"); |
|
65
|
4 |
|
$location = $request->getPost("location"); |
|
66
|
|
|
// Using ipValidation class from $di. |
|
67
|
4 |
|
$ipValidation = $this->di->get("ipvalidation"); |
|
68
|
4 |
|
$isIpValid = $ipValidation->isIpValid($location); |
|
69
|
4 |
|
$ipGeoModel = $this->di->get("ipgeo"); |
|
70
|
4 |
|
$curlhandler = $this->di->get("curlhandler"); |
|
71
|
4 |
|
$weatherModel = $this->di->get("weather"); |
|
72
|
4 |
|
$weatherModel->setCurl($curlhandler); |
|
73
|
4 |
|
$ipGeoModel->setCurl($curlhandler); |
|
74
|
4 |
|
$isLocationValid = $weatherModel->getCoordinates($location); |
|
75
|
4 |
|
$coords = null; |
|
76
|
|
|
|
|
77
|
4 |
|
if ($isIpValid) { |
|
78
|
1 |
|
$apiRes = $ipGeoModel->fetchData($location); |
|
79
|
|
|
$coords = [ |
|
80
|
1 |
|
"lon" => $apiRes["longitude"] ?? null, |
|
81
|
1 |
|
"lat" => $apiRes["latitude"] ?? null, |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
4 |
|
if ($isLocationValid) { |
|
86
|
2 |
|
$coords = $isLocationValid; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
if ($coords) { |
|
90
|
3 |
|
$weatherChoice = $request->getPost("radiochoice"); |
|
91
|
3 |
|
if ($weatherChoice === "upcoming") { |
|
92
|
1 |
|
$weatherData = $weatherModel->fetchData($coords); |
|
93
|
2 |
|
} else if ($weatherChoice === "past") { |
|
94
|
1 |
|
$weatherData = $weatherModel->fetchDataMulti($coords); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
4 |
|
$title = "Väderprognos"; |
|
99
|
|
|
|
|
100
|
|
|
$data = [ |
|
101
|
4 |
|
"title" => $title, |
|
102
|
4 |
|
"weatherData" => $weatherData ?? null, |
|
103
|
4 |
|
"coords" => $coords ?? null, |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
4 |
|
$page->add("blixter/weather/header", $data); |
|
107
|
4 |
|
$page->add("blixter/weather/weather-result", $data); |
|
108
|
|
|
|
|
109
|
|
|
// Deal with the action and return a response. |
|
110
|
4 |
|
return $page->render([ |
|
111
|
4 |
|
"title" => $title, |
|
112
|
|
|
]); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|