[ Index ]
 

Code source de Horde 3.1.3

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

title

Body

[fermer]

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

   1  <?php
   2  
   3  $block_name = _("Metar Weather");
   4  
   5  /**
   6   * The Horde_Block_metar class provides an applet for the portal
   7   * screen to display METAR weather data for a specified location
   8   * (currently airports).
   9   *
  10   * $Horde: horde/lib/Block/metar.php,v 1.22.10.6 2006/06/23 19:31:28 jan Exp $
  11   *
  12   * @package Horde_Block
  13   */
  14  class Horde_Block_Horde_metar extends Horde_Block {
  15  
  16      var $_app = 'horde';
  17  
  18      /**
  19       * The title to go in this block.
  20       *
  21       * @return string   The title text.
  22       */
  23      function _title()
  24      {
  25          return _("Current Weather");
  26      }
  27  
  28      function _params()
  29      {
  30          if (!@include_once 'Services/Weather.php') {
  31              Horde::logMessage('The metar block will not work without Services_Weather from PEAR. Run pear install Services_Weather.',
  32                                __FILE__, __LINE__, PEAR_LOG_ERR);
  33              return array(
  34                  'error' => array(
  35                      'type' => 'error',
  36                      'name' => _("Error"),
  37                      'default' => _("Metar block not available.")
  38                  )
  39              );
  40          } else {
  41              global $conf;
  42  
  43              // Get locations from the database.
  44              require_once 'DB.php';
  45              $db = &DB::connect($conf['sql']);
  46              if (is_a($db, 'PEAR_Error')) {
  47                  return $db;
  48              }
  49  
  50              // Set DB portability options.
  51              switch ($db->phptype) {
  52              case 'mssql':
  53                  $db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM);
  54                  break;
  55              default:
  56                  $db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS);
  57              }
  58  
  59              $result = $db->query('SELECT icao, name, country FROM metarAirports ORDER BY country');
  60              if (is_a($result, 'PEAR_Error')) {
  61                  return $result;
  62              }
  63  
  64              $locations = array();
  65              while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
  66                  $locations[$row['country']][$row['icao']] = $row['name'];
  67              }
  68  
  69              return array(
  70                  'location' => array(
  71                      'type' => 'mlenum',
  72                      'name' => _("Location"),
  73                      'default' => 'KSFB',
  74                      'values' => $locations,
  75                  ),
  76                  'units' => array(
  77                      'type' => 'enum',
  78                      'name' => _("Units"),
  79                      'default' => 's',
  80                      'values' => array(
  81                          's' => _("Standard"),
  82                          'm' => _("Metric")
  83                      )
  84                  ),
  85                  'knots' => array(
  86                      'type' => 'checkbox',
  87                      'name' => _("Wind speed in knots"),
  88                      'default' => 0
  89                  ),
  90                  'taf' => array(
  91                      'type' => 'checkbox',
  92                      'name' => _("Display forecast (TAF)"),
  93                      'default' => 0
  94                  )
  95              );
  96          }
  97      }
  98  
  99      function _row($label, $content)
 100      {
 101          return '<br /><strong>' . $label . ':</strong> ' . $content;
 102      }
 103  
 104      function _sameRow($label, $content)
 105      {
 106          return ' <strong>' . $label . ':</strong> ' . $content;
 107      }
 108  
 109      /**
 110       * The content to go in this block.
 111       *
 112       * @return string   The content
 113       */
 114      function _content()
 115      {
 116          if (!@include_once 'Services/Weather.php') {
 117              Horde::logMessage('The metar block will not work without Services_Weather from PEAR. Run pear install Services_Weather.',
 118                                __FILE__, __LINE__, PEAR_LOG_ERR);
 119              return _("Metar block not available. Details have been logged for the administrator.");
 120          }
 121  
 122          global $conf;
 123          static $metarLocs;
 124  
 125          if (!isset($conf['sql'])) {
 126              return _("A database backend is required for this block.");
 127          }
 128  
 129          if (empty($this->_params['location'])) {
 130              return _("No location is set.");
 131          }
 132  
 133          if (!is_array($metarLocs)) {
 134              $metarLocs = $this->getParams();
 135          }
 136  
 137          require_once 'Services/Weather.php';
 138          $metar = &Services_Weather::service('METAR', array('debug' => 0));
 139          $metar->setMetarDB($conf['sql']);
 140          $metar->setUnitsFormat($this->_params['units']);
 141          $metar->setDateTimeFormat('M j, Y', 'H:i');
 142          $metar->setMetarSource('http');
 143  
 144          $units = $metar->getUnitsFormat($this->_params['units']);
 145          $weather = $metar->getWeather($this->_params['location']);
 146  
 147          $html = '<table width="100%" cellspacing="0">' .
 148              '<tr><td class="control"><strong>' .
 149              sprintf('%s, %s (%s)',
 150                      $metarLocs['location']['values'][$this->_params['__location']][$this->_params['location']],
 151                      $this->_params['__location'],
 152                      $this->_params['location']) .
 153              '</strong></td></tr></table><strong>' . _("Last Updated:") . '</strong> ' .
 154              $weather['update'] . '<br /><br />';
 155  
 156          // Wind.
 157          if (isset($weather['wind'])) {
 158              $html .= '<strong>' . _("Wind:") . '</strong> ';
 159              if ($weather['windDirection'] == 'Variable') {
 160                  if (!empty($this->_params['knots'])) {
 161                      $html .= sprintf(_("%s at %s %s"),
 162                          $weather['windDirection'],
 163                          round($metar->convertSpeed($weather['wind'],
 164                              $units['wind'], 'kt')),
 165                          'kt');
 166                  } else {
 167                      $html .= sprintf(_("%s at %s %s"),
 168                          $weather['windDirection'],
 169                          round($weather['wind']),
 170                          $units['wind']);
 171                  }
 172              } elseif (($weather['windDegrees'] == '000') &&
 173                          ($weather['wind'] == '0')) {
 174                  $html .= sprintf(_("calm"));
 175              } else {
 176                  $html .= sprintf(_("from the %s (%s) at %s %s"),
 177                                   $weather['windDirection'],
 178                                   $weather['windDegrees'],
 179                                   empty($this->_params['knots']) ?
 180                                   round($weather['wind']) :
 181                                   round($metar->convertSpeed($weather['wind'], $units['wind'], 'kt')),
 182                                   empty($this->_params['knots']) ?
 183                                   $units['wind'] :
 184                                   'kt');
 185              }
 186          }
 187          if (isset($weather['windGust'])) {
 188              if ($weather['windGust']) {
 189                  if (!empty($this->_params['knots'])) {
 190                      $html .= sprintf(_(", gusting %s %s"),
 191                          round($metar->convertSpeed($weather['windGust'],
 192                          $units['wind'], 'kt')),
 193                          'kt');
 194                  } else {
 195                      $html .= sprintf(_(", gusting %s %s"),
 196                          round($weather['windGust']),
 197                          $units['wind']);
 198                  }
 199              }
 200          }
 201          if (isset($weather['windVariability'])) {
 202              if ($weather['windVariability']['from']) {
 203                  $html .= sprintf(_(", variable from %s to %s"),
 204                      $weather['windVariability']['from'],
 205                      $weather['windVariability']['to']);
 206              }
 207          }
 208  
 209          // Visibility.
 210          if (isset($weather['visibility'])) {
 211              $html .= $this->_sameRow(_("Visibility"), $weather['visibility'] . ' ' . $units['vis']);
 212          }
 213  
 214          // Temperature/DewPoint.
 215          if (isset($weather['temperature'])) {
 216              $html .= $this->_row(_("Temperature"), round($weather['temperature']) . '&deg;' . String::upper($units['temp']));
 217          }
 218          if (isset($weather['dewPoint'])) {
 219              $html .= $this->_sameRow(_("Dew Point"), round($weather['dewPoint']) . '&deg;' . String::upper($units['temp']));
 220          }
 221          if (isset($weather['feltTemperature'])) {
 222              $html .= $this->_sameRow(_("Feels Like"), round($weather['feltTemperature']) . '&deg;' . String::upper($units['temp']));
 223          }
 224  
 225          // Pressure.
 226          if (isset($weather['pressure'])) {
 227              $html .= $this->_row(_("Pressure"), $weather['pressure'] . ' ' . $units['pres']);
 228          }
 229  
 230          // Humidity.
 231          if (isset($weather['humidity'])) {
 232              $html .= $this->_sameRow(_("Humidity"), round($weather['humidity']) . '%');
 233          }
 234  
 235          // Clouds.
 236          if (isset($weather['clouds'])) {
 237              $clouds = '';
 238              foreach ($weather['clouds'] as $cloud) {
 239                  $clouds .= '<br />';
 240                  if (isset($cloud['height'])) {
 241                      $clouds .= sprintf(_("%s at %s %s"), $cloud['amount'], $cloud['height'], $units['height']);
 242                  } else {
 243                      $clouds .= $cloud['amount'];
 244                  }
 245              }
 246              $html .= $this->_row(_("Clouds"), $clouds);
 247          }
 248  
 249          // Conditions.
 250          if (isset($weather['condition'])) {
 251              $html .= $this->_row(_("Conditions"), $weather['condition']);
 252          }
 253  
 254          // Remarks.
 255          if (isset($weather['remark'])) {
 256              $remarks = '';
 257              $other = '';
 258              foreach ($weather['remark'] as $remark => $value) {
 259                  switch ($remark) {
 260                  case 'seapressure':
 261                      $remarks .= '<br />' . _("Pressure at sea level: ") . $value . ' ' . $units['pres'];
 262                      break;
 263  
 264                  case 'precipitation':
 265                      foreach ($value as $precip) {
 266                          if (is_numeric($precip['amount'])) {
 267                              $remarks .= '<br />' .
 268                                  sprintf(_("Precipitation for last %s hour(s): "),
 269                                          $precip['hours']) .
 270                                  $precip['amount'] . ' ' . $units['rain'];
 271                          } else {
 272                              $remarks .= '<br />' .
 273                                  sprintf(_("Precipitation for last %s hour(s): "),
 274                                          $precip['hours']) . $precip['amount'];
 275                          }
 276                      }
 277                      break;
 278  
 279                  case 'snowdepth':
 280                      $remarks .= '<br />' . _("Snow depth: ") . $value . ' ' . $units['rain'];
 281                      break;
 282  
 283                  case 'snowequiv':
 284                      $remarks .= '<br />' . _("Snow equivalent in water: ") . $value . ' ' . $units['rain'];
 285                      break;
 286  
 287                  case 'sunduration':
 288                      $remarks .= '<br />' . sprintf(_("%s minutes"), $value);
 289                      break;
 290  
 291                  case '1htemp':
 292                      $remarks .= '<br />' . _("Temp for last hour: ") . round($value) . '&deg;' . String::upper($units['temp']);
 293                      break;
 294  
 295                  case '1hdew':
 296                      $remarks .= '<br />' . _("Dew Point for last hour: ") . round($value) . '&deg;' . String::upper($units['temp']);
 297                      break;
 298  
 299                  case '6hmaxtemp':
 300                      $remarks .= '<br />' . _("Max temp last 6 hours: ") . round($value) . '&deg;' . String::upper($units['temp']);
 301                      break;
 302  
 303                  case '6hmintemp':
 304                      $remarks .= '<br />' . _("Min temp last 6 hours: ") . round($value) . '&deg;' . String::upper($units['temp']);
 305                      break;
 306  
 307                  case '24hmaxtemp':
 308                      $remarks .= '<br />' . _("Max temp last 24 hours: ") . round($value) . '&deg;' . String::upper($units['temp']);
 309                      break;
 310  
 311                  case '24hmintemp':
 312                      $remarks .= '<br />' . _("Min temp last 24 hours: ") . round($value) . '&deg;' . String::upper($units['temp']);
 313                      break;
 314  
 315                  case 'sensors':
 316                      foreach ($value as $sensor) {
 317                          $remarks .= '<br />' .
 318                              _("Sensor: ") . $sensor;
 319                      }
 320                      break;
 321  
 322                  default:
 323                      $other .= '<br />' . $value;
 324                      break;
 325                  }
 326              }
 327  
 328              $html .= $this->_row(_("Remarks"), $remarks . $other);
 329          }
 330  
 331          // TAF
 332          if (!empty($this->_params['taf'])) {
 333              $taf = $metar->getForecast($this->_params['location']);
 334              if (!is_a($taf, 'PEAR_Error')) {
 335                  $forecast = '<table width="100%" cellspacing="0">';
 336                  $forecast .= '<tr><td class="control" colspan="2"><center><strong>' . _("Forecast (TAF)") . '</strong></td></tr></table>';
 337                  $forecast .= '<strong>Valid: </strong>' . $taf['validFrom'] . ' - ' . $taf['validTo'] . '<br /><br />';
 338                  $item = 0;
 339                  foreach ($taf['time'] as $time => $entry) {
 340                      $item++;
 341                      $forecast .= '<table width="100%" cellspacing="0">';
 342                      $forecast .= '<tr class="item' . ($item % 2) . '">';
 343                      $forecast .= '<td align="center" width="50">' . $time . '</td><td><strong>Wind:</strong> ';
 344                      if (isset($entry['wind'])) {
 345                          if ($entry['windDirection'] == 'Variable') {
 346                              if (!empty($this->_params['knots'])) {
 347                                  $forecast .= sprintf(_("%s at %s %s"),
 348                                      strtolower($entry['windDirection']),
 349                                      round($metar->convertSpeed($entry['wind'],
 350                                          $units['wind'], 'kt')),
 351                                      'kt');
 352                              } else {
 353                                  $forecast .= sprintf(_("%s at %s %s"),
 354                                      $entry['windDirection'],
 355                                      round($entry['wind']),
 356                                      $units['wind']);
 357                              }
 358                          } elseif (($entry['windDegrees'] == '000') &&
 359                                      ($entry['wind'] == '0')) {
 360                              $forecast .= sprintf(_("calm"));
 361                          } else {
 362                              $forecast .= sprintf(_("from the %s (%s) at %s %s"),
 363                                               $entry['windDirection'],
 364                                               $entry['windDegrees'],
 365                                               empty($this->_params['knots']) ?
 366                                               round($entry['wind']) :
 367                                               round($metar->convertSpeed($entry['wind'], $units['wind'], 'kt')),
 368                                               empty($this->_params['knots']) ?
 369                                               $units['wind'] :
 370                                               'kt');
 371                          }
 372                          $forecast .= '<br />';
 373                      }
 374                      if (isset($entry['temperatureLow']) || isset($entry['temperatureHigh'])) {
 375                          $forecast .= '<strong>Temperature</strong>';
 376                          if (isset($entry['temperatureLow'])) {
 377                              $forecast .= '<strong> Low:</strong>';
 378                              $forecast .= $entry['temperatureLow'];
 379                          }
 380                          if (isset($entry['temperatureHigh'])) {
 381                              $forecast .= '<strong> High:</strong>';
 382                              $forecast .= $entry['temperatureHigh'];
 383                          }
 384                          $forecast .= '<br />';
 385                      }
 386                      if (isset($entry['windshear'])) {
 387                          $forecast .= '<strong>Windshear:</strong>';
 388                          $forecast .= sprintf(_("from the %s (%s) at %s %s"),
 389                                          $entry['windshearDirection'],
 390                                          $entry['windshearDegrees'],
 391                                          $entry['windshearHeight'],
 392                                          $units['height']);
 393                          $forecast .= '<br />';
 394                      }
 395                      if (isset($entry['visibility'])) {
 396                          $forecast .= '<strong>Visibility:</strong> ';
 397                          $forecast .= strtolower($entry['visQualifier']) . ' ' . $entry['visibility'] . ' ' . $units['vis'];
 398                          $forecast .= '<br />';
 399                      }
 400                      if (isset($entry['condition'])) {
 401                          $forecast .= '<strong>Conditions:</strong> ';
 402                          $forecast .= $entry['condition'];
 403                          $forecast .= '<br />';
 404                      }
 405                      $forecast .= '<strong>Clouds:</strong> ';
 406                      foreach ($entry['clouds'] as $clouds) {
 407                          if (isset($clouds['type'])) {
 408                              $forecast .= ' ' . $clouds['type'];
 409                          }
 410                          $forecast .= ' ' . $clouds['amount'];
 411                          if (isset($clouds['height'])) {
 412                              $forecast .= ' at ' . $clouds['height'] . ' ' . $units['height'];
 413                          } else {
 414                              $forecast .= ' ';
 415                          }
 416                      }
 417                      $forecast .= '</td></tr>';
 418                      if (isset($entry['fmc'])) {
 419                          $item++;
 420                          foreach ($entry['fmc'] as $fmcEntry) {
 421                              $forecast .= '<tr class="item' . ($item % 2) . '">';
 422                              $forecast .= '<td align="center" width="50">';
 423                              $forecast .= '* ' . $fmcEntry['from'] . '<br /> - ' . $fmcEntry['to'] . '</td>';
 424                              $forecast .= '<td>';
 425                              $forecast .= '<strong>Type: </strong>' . $fmcEntry['type'];
 426                              if (isset($fmcEntry['probability'])) {
 427                                  $forecast .= ' <strong> Prob: </strong>' . $fmcEntry['probability'] . '%';
 428                              }
 429                              if (isset($fmcEntry['condition'])) {
 430                                  $forecast .= ' <strong> Conditions: </strong>' . $fmcEntry['condition'];
 431                              }
 432                              if (isset($fmcEntry['clouds'])) {
 433                                  $forecast .= ' <strong>Clouds:</strong>';
 434                                  foreach ($fmcEntry['clouds'] as $fmcClouds) {
 435                                      if (isset($fmcClouds['type'])) {
 436                                          $forecast .= ' ' . $fmcClouds['type'];
 437                                      }
 438                                      if (isset($fmcClouds['height'])) {
 439                                          $forecast .= ' ' . $fmcClouds['amount'];
 440                                          $forecast .= ' ' . $fmcClouds['height'];
 441                                          $forecast .= ' ' . $units['height'];
 442                                      } else {
 443                                          $forecast .= ' ' . $fmcClouds['amount'];
 444                                      }
 445                                  }
 446                              }
 447                              if (isset($fmcEntry['visQualifier'])) {
 448                                  $forecast .= ' <strong>Visibility:</strong> ';
 449                                  $forecast .= strtolower($fmcEntry['visQualifier']) . ' ';
 450                                  $forecast .= $fmcEntry['visibility'] . ' ' . $units['vis'];
 451                              }
 452                              $forecast .= '</td></tr>';
 453                          }
 454                      }
 455  
 456                  }
 457                  $forecast .= '</table>';
 458  
 459                  $html .= $forecast;
 460              }
 461          }
 462  
 463          return $html;
 464      }
 465  
 466  }


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