]> git.mxchange.org Git - friendica-addons.git/blob - geonames/geonames.php
Move Config::get() to DI::config()->get()
[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 use Friendica\App;
10 use Friendica\Core\Config;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Renderer;
14 use Friendica\DI;
15 use Friendica\Util\ConfigFileLoader;
16 use Friendica\Util\Network;
17 use Friendica\Util\XML;
18
19 function geonames_install()
20 {
21         Hook::register('load_config', __FILE__, 'geonames_load_config');
22
23         /* Our addon will attach in three places.
24          * The first is just prior to storing a local post.
25          */
26
27         Hook::register('post_local', __FILE__, 'geonames_post_hook');
28
29         /* Then we'll attach into the addon settings page, and also the
30          * settings post hook so that we can create and update
31          * user preferences.
32          */
33
34         Hook::register('addon_settings', __FILE__, 'geonames_addon_settings');
35         Hook::register('addon_settings_post', __FILE__, 'geonames_addon_settings_post');
36 }
37
38 function geonames_load_config(App $a, ConfigFileLoader $loader)
39 {
40         $a->getConfigCache()->load($loader->loadAddonConfig('geonames'));
41 }
42
43 function geonames_post_hook(App $a, array &$item)
44 {
45         /* An item was posted on the local system.
46          * We are going to look for specific items:
47          *      - A status post by a profile owner
48          *      - The profile owner must have allowed our addon
49          */
50
51         Logger::log('geonames invoked');
52
53         if (!local_user()) {   /* non-zero if this is a logged in user of this system */
54                 return;
55         }
56
57         if (local_user() != $item['uid']) {   /* Does this person own the post? */
58                 return;
59         }
60
61         if ($item['parent']) {   /* If the item has a parent, this is a comment or something else, not a status post. */
62                 return;
63         }
64
65         /* Retrieve our personal config setting */
66
67         $geo_account = DI::config()->get('geonames', 'username');
68         $active = DI::pConfig()->get(local_user(), 'geonames', 'enable');
69
70         if (!$geo_account || !$active) {
71                 return;
72         }
73
74         if (!$item['coord'] || $item['location']) {
75                 return;
76         }
77
78         $coords = explode(' ', $item['coord']);
79
80         /* OK, we're allowed to do our stuff. */
81
82         $s = Network::fetchUrl('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
83
84         if (!$s) {
85                 return;
86         }
87
88         $xml = XML::parseString($s);
89
90         if ($xml->geoname->name && $xml->geoname->countryName) {
91                 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
92         }
93
94         return;
95 }
96
97 /**
98  * Callback from the settings post function.
99  * We will make sure we've got a valid user account
100  * and if so set our configuration setting for this person.
101  *
102  * @param App   $a
103  * @param array $post The $_POST array
104  */
105 function geonames_addon_settings_post(App $a, array $post)
106 {
107         if (!local_user() || empty($_POST['geonames-submit'])) {
108                 return;
109         }
110
111         DI::pConfig()->set(local_user(), 'geonames', 'enable', intval($_POST['geonames-enable']));
112
113         info(DI::l10n()->t('Geonames settings updated.'));
114 }
115
116 /**
117  * Called from the Addon Setting form.
118  * Add our own settings info to the page.
119  *
120  * @param App    $a
121  * @param string $s
122  * @throws Exception
123  */
124 function geonames_addon_settings(App $a, &$s)
125 {
126         if (!local_user()) {
127                 return;
128         }
129
130         $geo_account = DI::config()->get('geonames', 'username');
131
132         if (!$geo_account) {
133                 return;
134         }
135
136         /* Add our stylesheet to the page so we can make our settings look nice */
137         $stylesheetPath = __DIR__ . '/geonames.css';
138         DI::page()->registerStylesheet($stylesheetPath);
139
140         /* Get the current state of our config variable */
141         $enabled = intval(DI::pConfig()->get(local_user(), 'geonames', 'enable'));
142
143         $t = Renderer::getMarkupTemplate('settings.tpl', __DIR__);
144         $s .= Renderer::replaceMacros($t, [
145                 '$title' => DI::l10n()->t('Geonames Settings'),
146                 '$description' => DI::l10n()->t('Replace numerical coordinates by the nearest populated location name in your posts.'),
147                 '$enable' => ['geonames-enable', DI::l10n()->t('Enable Geonames Addon'), $enabled],
148                 '$submit' => DI::l10n()->t('Save Settings')
149         ]);
150 }