hellemarck /
ramverk1-weather_module
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Anax\Controller; |
||
| 4 | |||
| 5 | use Anax\Commons\ContainerInjectableInterface; |
||
| 6 | use Anax\Commons\ContainerInjectableTrait; |
||
| 7 | use Anax\Models\GeoApi; |
||
| 8 | use Anax\Models\CurrentIp; |
||
| 9 | use Anax\Models\WeatherApi; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Controllerclass for weather forecast and history |
||
| 13 | */ |
||
| 14 | class WeatherController implements ContainerInjectableInterface |
||
| 15 | { |
||
| 16 | use ContainerInjectableTrait; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * rendering index page for user to type ip address or coordinates |
||
| 20 | * with current ip-address as default |
||
| 21 | */ |
||
| 22 | 1 | public function indexAction() |
|
| 23 | { |
||
| 24 | 1 | $page = $this->di->get("page"); |
|
| 25 | 1 | $title = "Väderprognoser"; |
|
| 26 | |||
| 27 | 1 | $userIP = $this->di->get("currentip"); |
|
| 28 | |||
| 29 | 1 | $page->add("weather/index", $userIP); |
|
| 30 | 1 | return $page->render([ |
|
| 31 | 1 | "title" => $title, |
|
| 32 | ]); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * 7 day weather forecast / 5 days history - using the model WeatherAPI |
||
| 37 | * takes user input as "search" and sends it to model, returns to view |
||
| 38 | */ |
||
| 39 | public function searchAction() |
||
| 40 | { |
||
| 41 | $page = $this->di->get("page"); |
||
| 42 | $search = $_GET["location"]; |
||
| 43 | |||
| 44 | $type = $this->di->get("request")->getGet("type"); |
||
| 45 | $search = str_replace(' ', '', $search); |
||
| 46 | |||
| 47 | $weatherObj = $this->di->get("weatherapi"); |
||
| 48 | $geoObj = $this->di->get("geoapi"); |
||
| 49 | |||
| 50 | if (strpos($search, ",") == true) { |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 51 | $split = explode(",", $search); |
||
| 52 | if (!ctype_alpha($split[0]) && !ctype_alpha($split[1])) { |
||
| 53 | $weatherObj->setCoordinates($split[0], $split[1]); |
||
| 54 | $weather = ($type == "coming") ? ($weatherObj->comingWeather($split[0], $split[1])) : $weatherObj->pastWeather($split[0], $split[1]); |
||
| 55 | } else { |
||
| 56 | $weather = "Felaktig söksträng, försök igen."; |
||
| 57 | } |
||
| 58 | } else { |
||
| 59 | $res = $geoObj->findGeoLocation($search); |
||
| 60 | if ($res["longitude"] !== "-") { |
||
| 61 | $weatherObj->setCoordinates($res["latitude"], $res["longitude"]); |
||
| 62 | $weather = ($type == "coming") ? ($weatherObj->comingWeather($res["latitude"], $res["longitude"])) : $weatherObj->pastWeather($res["latitude"], $res["longitude"]); |
||
| 63 | } else { |
||
| 64 | $weather = "Felaktig söksträng, försök igen."; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | $data = [ |
||
| 69 | "weather" => $weather ?? null, |
||
| 70 | "coordinates" => $weatherObj->getCoordinates() ?? null, |
||
| 71 | "location" => $weatherObj->getLocation() ?? null |
||
| 72 | ]; |
||
| 73 | |||
| 74 | $title = "Resultat"; |
||
| 75 | $page->add("weather/result", $data); |
||
| 76 | |||
| 77 | return $page->render([ |
||
| 78 | "title" => $title, |
||
| 79 | ]); |
||
| 80 | } |
||
| 81 | } |
||
| 82 |