forked from derfl0/WeatherWidget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeatherWidgetPlugin.class.php
86 lines (73 loc) · 2.89 KB
/
WeatherWidgetPlugin.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* WeatherWidgetPlugin.class.php
*
* Widget that shows you the Weather
*
* @author Florian Bieringer <florian.bieringer@uni-passau.de>
* @version 1.0
* @author Anna Kirpichnikova <a.Kirpichnikova@stud.hs-wismar.de> and Jakob Diel <jakob.diel@hs-wismar.de>
* @version 1.1
*/
class WeatherWidgetPlugin extends StudIPPlugin implements PortalPlugin {
const APIKEY = '02f7c0bdaae418cfad0f061298b3f8c3';
const URL = "http://api.openweathermap.org/data/2.5/weather?q=";
const FORECAST_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?q=";
const LOCATION = "passau";
const LANGUAGE = "&lang=";
const CACHENAME = "plugin/weatherwidget";
public static $weather;
public static $forecast;
public function getWeather() {
// Check class cache
if (self::$weather != null) {
return json_decode(self::$weather);
}
// Check application cache
$cache = StudipCacheFactory::getCache();
if (!$data = $cache->read(self::CACHENAME . "/current")) {
ini_set('default_socket_timeout', 2);
$handle = fopen(self::URL . self::LOCATION . self::LANGUAGE . trim($_SESSION['_language'], '_DEN') . '&APPID=' . self::APIKEY, "r");
if ($handle) {
$data = fgets($handle);
}
if ($data) {
$cache->write(self::CACHENAME . "/current", $data, 300);
self::$weather = $data;
}
}
return json_decode($data);
}
public function getForecast() {
bindtextdomain('wetter', $this->getPluginPath()."/locale");
// Check class cache
if (self::$forecast != null) {
return json_decode(self::$forecast);
}
// Check application cache
$cache = StudipCacheFactory::getCache();
if (!$data = $cache->read(self::CACHENAME . "/forecast")) {
ini_set('default_socket_timeout', 2);
$handle = fopen(self::FORECAST_URL . self::LOCATION . self::LANGUAGE . trim($_SESSION['_language'], '_DEN') . '&APPID=' . self::APIKEY, "r");
if ($handle) {
$data = fgets($handle);
}
if ($data) {
$cache->write(self::CACHENAME . "/forecast", $data, 300);
self::$weather = $data;
}
}
return json_decode($data);
}
public function getPluginName() {
return dgettext('wetter','Wetter in') . ' ' . self::getWeather()->name;
}
public function getPortalTemplate() {
$templatefactory = new Flexi_TemplateFactory(__DIR__ . "/templates");
$template = $templatefactory->open("index.php");
$template->set_attribute("data", self::getWeather());
$template->set_attribute("forecast", self::getForecast());
$template->set_attribute("iconUrl", $this->getPluginURL() . '/icons/');
return $template;
}
}