]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PhpWeather.php
more php-4.0.6 compatibility: superglobals
[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     if (!isset($_SERVER))
53         $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
54     @include_once($_SERVER['DOCUMENT_ROOT'] . '/phpweather-2.1.0/phpweather.php');
55 }
56
57 class WikiPlugin_PhpWeather
58 extends WikiPlugin
59 {
60     function getName () {
61         return _("PhpWeather");
62     }
63
64     function getDescription () {
65         return _("The PhpWeather plugin provides weather reports from the Internet.");
66     }
67
68     function getVersion() {
69         return preg_replace("/[Revision: $]/", '',
70                             "\$Revision: 1.11 $");
71     }
72
73     function getDefaultArguments() {
74         global $LANG;
75         return array('icao'  => 'EKAH',
76                      'cc'    => 'DK',
77                      'language'  => 'en',
78                      'menu'  => false,
79                      'units' => 'both_metric');
80     }
81
82     function run($dbi, $argstr, &$request, $basepage) {
83         // When 'phpweather/phpweather.php' is not installed then
84         // PHPWEATHER_BASE_DIR will be undefined
85         if (!defined('PHPWEATHER_BASE_DIR'))
86             return $this->error(_("You have to configure it before use.")); //early return
87
88         require_once(PHPWEATHER_BASE_DIR . '/output/pw_images.php');
89         require_once(PHPWEATHER_BASE_DIR . '/pw_utilities.php');
90
91         extract($this->getArgs($argstr, $request));
92         $html = HTML();
93
94         $w = new phpweather(); // Our weather object
95
96         if (!empty($icao)) {
97             /* We assign the ICAO to the weather object: */
98             $w->set_icao($icao);
99             if (!$w->get_country_code()) {
100                 /* The country code couldn't be resolved, so we
101                  * shouldn't use the ICAO: */
102                 trigger_error(sprintf(_("The ICAO '%s' wasn't recognized."),
103                                       $icao), E_USER_NOTICE);
104                 $icao = '';
105             }
106         }
107
108         if (!empty($icao)) {
109
110             /* We check and correct the language if necessary: */
111             //if (!in_array($language, array_keys($w->get_languages('text')))) {
112             if (!in_array($language, array_keys(get_languages('text')))) {
113                 trigger_error(sprintf(_("%s does not know about the language '%s', using 'en' instead."),
114                                       $this->getName(), $language),
115                               E_USER_NOTICE);
116                 $language = 'en';
117             }
118
119             $class = "pw_text_$language";
120             require_once(PHPWEATHER_BASE_DIR . "/output/$class.php");
121
122             $t = new $class($w);
123             $t->set_pref_units($units);
124             $i = new pw_images($w);
125
126             $i_temp = HTML::img(array('src' => $i->get_temp_image()));
127             $i_wind = HTML::img(array('src' => $i->get_winddir_image()));
128             $i_sky  = HTML::img(array('src' => $i->get_sky_image()));
129
130             $m = $t->print_pretty();
131
132             $m_td = HTML::td(HTML::p(new RawXml($m)));
133
134             $i_tr = HTML::tr();
135             $i_tr->pushContent(HTML::td($i_temp));
136             $i_tr->pushContent(HTML::td($i_wind));
137
138             $i_table = HTML::table($i_tr);
139             $i_table->pushContent(HTML::tr(HTML::td(array('colspan' => '2'),
140                                                     $i_sky)));
141
142             $tr = HTML::tr();
143             $tr->pushContent($m_td);
144             $tr->pushContent(HTML::td($i_table));
145
146             $html->pushContent(HTML::table($tr));
147
148         }
149
150         /* We make a menu if asked to, or if $icao is empty: */
151         if ($menu || empty($icao)) {
152
153             $form_arg = array('action' => $request->getURLtoSelf(),
154                               'method' => 'get');
155
156             /* The country box is always part of the menu: */
157             $p1 = HTML::p(new RawXml(get_countries_select($w, $cc)));
158
159             /* We want to save the language: */
160             $p1->pushContent(HTML::input(array('type'  => 'hidden',
161                                                'name'  => 'language',
162                                                'value' => $language)));
163             /* And also the ICAO: */
164             $p1->pushContent(HTML::input(array('type'  => 'hidden',
165                                                'name'  => 'icao',
166                                                'value' => $icao)));
167
168             $caption = (empty($cc) ? _("Submit country") : _("Change country"));
169             $p1->pushContent(HTML::input(array('type'  => 'submit',
170                                                'value' => $caption)));
171
172             $html->pushContent(HTML::form($form_arg, $p1));
173
174             if (!empty($cc)) {
175                 /* We have selected a country, now display a list with
176                  * the available stations in that country: */
177                 $p2 = HTML::p();
178
179                 /* We need the country code after the form is submitted: */
180                 $p2->pushContent(HTML::input(array('type'  => 'hidden',
181                                                    'name'  => 'cc',
182                                                    'value' => $cc)));
183
184                 $p2->pushContent(new RawXml(get_stations_select($w, $cc, $icao)));
185                 $p2->pushContent(new RawXml(get_languages_select($language)));
186                 $p2->pushContent(HTML::input(array('type'  => 'submit',
187                                                    'value' => _("Submit location"))));
188
189                 $html->pushContent(HTML::form($form_arg, $p2));
190
191             }
192
193         }
194
195         return $html;
196     }
197 };
198
199 // $Log: not supported by cvs2svn $
200 // Revision 1.10  2004/02/17 12:11:36  rurban
201 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
202 //
203 // Revision 1.9  2003/01/28 21:10:38  zorloc
204 // Better error messages from PhpWeather Plugin -- Martin Geisler
205 //
206 // Revision 1.8  2003/01/18 22:01:43  carstenklapp
207 // Code cleanup:
208 // Reformatting & tabs to spaces;
209 // Added copyleft, getVersion, getDescription, rcs_id.
210 //
211 // Revision 1.7  2002/12/31 20:53:40  carstenklapp
212 // Bugfixes: Fixed menu language selection (incorrect parameters to
213 // $w->get_languages_select() & form input 'language' instead of 'lang').
214
215 // For emacs users
216 // Local Variables:
217 // mode: php
218 // tab-width: 8
219 // c-basic-offset: 4
220 // c-hanging-comment-ender-p: nil
221 // indent-tabs-mode: nil
222 // End:
223 ?>