]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PhpWeather.php
New FSF address
[SourceForge/phpwiki.git] / lib / plugin / PhpWeather.php
1 <?php // -*-php-*-
2 // $Id$
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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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  * <<PhpWeather >>
32  * <<PhpWeather menu=true >>
33  * <<PhpWeather icao=KJFK >>
34  * <<PhpWeather language=en >>
35  * <<PhpWeather units=only_metric >>
36  * <<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. Try some default directories.
47 // Better define PHPWEATHER_BASE_DIR to the directory 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.2.1/phpweather.php');
55     if (!defined('PHPWEATHER_BASE_DIR'))
56         @include_once($_SERVER['DOCUMENT_ROOT'] . '/phpweather/phpweather.php');
57 }
58
59 class WikiPlugin_PhpWeather
60 extends WikiPlugin
61 {
62     function getName () {
63         return _("PhpWeather");
64     }
65
66     function getDescription () {
67         return _("The PhpWeather plugin provides weather reports from the Internet.");
68     }
69
70     function getDefaultArguments() {
71         global $LANG;
72         return array('icao'  => 'EKAH',
73                      'cc'    => 'DK',
74                      'language'  => 'en',
75                      'menu'  => false,
76                      'units' => 'both_metric');
77     }
78
79     function run($dbi, $argstr, &$request, $basepage) {
80         // When 'phpweather/phpweather.php' is not installed then
81         // PHPWEATHER_BASE_DIR will be undefined.
82         if (!defined('PHPWEATHER_BASE_DIR'))
83             return $this->error(_("You have to define PHPWEATHER_BASE_DIR before use. (config/config.ini)")); //early return
84
85         require_once(PHPWEATHER_BASE_DIR . '/output/pw_images.php');
86         require_once(PHPWEATHER_BASE_DIR . '/pw_utilities.php');
87
88         extract($this->getArgs($argstr, $request));
89         $html = HTML();
90
91         $w = new phpweather(); // Our weather object
92
93         if (!empty($icao)) {
94             /* We assign the ICAO to the weather object: */
95             $w->set_icao($icao);
96             if (!$w->get_country_code()) {
97                 /* The country code couldn't be resolved, so we
98                  * shouldn't use the ICAO: */
99                 trigger_error(sprintf(_("The ICAO '%s' wasn't recognized."),
100                                       $icao), E_USER_NOTICE);
101                 $icao = '';
102             }
103         }
104
105         if (!empty($icao)) {
106
107             /* We check and correct the language if necessary: */
108             //if (!in_array($language, array_keys($w->get_languages('text')))) {
109             if (!in_array($language, array_keys(get_languages('text')))) {
110                 trigger_error(sprintf(_("%s does not know about the language '%s', using 'en' instead."),
111                                       $this->getName(), $language),
112                               E_USER_NOTICE);
113                 $language = 'en';
114             }
115
116             $class = "pw_text_$language";
117             require_once(PHPWEATHER_BASE_DIR . "/output/$class.php");
118
119             $t = new $class($w);
120             $t->set_pref_units($units);
121             $i = new pw_images($w);
122
123             $i_temp = HTML::img(array('src' => $i->get_temp_image()));
124             $i_wind = HTML::img(array('src' => $i->get_winddir_image()));
125             $i_sky  = HTML::img(array('src' => $i->get_sky_image()));
126
127             $m = $t->print_pretty();
128
129             $m_td = HTML::td(HTML::p(new RawXml($m)));
130
131             $i_tr = HTML::tr();
132             $i_tr->pushContent(HTML::td($i_temp));
133             $i_tr->pushContent(HTML::td($i_wind));
134
135             $i_table = HTML::table($i_tr);
136             $i_table->pushContent(HTML::tr(HTML::td(array('colspan' => '2'),
137                                                     $i_sky)));
138
139             $tr = HTML::tr();
140             $tr->pushContent($m_td);
141             $tr->pushContent(HTML::td($i_table));
142
143             $html->pushContent(HTML::table($tr));
144
145         }
146
147         /* We make a menu if asked to, or if $icao is empty: */
148         if ($menu || empty($icao)) {
149
150             $form_arg = array('action' => $request->getURLtoSelf(),
151                               'method' => 'get');
152
153             /* The country box is always part of the menu: */
154             $p1 = HTML::p(new RawXml(get_countries_select($w, $cc)));
155
156             /* We want to save the language: */
157             $p1->pushContent(HTML::input(array('type'  => 'hidden',
158                                                'name'  => 'language',
159                                                'value' => $language)));
160             /* And also the ICAO: */
161             $p1->pushContent(HTML::input(array('type'  => 'hidden',
162                                                'name'  => 'icao',
163                                                'value' => $icao)));
164
165             $caption = (empty($cc) ? _("Submit country") : _("Change country"));
166             $p1->pushContent(HTML::input(array('type'  => 'submit',
167                                                'value' => $caption)));
168
169             $html->pushContent(HTML::form($form_arg, $p1));
170
171             if (!empty($cc)) {
172                 /* We have selected a country, now display a list with
173                  * the available stations in that country: */
174                 $p2 = HTML::p();
175
176                 /* We need the country code after the form is submitted: */
177                 $p2->pushContent(HTML::input(array('type'  => 'hidden',
178                                                    'name'  => 'cc',
179                                                    'value' => $cc)));
180
181                 $p2->pushContent(new RawXml(get_stations_select($w, $cc, $icao)));
182                 $p2->pushContent(new RawXml(get_languages_select($language)));
183                 $p2->pushContent(HTML::input(array('type'  => 'submit',
184                                                    'value' => _("Submit location"))));
185
186                 $html->pushContent(HTML::form($form_arg, $p2));
187
188             }
189
190         }
191
192         return $html;
193     }
194 };
195
196 // Local Variables:
197 // mode: php
198 // tab-width: 8
199 // c-basic-offset: 4
200 // c-hanging-comment-ender-p: nil
201 // indent-tabs-mode: nil
202 // End:
203 ?>