4 * Description: Use Geonames service to resolve nearest populated location for given latitude, longitude
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
9 * Pre-requisite: Register a username at geonames.org
10 * and set in .htconfig.php
12 * $a->config['geonames']['username'] = 'your_username';
13 * Also visit http://geonames.org/manageaccount and enable access to the free web services
15 * When plugin is installed, the system calls the plugin
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.
24 function geonames_install() {
28 * Our plugin will attach in three places.
29 * The first is just prior to storing a local post.
33 register_hook('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
37 * Then we'll attach into the plugin settings page, and also the
38 * settings post hook so that we can create and update
43 register_hook('plugin_settings', 'addon/geonames/geonames.php', 'geonames_settings');
44 register_hook('plugin_settings_post', 'addon/geonames/geonames.php', 'geonames_settings_post');
46 logger("installed geonames");
50 function geonames_uninstall() {
54 * uninstall unregisters any hooks created with register_hook
55 * during install. It may also delete configuration settings
56 * and any other cleanup.
60 unregister_hook('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
61 unregister_hook('plugin_settings', 'addon/geonames/geonames.php', 'geonames_settings');
62 unregister_hook('plugin_settings_post', 'addon/geonames/geonames.php', 'geonames_settings_post');
65 logger("removed geonames");
70 function geonames_post_hook($a, &$item) {
74 * An item was posted on the local system.
75 * We are going to look for specific items:
76 * - A status post by a profile owner
77 * - The profile owner must have allowed our plugin
81 logger('geonames invoked');
83 if(! local_user()) /* non-zero if this is a logged in user of this system */
86 if(local_user() != $item['uid']) /* Does this person own the post? */
89 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
92 /* Retrieve our personal config setting */
94 $geo_account = get_config('geonames', 'username');
95 $active = get_pconfig(local_user(), 'geonames', 'enable');
97 if((! $geo_account) || (! $active))
100 if((! $item['coord']) || ($item['location']))
103 $coords = explode(' ',$item['coord']);
107 * OK, we're allowed to do our stuff.
111 $s = fetch_url('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
116 $xml = parse_xml_string($s);
118 if($xml->geoname->name && $xml->geoname->countryName)
119 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
122 // logger('geonames : ' . print_r($xml,true), LOGGER_DATA);
131 * Callback from the settings post function.
132 * $post contains the $_POST array.
133 * We will make sure we've got a valid user account
134 * and if so set our configuration setting for this person.
138 function geonames_settings_post($a,$post) {
139 if(! local_user() || (! x($_POST,'geonames-submit')))
141 set_pconfig(local_user(),'geonames','enable',intval($_POST['geonames']));
143 info( t('Geonames settings updated.') . EOL);
149 * Called from the Plugin Setting form.
150 * Add our own settings info to the page.
156 function geonames_settings(&$a,&$s) {
161 $geo_account = get_config('geonames', 'username');
166 /* Add our stylesheet to the page so we can make our settings look nice */
168 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/geonames/geonames.css' . '" media="all" />' . "\r\n";
170 /* Get the current state of our config variable */
172 $enabled = get_pconfig(local_user(),'geonames','enable');
174 $checked = (($enabled) ? ' checked="checked" ' : '');
176 /* Add some HTML to the existing form */
178 $s .= '<div class="settings-block">';
179 $s .= '<h3>' . t('Geonames Settings') . '</h3>';
180 $s .= '<div id="geonames-enable-wrapper">';
181 $s .= '<label id="geonames-enable-label" for="geonames-checkbox">' . t('Enable Geonames Plugin') . '</label>';
182 $s .= '<input id="geonames-checkbox" type="checkbox" name="geonames" value="1" ' . $checked . '/>';
183 $s .= '</div><div class="clear"></div>';
185 /* provide a submit button */
187 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="geonames-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';