[dev] Patch for weatherdotcom.php

Rick Emery rick at emery.homelinux.net
Sat Jan 24 13:19:42 PST 2004


I've read through the guide that came with the sdk for weather.com. This patch
brings the block closer to compliance with their requirements. Changes
include:

1. The weather.com logo and an attribution to weather.com for providing the
data are required on the displayed page. Both are supposed to link to the
weather.com site. I've moved the logo from the title to the bottom of the
block, and added the attribution next to it.

2. It turns out that weather.com accepts a zip code as a location ID. I added a
check to see if the user entered a zip code; if so, no search is necessary and
the zip code becomes the location ID. This may also require modification of
the validation routine when the blocks use horde_form validation. I also added
some error-checking to catch and report invalid locations. This should also
become unnecessary (or redundant) after horde_form validation is used.

3. Display the update time as local time (instead of GMT).

4. The forecast for the current day (not night) is no longer valid after 14:00
local time, so don't display it. Also, use day headers "Today" and "Tomorrow"
(instead of the day name). Also, does anybody (Jan) know if the date function
returns the day name gettext-ized? If not, could somebody take a look at the
code and share the best way to accomplish that?

Thanks,
Rick

P.S. I probably should have sent these changes as individual, smaller patches;
but once I got started, I couldn't stop myself. I'll try to do better in the
future :-)

--
Rick Emery

"When once you have tasted flight, you will forever walk the Earth
 with your eyes turned skyward, for there you have been, and there
 you will always long to return"
                                              -- Leonardo Da Vinci
-------------- next part --------------
Index: weatherdotcom.php
===================================================================
RCS file: /repository/horde/lib/Block/weatherdotcom.php,v
retrieving revision 1.3
diff -u -r1.3 weatherdotcom.php
--- weatherdotcom.php	23 Jan 2004 13:42:25 -0000	1.3
+++ weatherdotcom.php	24 Jan 2004 21:12:34 -0000
@@ -20,9 +20,7 @@
      */
     function _title()
     {
-        // Display the required weather.com logo in the title.
-        return Horde::img('block/weatherdotcom/32x32/TWClogo_32px.png',
-            'weather.com logo') . _(" Weather Forecast");
+        return _("Weather Forecast");
     }
 
     /**
@@ -98,17 +96,30 @@
         $weatherDotCom->setUnitsFormat($this->_params['units']);
         $weatherDotCom->setDateTimeFormat("m.d.Y", "H:i");
 
-        // This should already have been validated in getParams (if I
+        // 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 (if I
         // ever figure out how to get that working).
-        $search = $weatherDotCom->searchLocation($this->_params['location']);
+        $search = (preg_match('/\\d{5}(-\\d{5})?/',$this->_params['location']) ?
+            $this->_params['location'] :
+            $weatherDotCom->searchLocation($this->_params['location']));
         $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();
+        }
         $units = $weatherDotCom->getUnits(0, $this->_params['units']);
 
-        // location and last time
+        // location and local time
         $html .= "<table width=100%><tr><td class=control>";
-        $html .= '<b>' . $location['name'] . '</b>' . ' ' . $weather['update'];
+        $html .= '<b>' . $location['name'] . '</b>' . ' local time ' . $location['time'];
         $html .= "</b></td></tr></table>";
 
         // sunrise/sunset
@@ -176,44 +187,54 @@
             . '"><center><b>' . $this->_params['days'] .
             '-day forecast</b></center></td></tr><tr>';
         $futureDays = 0;
-        foreach ($forecast['days'] as $day) {
+        foreach ($forecast['days'] as $which => $day) {
             $html .= '<td colspan="2" align="center">';
 
             // day name
-            $html .= '<b>' .
-                date("D",
-                mktime(0, 0, 0, date("m"), date("d") + $futureDays, date("Y"))) .
-                '</b><br />';
+            $html .= '<b>';
+            if ($which == 0) {
+                $html .= _("Today");
+                if (strtotime($location['time']) > strtotime('14:00')) {
+                }
+            } elseif ($which == 1) {
+                $html .= _("Tomorrow");
+            } else {
+                $html .= date("l",
+                    mktime(0, 0, 0, date("m"), date("d") + $futureDays, date("Y")));
+            }
+            $html .= '</b><br />';
             $futureDays++;
 
             // high/low temp
-            // been having a lot of trouble with the current day's high being 0
-            if ($day['tempertureHigh'] >= $day['temperatureLow']) {
+            // 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">' . $day['tempertureHigh'] .
                     '&deg;' . strtoupper($units['temp']) . '</span>/';
-                $html .= '<span style="color:blue">' . $day['temperatureLow'] .
-                    '&deg;' . strtoupper($units['temp']) . '</span>';
-            } else {
-                $html .= '&nbsp;';
             }
-
+            $html .= '<span style="color:blue">' . $day['temperatureLow'] .
+                '&deg;' . strtoupper($units['temp']) . '</span>';
             $html .= '</td>';
         }
         $html .= '</tr><tr>';
 
         $elementWidth = 100 / ($this->_params['days'] * 2);
 
-        foreach ($forecast['days'] as $day) {
+        foreach ($forecast['days'] as $which => $day) {
 
             // day forecast
             $html .= '<td align="center" valign="top" width="'
                 . $elementWidth . '%">';
-
-            $html .= '<b><i>Day</i></b><br />';
-            $html .= Horde::img('block/weatherdotcom/23x23/' .
-                $day['day']['conditionIcon'] . '.png',
-                $day['day']['condition']);
-            $html .= '<br />' . $day['day']['condition'];
+            if ($which > 0 || ($which == 0 &&
+                (strtotime($location['time']) < strtotime('14:00')))) {
+                $html .= '<b><i>Day</i></b><br />';
+                $html .= Horde::img('block/weatherdotcom/23x23/' .
+                    $day['day']['conditionIcon'] . '.png',
+                    $day['day']['condition']);
+                    $html .= '<br />' . $day['day']['condition'];
+            } else {
+                $html .= '&nbsp;';
+            }
             $html .= '</td>';
 
             //night forecast
@@ -228,6 +249,19 @@
             $html .= '</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 .= '<table width=100%><tr>';
+        $html .= '<td align=right class=control>';
+        $html .= 'Weather data provided by ';
+        $html .= Horde::link('http://www.weather.com/?prod=xoap&par=PartnerID',
+            'weather.com', '', '_blank', '', 'weather.com');
+        $html .= '<i>weather.com</i>&reg; ';
+        $html .= Horde::img('block/weatherdotcom/32x32/TWClogo_32px.png',
+            'weather.com logo');
+        $html .= '</a></td></tr></table>';
 
         return $html;
     }


More information about the dev mailing list