1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Osln\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
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
14
|
|
|
*/ |
15
|
|
|
class WeatherController implements ContainerInjectableInterface |
16
|
|
|
{ |
17
|
|
|
use ContainerInjectableTrait; |
18
|
3 |
|
public function initialize() : void |
19
|
|
|
{ |
20
|
3 |
|
$this->config = new LoadConfig(); |
|
|
|
|
21
|
3 |
|
$this->ipvalidate = new Ipvalidate(); |
|
|
|
|
22
|
3 |
|
} |
23
|
|
|
/** |
24
|
|
|
* This is the index method action, it handles: |
25
|
|
|
* ANY METHOD mountpoint |
26
|
|
|
* ANY METHOD mountpoint/ |
27
|
|
|
* ANY METHOD mountpoint/index |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
1 |
|
public function indexAction() : object |
32
|
|
|
{ |
33
|
1 |
|
$page = $this->di->get("page"); |
34
|
1 |
|
$session = $this->di->get("session"); |
35
|
|
|
$data = [ |
36
|
1 |
|
"title" => "Väder", |
37
|
1 |
|
"forecast" => $session->has("forecast") ? $session->get("forecast") : "", |
38
|
1 |
|
"month" => $session->has("month") ? $session->get("month") : "", |
39
|
1 |
|
"error" => $session->has("error") ? $session->get("error") : "", |
40
|
1 |
|
"location" => "", |
41
|
|
|
]; |
42
|
1 |
|
$page->add("osln/weather/default", $data); |
43
|
1 |
|
$session->delete("error"); |
44
|
1 |
|
return $page->render(); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function responseActionGet() |
48
|
|
|
{ |
49
|
1 |
|
$search = $this->di->request->getGet("location"); |
|
|
|
|
50
|
1 |
|
$session = $this->di->get("session"); |
51
|
1 |
|
$session->delete("month"); |
52
|
1 |
|
$url = $this->ipvalidate->getUrl($search); |
53
|
|
|
|
54
|
1 |
|
$data = $this->di->curl->fetch($url); |
|
|
|
|
55
|
1 |
|
$forecast = null; |
56
|
|
|
|
57
|
1 |
|
if (isset($data["latitude"])) { |
58
|
1 |
|
$forecast = $this->di->weather->forecast($data["latitude"], $data["longitude"]); |
|
|
|
|
59
|
1 |
|
} elseif (isset($data[0]["lat"])) { |
60
|
1 |
|
$forecast = $this->di->weather->forecast($data[0]["lat"], $data[0]["lon"]); |
61
|
|
|
} else { |
62
|
1 |
|
$session->set("error", "Could not find this location"); |
63
|
|
|
} |
64
|
1 |
|
$session->set("forecast", $forecast); |
65
|
|
|
|
66
|
1 |
|
$resp = $this->di->get("response"); |
67
|
1 |
|
return $resp->redirect("weather"); |
68
|
|
|
// return [$data]; |
69
|
|
|
} |
70
|
1 |
|
public function previousActionGet() |
71
|
|
|
{ |
72
|
1 |
|
$search = $this->di->request->getGet("location"); |
|
|
|
|
73
|
1 |
|
$session = $this->di->get("session"); |
74
|
1 |
|
$url = $this->ipvalidate->getUrl($search); |
75
|
|
|
|
76
|
1 |
|
$data = $this->di->curl->fetch($url); |
|
|
|
|
77
|
1 |
|
$forecast = null; |
78
|
1 |
|
$month = null; |
79
|
|
|
|
80
|
1 |
|
if (isset($data["latitude"])) { |
81
|
1 |
|
$forecast = $this->di->weather->forecast($data["latitude"], $data["longitude"]); |
|
|
|
|
82
|
1 |
|
$month = $this->di->weather->getLastMonth($data["latitude"], $data["longitude"]); |
83
|
1 |
|
} elseif (isset($data[0]["lat"])) { |
84
|
1 |
|
$forecast = $this->di->weather->forecast($data[0]["lat"], $data[0]["lon"]); |
85
|
1 |
|
$month = $this->di->weather->getLastMonth($data[0]["lat"], $data[0]["lon"]); |
86
|
|
|
} else { |
87
|
1 |
|
$session->set("error", "Could not find this location"); |
88
|
|
|
} |
89
|
|
|
// return [$forecast]; |
90
|
1 |
|
$session->set("forecast", $forecast); |
91
|
1 |
|
$session->set("month", $month); |
92
|
|
|
|
93
|
1 |
|
$resp = $this->di->get("response"); |
94
|
1 |
|
return $resp->redirect("weather"); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|