1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Anax\Controller;
|
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
|
|
|
/**
|
27
|
|
|
* This is the index method action, it handles:
|
28
|
|
|
* ANY METHOD mountpoint
|
29
|
|
|
* ANY METHOD mountpoint/
|
30
|
|
|
* ANY METHOD mountpoint/index
|
31
|
|
|
*
|
32
|
|
|
* @return string
|
33
|
|
|
*/
|
34
|
1 |
|
public function indexActionGet() : object
|
35
|
|
|
{
|
36
|
1 |
|
$page = $this->di->get("page");
|
37
|
|
|
|
38
|
|
|
$data = [
|
39
|
1 |
|
"error" => $this->di->session->getOnce("error") ?? null,
|
|
|
|
|
40
|
1 |
|
"when" => $this->di->session->getOnce("when") ?? null,
|
41
|
1 |
|
"search" => $this->di->session->getOnce("search") ?? null,
|
42
|
1 |
|
"temp" => $this->di->session->getOnce("temp") ?? null,
|
43
|
1 |
|
"weather" => $this->di->session->getOnce("weather") ?? null,
|
44
|
1 |
|
"time" => $this->di->session->getOnce("time") ?? null,
|
45
|
1 |
|
"lon" => $this->di->session->getOnce("lon") ?? null,
|
46
|
1 |
|
"lat" => $this->di->session->getOnce("lat") ?? null,
|
47
|
|
|
];
|
48
|
1 |
|
$page->add("weather/geoweather", $data);
|
49
|
|
|
|
50
|
1 |
|
return $page->render();
|
51
|
|
|
}
|
52
|
1 |
|
public function indexActionPost()
|
53
|
|
|
{
|
54
|
1 |
|
$response = $this->di->get("response");
|
55
|
1 |
|
$search = $this->di->get("request")->getPost("search");
|
56
|
1 |
|
$when = $this->di->get("request")->getPost("when");
|
57
|
1 |
|
$this->di->session->set("search", $search);
|
|
|
|
|
58
|
1 |
|
$weather = $this->di->get("weather");
|
59
|
|
|
|
60
|
|
|
|
61
|
1 |
|
$ip = $this->di->request->getGet("search");
|
|
|
|
|
62
|
1 |
|
$validator = new \Anax\Model\ipValidation;
|
63
|
1 |
|
$res = $validator->toJson($ip);
|
64
|
|
|
|
65
|
1 |
|
if ($res["valid"] == "valid IP"){
|
66
|
|
|
$locationInfo = $weather->getLocationData($res["city"]);
|
67
|
|
|
} else {
|
68
|
1 |
|
if (!isset($search)){
|
69
|
1 |
|
$locationInfo = $weather->getLocationData("");
|
70
|
|
|
} else{
|
71
|
|
|
$locationInfo = $weather->getLocationData($search);
|
72
|
|
|
}
|
73
|
|
|
}
|
74
|
1 |
|
$lon = $locationInfo["lon"] ?? null;
|
75
|
1 |
|
$lat = $locationInfo["lat"] ?? null;
|
76
|
1 |
|
$this->di->session->set("lon", $lon);
|
77
|
1 |
|
$this->di->session->set("lat", $lat);
|
78
|
1 |
|
$error = $locationInfo["error"] ?? null;
|
79
|
1 |
|
$this->di->session->set("error", $error);
|
80
|
|
|
|
81
|
1 |
|
if ($locationInfo["error"] == null){
|
82
|
|
|
$weatherInfo = $weather->getWeather($when,$lon,$lat);
|
83
|
|
|
$time = $weatherInfo["time"] ?? null;
|
84
|
|
|
$this->di->session->set("time", $time);
|
85
|
|
|
|
86
|
|
|
$temp = $weatherInfo["temp"] ?? null;
|
87
|
|
|
$this->di->session->set("temp", $temp);
|
88
|
|
|
|
89
|
|
|
$weather = $weatherInfo["weather"] ?? null;
|
90
|
|
|
$this->di->session->set("weather", $weather);
|
91
|
|
|
|
92
|
|
|
$error = $weatherInfo["error"] ?? null;
|
93
|
|
|
$this->di->session->set("error", $error);
|
94
|
|
|
}
|
95
|
1 |
|
return $response->redirect("weather");
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
}
|
99
|
|
|
|