Weather Applet

Dan Chlopek <[email protected]> Tue, 21 Oct 2003 17:54:46 -0500
Newsgroups gmane.comp.horde.jonah
Message-ID <[email protected]>
>Right. Jonah's initial purpose was to be the headline feeds, then to be
>something like what the Horde portal already is today. So I'd prefer to move
>the block-like things into just portal applets, and have Jonah focus on
>news/content authoring.
>
>-chuck

I've hacked something together to create an applet using the ejse driver and
some of the Jonah weather stuff.  It asks the user for a zip code.  This way u
can set up multiple Weather applets on yor main horde page.

I put it into horde/lib/Block as weather.php

here's what I added to horde/config/registry.php:
$this->applets['weather'] = array(
    'name' => _("View weather by ZIP code"),
    'type' => 'weather',
    'default' => ''
);

I'm attaching my weather.php. I'm new to php and the horde framework, but i
managed to get something that works.  I'd like to move all the functionality
into the weather.php Block class, so that if the weather functionality leaves
the jonah project. I wasnt sure on how to pull that off, so for now it
references the ejse.php file from jonah/lib/Weather as well as Jonah.php and
base.php from jonah/lib

Any comments, suggestions would be appreciated.


--
Dan Chlopek
[email protected]
AIM: Danimal4326


-- 
Jonah mailing list
Frequently Asked Questions: http://horde.org/faq/
To unsubscribe, mail: [email protected]
weather.php (application/x-httpd-php, 8.3 KB)
<?php
/**
 * $Horde: horde/lib/Block/weather.php,v 1.3 2003/08/05 01:46:56 chuck Exp $
 *
 * @package Horde
 */
class Horde_Block_weather extends Horde_Block {

    /**
     * Constructor.
     */
    function Horde_Block_weather($params = array())
    {
        $this->_params = $params;
        $this->_app = 'horde';
        $this->_type = 'weather';
    }

    /**
     * Does this block have any user editable parameters?
     *
     * @return boolean  True is there are editable parameters.
     *                  False if there are not.
     */
    function isEditable()
    {
        return true;
    }

    /**
     * The title to go in this block.
     *
     * @return string   The title text.
     */
    function _title()
    {

	return _("Current Weather for zip ") . $this->_params['weather'];

    }

    /**
     * The content to go in this block.
     *
     * @return string   The content
     */
    function _content()
    {

	/* Find the base file path of Jonah. */
	@define('JONAH_BASE', dirname(__FILE__) . '/../../jonah/');

        $location = $this->_params['weather'];

        require_once JONAH_BASE . '/lib/Jonah.php';
        require_once JONAH_BASE . '/lib/Weather.php';

        $html = '<table border="0" cellspacing="0" cellpadding="0"><tr><td class="text" valign="top">';

            /* Get weekday names and weather info into the .po files. */
            array(_("Monday"), _("Tuesday"), _("Wednesday"), _("Thursday"), _("Friday"), _("Saturday"), _("Sunday"));
            array(_("windy"), _("thunder storms"), _("snow"), _("rain"),
                  _("heavy rain"), _("sleet"), _("light flurries"),
                  _("flurries"), _("snow and wind"), _("dust"), _("fog"),
                  _("haze"), _("smoke"), _("cold"), _("cloudy"), _("mostly cloudy"),
                  _("partly cloudy"), _("sunny"), _("mostly sunny"), _("hot"));

            require_once JONAH_BASE .'/lib/Weather/ejse.php';
            
            $weatherSource = new Jonah_Weather_ejse();
            $weather = $weatherSource->getWeather('http://www.ejse.com/WeatherService/Service.asmx/GetExtendedWeatherInfo?zipCode=' . $location);

            $html .= '<table border="0" cellspacing="0" cellpadding="1"><tr><td class="control"><b>';
            $sep = '';
            if (!empty($weather['conditions']['city'])) {
                $html .= $weather['conditions']['city'];
                $sep = ', ';
            }
            if (!empty($weather['conditions']['country']) && ($weather['conditions']['country'] != 'USA')) {
                $html .= $sep . $weather['conditions']['country'];
            } elseif (!empty($weather['conditions']['state'])) {
                $html .= $sep . $weather['conditions']['state'];
            }
            $html .= '</b>';
            if (!empty($weather['conditions']['date'])) {
                $html = sprintf(_("%s at %s"), $html, strftime('%X', strtotime($weather['conditions']['date'])));
            }
            $html .= '</td></tr><tr><td>';
            if (isset($weather['conditions']['temp'])) {
                $html .= sprintf(_("The temperature is <b>%s</b>, "),
                                 /*$prefs->getValue('metric') ?
                                 round((($weather['conditions']['temp'] - 32) * 5) / 9) . '&deg;C' : */
                                 $weather['conditions']['temp'] . '&deg;F');
            }
            if (isset($weather['conditions']['humidity'])) {
                $html .= sprintf(_("humidity is at %s"), $weather['conditions']['humidity'] . '% ');
            }
            if (isset($weather['conditions']['windDir'])) {
                if ($weather['conditions']['windDir'] == 'CALM') {
                    $html .= _("and there is no wind measured. ");
                } elseif (!empty($weather['conditions']['windDir'])) {
                    if (isset($weather['conditions']['windStrength'])) {
                        $html .= sprintf(_("and the wind is %s at %s. "),
                                         $weather['conditions']['windDir'],
                                         /*$prefs->getValue('metric') ?
                                         round($weather['conditions']['windStrength'] * 1609 / 1000) . _(" km/h") :*/
                                         $weather['conditions']['windStrength'] . _("mph"));
                    } else {
                        $html .= sprintf(_("and the wind is %s. "), $weather['conditions']['windDir']);
                    }
                }
            }
            if (isset($weather['conditions']['realtemp'])) {
                $html .= sprintf(_("It feels like %s. "),
                                 /*$prefs->getValue('metric') ?
                                 round((($weather['conditions']['realtemp'] - 32) * 5) / 9) . '&deg;C' : */
                                 $weather['conditions']['realtemp'] . '&deg;F');
            }
            if (isset($weather['conditions']['barometer'])) {
                $html .= sprintf(_("Barometer shows %s. "),
                                 /*$prefs->getValue('metric') ?
                                 round($weather['conditions']['barometer'] * 3386 / 100) . ' hP' :*/
                                 $weather['conditions']['barometer']);
            }
            $visibility = '';
            if (isset($weather['conditions']['visibility'])) {
                $visibility = sprintf(_("Visibility is %s"),
                                      $weather['conditions']['visibility'] == 999 ?
                                      _("infinite") :
                                      (/*$prefs->getValue('metric') ?
                                       round(1609 * $weather['conditions']['visibility'] / 1000) . _(" km") :*/
                                       $weather['conditions']['visibility'] . _(" miles")));
            }
            $html .= $visibility;
            if (isset($weather['conditions']['uv'])) {
                if ($weather['conditions']['uv'] <= 2.5) {
                    $uv = sprintf(_("minimal %s"), $weather['conditions']['uv']);
                } elseif ($weather['conditions']['uv'] <= 4.5) {
                    $uv = sprintf(_("low %s"), $weather['conditions']['uv']);
                } elseif ($weather['conditions']['uv'] <= 6.5) {
                    $uv = sprintf(_("moderate %s"), $weather['conditions']['uv']);
                } elseif ($weather['conditions']['uv'] <= 9.5) {
                    $uv = sprintf(_("high %s. Use sunscreen"), $weather['conditions']['uv']);
                } else {
                    $uv = sprintf(_("very high %s. Stay indoors"), $weather['conditions']['uv']);
                }
                $html .= sprintf(empty($visibility) ? _("The UV index is a %s. ") : _(" and the UV index is a %s. "), $uv);
            } elseif (!empty($visibility)) {
                $html .= '. ';
            }
            $html .= '</td></tr>';
            if (isset($weather['forecast']) && (count($weather['forecast']) > 1)) {
                $html .= '<tr><td class="control"><b>' . sprintf(_("%d-Day Forecast"), count($weather['forecast'])) . '</b></td></tr><tr><td><table border="0" align="center"><tr>';
                foreach ($weather['forecast'] as $day => $forecast) {
                    $html .= '<td align="center" width="75"><b>' . _($day) . '</b><br /><span style="color:red">';
                    /*if ($prefs->getValue('metric')) {
                        $html .= round((($forecast['high'] - 32) * 5) / 9);
                    } else { */
                        $html .= $forecast['high'];
                    /*}*/
                    $html .= '&deg;</span>/<span style="color:blue">';
                    /*if ($prefs->getValue('metric')) {
                        $html .= round((($forecast['low'] - 32) * 5) / 9);
                    } else { */
                        $html .= $forecast['low'];
                   /* } */
                    $html .= '&deg;</span><br />';
                    if (isset($forecast['conditions'])) {
                        $html .= Horde::img('weather/' . $forecast['wid'] . '.gif', gettext(String::lower($forecast['conditions'])), 'align="middle"', '/horde3/jonah/graphics');
                    }
                    $html .= '</td>';
                }
                $html .= '</tr></table></td></tr>';
            }
            $html .= '</table>';
        

        $html .= '</td></tr></table>';
        return $html; 


    }

}