mangepong /
ramverk1-weather
| 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 WeatherCheckController implements ContainerInjectableInterface |
||
| 22 | { |
||
| 23 | use ContainerInjectableTrait; |
||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string $db a sample member variable that gets initialised |
||
| 29 | */ |
||
| 30 | private $db = "not active"; |
||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * The initialize method is optional and will always be called before the |
||
| 36 | * target method/action. This is a convienient method where you could |
||
| 37 | * setup internal properties that are commonly used by several methods. |
||
| 38 | * |
||
| 39 | * @return void |
||
| 40 | */ |
||
| 41 | public function initialize() : void |
||
| 42 | { |
||
| 43 | // Use to initialise member variables. |
||
| 44 | $this->db = "active"; |
||
| 45 | $this->IpCurl = new IpCurl(); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 46 | $this->WeatherCurl = new WeatherCurl(); |
||
|
0 ignored issues
–
show
|
|||
| 47 | } |
||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * This is the index method action, it handles: |
||
| 53 | * ANY METHOD mountpoint |
||
| 54 | * ANY METHOD mountpoint/ |
||
| 55 | * ANY METHOD mountpoint/index |
||
| 56 | * |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | public function indexAction() : object |
||
| 60 | { |
||
| 61 | $title = "Weather Check"; |
||
| 62 | |||
| 63 | $page = $this->di->get("page"); |
||
| 64 | |||
| 65 | if (!empty($_SERVER['HTTP_CLIENT_IP'])) { |
||
| 66 | //ip from share internet |
||
| 67 | $ip = $_SERVER['HTTP_CLIENT_IP']; |
||
| 68 | } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
||
| 69 | //ip pass from proxy |
||
| 70 | $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
||
| 71 | } else { |
||
| 72 | if (isset($_SERVER['REMOTE_ADDR'])) { |
||
| 73 | $ip = $_SERVER['REMOTE_ADDR']; |
||
| 74 | } else { |
||
| 75 | $ip = "8.8.8.8"; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | $page->add("anax/weather/index", [ |
||
| 80 | "ip" => $ip, |
||
| 81 | ]); |
||
| 82 | |||
| 83 | return $page->render([ |
||
| 84 | "title" => $title, |
||
| 85 | ]); |
||
| 86 | } |
||
| 87 | |||
| 88 | |||
| 89 | public function indexActionPost() |
||
| 90 | { |
||
| 91 | $response = $this->di->get("response"); |
||
|
0 ignored issues
–
show
|
|||
| 92 | $request = $this->di->get("request"); |
||
| 93 | $session = $this->di->get("session"); |
||
|
0 ignored issues
–
show
|
|||
| 94 | $ip = $request->getPost("ip"); |
||
| 95 | |||
| 96 | $ipres = $this->IpCurl->curl($ip); |
||
| 97 | $lat = $ipres["latitude"]; |
||
| 98 | $long = $ipres["longitude"]; |
||
| 99 | $weatherres = $this->WeatherCurl->curl($lat, $long); |
||
| 100 | |||
| 101 | if (isset($weatherres[0]["daily"])) { |
||
| 102 | $res = $weatherres; |
||
| 103 | } else { |
||
| 104 | $jsonres = [ |
||
| 105 | "Message" => "No data found for this location!", |
||
| 106 | ]; |
||
| 107 | return [$jsonres]; |
||
| 108 | } |
||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | $title = "Weather Check"; |
||
| 113 | |||
| 114 | $page = $this->di->get("page"); |
||
| 115 | |||
| 116 | $page->add("anax/weather/result", [ |
||
| 117 | "res" => $res, |
||
| 118 | "lat" => $lat, |
||
| 119 | "long" => $long, |
||
| 120 | ]); |
||
| 121 | |||
| 122 | return $page->render([ |
||
| 123 | "title" => $title, |
||
| 124 | ]); |
||
| 125 | } |
||
| 126 | } |
||
| 127 |