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