3 * Name: Current Weather
4 * Description: Shows current weather conditions for user's location on their network page.
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>
11 use Cmfcmf\OpenWeatherMap;
12 use Cmfcmf\OpenWeatherMap\Exception as OWMException;
14 // Must point to composer's autoload file.
15 require('vendor/autoload.php');
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');
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');
32 function curweather_network_mod_init(&$fk_app,&$b) {
34 if(! intval(get_pconfig(local_user(),'curweather','curweather_enable')))
37 $fk_app->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $fk_app->get_baseurl() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n";
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');
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');
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();
56 $weather = $owm->getWeather($rpt, $units, $lang, $appid);
57 $temp = $weather->temperature->getValue();
58 if ( $units === 'metric') {
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() . ').');
73 $curweather = '<div id="curweather-network" class="widget">
74 <div class="title tool">
75 <h4>'.t("Current Weather").': '.$weather->city->name.'</h4></div>';
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>';
83 $curweather .= '</div><div class="clear"></div>';
85 $fk_app->page['aside'] = $curweather.$fk_app->page['aside'];
90 function curweather_plugin_settings_post($a,$post) {
91 if(! local_user() || (! x($_POST,'curweather-settings-submit')))
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']));
97 info( t('Current Weather settings updated.') . EOL);
101 function curweather_plugin_settings(&$a,&$s) {
106 /* Get the current state of our config variable */
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');
112 $noappidtext = t('No APPID found, please contact your admin to optain one.');
116 $enable = intval(get_pconfig(local_user(),'curweather','curweather_enable'));
117 $enable_checked = (($enable) ? ' checked="checked" ' : '');
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, '')
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())
138 if ($_POST['curweather-submit']) {
139 set_config('curweather','appid',trim($_POST['appid']));
140 info( t('Curweather settings saved.'.EOL));
143 function curweather_plugin_admin (&$a, &$o) {
144 if(! is_site_admin())
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'))