/
Weather.php
185 lines (168 loc) · 5.99 KB
/
Weather.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Portal block for displaying weather information obtained via
* Horde_Service_Weather.
*
* Copyright 2011-2016 Horde LLC (http://www.horde.org/)
*
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @license http://www.horde.org/licenses/bsd BSD
* @package Horde
*/
/**
* Horde_Block_Weather
*
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package Horde
*/
class Horde_Block_Weather extends Horde_Core_Block
{
/**
*/
public $updateable = true;
protected $_refreshParams;
public $autoUpdateMethod = 'refreshContent';
/**
* @var Horde_Service_Weather_Base
*/
protected $_weather;
/**
*/
public function __construct($app, $params = array())
{
global $injector;
parent::__construct($app, $params);
try {
$this->_weather = $injector->getInstance('Horde_Weather');
} catch (Horde_Exception $e) {
$this->enabled = false;
}
$this->_name = _("Weather");
}
/**
* Handle user initiated block refresh. Set a private member to avoid
* BC issues with having to add a parameter to the _content method.
*
* @param Horde_Variables $vars
*
* @return string
*/
public function refreshContent($vars = null)
{
if (empty($vars) || empty($vars->location)) {
$this->_refreshParams = Horde_Variables::getDefaultVariables();
$this->_refreshParams->set('location', $this->_params['location']);
} else {
$this->_refreshParams = $vars;
}
return $this->_content();
}
/**
*/
protected function _title()
{
return _("Weather");
}
/**
*/
protected function _params()
{
$lengths = $this->_weather->getSupportedForecastLengths();
return array(
'location' => array(
'type' => 'text',
'name' => _("Location"),
'default' => 'Boston,MA'
),
'units' => array(
'type' => 'enum',
'name' => _("Units"),
'default' => 'standard',
'values' => array(
Horde_Service_Weather::UNITS_STANDARD => _("English"),
Horde_Service_Weather::UNITS_METRIC => _("Metric")
)
),
'days' => array(
'type' => 'enum',
'name' => _("Forecast Days (note that the returned forecast returns both day and night; a large number here could result in a wide block.)"),
'default' => 3,
'values' => $lengths
),
'detailedForecast' => array(
'type' => 'checkbox',
'name' => _("Display detailed forecast?"),
'default' => 0
),
'showMap' => array(
'type' => 'checkbox',
'name' => _("Display the OpenWeatherMap map?"),
'default' => 0)
);
}
/**
*/
protected function _content()
{
global $injector, $language, $page_output, $prefs, $registry;
// Set the requested units.
$this->_weather->units = $this->_params['units'];
$view = $injector->getInstance('Horde_View');
if (!empty($this->_refreshParams) && !empty($this->_refreshParams->location)) {
$location = $this->_refreshParams->location;
$view->instance = '';
} else {
$view->instance = hash('md5', mt_rand());
$injector->getInstance('Horde_Core_Factory_Imple')->create(
'WeatherLocationAutoCompleter',
array(
'id' => 'location' . $view->instance,
'instance' => $view->instance
)
);
$view->requested_location = $this->_params['location'];
$location = $this->_params['location'];
}
$view->units = $this->_weather->getUnits($this->_weather->units);
$view->params = $this->_params;
$view->link = $this->_weather->link;
$view->title = $this->_weather->title;
if ($this->_weather->logo) {
$view->logo = $this->_weather->logo;
}
// Test location
try {
$view->location = $this->_weather->searchLocations($location);
} catch (Horde_Service_Weather_Exception $e) {
return $e->getMessage();
}
try {
$view->forecast = $this->_weather->getForecast($view->location->code, $this->_params['days']);
$view->station = $this->_weather->getStation();
$view->current = $this->_weather->getCurrentConditions($view->location->code);
// @todo: Add link to put alert text in redbox.
$view->timezone = $prefs->getValue('timezone');
$view->dateFormat = $prefs->getValue('date_format');
$view->timeFormat = $prefs->getValue('time_format');
$view->alerts = $this->_weather->getAlerts($view->location->code);
$view->radar = $this->_weather->getRadarImageUrl($location);
} catch (Horde_Service_Weather_Exception $e) {
return $e->getMessage();
}
$view->languageFilter = '/^'
. $registry->nlsconfig->languages[$language] . ': /i';
if (!empty($this->_params['showMap']) && !empty($view->instance)) {
$view->map = true;
$page_output->addScriptFile('weatherblockmap.js', 'horde');
Horde_Core_HordeMap::init(array('providers' => array('owm', 'osm')));
$page_output->addInlineScript(array(
'WeatherBlockMap.initializeMap("' . $view->instance . '", { lat: "' . $view->location->lat . '", lon: "' . $view->location->lon . '"});$("weathermaplayer_' . $view->instance . '").show();'
), true);
}
if (!empty($view->instance)) {
return $view->render('block/weather');
} else {
return $view->render('block/weather_content');
}
}
}