.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Services;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Http\Exceptions\HttpServiceUnavailableException;
use Fisharebest\Webtrees\Http\RequestHandlers\ModulesMapProvidersPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\ModuleMapProviderInterface;
/**
* Generate the configuration data needed to create a LeafletJs map.
*/
class LeafletJsService
{
private ModuleService $module_service;
/**
* @param ModuleService $module_service
*/
public function __construct(ModuleService $module_service)
{
$this->module_service = $module_service;
}
/**
* @return object
*/
public function config(): object
{
$default = 'openstreetmap';
$map_providers = $this->module_service
->findByInterface(ModuleMapProviderInterface::class)
->map(static fn(ModuleMapProviderInterface $map_provider): object => (object) [
'children' => $map_provider->leafletJsTileLayers(),
'collapsed' => true,
'default' => $map_provider->name() === $default,
'label' => $map_provider->title(),
])
->values();
if ($map_providers->isEmpty()) {
$message = I18N::translate('To display a map, you need to enable a map-provider in the control panel.');
if (Auth::isAdmin()) {
$url = route(ModulesMapProvidersPage::class);
$message .= ' — ' . I18N::translate('Map providers') . '';
}
throw new HttpServiceUnavailableException($message);
}
$enter_fullscreen_icon = '' . view('icons/enter-fullscreen') . '';
$exit_fullscreen_icon = '' . view('icons/exit-fullscreen') . '';
return (object) [
'i18n' => [
'reset' => I18N::translate('Reload map'),
'zoomIn' => I18N::translate('Zoom in'),
'zoomOut' => I18N::translate('Zoom out'),
],
'icons' => [
'collapse' => view('icons/collapse'),
'expand' => view('icons/expand'),
'reset' => view('icons/undo'),
'fullScreen' => $enter_fullscreen_icon . $exit_fullscreen_icon,
],
'mapProviders' => $map_providers,
];
}
}