Update t() calls
[friendica-addons.git] / geocoordinates / geocoordinates.php
1 <?php
2 /**
3  * Name: Geocoordinates
4  * Description: Use the OpenCage Geocoder http://geocoder.opencagedata.com to resolve nearest populated location for given latitude, longitude. Derived from "geonames"
5  * Version: 0.1
6  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
7  */
8 use Friendica\Core\Addon;
9 use Friendica\Core\Cache;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12
13 function geocoordinates_install()
14 {
15         Addon::registerHook('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
16         Addon::registerHook('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
17 }
18
19
20 function geocoordinates_uninstall()
21 {
22         Addon::unregisterHook('post_local',    'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
23         Addon::unregisterHook('post_remote',    'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
24 }
25
26 function geocoordinates_resolve_item(&$item)
27 {
28         if((!$item["coord"]) || ($item["location"]))
29                 return;
30
31         $key = Config::get("geocoordinates", "api_key");
32         if ($key == "")
33                 return;
34
35         $language = Config::get("geocoordinates", "language");
36         if ($language == "")
37                 $language = "de";
38
39         $coords = explode(' ',$item["coord"]);
40
41         if (count($coords) < 2)
42                 return;
43
44         $coords[0] = round($coords[0], 5);
45         $coords[1] = round($coords[1], 5);
46
47         $result = Cache::get("geocoordinates:".$language.":".$coords[0]."-".$coords[1]);
48         if (!is_null($result)) {
49                 $item["location"] = $result;
50                 return;
51         }
52
53         $s = fetch_url("https://api.opencagedata.com/geocode/v1/json?q=".$coords[0].",".$coords[1]."&key=".$key."&language=".$language);
54
55         if (!$s) {
56                 logger("API could not be queried", LOGGER_DEBUG);
57                 return;
58         }
59
60         $data = json_decode($s);
61
62         if ($data->status->code != "200") {
63                 logger("API returned error ".$data->status->code." ".$data->status->message, LOGGER_DEBUG);
64                 return;
65         }
66
67         if (($data->total_results == 0) || (count($data->results) == 0)) {
68                 logger("No results found for coordinates ".$item["coord"], LOGGER_DEBUG);
69                 return;
70         }
71
72         $item["location"] = $data->results[0]->formatted;
73
74         logger("Got location for coordinates ".$coords[0]."-".$coords[1].": ".$item["location"], LOGGER_DEBUG);
75
76         if ($item["location"] != "")
77                 Cache::set("geocoordinates:".$language.":".$coords[0]."-".$coords[1], $item["location"]);
78 }
79
80 function geocoordinates_post_hook($a, &$item)
81 {
82         geocoordinates_resolve_item($item);
83 }
84
85 function geocoordinates_addon_admin(&$a, &$o)
86 {
87
88         $t = get_markup_template("admin.tpl", "addon/geocoordinates/");
89
90         $o = replace_macros($t, [
91                 '$submit' => L10n::t('Save Settings'),
92                 '$api_key' => ['api_key', L10n::t('API Key'), Config::get('geocoordinates', 'api_key'), ''],
93                 '$language' => ['language', L10n::t('Language code (IETF format)'), Config::get('geocoordinates', 'language'), ''],
94         ]);
95 }
96
97 function geocoordinates_addon_admin_post(&$a)
98 {
99         $api_key  = ((x($_POST, 'api_key')) ? notags(trim($_POST['api_key']))   : '');
100         Config::set('geocoordinates', 'api_key', $api_key);
101
102         $language  = ((x($_POST, 'language')) ? notags(trim($_POST['language']))   : '');
103         Config::set('geocoordinates', 'language', $language);
104         info(L10n::t('Settings updated.'). EOL);
105 }