]> git.mxchange.org Git - friendica-addons.git/blob - geonames/geonames.php
Update t() calls
[friendica-addons.git] / geonames / geonames.php
1 <?php
2 /**
3  * Name: Geonames
4  * Description: Use Geonames service to resolve nearest populated location for given latitude, longitude
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  * 
8  * 
9  * Pre-requisite: Register a username at geonames.org
10  * and set in .htconfig.php
11  *
12  * $a->config['geonames']['username'] = 'your_username';
13  * Also visit http://geonames.org/manageaccount and enable access to the free web services
14  *
15  * When addon is installed, the system calls the addon
16  * name_install() function, located in 'addon/name/name.php',
17  * where 'name' is the name of the addon.
18  * If the addon is removed from the configuration list, the 
19  * system will call the name_uninstall() function.
20  *
21  */
22 use Friendica\Core\Addon;
23 use Friendica\Core\Config;
24 use Friendica\Core\L10n;
25 use Friendica\Core\PConfig;
26
27 function geonames_install() {
28
29         /**
30          * 
31          * Our addon will attach in three places.
32          * The first is just prior to storing a local post.
33          *
34          */
35
36         Addon::registerHook('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
37
38         /**
39          *
40          * Then we'll attach into the addon settings page, and also the 
41          * settings post hook so that we can create and update
42          * user preferences.
43          *
44          */
45
46         Addon::registerHook('addon_settings', 'addon/geonames/geonames.php', 'geonames_addon_admin');
47         Addon::registerHook('addon_settings_post', 'addon/geonames/geonames.php', 'geonames_addon_admin_post');
48
49         logger("installed geonames");
50 }
51
52
53 function geonames_uninstall() {
54
55         /**
56          *
57          * uninstall unregisters any hooks created with register_hook
58          * during install. It may also delete configuration settings
59          * and any other cleanup.
60          *
61          */
62
63         Addon::unregisterHook('post_local',    'addon/geonames/geonames.php', 'geonames_post_hook');
64         Addon::unregisterHook('addon_settings', 'addon/geonames/geonames.php', 'geonames_addon_admin');
65         Addon::unregisterHook('addon_settings_post', 'addon/geonames/geonames.php', 'geonames_addon_admin_post');
66
67
68         logger("removed geonames");
69 }
70
71
72
73 function geonames_post_hook($a, &$item) {
74
75         /**
76          *
77          * An item was posted on the local system.
78          * We are going to look for specific items:
79          *      - A status post by a profile owner
80          *      - The profile owner must have allowed our addon
81          *
82          */
83
84         logger('geonames invoked');
85
86         if(! local_user())   /* non-zero if this is a logged in user of this system */
87                 return;
88
89         if(local_user() != $item['uid'])    /* Does this person own the post? */
90                 return;
91
92         if($item['parent'])   /* If the item has a parent, this is a comment or something else, not a status post. */
93                 return;
94
95         /* Retrieve our personal config setting */
96
97         $geo_account = Config::get('geonames', 'username');
98         $active = PConfig::get(local_user(), 'geonames', 'enable');
99
100         if((! $geo_account) || (! $active))
101                 return;
102
103         if((! $item['coord']) || ($item['location']))
104                 return;
105
106         $coords = explode(' ',$item['coord']);
107
108         /**
109          *
110          * OK, we're allowed to do our stuff.
111          *
112          */
113
114         $s = fetch_url('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
115
116         if(! $s)
117                 return;
118
119         $xml = parse_xml_string($s);
120
121         if($xml->geoname->name && $xml->geoname->countryName)
122                 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
123
124
125 //      logger('geonames : ' . print_r($xml,true), LOGGER_DATA);
126         return;
127 }
128
129
130
131
132 /**
133  *
134  * Callback from the settings post function.
135  * $post contains the $_POST array.
136  * We will make sure we've got a valid user account
137  * and if so set our configuration setting for this person.
138  *
139  */
140
141 function geonames_addon_admin_post($a,$post) {
142         if(! local_user() || (! x($_POST,'geonames-submit')))
143                 return;
144         PConfig::set(local_user(),'geonames','enable',intval($_POST['geonames']));
145
146         info(L10n::t('Geonames settings updated.') . EOL);
147 }
148
149
150 /**
151  *
152  * Called from the Addon Setting form. 
153  * Add our own settings info to the page.
154  *
155  */
156
157
158
159 function geonames_addon_admin(&$a,&$s) {
160
161         if(! local_user())
162                 return;
163
164         $geo_account = Config::get('geonames', 'username');
165
166         if(! $geo_account)
167                 return;
168
169         /* Add our stylesheet to the page so we can make our settings look nice */
170
171         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/geonames/geonames.css' . '" media="all" />' . "\r\n";
172
173         /* Get the current state of our config variable */
174
175         $enabled = PConfig::get(local_user(),'geonames','enable');
176
177         $checked = (($enabled) ? ' checked="checked" ' : '');
178
179         /* Add some HTML to the existing form */
180
181         $s .= '<div class="settings-block">';
182         $s .= '<h3>' . L10n::t('Geonames Settings') . '</h3>';
183         $s .= '<div id="geonames-enable-wrapper">';
184         $s .= '<label id="geonames-enable-label" for="geonames-checkbox">' . L10n::t('Enable Geonames Addon') . '</label>';
185         $s .= '<input id="geonames-checkbox" type="checkbox" name="geonames" value="1" ' . $checked . '/>';
186         $s .= '</div><div class="clear"></div>';
187
188         /* provide a submit button */
189
190         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="geonames-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
191
192 }