* @license http://www.horde.org/licenses/bsd BSD * @package Horde */ /** * Horde_Block_Weather * * @author Michael J Rubinsky * @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 ) ); } /** */ protected function _content() { // Set the requested units. $this->_weather->units = $this->_params['units']; if (!empty($this->_refreshParams) && !empty($this->_refreshParams->location)) { $location = $this->_refreshParams->location; $html = ''; $instance = ''; } else { $instance = hash('md5', mt_rand()); $GLOBALS['injector'] ->getInstance('Horde_Core_Factory_Imple') ->create( 'WeatherLocationAutoCompleter', array( 'id' => 'location' . $instance, 'instance' => $instance ) ); $html = '
'; $location = $this->_params['location']; } // Test location try { $location = $this->_weather->searchLocations($location); } catch (Horde_Service_Weather_Exception $e) { return $e->getMessage(); } $html .= '
'; if (is_array($location)) { // Several locations returned due to imprecise location parameter. $html = sprintf(_("Several locations possible with the parameter: %s"), $this->_params['location']) . '
'; foreach ($location as $real_location) { $html .= '
  • ' . $real_location->city . ', ' . $real_location->state . '(' . $real_location->code . ")
  • \n"; } $html .= ''; return $html; } try { $forecast = $this->_weather->getForecast($location->code, $this->_params['days']); $station = $this->_weather->getStation(); $current = $this->_weather->getCurrentConditions($location->code); } catch (Horde_Service_Weather_Exception $e) { return $e->getMessage(); } // Units to display as $units = $this->_weather->getUnits($this->_weather->units); // Location and local time. $html .= '
    ' . '' . $station->name . ''; if ($current->time->timestamp()) { $html .= ' ' . sprintf(_("Local time: %s %s (UTC %s)"), $current->time->strftime($GLOBALS['prefs']->getValue('date_format')), $current->time->strftime($GLOBALS['prefs']->getValue('time_format')), $station->getOffset()); } $html .= '
    '; $html .= '
    '; // Sunrise/sunset. if ($station->sunrise) { $html .= '' . _("Sunrise") . ': ' . Horde_Themes_Image::tag('block/sunrise/sunrise.png', array('alt' => _("Sunrise"))) . sprintf("%s %s", $station->sunrise->strftime($GLOBALS['prefs']->getValue('date_format')), $station->sunrise->strftime($GLOBALS['prefs']->getValue('time_format'))); $html .= ' ' . _("Sunset") . ': ' . Horde_Themes_Image::tag('block/sunrise/sunset.png', array('alt' => _("Sunset"))) . sprintf("%s %s", $station->sunset->strftime($GLOBALS['prefs']->getValue('date_format')), $station->sunset->strftime($GLOBALS['prefs']->getValue('time_format'))); $html .= '
    '; } // Temperature. $html .= '' . _("Temperature") . ': ' . $current->temp . '°' . Horde_String::upper($units['temp']); // Dew point. if (is_numeric($current->dewpoint)) { $html .= ' ' . _("Dew point") . ': ' . round($current->dewpoint) . '°' . Horde_String::upper($units['temp']); } // Feels like temperature. // @TODO: Need to parse if wind chill/heat index etc.. // $html .= ' ' . _("Feels like: ") . '' . // round($this->_weather['feltTemperature']) . '°' . Horde_String::upper($units['temp']); // Pressure and trend. if ($current->pressure) { $html .= '
    ' . _("Pressure") . ': '; $trend = $current->pressure_trend; if (empty($trend)) { $html .= sprintf('%d %s', round($current->pressure), $units['pres']); } else { $html .= sprintf(_("%d %s and %s"), round($current->pressure), $units['pres'], _($trend)); } } if ($current->wind_direction) { // Wind. $html .= '
    ' . _("Wind") . ': '; $html .= sprintf( _("From the %s (%s °) at %s %s"), $current->wind_direction, $current->wind_degrees, $current->wind_speed, $units['wind']); if ($current->wind_gust > 0) { $html .= ', ' . _("gusting") . ' ' . $current->wind_gust . ' ' . $units['wind']; } } // Humidity. if ($current->humidity) { $html .= '
    ' . _("Humidity") . ': ' . $current->humidity; } if ($current->visibility) { // Visibility. $html .= ' ' . _("Visibility") . ': ' . round($current->visibility) . ' ' . $units['vis']; } // Current condition. $condition = $current->condition; $html .= '
    ' . _("Current condition") . ': ' . Horde_Themes_Image::tag('weather/32x32/' . $current->icon) . ' ' . $condition . '
    '; // Forecast if ($this->_params['days'] > 0) { $html .= '
    ' . sprintf(_("%d-day forecast"), $this->_params['days']) . '
    '; $futureDays = 0; $html .= ''; // Headers. $html .= ''; $html .= ''; if (isset($this->_params['detailedForecast'])) { if (in_array(Horde_Service_Weather::FORECAST_FIELD_PRECIPITATION, $forecast->fields)) { $html .= ''; } if (in_array(Horde_Service_Weather::FORECAST_FIELD_HUMIDITY, $forecast->fields)) { $html .= ''; } if (in_array(Horde_Service_Weather::FORECAST_FIELD_WIND, $forecast->fields)) { $html .= ''; } } $html .= ''; $which = -1; foreach ($forecast as $day) { $which++; if ($which > $this->_params['days']) { break; } $html .= ''; // Day name. $html .= ''; // Forecast condition. $condition = $day->conditions; // Temperature. $html .= ''; // Condition. $html .= ''; if (isset($this->_params['detailedForecast'])) { if (in_array(Horde_Service_Weather::FORECAST_FIELD_PRECIPITATION, $forecast->fields)) { $html .= ''; } if (in_array(Horde_Service_Weather::FORECAST_FIELD_HUMIDITY, $forecast->fields)) { $html .= ''; } if (in_array(Horde_Service_Weather::FORECAST_FIELD_WIND, $forecast->fields)) { // Winds. if ($day->wind_direction) { $html .= ''; } else { $html .= ''; } } } $html .= ''; $futureDays++; } $html .= '
    ' . _("Day") . '' . sprintf(_("Temperature%s(%sHi%s/%sLo%s)"), '
    ', '', '', '', '') . '
    ' . _("Condition") . '' . sprintf(_("Precipitation%schance"), '
    ') . '
    ' . _("Humidity") . '' . _("Wind") . '
    '; 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 .= '
    ' . strftime('%b %d', mktime(0, 0, 0, date('m'), date('d') + $futureDays, date('Y'))) . '
    ' . '' . $day->high . '°' . Horde_String::upper($units['temp']) . '/' . '' . $day->low . '°' . Horde_String::upper($units['temp']) . '' . Horde_Themes_Image::tag('weather/32x32/' . $day->icon) . '
    ' . $condition . '
    ' . ($day->precipitation_percent >= 0 ? $day->precipitation_percent . '%' : _("N/A")) . '' . ($day->humidity ? $day->humidity . '%': _("N/A")) . '' . ' ' . sprintf(_("From the %s at %s %s"), $day->wind_direction, $day->wind_speed, $units['wind']); if ($day->wind_gust && $day->wind_gust > $day->wind_speed) { $html .= ', ' . _("gusting") . ' ' . $day->wind_gust . ' ' . $units['wind']; } $html .= '' . _("N/A") . '
    '; } if ($this->_weather->logo) { $html .= '
    ' . _("Weather data provided by") . ' ' . Horde::link( Horde::externalUrl($this->_weather->link), $this->_weather->title, '', '_blank', '', $this->_weather->title) . Horde_Themes_Image::tag($this->_weather->logo) . '
    '; } else { $html .= '
    ' . _("Weather data provided by") . ' ' . Horde::link( Horde::externalUrl($this->_weather->link), $this->_weather->title, '', '_blank', '', $this->_weather->title) . '' . $this->_weather->title . '' . '
    '; } return $html . '
    '; } }