]> git.mxchange.org Git - friendica-addons.git/blob - curweather/curweather.php
typo
[friendica-addons.git] / curweather / curweather.php
1 <?php
2 /**
3  * Name: Current Weather 
4  * Description: Shows current weather conditions for user's location on their network page.
5  * Version: 1.1
6  * Author: Tony Baldwin <http://friendica.tonybaldwin.info/u/t0ny>
7  * Author: Fabio Comuni <http://kirkgroup.com/u/fabrixxm>
8  * Author: Tobias Diekershoff <https://f.diekershoff.de/u/tobias>
9  *
10  */
11 use Cmfcmf\OpenWeatherMap;
12 use Cmfcmf\OpenWeatherMap\Exception as OWMException;
13
14 // Must point to composer's autoload file.
15 require('vendor/autoload.php');
16
17 function curweather_install() {
18         register_hook('network_mod_init', 'addon/curweather/curweather.php', 'curweather_network_mod_init');
19         register_hook('plugin_settings', 'addon/curweather/curweather.php', 'curweather_plugin_settings');
20         register_hook('plugin_settings_post', 'addon/curweather/curweather.php', 'curweather_plugin_settings_post');
21
22 }
23
24 function curweather_uninstall() {
25         unregister_hook('network_mod_init', 'addon/curweather/curweather.php', 'curweather_network_mod_init');
26         unregister_hook('plugin_settings', 'addon/curweather/curweather.php', 'curweather_plugin_settings');
27         unregister_hook('plugin_settings_post', 'addon/curweather/curweather.php', 'curweather_plugin_settings_post');
28
29 }
30
31
32 function curweather_network_mod_init(&$fk_app,&$b) {
33
34     if(! intval(get_pconfig(local_user(),'curweather','curweather_enable')))
35         return;
36
37     $fk_app->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $fk_app->get_baseurl() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n";
38
39     // the getweather file does all the work here
40     // the $rpt value is needed for location
41     // which getweather uses to fetch the weather data for weather and temp
42     $rpt = get_pconfig(local_user(), 'curweather', 'curweather_loc');
43
44
45     //  set the language to the browsers language and use metric units
46     $lang = $_SESSION['language'];
47     $units = get_pconfig( local_user(), 'curweather', 'curweather_units');
48     $appid = get_config('curweather','appid');
49     if ($units==="")
50         $units = 'metric';
51     // Get OpenWeatherMap object. Don't use caching (take a look into
52     // Example_Cache.php to see how it works).
53     $owm = new OpenWeatherMap();
54     
55     try {
56         $weather = $owm->getWeather($rpt, $units, $lang, $appid);
57         $temp = $weather->temperature->getValue();
58         if ( $units === 'metric') {
59             $temp .= '°C';
60         } else {
61             $temp .= '°F';
62         };
63         $rhumid = $weather->humidity;
64         $pressure = $weather->pressure;
65         $wind = $weather->wind->speed . " " . $weather->wind->direction;
66         $description = $weather->clouds->getDescription();
67     } catch(OWMException $e) {
68         alert ( 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').');
69     } catch(\Exception $e) {
70         alert ('General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').');
71     }
72
73     $curweather = '<div id="curweather-network" class="widget">
74                 <div class="title tool">
75                 <h4>'.t("Current Weather").': '.$weather->city->name.'</h4></div>';
76
77     $curweather .= "$description; $temp<br />";
78     $curweather .= t('Relative Humidity').": $rhumid<br />";
79     $curweather .= t('Pressure').": $pressure<br />";
80     $curweather .= t('Wind').": $wind<br />";
81     $curweather .= '<span style="font-size:0.8em;">'. t('Data by').': <a href="http://openweathermap.org">OpenWeatherMap</a>. <a href="http://openweathermap.org/Maps?zoom=7&lat='.$weather->city->lat.'&lon='.$weather->city->lon.'&layers=B0FTTFF">'.t('Show on map').'</a></span>';
82
83     $curweather .= '</div><div class="clear"></div>';
84
85     $fk_app->page['aside'] = $curweather.$fk_app->page['aside'];
86
87 }
88
89
90 function curweather_plugin_settings_post($a,$post) {
91         if(! local_user() || (! x($_POST,'curweather-settings-submit')))
92                 return;
93         set_pconfig(local_user(),'curweather','curweather_loc',trim($_POST['curweather_loc']));
94         set_pconfig(local_user(),'curweather','curweather_enable',intval($_POST['curweather_enable']));
95         set_pconfig(local_user(),'curweather','curweather_units',trim($_POST['curweather_units']));
96
97         info( t('Current Weather settings updated.') . EOL);
98 }
99
100
101 function curweather_plugin_settings(&$a,&$s) {
102
103         if(! local_user())
104                 return;
105
106         /* Get the current state of our config variable */
107
108         $curweather_loc = get_pconfig(local_user(), 'curweather', 'curweather_loc');
109         $curweather_units = get_pconfig(local_user(), 'curweather', 'curweather_units');
110         $appid = get_config('curweather','appid');
111         if (x($appid)) { 
112                 $noappidtext = t('No APPID found, please contact your admin to optain one.');
113         } else {
114             $noappidtext = '';
115         }
116         $enable = intval(get_pconfig(local_user(),'curweather','curweather_enable'));
117         $enable_checked = (($enable) ? ' checked="checked" ' : '');
118         
119         // load template and replace the macros
120         $t = get_markup_template("settings.tpl", "addon/curweather/" );
121         $s = replace_macros ($t, array(
122                 '$submit' => t('Save Settings'),            
123                 '$header' => t('curweather Settings'),
124                 '$noappidtext' => t('No APPID found, please contact your admin to optain one.'),
125                 '$info' => t('Enter either the name of your location or the zip code.'),
126                 '$curweather_loc' => array( 'curweather_loc', t('Your Location'), $curweather_loc, t('Identifier of your location (name or zip code), e.g. <em>Berlin,DE</em> or <em>14476,DE</em>.') ),
127                 '$curweather_units' => array( 'curweather_units', t('Units'), $curweather_units, t('select if the temperatur should be displayed in °C or °F'), array('metric'=>'°C', 'imperial'=>'°F')),
128                 '$enabled' => array( 'curweather_enable', t('Show weather data'), $enable, '')
129             ));
130         return;
131
132 }
133 // Config stuff for the admin panel to let the admin of the node set a APPID
134 // for accessing the API of openweathermap
135 function curweather_plugin_admin_post (&$a) {
136         if(! is_site_admin())
137             return;
138         if ($_POST['curweather-submit']) {
139             set_config('curweather','appid',trim($_POST['appid']));
140             info( t('Curweather settings saved.'.EOL));
141         }
142 }
143 function curweather_plugin_admin (&$a, &$o) {
144     if(! is_site_admin())
145             return;
146     $appid = get_config('curweather','appid');
147     $t = get_markup_template("admin.tpl", "addon/curweather/" );
148     $o = replace_macros ($t, array(
149         '$submit' => t('Save Settings'),
150         '$appid' => array('appid', t('Your APPID'), $appid, t('Your API key provided by OpenWeatherMap'))
151     ));
152 }