[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Block/ -> weatherdotcom.php (source)

   1  <?php
   2  
   3  /* Disable block if not configured. */
   4  if (!empty($GLOBALS['conf']['weatherdotcom']['partner_id']) &&
   5      !empty($GLOBALS['conf']['weatherdotcom']['license_key'])) {
   6      $block_name = _("weather.com");
   7  }
   8  
   9  /**
  10   * The Horde_Block_weatherdotcom class provides an applet for the
  11   * portal screen to display weather and forecast data from weather.com
  12   * for a specified location.
  13   *
  14   * $Horde: horde/lib/Block/weatherdotcom.php,v 1.37.4.13 2005/12/05 03:41:00 chuck Exp $
  15   *
  16   * @package Horde_Block
  17   */
  18  class Horde_Block_Horde_weatherdotcom extends Horde_Block {
  19  
  20      var $_app = 'horde';
  21  
  22      /**
  23       * The title to go in this block.
  24       *
  25       * @return string   The title text.
  26       */
  27      function _title()
  28      {
  29          return _("Weather Forecast");
  30      }
  31  
  32      /**
  33       * The parameters to go with this block.
  34       *
  35       * @return array  An array containing the parameters.
  36       */
  37      function _params()
  38      {
  39          if (!(@include_once 'Services/Weather.php') ||
  40              !(@include_once 'Cache.php') ||
  41              !(@include_once 'XML/Serializer.php') ||
  42              !ini_get('allow_url_fopen')) {
  43              Horde::logMessage('The weather.com block will not work without PEAR\'s Services_Weather, Cache, and XML_ Serializer packages, and allow_url_fopen enabled. Run `pear install Services_Weather Cache XML_Serializer´ and ensure that allow_url_fopen is enabled in php.ini.',
  44                                __FILE__, __LINE__, PEAR_LOG_ERR);
  45              $params = array(
  46                  'error' => array(
  47                      'type' => 'error',
  48                      'name' => _("Error"),
  49                      'default' => _("The weather.com block is not available.")
  50                  )
  51              );
  52          } else {
  53              $params = array(
  54                  'location' => array(
  55                      // 'type' => 'weatherdotcom',
  56                      'type' => 'text',
  57                      'name' => _("Location"),
  58                      'default' => 'Boston, MA'
  59                  ),
  60                  'units' => array(
  61                      'type' => 'enum',
  62                      'name' => _("Units"),
  63                      'default' => 'standard',
  64                      '0' => 'none',
  65                      'values' => array(
  66                          'standard' => _("Standard"),
  67                          'metric' => _("Metric")
  68                      )
  69                  ),
  70                  'days' => array(
  71                      'type' => 'enum',
  72                      'name' => _("Forecast Days (note that the returned forecast returns both day and night; a large number here could result in a wide block)"),
  73                      'default' => '3',
  74                      'values' => array(
  75                          '1' => '1',
  76                          '2' => '2',
  77                          '3' => '3',
  78                          '4' => '4',
  79                          '5' => '5',
  80                          '6' => '6',
  81                          '7' => '7',
  82                          '8' => '8',
  83                          '9' => '9',
  84                          '10' => '10'
  85                      )
  86                  ),
  87                  'detailedForecast' => array(
  88                      'type' => 'checkbox',
  89                      'name' => _("Display detailed forecast"),
  90                      'default' => 0
  91                  )
  92              );
  93          }
  94  
  95          return $params;
  96      }
  97  
  98      /**
  99       * The content to go in this block.
 100       *
 101       * @return string   The content
 102       */
 103      function _content()
 104      {
 105          if (!(@include_once 'Services/Weather.php') ||
 106              !(@include_once 'Cache.php') ||
 107              !ini_get('allow_url_fopen')) {
 108              Horde::logMessage('The weather.com block will not work without the PEARServices_Weather and Cache packages, and allow_url_fopen enabled. Run pear install Services_Weather Cache, and ensure that allow_url_fopen_wrappers is enabled in php.ini.',
 109                                __FILE__, __LINE__, PEAR_LOG_ERR);
 110              return _("The weather.com block is not available.");
 111          }
 112  
 113          global $conf;
 114  
 115          if (empty($this->_params['location'])) {
 116              return _("No location is set.");
 117          }
 118  
 119          $weatherDotCom = &Services_Weather::service('WeatherDotCom');
 120          $weatherDotCom->setAccountData(
 121              (isset($conf['weatherdotcom']['partner_id']) ? $conf['weatherdotcom']['partner_id'] : ''),
 122              (isset($conf['weatherdotcom']['license_key']) ? $conf['weatherdotcom']['license_key'] : ''));
 123  
 124          $cacheDir = Horde::getTempDir();
 125          if (!$cacheDir) {
 126              return PEAR::raiseError(_("No temporary directory available for cache."), 'horde.error');
 127          } else {
 128              $weatherDotCom->setCache('file', array('cache_dir' => ($cacheDir . '/')));
 129          }
 130          $weatherDotCom->setDateTimeFormat('m.d.Y', 'H:i');
 131          $weatherDotCom->setUnitsFormat($this->_params['units']);
 132          $units = $weatherDotCom->getUnitsFormat();
 133  
 134          // If the user entered a zip code for the location, no need to
 135          // search (weather.com accepts zip codes as location IDs).
 136          // The location ID should already have been validated in
 137          // getParams.
 138          $search = (preg_match('/\b(?:\\d{5}(-\\d{5})?)|(?:[A-Z]{4}\\d{4})\b/',
 139              $this->_params['location'], $matches) ?
 140              $matches[0] :
 141              $weatherDotCom->searchLocation($this->_params['location']));
 142          if (is_a($search, 'PEAR_Error')) {
 143              switch ($search->getCode()) {
 144              case SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND:
 145                  return _("Requested service could not be found.");
 146              case SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION:
 147                  return _("Unknown location provided.");
 148              case SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA:
 149                  return _("Server data wrong or not available.");
 150              case SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED:
 151                  return _("Cache init was not completed.");
 152              case SERVICES_WEATHER_ERROR_DB_NOT_CONNECTED:
 153                  return _("MetarDB is not connected.");
 154              case SERVICES_WEATHER_ERROR_UNKNOWN_ERROR:
 155                  return _("An unknown error has occured.");
 156              case SERVICES_WEATHER_ERROR_NO_LOCATION:
 157                  return _("No location provided.");
 158              case SERVICES_WEATHER_ERROR_INVALID_LOCATION:
 159                  return _("Invalid location provided.");
 160              case SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID:
 161                  return _("Invalid partner id.");
 162              case SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE:
 163                  return _("Invalid product code.");
 164              case SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY:
 165                  return _("Invalid license key.");
 166              default:
 167                  return $search->getmessage();
 168              }
 169          }
 170  
 171          $html = '';
 172          if (is_array($search)) {
 173              // Several locations returned due to imprecise location
 174              // parameter.
 175              $html = _("Several locations possible with the parameter: ") .
 176                  $this->_params['location'] .
 177                  '<br /><ul>';
 178              foreach ($search as $id_weather=>$real_location) {
 179                  $html .= "<li>$real_location ($id_weather)</li>\n";
 180              }
 181              $html .= '</ul>';
 182              return $html;
 183          }
 184  
 185          $location = $weatherDotCom->getLocation($search);
 186          if (is_a($location, 'PEAR_Error')) {
 187              return $location->getmessage();
 188          }
 189          $weather = $weatherDotCom->getWeather($search);
 190          if (is_a($weather, 'PEAR_Error')) {
 191              return $weather->getmessage();
 192          }
 193          $forecast = $weatherDotCom->getForecast($search, $this->_params['days']);
 194          if (is_a($forecast, 'PEAR_Error')) {
 195              return $forecast->getmessage();
 196          }
 197  
 198          // Location and local time.
 199          $html .= '<div class="control">' .
 200              '<strong>' . $location['name'] . '</strong> ' . _("Local time: ") . $location['time'] .
 201              '</div>';
 202  
 203          // Sunrise/sunset.
 204          $html .= '<strong>' . _("Sunrise: ") . '</strong>' .
 205              Horde::img('block/sunrise/sunrise.png', _("Sunrise")) .
 206              $location['sunrise'];
 207          $html .= ' <strong>' . _("Sunset: ") . '</strong>' .
 208              Horde::img('block/sunrise/sunset.png', _("Sunset")) .
 209              $location['sunset'];
 210  
 211          // Temperature.
 212          $html .= '<br /><strong>' . _("Temperature: ") . '</strong>' .
 213              round($weather['temperature']) . '&deg;' . String::upper($units['temp']);
 214  
 215          // Dew point.
 216          $html .= ' <strong>' . _("Dew point: ") . '</strong>' .
 217              round($weather['dewPoint']) . '&deg;' . String::upper($units['temp']);
 218  
 219          // Feels like temperature.
 220          $html .= ' <strong>' . _("Feels like: ") . '</strong>' .
 221              round($weather['feltTemperature']) . '&deg;' . String::upper($units['temp']);
 222  
 223          // Pressure and trend.
 224          $html .= '<br /><strong>' . _("Pressure: ") . '</strong>';
 225          $trend = $weather['pressureTrend'];
 226          $html .= sprintf(_("%d %s and %s"),
 227                           round($weather['pressure']), $units['pres'],
 228                           _($trend));
 229  
 230          // Wind.
 231          $html .= '<br /><strong>' . _("Wind: ") . '</strong>';
 232          if ($weather['windDirection'] == 'VAR') {
 233              $html .= _("Variable");
 234          } elseif ($weather['windDirection'] == 'CALM') {
 235              $html .= _("Calm");
 236          } else {
 237              $html .= _("From the ") . $weather['windDirection'];
 238          if (isset($weather['windGust']) && $weather['windGust'] > 0) {
 239              $html .= ', ' . _("gusting") . ' ' . $weather['windGust'] .
 240                  ' ' . $units['wind'];
 241          }
 242  
 243              $html .= ' (' . $weather['windDegrees'] . ')';
 244          }
 245          $html .= _(" at ") . round($weather['wind']) . ' ' . $units['wind'];
 246  
 247          // Humidity.
 248          $html .= '<br /><strong>' . _("Humidity: ") . '</strong>' .
 249              $weather['humidity'] . '%';
 250  
 251          // Visibility.
 252          $html .= ' <strong>' . _("Visibility: ") . '</strong>' .
 253              (is_numeric($weather['visibility'])
 254               ? round($weather['visibility']) . ' ' . $units['vis']
 255               : $weather['visibility']);
 256  
 257          // UV index.
 258          $html .= ' <strong>' . _("U.V. index: ") . '</strong>';
 259          $uv = $weather['uvText'];
 260          $html .= $weather['uvIndex'] . ' - ' . _($uv);
 261  
 262          // Current condition.
 263          $condition = implode(' / ', array_map('_', explode(' / ', $weather['condition'])));
 264          $html .= '<br /><strong>' . _("Current condition: ") . '</strong>' .
 265              Horde::img('block/weatherdotcom/32x32/' .
 266                         ($weather['conditionIcon'] == '-' ? 'na' : $weather['conditionIcon']) . '.png',
 267                         $condition,
 268                         'align="middle"');
 269          $html .= ' ' . $condition;
 270  
 271          // Do the forecast now (if requested).
 272          if ($this->_params['days'] > 0) {
 273              $html .= '<div class="control" style="text-align:center"><strong>' .
 274                  sprintf(_("%d-day forecast"), $this->_params['days']) .
 275                  '</strong></div>';
 276  
 277              $futureDays = 0;
 278              $html .= '<table width="100%" cellspacing="3">';
 279              // Headers.
 280              $html .= '<tr>';
 281              $html .= '<th>' . _("Day") . '</th><th>&nbsp;</th><th>' .
 282                  sprintf(_("Temperature<br />(%sHi%s/%sLo%s) &deg;%s"),
 283                          '<span style="color:red">', '</span>',
 284                          '<span style="color:blue">', '</span>',
 285                          String::upper($units['temp'])) .
 286                  '</th><th>' . _("Condition") . '</th>' .
 287                  '<th>' . _("Precipitation<br />chance") . '</th>';
 288              if (isset($this->_params['detailedForecast'])) {
 289                  $html .= '<th>' . _("Humidity") . '</th><th>' . _("Wind") . '</th>';
 290              }
 291              $html .= '</tr>';
 292  
 293              foreach ($forecast['days'] as $which => $day) {
 294                  $html .= '<tr class="item0">';
 295  
 296                  // Day name.
 297                  $html .= '<td rowspan="2" style="border:1px solid #ddd; text-align:center">' .
 298                          '<strong>';
 299                  if ($which == 0) {
 300                      $html .= _("Today");
 301                  } elseif ($which == 1) {
 302                      $html .= _("Tomorrow");
 303                  } else {
 304                      $html .= strftime('%A', mktime(0, 0, 0, date('m'), date('d') + $futureDays, date('Y')));
 305                  }
 306                  $html .= '</strong><br />' .
 307                      strftime('%b %d', mktime(0, 0, 0, date('m'), date('d') + $futureDays, date('Y'))) .
 308                      '</td>' .
 309                      '<td style="border:1px solid #ddd; text-align:center">' .
 310                      '<span style="color:orange">' .
 311                      _("Day") . '</span></td>';
 312  
 313                  // The day portion of the forecast is no longer available after 2:00 p.m. local today.
 314                  if ($which == 0 && (strtotime($location['time']) >= strtotime('14:00'))) {
 315                      // Balance the grid.
 316                      $html .= '<td colspan="' .
 317                              ((isset($this->_params['detailedForecast']) ? '5' : '3') . '"') .
 318                              ' style="border:1px solid #ddd; text-align:center">' .
 319                              '&nbsp;<br />' . _("Information no longer available.") . '<br />&nbsp;' .
 320                              '</td>';
 321                  } else {
 322                      // Forecast condition.
 323                      $condition = implode(' / ', array_map('_', explode(' / ', $day['day']['condition'])));
 324  
 325                      // High temperature.
 326                      $html .= '<td style="border:1px solid #ddd; text-align:center">' .
 327                          '<span style="color:red">' .
 328                          round($day['temperatureHigh']) . '</span></td>';
 329  
 330                      // Condition.
 331                      $html .= '<td style="border:1px solid #ddd; text-align:center">' .
 332                          Horde::img('block/weatherdotcom/23x23/' . ($day['day']['conditionIcon'] == '-' ? 'na' : $day['day']['conditionIcon']) . '.png', $condition) .
 333                          '<br />' . $condition . '</td>';
 334  
 335                      // Precipitation chance.
 336                      $html .= '<td style="border:1px solid #ddd; text-align:center">' .
 337                          $day['day']['precipitation'] . '%' . '</td>';
 338  
 339                      // If a detailed forecast was requested, show humidity and
 340                      // winds.
 341                      if (isset($this->_params['detailedForecast'])) {
 342  
 343                          // Humidity.
 344                          $html .= '<td style="border:1px solid #ddd; text-align:center">' .
 345                              $day['day']['humidity'] . '%</td>';
 346  
 347                          // Winds.
 348                          $html .= '<td style="border:1px solid #ddd">' .
 349                              _("From the ") . $day['day']['windDirection'] .
 350                              _(" at ") . $day['day']['wind'] .
 351                              ' ' . $units['wind'];
 352                          if (isset($day['day']['windGust']) &&
 353                              $day['day']['windGust'] > 0) {
 354                              $html .= _(", gusting ") . $day['day']['windGust'] .
 355                                  ' ' . $units['wind'];
 356                          }
 357                      }
 358  
 359                      $html .= '</tr>';
 360                  }
 361  
 362                  // Night forecast
 363                  $night = implode(' / ', array_map('_', explode(' / ', $day['night']['condition'])));
 364  
 365                  // Shade it for visual separation.
 366                  $html .= '<tr class="item1">';
 367  
 368                  $html .= '<td style="border:1px solid #ddd; text-align:center">' .
 369                      _("Night") . '</td>';
 370  
 371                  // Low temperature.
 372                  $html .= '<td style="border:1px solid #ddd; text-align:center">' .
 373                      '<span style="color:blue">' .
 374                      round($day['temperatureLow']) . '</span></td>';
 375  
 376                  // Condition.
 377                  $html .= '<td style="border:1px solid #ddd; text-align:center">' .
 378                      Horde::img('block/weatherdotcom/23x23/' . ($day['night']['conditionIcon'] == '-' ? 'na' : $day['night']['conditionIcon']) . '.png', $night) .
 379                      '<br />' . $night . '</td>';
 380  
 381                  // Precipitation chance.
 382                  $html .= '<td style="border:1px solid #ddd; text-align:center">' .
 383                      $day['night']['precipitation'] . '%</td>';
 384  
 385                  // If a detailed forecast was requested, display humidity and
 386                  // winds.
 387                  if (isset($this->_params['detailedForecast'])) {
 388  
 389                      // Humidity.
 390                      $html .= '<td style="border:1px solid #ddd; text-align:center">' .
 391                          $day['night']['humidity'] . '%</td>';
 392  
 393                      // Winds.
 394                      $html .= '<td style="border:1px solid #ddd">' .
 395                          _("From the ") . $day['night']['windDirection'] .
 396                          _(" at ") . $day['night']['wind'] .
 397                          ' ' . $units['wind'];
 398                      if (isset($day['night']['windGust']) && $day['night']['windGust'] > 0) {
 399                          $html .= _(", gusting ") . $day['night']['windGust'] .
 400                              ' ' . $units['wind'];
 401                      }
 402                  }
 403  
 404                  $html .= '</tr>';
 405  
 406                  // Prepare for next day.
 407                  $futureDays++;
 408              }
 409              $html .= '</table>';
 410          }
 411  
 412          // Display a bar at the bottom of the block with the required
 413          // attribution to weather.com and the logo, both linked to
 414          // weather.com with the partner ID.
 415          return $html . '<div class="rightAlign linedRow">' .
 416              _("Weather data provided by") . ' ' .
 417              Horde::link(Horde::externalUrl('http://www.weather.com/?prod=xoap&amp;par=' .
 418                          $weatherDotCom->_partnerID),
 419                          'weather.com', '', '_blank', '', 'weather.com') .
 420              '<em>weather.com</em>&reg; ' .
 421              Horde::img('block/weatherdotcom/32x32/TWClogo_32px.png', 'weather.com logo') .
 422              '</a></div>';
 423      }
 424  
 425  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7