]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PhpWeather.php
Better error messages from PhpWeather Plugin -- Martin Geisler
[SourceForge/phpwiki.git] / lib / plugin / PhpWeather.php
1 <?php // -*-php-*-
2 rcs_id('$Id PhpWeather.php 2002-08-26 15:30:13 rurban$');
3 /**
4  Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * This plugin requires a separate program called PhpWeather. For more
25  * information and to download PhpWeather, see:
26  *
27  *   http://sourceforge.net/projects/phpweather/
28  *
29  * Usage:
30  *
31  * <?plugin PhpWeather ?>
32  * <?plugin PhpWeather menu=true ?>
33  * <?plugin PhpWeather icao=KJFK ?>
34  * <?plugin PhpWeather language=en ?>
35  * <?plugin PhpWeather units=only_metric ?>
36  * <?plugin PhpWeather icao||=CYYZ cc||=CA language||=en menu=true ?>
37  *
38  * If you want a menu, and you also want to change the default station
39  * or language, then you have to use the ||= form, or else the user
40  * wont be able to change the station or language.
41  *
42  * The units argument should be one of only_metric, only_imperial,
43  * both_metric, or both_imperial.
44  */
45
46 // We require the base class from PHP Weather, adjust this to match
47 // the location of PhpWeather on your server:
48 if (!defined('PHPWEATHER_BASE_DIR')) {
49     /* PhpWeather has not been loaded before. We include the base
50      * class from PhpWeather, adjust this to match the location of
51      * PhpWeather on your server: */
52     @include_once($_SERVER['DOCUMENT_ROOT'] . '/phpweather-2.1.0/phpweather.php');
53 }
54
55 class WikiPlugin_PhpWeather
56 extends WikiPlugin
57 {
58     function getName () {
59         return _("PhpWeather");
60     }
61
62     function getDescription () {
63         return _("The PhpWeather plugin provides weather reports from the Internet.");
64     }
65
66     function getVersion() {
67         return preg_replace("/[Revision: $]/", '',
68                             "\$Revision: 1.9 $");
69     }
70
71     function getDefaultArguments() {
72         global $LANG;
73         return array('icao'  => 'EKAH',
74                      'cc'    => 'DK',
75                      'language'  => 'en',
76                      'menu'  => false,
77                      'units' => 'both_metric');
78     }
79
80     function run($dbi, $argstr, $request) {
81         // When 'phpweather/phpweather.php' is not installed then
82         // PHPWEATHER_BASE_DIR will be undefined
83         if (!defined('PHPWEATHER_BASE_DIR'))
84             return $this->error(_("You have to configure it before use.")); //early return
85
86         require_once(PHPWEATHER_BASE_DIR . '/output/pw_images.php');
87         require_once(PHPWEATHER_BASE_DIR . '/pw_utilities.php');
88
89         extract($this->getArgs($argstr, $request));
90         $html = HTML();
91
92         $w = new phpweather(); // Our weather object
93
94         if (!empty($icao)) {
95             /* We assign the ICAO to the weather object: */
96             $w->set_icao($icao);
97             if (!$w->get_country_code()) {
98                 /* The country code couldn't be resolved, so we
99                  * shouldn't use the ICAO: */
100                 trigger_error(sprintf(_("The ICAO '%s' wasn't recognized."),
101                                       $icao), E_USER_NOTICE);
102                 $icao = '';
103             }
104         }
105
106         if (!empty($icao)) {
107
108             /* We check and correct the language if necessary: */
109             //if (!in_array($language, array_keys($w->get_languages('text')))) {
110             if (!in_array($language, array_keys(get_languages('text')))) {
111                 trigger_error(sprintf(_("%s does not know about the language '%s', using 'en' instead."),
112                                       $this->getName(), $language),
113                               E_USER_NOTICE);
114                 $language = 'en';
115             }
116
117             $class = "pw_text_$language";
118             require_once(PHPWEATHER_BASE_DIR . "/output/$class.php");
119
120             $t = new $class($w);
121             $t->set_pref_units($units);
122             $i = new pw_images($w);
123
124             $i_temp = HTML::img(array('src' => $i->get_temp_image()));
125             $i_wind = HTML::img(array('src' => $i->get_winddir_image()));
126             $i_sky  = HTML::img(array('src' => $i->get_sky_image()));
127
128             $m = $t->print_pretty();
129
130             $m_td = HTML::td(HTML::p(new RawXml($m)));
131
132             $i_tr = HTML::tr();
133             $i_tr->pushContent(HTML::td($i_temp));
134             $i_tr->pushContent(HTML::td($i_wind));
135
136             $i_table = HTML::table($i_tr);
137             $i_table->pushContent(HTML::tr(HTML::td(array('colspan' => '2'),
138                                                     $i_sky)));
139
140             $tr = HTML::tr();
141             $tr->pushContent($m_td);
142             $tr->pushContent(HTML::td($i_table));
143
144             $html->pushContent(HTML::table($tr));
145
146         }
147
148         /* We make a menu if asked to, or if $icao is empty: */
149         if ($menu || empty($icao)) {
150
151             $form_arg = array('action' => $request->getURLtoSelf(),
152                               'method' => 'get');
153
154             /* The country box is always part of the menu: */
155             $p1 = HTML::p(new RawXml(get_countries_select($w, $cc)));
156
157             /* We want to save the language: */
158             $p1->pushContent(HTML::input(array('type'  => 'hidden',
159                                                'name'  => 'language',
160                                                'value' => $language)));
161             /* And also the ICAO: */
162             $p1->pushContent(HTML::input(array('type'  => 'hidden',
163                                                'name'  => 'icao',
164                                                'value' => $icao)));
165
166             $caption = (empty($cc) ? _("Submit country") : _("Change country"));
167             $p1->pushContent(HTML::input(array('type'  => 'submit',
168                                                'value' => $caption)));
169
170             $html->pushContent(HTML::form($form_arg, $p1));
171
172             if (!empty($cc)) {
173                 /* We have selected a country, now display a list with
174                  * the available stations in that country: */
175                 $p2 = HTML::p();
176
177                 /* We need the country code after the form is submitted: */
178                 $p2->pushContent(HTML::input(array('type'  => 'hidden',
179                                                    'name'  => 'cc',
180                                                    'value' => $cc)));
181
182                 $p2->pushContent(new RawXml(get_stations_select($w, $cc, $icao)));
183                 $p2->pushContent(new RawXml(get_languages_select($language)));
184                 $p2->pushContent(HTML::input(array('type'  => 'submit',
185                                                    'value' => _("Submit location"))));
186
187                 $html->pushContent(HTML::form($form_arg, $p2));
188
189             }
190
191         }
192
193         return $html;
194     }
195 };
196
197 // $Log: not supported by cvs2svn $
198 // Revision 1.8  2003/01/18 22:01:43  carstenklapp
199 // Code cleanup:
200 // Reformatting & tabs to spaces;
201 // Added copyleft, getVersion, getDescription, rcs_id.
202 //
203 // Revision 1.7  2002/12/31 20:53:40  carstenklapp
204 // Bugfixes: Fixed menu language selection (incorrect parameters to
205 // $w->get_languages_select() & form input 'language' instead of 'lang').
206
207 // For emacs users
208 // Local Variables:
209 // mode: php
210 // tab-width: 8
211 // c-basic-offset: 4
212 // c-hanging-comment-ender-p: nil
213 // indent-tabs-mode: nil
214 // End:
215 ?>