1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jenel\Weather; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* WeatherController |
10
|
|
|
* get weather forecast from location. |
11
|
|
|
* |
12
|
|
|
* uses $di "weather" -> WeatherModel |
13
|
|
|
* uses $di "curl -> curl functions |
14
|
|
|
*/ |
15
|
|
|
class WeatherController implements ContainerInjectableInterface |
16
|
|
|
{ |
17
|
|
|
use ContainerInjectableTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* variables. |
21
|
|
|
*/ |
22
|
|
|
private $WeatherModel; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Initlize the object |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
5 |
|
public function initialize() : void |
30
|
|
|
{ |
31
|
5 |
|
$this->WeatherModel = $this->di->get("weather"); |
32
|
|
|
|
33
|
|
|
//inject the model with $di service |
34
|
5 |
|
$this->WeatherModel->setCurl($this->di->get("curl")); |
35
|
5 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Route for index of WeatherController |
39
|
|
|
* GET |
40
|
|
|
* mountpoint weather/ |
41
|
|
|
* mountpoint weather/index |
42
|
|
|
* |
43
|
|
|
* @return object webpage with start content. |
44
|
|
|
*/ |
45
|
2 |
|
public function indexActionGet() : object |
46
|
|
|
{ |
47
|
2 |
|
$title = "Weather"; |
48
|
2 |
|
$page = $this->di->get("page"); |
49
|
|
|
|
50
|
|
|
$data = [ |
51
|
2 |
|
"title" => $title, |
52
|
2 |
|
"content" => "", |
53
|
2 |
|
"position" => "" |
54
|
|
|
]; |
55
|
|
|
|
56
|
2 |
|
$page->add("weather/index", $data); |
57
|
2 |
|
$error = $this->di->get("session")->getOnce("error"); |
58
|
2 |
|
if ($error) { |
59
|
1 |
|
$page->add("weather/error", ["error" => "No valid geografic position. Try again."]); |
60
|
|
|
}; |
61
|
2 |
|
$page->add("weather/form", []); |
62
|
2 |
|
return $page->render(["title" => $title]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Route for index |
67
|
|
|
* POST |
68
|
|
|
*/ |
69
|
2 |
|
public function indexActionPost() : object |
70
|
|
|
{ |
71
|
2 |
|
$page = $this->di->get("page"); |
72
|
2 |
|
$request = $this->di->get("request"); |
73
|
|
|
|
74
|
2 |
|
$weatherModel = $this->WeatherModel; |
75
|
2 |
|
$location = $request->getPost("location"); |
76
|
2 |
|
$geo = $weatherModel->getGeo($location); |
77
|
|
|
|
78
|
2 |
|
if (empty($location) || empty($geo)) { |
79
|
1 |
|
$response = $this->di->get("response"); |
80
|
1 |
|
$this->di->get("session")->set("error", true); |
81
|
1 |
|
return $response->redirect("weather/"); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
$radio = $request->getPost("radio"); |
85
|
1 |
|
$data = $weatherModel->getAll($radio); |
86
|
|
|
|
87
|
1 |
|
$page->add("weather/index", $data); |
88
|
1 |
|
$page->add("weather/header", []); |
89
|
1 |
|
$page->add("weather/map", ["lat" =>$data['lat'], "lon" => $data['lon']]); |
90
|
1 |
|
$page->add("weather/result", $data['result']); |
91
|
1 |
|
return $page->render($data); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Route for api |
96
|
|
|
* mountpoint /api |
97
|
|
|
*/ |
98
|
1 |
|
public function apiActionGet() : object |
99
|
|
|
{ |
100
|
|
|
$data = [ |
101
|
1 |
|
"title" => "API" |
102
|
|
|
]; |
103
|
|
|
|
104
|
1 |
|
$this->di->get("page")->add("weather/api", $data); |
105
|
1 |
|
return $this->di->get("page")->render($data); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|