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