- added missing type-hints
- avoided local variables
- added some documentation
- changed double-quotes to single
This commit is contained in:
Roland Häder 2022-06-30 13:32:13 +02:00
parent c0c15644a3
commit 06b15a6ed0
No known key found for this signature in database
GPG key ID: C82EDE5DDFA0BA77
29 changed files with 191 additions and 204 deletions

View file

@ -18,58 +18,63 @@ function geocoordinates_install()
Hook::register('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
}
function geocoordinates_resolve_item(&$item)
function geocoordinates_resolve_item(array &$item)
{
if((!$item["coord"]) || ($item["location"]))
if ((!$item['coord']) || ($item['location'])) {
return;
}
$key = DI::config()->get("geocoordinates", "api_key");
if ($key == "")
$key = DI::config()->get('geocoordinates', 'api_key');
if ($key == '') {
return;
}
$language = DI::config()->get("geocoordinates", "language");
if ($language == "")
$language = "de";
$language = DI::config()->get('geocoordinates', 'language');
if ($language == '') {
$language = 'de';
}
$coords = explode(' ',$item["coord"]);
$coords = explode(' ', $item['coord']);
if (count($coords) < 2)
if (count($coords) < 2) {
return;
}
$coords[0] = round($coords[0], 5);
$coords[1] = round($coords[1], 5);
$result = DI::cache()->get("geocoordinates:".$language.":".$coords[0]."-".$coords[1]);
$result = DI::cache()->get('geocoordinates:' . $language . ':' . $coords[0] . '-' . $coords[1]);
if (!is_null($result)) {
$item["location"] = $result;
$item['location'] = $result;
return;
}
$s = DI::httpClient()->fetch("https://api.opencagedata.com/geocode/v1/json?q=" . $coords[0] . "," . $coords[1] . "&key=" . $key . "&language=" . $language);
$s = DI::httpClient()->fetch('https://api.opencagedata.com/geocode/v1/json?q=' . $coords[0] . ',' . $coords[1] . '&key=' . $key . '&language=' . $language);
if (!$s) {
Logger::info("API could not be queried");
Logger::info('API could not be queried');
return;
}
$data = json_decode($s);
if ($data->status->code != "200") {
Logger::info("API returned error ".$data->status->code." ".$data->status->message);
if ($data->status->code != '200') {
Logger::info('API returned error ' . $data->status->code . ' ' . $data->status->message);
return;
}
if (($data->total_results == 0) || (count($data->results) == 0)) {
Logger::info("No results found for coordinates ".$item["coord"]);
Logger::info('No results found for coordinates ' . $item['coord']);
return;
}
$item["location"] = $data->results[0]->formatted;
$item['location'] = $data->results[0]->formatted;
Logger::info("Got location for coordinates ".$coords[0]."-".$coords[1].": ".$item["location"]);
Logger::info('Got location for coordinates ' . $coords[0] . '-' . $coords[1] . ': ' . $item['location']);
if ($item["location"] != "")
DI::cache()->set("geocoordinates:".$language.":".$coords[0]."-".$coords[1], $item["location"]);
if ($item['location'] != '') {
DI::cache()->set('geocoordinates:' . $language.':' . $coords[0] . '-' . $coords[1], $item['location']);
}
}
function geocoordinates_post_hook(App $a, &$item)
@ -77,10 +82,9 @@ function geocoordinates_post_hook(App $a, &$item)
geocoordinates_resolve_item($item);
}
function geocoordinates_addon_admin(App $a, &$o)
function geocoordinates_addon_admin(App $a, string &$o)
{
$t = Renderer::getMarkupTemplate("admin.tpl", "addon/geocoordinates/");
$t = Renderer::getMarkupTemplate('admin.tpl', 'addon/geocoordinates/');
$o = Renderer::replaceMacros($t, [
'$submit' => DI::l10n()->t('Save Settings'),
@ -91,9 +95,6 @@ function geocoordinates_addon_admin(App $a, &$o)
function geocoordinates_addon_admin_post(App $a)
{
$api_key = trim($_POST['api_key'] ?? '');
DI::config()->set('geocoordinates', 'api_key', $api_key);
$language = trim($_POST['language'] ?? '');
DI::config()->set('geocoordinates', 'language', $language);
DI::config()->set('geocoordinates', 'api_key', trim($_POST['api_key'] ?? ''));
DI::config()->set('geocoordinates', 'language', trim($_POST['language'] ?? ''));
}