]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PhpWeather.php
Improved error message when PHP Weather is not yet installed or configured.
[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  * This plugin requires a separate program called PhpWeather. For more
5  * information and to download PhpWeather, see:
6  *
7  *   http://sourceforge.net/projects/phpweather/
8  *
9  * Usage:
10  *
11  * <?plugin PhpWeather ?>
12  * <?plugin PhpWeather menu=true ?>
13  * <?plugin PhpWeather icao=KJFK ?>
14  * <?plugin PhpWeather lang=en ?>
15  * <?plugin PhpWeather units=only_metric ?>
16  * <?plugin PhpWeather icao||=CYYZ lang||=en menu=true ?>
17  *
18  * If you want a menu, and you also want to change the default station
19  * or language, then you have to use the ||= form, or else the user
20  * wont be able to change the station or language.
21  *
22  * The units argument should be one of only_metric, only_imperial,
23  * both_metric, or both_imperial.
24  */
25
26 // We require the base class from PHP Weather, adjust this to match
27 // the location of PhpWeather on your server:
28 $WEATHER = $_SERVER['DOCUMENT_ROOT'] . '/phpweather/phpweather.php';
29 if(! @include_once($WEATHER)) {
30     if(!in_array($WEATHER, get_included_files()) ) {
31         $error = sprintf(_("Could not open file %s."), "'$WEATHER'") . " ";
32         $error .= sprintf(_("Make sure %s is installed and properly configured."),
33                           'PHP Weather');
34         trigger_error($error);
35     }
36 }
37
38 class WikiPlugin_PhpWeather
39 extends WikiPlugin
40 {
41     function getName () {
42         return _("PhpWeather");
43     }
44
45     function getDescription () {
46         return _("The PhpWeather plugin provides weather reports from the Internet.");
47     }
48     function getDefaultArguments() {
49         global $LANG;
50         return array('icao'  => 'EKAH',
51                      'cc'    => 'DK',
52                      'lang'  => 'en',
53                      'menu'  => false,
54                      'units' => 'both_metric');
55     }
56
57     function run($dbi, $argstr, $request) {
58         // When 'phpweather/phpweather.php' is not installed then
59         // PHPWEATHER_BASE_DIR will be undefined
60         if (!defined('PHPWEATHER_BASE_DIR'))
61             return fmt("Plugin %s failed.", $this->getName()); //early return
62
63         require_once(PHPWEATHER_BASE_DIR . '/output/pw_images.php');
64         require_once(PHPWEATHER_BASE_DIR . '/pw_utilities.php');
65
66         extract($this->getArgs($argstr, $request));
67         $html = HTML();
68
69         $w = new phpweather(); // Our weather object
70
71         if (!empty($icao)) {
72             /* We assign the ICAO to the weather object: */
73             $w->set_icao($icao);
74             if (!$w->get_country_code()) {
75                 /* The country code couldn't be resolved, so we
76                  * shouldn't use the ICAO: */
77                 trigger_error(sprintf(_("The ICAO '%s' wasn't recognized."),
78                                       $icao), E_USER_NOTICE);
79                 $icao = '';
80             }
81         }
82
83         if (!empty($icao)) {
84
85             /* We check and correct the language if necessary: */
86             //if (!in_array($lang, array_keys($w->get_languages('text')))) {
87             if (!in_array($lang, array_keys(get_languages('text')))) {
88                 trigger_error(sprintf(_("%s does not know about the language '%s', using 'en' instead."),
89                                       $this->getName(), $lang), E_USER_NOTICE);
90                 $lang = 'en';
91             }
92
93             $class = "pw_text_$lang";
94             require_once(PHPWEATHER_BASE_DIR . "/output/$class.php");
95
96             $t = new $class($w);
97             $t->set_pref_units($units);
98             $i = new pw_images($w);
99
100             $i_temp = HTML::img(array('src' => $i->get_temp_image()));
101             $i_wind = HTML::img(array('src' => $i->get_winddir_image()));
102             $i_sky  = HTML::img(array('src' => $i->get_sky_image()));
103
104             $m = $t->print_pretty();
105
106             $m_td = HTML::td(HTML::p(new RawXml($m)));
107
108             $i_tr = HTML::tr();
109             $i_tr->pushContent(HTML::td($i_temp));
110             $i_tr->pushContent(HTML::td($i_wind));
111
112             $i_table = HTML::table($i_tr);
113             $i_table->pushContent(HTML::tr(HTML::td(array('colspan' => '2'),
114                                                     $i_sky)));
115
116             $tr = HTML::tr();
117             $tr->pushContent($m_td);
118             $tr->pushContent(HTML::td($i_table));
119
120             $html->pushContent(HTML::table($tr));
121
122         }
123
124         /* We make a menu if asked to, or if $icao is empty: */
125         if ($menu || empty($icao)) {
126
127             $form_arg = array('action' => $request->getURLtoSelf(),
128                               'method' => 'get');
129
130             /* The country box is always part of the menu: */
131             $p1 = HTML::p(new RawXml(get_countries_select($w, $cc)));
132
133             /* We want to save the language: */
134             $p1->pushContent(HTML::input(array('type'  => 'hidden',
135                                                'name'  => 'lang',
136                                                'value' => $lang)));
137             /* And also the ICAO: */
138             $p1->pushContent(HTML::input(array('type'  => 'hidden',
139                                                'name'  => 'icao',
140                                                'value' => $icao)));
141
142             $caption = (empty($cc) ? _("Submit country") : _("Change country"));
143             $p1->pushContent(HTML::input(array('type'  => 'submit',
144                                                'value' => $caption)));
145
146             $html->pushContent(HTML::form($form_arg, $p1));
147
148             if (!empty($cc)) {
149                 /* We have selected a country, now display a list with
150                  * the available stations in that country: */
151                 $p2 = HTML::p();
152
153                 /* We need the country code after the form is submitted: */
154                 $p2->pushContent(HTML::input(array('type'  => 'hidden',
155                                                    'name'  => 'cc',
156                                                    'value' => $cc)));
157
158                 $p2->pushContent(new RawXml(get_stations_select($w, $cc, $icao)));
159                 $p2->pushContent(new RawXml(get_languages_select($w, $lang)));
160                 $p2->pushContent(HTML::input(array('type'  => 'submit',
161                                                    'value' => 'Submit location')));
162
163                 $html->pushContent(HTML::form($form_arg, $p2));
164
165             }
166
167         }
168
169         return $html;
170     }
171 };
172
173 // For emacs users
174 // Local Variables:
175 // mode: php
176 // tab-width: 8
177 // c-basic-offset: 4
178 // c-hanging-comment-ender-p: nil
179 // indent-tabs-mode: nil
180 // End:
181 ?>