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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
<?php
$block_name = _("weather.com");
/**
* The Horde_Block_weatherdotcom class provides an applet for the
* portal screen to display weather and forecast data from weather.com
* for a specified location.
*
* $Horde: horde/lib/Block/weatherdotcom.php,v 1.37.4.3 2005/03/16 13:26:31 jan Exp $
*
* @package Horde_Block
*/
class Horde_Block_Horde_weatherdotcom extends Horde_Block {
var $_app = 'horde';
/**
* The title to go in this block.
*
* @return string The title text.
*/
function _title()
{
return _("Weather Forecast");
}
/**
* The parameters to go with this block.
*
* @return array An array containing the parameters.
*/
function _params()
{
if (!@include_once 'Services/Weather.php') {
Horde::logMessage('The weather.com block will not work without Services_Weather from PEAR. Run pear install Services_Weather.',
__FILE__, __LINE__, PEAR_LOG_ERR);
$params = array(
'error' => array(
'type' => 'error',
'name' => _("Error"),
'default' => _("The weather.com block is not available.")
)
);
} elseif (!ini_get('allow_url_fopen')) {
Horde::logMessage('The weather.com block will not work without allow_url_fopen enabled in the PHP configuration.',
__FILE__, __LINE__, PEAR_LOG_ERR);
$params = array(
'error' => array(
'type' => 'error',
'name' => _("Error"),
'default' => _("The weather.com block is not available.")
)
);
} else {
$params = array(
'location' => array(
// 'type' => 'weatherdotcom',
'type' => 'text',
'name' => _("Location"),
'default' => 'Boston, MA'
),
'units' => array(
'type' => 'enum',
'name' => _("Units"),
'default' => 'standard',
'values' => array(
'standard' => _("Standard"),
'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' => array(
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6',
'7' => '7',
'8' => '8'
)
),
);
}
return $params;
}
/**
* The content to go in this block.
*
* @return string The content
*/
function _content()
{
if (!@include_once 'Services/Weather.php') {
Horde::logMessage('The weather.com block will not work without Services_Weather from PEAR. Run pear install Services_Weather.',
__FILE__, __LINE__, PEAR_LOG_ERR);
return _("The weather.com block is not available.");
} elseif (!ini_get('allow_url_fopen')) {
Horde::logMessage('The weather.com block will not work without allow_url_fopen enabled in the PHP configuration.',
__FILE__, __LINE__, PEAR_LOG_ERR);
return _("The weather.com block is not available.");
}
global $conf;
$cacheDir = Horde::getTempDir();
$html = '';
if (empty($this->_params['location'])) {
return _("No location is set.");
}
$weatherDotCom = &Services_Weather::service('WeatherDotCom');
$weatherDotCom->setAccountData(
(isset($conf['weatherdotcom']['partner_id']) ? $conf['weatherdotcom']['partner_id'] : ''),
(isset($conf['weatherdotcom']['license_key']) ? $conf['weatherdotcom']['license_key'] : ''));
if (!$cacheDir) {
return PEAR::raiseError(_("No temporary directory available for cache."), 'horde.error');
} else {
$weatherDotCom->setCache('file', array('cache_dir' => ($cacheDir . '/')));
}
$weatherDotCom->setDateTimeFormat('m.d.Y', 'H:i');
$weatherDotCom->setUnitsFormat($this->_params['units']);
$units = $weatherDotCom->getUnitsFormat();
// If the user entered a zip code for the location, no need to
// search (weather.com accepts zip codes as location IDs).
// The location ID should already have been validated in
// getParams.
$search = (preg_match('/\b(?:\\d{5}(-\\d{5})?)|(?:[A-Z]{4}\\d{4})\b/',
$this->_params['location'], $matches) ?
$matches[0] :
$weatherDotCom->searchLocation($this->_params['location']));
if (is_a($search, 'PEAR_Error')) {
return $search->getmessage();
}
if (is_array($search)) {
// Several locations returned due to imprecise location
// parameter.
$html = _("Several locations possible with the parameter: ");
$html .= $this->_params['location'];
$html .= "<br/><ul>";
foreach ($search as $id_weather=>$real_location) {
$html .= "<li>$real_location ($id_weather)</li>\n";
}
$html .= "</ul>";
return $html;
}
$location = $weatherDotCom->getLocation($search);
if (is_a($location, 'PEAR_Error')) {
return $location->getmessage();
}
$weather = $weatherDotCom->getWeather($search);
if (is_a($weather, 'PEAR_Error')) {
return $weather->getmessage();
}
$forecast = $weatherDotCom->getForecast($search, $this->_params['days']);
if (is_a($forecast, 'PEAR_Error')) {
return $forecast->getmessage();
}
// Location and local time.
$html .= '<div class="control">';
$html .= '<b>' . $location['name'] . '</b> ' . _("Local time: ") . $location['time'];
$html .= '</div>';
// Sunrise/sunset.
$html .= '<b>' . _("Sunrise: ") . '</b>' .
Horde::img('block/sunrise/sunrise.png', _("Sunrise")) .
$location['sunrise'];
$html .= ' <b>' . _("Sunset: ") . '</b>' .
Horde::img('block/sunrise/sunset.png', _("Sunset")) .
$location['sunset'];
// Temperature.
$html .= '<br /><b>' . _("Temperature: ") . '</b>';
$html .= round($weather['temperature']) . '°' . String::upper($units['temp']);
// Dew point.
$html .= ' <b>' . _("Dew point: ") . '</b>';
$html .= round($weather['dewPoint']) . '°' . String::upper($units['temp']);
// Feels like temperature.
$html .= ' <b>' . _("Feels like: ") . '</b>';
$html .= round($weather['feltTemperature']) . '°' . String::upper($units['temp']);
// Pressure and trend.
$html .= '<br /><b>' . _("Pressure: ") . '</b>';
$trend = $weather['pressureTrend'];
$html .= sprintf(_("%d %s and %s"),
round($weather['pressure']), $units['pres'],
_($trend));
// Wind.
$html .= '<br /><b>' . _("Wind: ") . '</b>';
if ($weather['windDirection'] == 'VAR') {
$html .= _("Variable");
} elseif ($weather['windDirection'] == 'CALM') {
$html .= _("Calm");
} else {
$html .= _("From the ") . $weather['windDirection'];
$html .= ' (' . $weather['windDegrees'] . ')';
}
$html .= _(" at ") . round($weather['wind']) . ' ' . $units['wind'];
// Humidity.
$html .= '<br /><b>' . _("Humidity: ") . '</b>';
$html .= $weather['humidity'] . '%';
// Visibility.
$html .= ' <b>' . _("Visibility: ") . '</b>';
$html .= is_numeric($weather['visibility']) ?
round($weather['visibility']) . ' ' . $units['vis'] :
$weather['visibility'];
// UV index.
$html .= ' <b>' . _("U.V. index: ") . '</b>';
$uv = $weather['uvText'];
$html .= $weather['uvIndex'] . ' - ' . _($uv);
// Current condition.
$condition = implode(' / ', array_map('_', explode(' / ', $weather['condition'])));
$html .= '<br /><b>' . _("Current condition: ") . '</b>' .
Horde::img('block/weatherdotcom/32x32/' .
($weather['conditionIcon'] == '-' ? 'na' : $weather['conditionIcon']) . '.png',
$condition,
'align="middle"');
$html .= ' ' . $condition;
// Do the forecast now.
$html .= '<div class="control" style="text-align:center"><b>' . sprintf(_("%d-day forecast"), $this->_params['days'])
. '</b></div>';
$futureDays = 0;
$html .= '<table cellspacing="3" cellpadding="1" width="100%"><tr>';
foreach ($forecast['days'] as $which => $day) {
$html .= '<td align="center" style="vertical-align:top; border:1px solid #dddddd; width:' . round(100 / count($forecast['days'])) . '%;">';
// Day name.
$html .= '<b>';
if ($which == 0) {
$html .= _("Today");
} elseif ($which == 1) {
$html .= _("Tomorrow");
} else {
$html .= strftime('%A', mktime(0, 0, 0, date('m'), date('d') + $futureDays, date('Y')));
}
$html .= '</b><br />';
$futureDays++;
// High/low temp. If after 2 p.m. local time, the "day"
// forecast is no longer valid.
if ($which > 0 || ($which == 0 &&
(strtotime($location['time']) < strtotime('14:00')))) {
$html .= '<span style="color:red">' . round($day['temperatureHigh']) .
'°' . String::upper($units['temp']) . '</span>/';
}
$html .= '<span style="color:blue">' . round($day['temperatureLow']) .
'°' . String::upper($units['temp']) . '</span>';
$html .= '<div>';
// Day forecast.
$html .= '<span style="text-align:center; float:left; width:50%;">';
if ($which > 0 || ($which == 0 &&
(strtotime($location['time']) < strtotime('14:00')))) {
$condition = implode(' / ', array_map('_', explode(' / ', $day['day']['condition'])));
$html .= '<b><i>' . _("Day") . '</i></b><br/>';
$html .= Horde::img('block/weatherdotcom/23x23/' .
($day['day']['conditionIcon'] == '-' ? 'na' : $day['day']['conditionIcon']) . '.png',
$condition);
$html .= '<br />' . $condition;
} else {
$html .= ' ';
}
$html .= '</span>';
// Night forecast.
$html .= '<span style="text-align:center; float:right; width:50%;">';
$html .= '<b><i>' . _("Night") . '</i></b><br/>';
$night = implode(' / ', array_map('_', explode(' / ', $day['night']['condition'])));
$html .= Horde::img('block/weatherdotcom/23x23/' .
($day['night']['conditionIcon'] == '-' ? 'na' : $day['night']['conditionIcon']) . '.png',
$night);
$html .= '<br />' . $night;
$html .= '</span>';
$html .= '</div></td>';
}
$html .= '</tr></table>';
// Display a bar at the bottom of the block with the required
// attribution to weather.com and the logo, both linked to
// weather.com with the partner ID.
$html .= '<div style="text-align:right;" class="linedRow">';
$html .= _("Weather data provided by") . ' ';
$html .= Horde::link('http://www.weather.com/?prod=xoap&par=' .
$weatherDotCom->_partnerID,
'weather.com', '', '_blank', '', 'weather.com');
$html .= '<i>weather.com</i>® ';
$html .= Horde::img('block/weatherdotcom/32x32/TWClogo_32px.png',
'weather.com logo', 'align="middle"');
$html .= '</a></div>';
return $html;
}
/**
* Possible strings from weather.com, just to have them in the
* source for localization reasons. This method should never be
* called (and doesn't need to be); it's purpose is to ensure that
* xgettext has a place to pull these strings from.
*/
function defineGettextStrings()
{
_("AM Rain");
_("AM Showers");
_("AM Snow Showers");
_("Clear");
_("Cloudy");
_("Fair");
_("Few Showers");
_("Few Snow Showers");
_("Fog");
_("Haze");
_("Isolated T-Storms");
_("Light Drizzle");
_("Light Rain");
_("Light Rain Early");
_("Light Rain Late");
_("Light Rain Shower");
_("Light Rain with Thunder");
_("Light Snow");
_("Light Snow Shower");
_("Mostly Clear");
_("Mostly Cloudy");
_("Mostly Sunny");
_("Partly Cloudy");
_("PM Light Rain");
_("PM Showers");
_("PM Snow");
_("PM Snow Showers");
_("PM T-Storms");
_("Rain");
_("Rain Early");
_("Rain Late");
_("Rain Shower");
_("Rain and Snow");
_("Rain to Snow");
_("Showers");
_("Showers Early");
_("Showers Late");
_("Showers in the Vicinity");
_("Snow");
_("Snow Shower");
_("Snow Showers");
_("Snow Showers Early");
_("Snow Showers Late");
_("Sunny");
_("T-Storm");
_("T-Storms");
_("Thunder");
_("Wind");
_("Wind Early");
_("Low");
_("Moderate");
_("High");
_("Very High");
_("falling");
_("steady");
_("rising");
}
}
|