/
weather_portal.class.php
166 lines (145 loc) · 7.3 KB
/
weather_portal.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/* Project: EQdkp-Plus
* Package: Weather Portal Module
* Link: http://eqdkp-plus.eu
*
* Copyright (C) 2006-2015 EQdkp-Plus Developer Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if ( !defined('EQDKP_INC') ){
header('HTTP/1.0 404 Not Found');exit;
}
class weather_portal extends portal_generic {
public static $shortcuts = array('puf' => 'urlfetcher');
protected static $path = 'weather';
protected static $data = array(
'name' => 'Weather',
'version' => '4.0.0',
'author' => 'WalleniuM',
'icon' => 'fa-cloud',
'contact' => EQDKP_PROJECT_URL,
'description' => 'Shows the weather',
'lang_prefix' => 'weather_',
'multiple' => true,
);
protected static $positions = array('left1', 'left2', 'right');
protected static $install = array(
'autoenable' => '1',
'defaultposition' => 'left2',
'defaultnumber' => '11',
);
protected static $apiLevel = 20;
public function get_settings($state){
$settings = array(
'tempformat' => array(
'type' => 'dropdown',
'options' => array('C' => 'C','F' => 'F'),
),
'geolocation' => array(
'type' => 'radio',
),
'default_country' => array(
'type' => 'dropdown',
'options' => $this->pdh->get('user', 'country_list'),
),
'default_town' => array(
'type' => 'text',
),
);
return $settings;
}
public function output() {
$this->tpl->js_file($this->root_path.'portal/weather/js/jquery.simpleopenweather.js');
$this->tpl->css_file($this->root_path.'portal/weather/font/font.css');
$this->tpl->add_css("
.weatherCity {
border-bottom:2px #66676c solid;
padding:0px 0px 8px 0px;
text-transform: uppercase;
font-size: 1em;
}
.weatherMiddle{
margin-top:4px;
font-weight:300;
vertical-align: middle;
}
.weatherTemp {
font-size:32px;
margin: 0px 0px 0px 10px;
}
.weatherDesc, .weatherHumidity, .weatherWind {
font-weight: bold;
margin: 0px 0px 8px 0px;
}
.weatherDesc {
margin: 20px 0px 8px 0px;
}
");
$temperature_unit = ($this->config('tempformat') == 'F') ? '°F' : '°C';
$tempformat = ($this->config('tempformat') == 'F') ? 'imperial' : 'metric';
$strCountry = $this->config('default_country');
$strTown = $this->config('default_town');
//Overwrite the Defaults with Userdata
$strUserCountry = $strUserTown = "";
if ($this->user->is_signedin()){
$strUserCountry = $this->pdh->get('user', 'country', array($this->user->id));
if ($strUserCountry != "") $strCountry = $strUserCountry;
$strUserTown = $this->pdh->get('user', 'profilefield_by_name', array($this->user->id, 'location', true));
if ($strUserTown != "") $strTown = $strUserTown;
}
//User has both in his profile
if($strUserCountry != "" && $strUserTown != ""){
$this->tpl->add_js('$("#weather_'.$this->id.'").simpleopenweather({template: \'<div style="display: block;" class="toggle_container"><div class="weatherCity"><i class="fa fa-map-marker fa-fw"></i> {{place}}</div><div class="weatherMiddle">{{icon}}<span class="weatherTemp">{{temperature}} '.$temperature_unit.'</span></div><div class="weatherDesc"><i class="fa fa-tag fa-fw"></i> {{sky}}</div><div class="weatherHumidity"><i class="fa fa-tint fa-fw"></i> {{humidity}} %</div><div class="weatherWind"><i class="fa fa-cloud"></i> {{wind.speed}} m/s, {{wind.direction}} °</div>\', lang:"'.$this->user->lang('XML_LANG').'", units: "'.$tempformat.'", iconfont: true});', "docready");
return '<div id="weather_'.$this->id.'" class="simpleopenweather" data-simpleopenweather-city="'.$strUserTown.', '.$strUserCountry.'"></div>';
} elseif($this->config('geolocation') == '1'){
//User has nothing in profile, let's try geolocation
$js = '
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(locationSuccess, locationError);
}else{
showError("Your browser does not support Geolocation!");
}
function locationSuccess(position) {
$("#weather_'.$this->id.'").simpleopenweather({latitude: position.coords.latitude, longitude: position.coords.longitude, template: \'<div style="display: block;" class="toggle_container"><div class="weatherCity"><i class="fa fa-map-marker fa-fw"></i> {{place}}</div><div class="weatherMiddle">{{icon}}<span class="weatherTemp">{{temperature}} '.$temperature_unit.'</span></div><div class="weatherDesc"><i class="fa fa-tag fa-fw"></i> {{sky}}</div><div class="weatherHumidity"><i class="fa fa-tint fa-fw"></i> {{humidity}} %</div><div class="weatherWind"><i class="fa fa-cloud"></i> {{wind.speed}} m/s, {{wind.direction}} °</div>\', lang:"'.$this->user->lang('XML_LANG').'", units: "'.$tempformat.'", iconfont: true});
}
function showError(msg){
$("#error").html(msg);
}';
if ($strTown != "" && $strCountry != ""){
$js .= '
function locationError(error){
$("#weather_'.$this->id.'").attr("data-simpleopenweather-city", "'.$strTown.', '.$strCountry.'");
$("#weather_'.$this->id.'").simpleopenweather({template: \'<div style="display: block;" class="toggle_container"><div class="weatherCity"><i class="fa fa-map-marker fa-fw"></i> {{place}}</div><div class="weatherMiddle">{{icon}}<span class="weatherTemp">{{temperature}} '.$temperature_unit.'</span></div><div class="weatherDesc"><i class="fa fa-tag fa-fw"></i> {{sky}}</div><div class="weatherHumidity"><i class="fa fa-tint fa-fw"></i> {{humidity}} %</div><div class="weatherWind"><i class="fa fa-cloud"></i> {{wind.speed}} m/s, {{wind.direction}} °</div>\', lang:"'.$this->user->lang('XML_LANG').'", units: "'.$tempformat.'", iconfont: true});
}
';
} else {
$js .= '
function locationError(error){
showError(error);
}
';
}
$this->tpl->add_js($js, "docready");
return '<div id="weather_'.$this->id.'" class="simpleopenweather"></div>';
}elseif($strCountry != "" && $strTown != ""){
//Show the default weather
$this->tpl->add_js('$("#weather_'.$this->id.'").simpleopenweather({template: \'<div style="display: block;" class="toggle_container"><div class="weatherCity"><i class="fa fa-map-marker fa-fw"></i> {{place}}</div><div class="weatherMiddle">{{icon}}<span class="weatherTemp">{{temperature}} '.$temperature_unit.'</span></div><div class="weatherDesc"><i class="fa fa-tag fa-fw"></i> {{sky}}</div><div class="weatherHumidity"><i class="fa fa-tint fa-fw"></i> {{humidity}} %</div><div class="weatherWind"><i class="fa fa-cloud"></i> {{wind.speed}} m/s, {{wind.direction}} °</div>\', lang:"'.$this->user->lang('XML_LANG').'", units: "'.$tempformat.'", iconfont: true});', "docready");
return '<div id="weather_'.$this->id.'" class="simpleopenweather" data-simpleopenweather-city="'.$strTown.', '.$strCountry.'"></div>';
}else {
return $this->user->lang('weather_no_data');
}
}
}
?>