From: Michael J. Rubinsky Date: Sun, 7 Jun 2009 18:56:56 +0000 (-0400) Subject: Attempt to get a good weatherdotcom location string from Turba's X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=2a53d957d3e4c041d7c2c70fb88f2c3d0d9d2c25;p=horde.git Attempt to get a good weatherdotcom location string from Turba's 'own' contact --- diff --git a/timeobjects/lib/Driver/Weather.php b/timeobjects/lib/Driver/Weather.php deleted file mode 100644 index 509d5e09f..000000000 --- a/timeobjects/lib/Driver/Weather.php +++ /dev/null @@ -1,153 +0,0 @@ - - * @package TimeObjects - */ -class TimeObjects_Driver_Weather extends TimeObjects_Driver -{ - protected $_params = array('location' => '08080', - 'units' => 'standard', - 'days' => 5); - - /** - * - * @param mixed $start The start time of the period - * @param mixed $time The end time of the period - * - * @return array of listTimeObjects arrays. - */ - public function listTimeObjects($start = null, $time = null) - { - global $conf; - - if (!class_exists('Services_Weather') || !class_exists('Cache')) { - throw new TimeObjects_Exception('Services_Weather or PEAR Cache Classes not found.'); - } - - $options = array(); - if (!empty($conf['http']['proxy']['proxy_host'])) { - $proxy = 'http://'; - if (!empty($conf['http']['proxy']['proxy_user'])) { - $proxy .= urlencode($conf['http']['proxy']['proxy_user']); - if (!empty($conf['http']['proxy']['proxy_pass'])) { - $proxy .= ':' . urlencode($conf['http']['proxy']['proxy_pass']); - } - $proxy .= '@'; - } - $proxy .= $conf['http']['proxy']['proxy_host']; - if (!empty($conf['http']['proxy']['proxy_port'])) { - $proxy .= ':' . $conf['http']['proxy']['proxy_port']; - } - - $options['httpProxy'] = $proxy; - } - - if (empty($this->_params['location'])) { - throw new TimeObjects_Exception(_("No location is set.")); - } - - $weatherDotCom = &Services_Weather::service('WeatherDotCom', $options); - $weatherDotCom->setAccountData( - (isset($conf['weatherdotcom']['partner_id']) ? $conf['weatherdotcom']['partner_id'] : ''), - (isset($conf['weatherdotcom']['license_key']) ? $conf['weatherdotcom']['license_key'] : '')); - - $cacheDir = Horde::getTempDir(); - if (!$cacheDir) { - throw new TimeObjects_Exception(_("No temporary directory available for cache.")); - } else { - $weatherDotCom->setCache('file', array('cache_dir' => ($cacheDir . '/'))); - } - $weatherDotCom->setDateTimeFormat('m.d.Y', 'H:i'); - $weatherDotCom->setUnitsFormat($this->_params['units']); - $units = $weatherDotCom->getUnitsFormat(); - - // 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. - // - // @TODO: These need to all be changed to use exceptions - $search = (preg_match('/\b(?:\\d{5}(-\\d{5})?)|(?:[A-Z]{4}\\d{4})\b/', - $this->_params['location'], $matches) ? - $matches[0] : - $weatherDotCom->searchLocation($this->_params['location'])); - if (is_a($search, 'PEAR_Error')) { - switch ($search->getCode()) { - case SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND: - throw new TimeObjects_Exception(_("Requested service could not be found.")); - case SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION: - throw new TimeObjects_Exception(_("Unknown location provided.")); - case SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA: - throw new TimeObjects_Exception(_("Server data wrong or not available.")); - case SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED: - throw new TimeObjects_Exception(_("Cache init was not completed.")); - case SERVICES_WEATHER_ERROR_DB_NOT_CONNECTED: - throw new TimeObjects_Exception(_("MetarDB is not connected.")); - case SERVICES_WEATHER_ERROR_UNKNOWN_ERROR: - throw new TimeObjects_Exception(_("An unknown error has occured.")); - case SERVICES_WEATHER_ERROR_NO_LOCATION: - throw new TimeObjects_Exception(_("No location provided.")); - case SERVICES_WEATHER_ERROR_INVALID_LOCATION: - throw new TimeObjects_Exception(_("Invalid location provided.")); - case SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID: - throw new TimeObjects_Exception(_("Invalid partner id.")); - case SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE: - throw new TimeObjects_Exception(_("Invalid product code.")); - case SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY: - throw new TimeObjects_Exception(_("Invalid license key.")); - default: - throw new TimeObjects_Exception($search->getMessage()); - } - } - if (is_array($search)) { - throw new TimeObjects_Exception(_(sprintf("Several locations possible with the paramter: ", $this->_params['location']))); - } - $forecast = $weatherDotCom->getForecast($search, $this->_params['days']); - if (is_a($forecast, 'PEAR_Error')) { - throw new TimeObjects_Exception($forecast->getMessage()); - } - - $now = new Horde_Date(time()); - $objects = array(); - foreach ($forecast['days'] as $which => $data) { - $day = new Horde_Date($now); - $day->mday += $which; - $day_end = new Horde_Date($day); - $day_end->mday++; - - // For day 0, the day portion isn't available after a certain time - // simplify and just check for it's presence or use night. - $title = sprintf("%s %d%s/%d%s", (!empty($data['day']['condition']) ? $data['day']['condition'] : $data['night']['condition']), - $data['temperatureHigh'], - $units['temp'], - $data['temperatureLow'], - $units['temp']); - $objects[] = array('id' => $day->timestamp(), //??? - 'title' => $title, - 'start' => sprintf('%d-%02d-%02dT00:00:00', - $day->year, - $day->month, - $day->mday), - 'end' => sprintf('%d-%02d-%02dT00:00:00', - $day_end->year, - $day_end->month, - $day_end->mday), - 'recurrence' => Horde_Date_Recurrence::RECUR_NONE, - 'params' => array(), - 'icon' => Horde::url($GLOBALS['registry']->getImageDir('horde') . '/block/weatherdotcom/23x23/' . ($data['day']['conditionIcon'] == '-' ? 'na' : $data['day']['conditionIcon']) . '.png', true, false) - ); - } - - return $objects; - } - -} \ No newline at end of file diff --git a/timeobjects/lib/Driver/Weatherdotcom.php b/timeobjects/lib/Driver/Weatherdotcom.php index 55f555840..cf4790ddc 100644 --- a/timeobjects/lib/Driver/Weatherdotcom.php +++ b/timeobjects/lib/Driver/Weatherdotcom.php @@ -14,10 +14,22 @@ */ class TimeObjects_Driver_Weatherdotcom extends TimeObjects_Driver { - protected $_params = array('location' => '08080', - 'units' => 'standard', + protected $_params = array('units' => 'standard', 'days' => 5); + public function __construct($params) + { + if (empty($params['location'])) { + // Try to get a good location string from Turba's "own" contact + $own = $GLOBALS['registry']->horde->getPreference('turba', 'own_contact'); + @list($source, $id) = explode(';', $own); + $contact = $GLOBALS['registry']->contacts->getContact($source, $id); + $params['location'] = $contact['homeCity'] . (!empty($contact['homeProvince']) ? ', ' . $contact['homeProvince'] : '') . (!empty($contact['homeCountry']) ? ', ' . $contact['homeCountry'] : ''); + } + + parent::__construct($params); + } + /** * * @param mixed $start The start time of the period