[dev] weatherdotcom portal block issues

Rick Emery rick at emery.homelinux.net
Thu Feb 5 13:16:34 PST 2004


Quoting Chuck Hagenbuch <chuck at horde.org>:

> Quoting Rick Emery <rick at emery.homelinux.net>:
>
>> OK, how about this? It puts the check in Collection.php, and is only 
>> done (as
>> far as I can tell) when the portal layout screen is displayed.
>
> Hardcoding checks for specific blocks into the general collection lib is
> definitely worse, from a design/overall perspective, than the registry.php
> solution.
>
> -chuck

OK, let me try something else :-) Feedback, please.

The attached patch to registry.php.dist removes the check for Services_Weather.

The weather.patch does the following:

1. Collection.php - adds widget named 'error'. I did this so that I could
display an error on the portal layout inside the block during configuration.

2. metar.php - during configuration, checks to see if Services_Weather is
installed. If it isn't, instead of displaying the configuration widgets, it
displays a message that the block is unavailable and logs the reason in the
horde log.

Also, it checks again for Services_Weather (since the file has to be included
anyway) and performs the same actions as the configuration if not installed.

3. weatherdotcom.php - Same as metar.php, plus a modification to better display
the forecast.

I think this will all go away when the blocks use Horde_Form for rendering, but
I just don't have the knowledge or the time to dig into that right now.

Ideally, I'd like to not display the weather blocks as available if
Services_Weather isn't installed, but other than the overhead-heavy check in
registry.php, I can't think of another solution.

As always, any feedback will be gratefully received.

Thanks in advance,
Rick

--
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: registry.php.dist
===================================================================
RCS file: /repository/horde/config/registry.php.dist,v
retrieving revision 1.209
diff -u -r1.209 registry.php.dist
--- registry.php.dist	4 Feb 2004 18:15:47 -0000	1.209
+++ registry.php.dist	5 Feb 2004 21:05:04 -0000
@@ -590,13 +590,10 @@
     'name' => _("Moon Phases"),
 );
 
-$result = @include_once 'Services/Weather.php';
-if ($result) {
-    $this->applets['metar'] = array(
-        'name' => _("Metar Weather"),
-    );
+$this->applets['metar'] = array(
+    'name' => _("Metar Weather"),
+);
 
-    $this->applets['weatherdotcom'] = array(
-        'name' => _("weather.com"),
-    );
-}
+$this->applets['weatherdotcom'] = array(
+    'name' => _("weather.com"),
+);
-------------- next part --------------
Index: Collection.php
===================================================================
RCS file: /repository/horde/lib/Block/Collection.php,v
retrieving revision 1.16
diff -u -r1.16 Collection.php
--- Collection.php	17 Jan 2004 19:29:07 -0000	1.16
+++ Collection.php	5 Feb 2004 21:04:42 -0000
@@ -222,6 +222,10 @@
         case 'text':
             $widget = sprintf('<input type="text" name="params[%s]" value="%s" />', $param_id, empty($val) ? $param['default'] : $val[$param_id]);
             break;
+
+        case 'error':
+            $widget = '<span class="form-error">' . $val[$param_id] . '</span>';
+            break;
         }
 
         return $widget;
Index: metar.php
===================================================================
RCS file: /repository/horde/lib/Block/metar.php,v
retrieving revision 1.9
diff -u -r1.9 metar.php
--- metar.php	5 Feb 2004 18:33:20 -0000	1.9
+++ metar.php	5 Feb 2004 21:04:42 -0000
@@ -25,47 +25,60 @@
 
     function getParams()
     {
-        global $conf;
+        if (!(@include_once 'Services/Weather.php')) {
+            Horde::logMessage('PEAR Services_Weather not installed. Metar block not available',
+                __FILE__, __LINE__, PEAR_LOG_ERR);
+            return array(
+                'error' => array(
+                    'type' => 'error',
+                    'name' => _("Error"),
+                    'default' => _("Metar block not available. Details have been logged for the administrator.")
+                )
+            );
+        } else {
 
-        // Get locations from the database.
-        require_once 'DB.php';
-        $db = &DB::connect($conf['sql']);
-        if (is_a($db, 'PEAR_Error')) {
-            return $db;
-        }
+            global $conf;
 
-        $result = $db->query('SELECT icao, name, country FROM metarAirports ORDER BY country');
-        if (is_a($result, 'PEAR_Error')) {
-            return $result;
-        }
-
-        $locations = array();
-        while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
-            $locations[$row['country']][$row['icao']] = $row['name'];
-        }
-
-        return array(
-            'location' => array(
-                'type' => 'mlenum',
-                'name' => _("Location"),
-                'default' => 'KSFB',
-                'values' => $locations,
-            ),
-            'units' => array(
-                'type' => 'enum',
-                'name' => _("Units"),
-                'default' => 's',
-                'values' => array(
-                    's' => _("Standard"),
-                    'm' => _("Metric")
+            // Get locations from the database.
+            require_once 'DB.php';
+            $db = &DB::connect($conf['sql']);
+            if (is_a($db, 'PEAR_Error')) {
+                return $db;
+            }
+
+            $result = $db->query('SELECT icao, name, country FROM metarAirports ORDER BY country');
+            if (is_a($result, 'PEAR_Error')) {
+                return $result;
+            }
+
+            $locations = array();
+            while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
+                $locations[$row['country']][$row['icao']] = $row['name'];
+            }
+
+            return array(
+                'location' => array(
+                    'type' => 'mlenum',
+                    'name' => _("Location"),
+                    'default' => 'KSFB',
+                    'values' => $locations,
+                ),
+                'units' => array(
+                    'type' => 'enum',
+                    'name' => _("Units"),
+                    'default' => 's',
+                    'values' => array(
+                        's' => _("Standard"),
+                        'm' => _("Metric")
+                    )
+                ),
+                'knots' => array(
+                    'type' => 'checkbox',
+                    'name' => _("Wind speed in knots"),
+                    'default' => 0
                 )
-            ),
-            'knots' => array(
-                'type' => 'checkbox',
-                'name' => _("Wind speed in knots"),
-                'default' => 0
-            )
-        );
+            );
+        }
     }
 
     function _row($label, $content)
@@ -80,6 +93,12 @@
      */
     function _content()
     {
+        if (!(@include_once "Services/Weather.php")) {
+            Horde::logMessage('PEAR Services_Weather not installed. Metar block not available',
+                __FILE__, __LINE__, PEAR_LOG_ERR);
+            return _("Metar block not available. Details have been logged for the administrator.");
+        }
+
         global $conf;
         static $metarLocs;
 
Index: weatherdotcom.php
===================================================================
RCS file: /repository/horde/lib/Block/weatherdotcom.php,v
retrieving revision 1.7
diff -u -r1.7 weatherdotcom.php
--- weatherdotcom.php	2 Feb 2004 03:19:25 -0000	1.7
+++ weatherdotcom.php	5 Feb 2004 21:04:42 -0000
@@ -30,38 +30,50 @@
      */
     function getParams()
     {
-        $params = array(
-            'location' => array(
-                // 'type' => 'weatherdotcom',
-                'type' => 'text',
-                'name' => _("Location"),
-                'default' => 'Boston, MA'
-            ),
-            'units' => array(
-                'type' => 'enum',
-                'name' => _("Units"),
-                'default' => 's',
-                'values' => array(
-                    'standard' => _("Standard"),
-                    'metric' => _("Metric")
+        if (!(@include_once 'Services/Weather.php')) {
+            Horde::logMessage('PEAR Services_Weather not installed. weather.com block not available',
+                __FILE__, __LINE__, PEAR_LOG_ERR);
+            $params = array(
+                'error' => array(
+                    'type' => 'error',
+                    'name' => _("Error"),
+                    'default' => _("weather.com block not available. Details have been logged for the administrator.")
                 )
-            ),
-            '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'
-                )
-            ),
-        );
+            );
+        } else {
+            $params = array(
+                'location' => array(
+                    // 'type' => 'weatherdotcom',
+                    'type' => 'text',
+                    'name' => _("Location"),
+                    'default' => 'Boston, MA'
+                ),
+                'units' => array(
+                    'type' => 'enum',
+                    'name' => _("Units"),
+                    'default' => 's',
+                    '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;
     }
@@ -73,6 +85,12 @@
      */
     function _content()
     {
+        if (!(@include_once "Services/Weather.php")) {
+            Horde::logMessage('PEAR Services_Weather not installed. weather.com block not available',
+                __FILE__, __LINE__, PEAR_LOG_ERR);
+            return _("weather.com block not available. Details have been logged for the administrator.");
+        }
+
         global $conf;
 
         $cacheDir = Horde::getTempDir();
@@ -82,7 +100,6 @@
             return _("No location is set.");
         }
 
-        require_once "Services/Weather.php";
 
         $weatherDotCom = &Services_Weather::service("WeatherDotCom");
 
@@ -187,7 +204,7 @@
         $html .= ' ' . $weather['condition'];
 
         // Do the forecast now.
-        $html .= '<table border="0" align="center"><tr>';
+        $html .= '<table border="0" width="100%" align="center"><tr>';
         $html .= '<tr><td class="control" colspan="'
             . $this->_params['days'] * 2
             . '"><center><b>' . $this->_params['days'] .


More information about the dev mailing list