xref: /webtrees/app/Module/PedigreeMapModule.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
11f374598SGreg Roach<?php
23976b470SGreg Roach
31f374598SGreg Roach/**
41f374598SGreg Roach * webtrees: online genealogy
590949315SGreg Roach * Copyright (C) 2021 webtrees development team
61f374598SGreg Roach * This program is free software: you can redistribute it and/or modify
71f374598SGreg Roach * it under the terms of the GNU General Public License as published by
81f374598SGreg Roach * the Free Software Foundation, either version 3 of the License, or
91f374598SGreg Roach * (at your option) any later version.
101f374598SGreg Roach * This program is distributed in the hope that it will be useful,
111f374598SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
121f374598SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131f374598SGreg Roach * GNU General Public License for more details.
141f374598SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
161f374598SGreg Roach */
17fcfa147eSGreg Roach
181f374598SGreg Roachdeclare(strict_types=1);
191f374598SGreg Roach
201f374598SGreg Roachnamespace Fisharebest\Webtrees\Module;
211f374598SGreg Roach
2271378461SGreg Roachuse Aura\Router\RouterContainer;
2371378461SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
2471378461SGreg Roachuse Fisharebest\Webtrees\Auth;
251f374598SGreg Roachuse Fisharebest\Webtrees\Fact;
26752e4449SGreg Roachuse Fisharebest\Webtrees\Gedcom;
271f374598SGreg Roachuse Fisharebest\Webtrees\I18N;
281f374598SGreg Roachuse Fisharebest\Webtrees\Individual;
291f374598SGreg Roachuse Fisharebest\Webtrees\Menu;
30c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\PlaceLocation;
31c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Registry;
32aca28033SGreg Roachuse Fisharebest\Webtrees\Services\ChartService;
33c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Services\LeafletJsService;
346fcafd02SGreg Roachuse Fisharebest\Webtrees\Services\RelationshipService;
35*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
366ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
376ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3871378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
39752e4449SGreg Roach
409e18e23bSGreg Roachuse function app;
41c9a496a6SGreg Roachuse function array_key_exists;
429e18e23bSGreg Roachuse function assert;
43abaef046SGreg Roachuse function intdiv;
4411b6f9c6SGreg Roachuse function redirect;
4511b6f9c6SGreg Roachuse function route;
46c9a496a6SGreg Roachuse function ucfirst;
4771378461SGreg Roachuse function view;
481f374598SGreg Roach
491f374598SGreg Roach/**
501f374598SGreg Roach * Class PedigreeMapModule
511f374598SGreg Roach */
5271378461SGreg Roachclass PedigreeMapModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
531f374598SGreg Roach{
5449a243cbSGreg Roach    use ModuleChartTrait;
5549a243cbSGreg Roach
5672f04adfSGreg Roach    protected const ROUTE_URL = '/tree/{tree}/pedigree-map-{generations}/{xref}';
5771378461SGreg Roach
58e759aebbSGreg Roach    // Defaults
59e759aebbSGreg Roach    public const DEFAULT_GENERATIONS = '4';
6071378461SGreg Roach    public const DEFAULT_PARAMETERS  = [
6171378461SGreg Roach        'generations' => self::DEFAULT_GENERATIONS,
6271378461SGreg Roach    ];
63e759aebbSGreg Roach
64e759aebbSGreg Roach    // Limits
65*b55cbc6bSGreg Roach    public const MINIMUM_GENERATIONS = 1;
66e759aebbSGreg Roach    public const MAXIMUM_GENERATIONS = 10;
67e759aebbSGreg Roach
680ef51d00SGreg Roach    // CSS colors defined for each generation
690ef51d00SGreg Roach    private const GENERATION_COLORS = 12;
70c9a496a6SGreg Roach
71c9c6f2ecSGreg Roach    private ChartService $chart_service;
72c9c6f2ecSGreg Roach
73c9c6f2ecSGreg Roach    private LeafletJsService $leaflet_js_service;
7457ab2231SGreg Roach
7557ab2231SGreg Roach    /**
7657ab2231SGreg Roach     * PedigreeMapModule constructor.
7757ab2231SGreg Roach     *
7857ab2231SGreg Roach     * @param ChartService     $chart_service
79c9c6f2ecSGreg Roach     * @param LeafletJsService $leaflet_js_service
8057ab2231SGreg Roach     */
81c9c6f2ecSGreg Roach    public function __construct(ChartService $chart_service, LeafletJsService $leaflet_js_service)
823976b470SGreg Roach    {
8357ab2231SGreg Roach        $this->chart_service      = $chart_service;
84c9c6f2ecSGreg Roach        $this->leaflet_js_service = $leaflet_js_service;
8557ab2231SGreg Roach    }
8657ab2231SGreg Roach
87961ec755SGreg Roach    /**
8871378461SGreg Roach     * Initialization.
8971378461SGreg Roach     *
909e18e23bSGreg Roach     * @return void
9171378461SGreg Roach     */
929e18e23bSGreg Roach    public function boot(): void
9371378461SGreg Roach    {
949e18e23bSGreg Roach        $router_container = app(RouterContainer::class);
959e18e23bSGreg Roach        assert($router_container instanceof RouterContainer);
969e18e23bSGreg Roach
9771378461SGreg Roach        $router_container->getMap()
9872f04adfSGreg Roach            ->get(static::class, static::ROUTE_URL, $this)
9971378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST)
10071378461SGreg Roach            ->tokens([
10171378461SGreg Roach                'generations' => '\d+',
10271378461SGreg Roach            ]);
10371378461SGreg Roach    }
10471378461SGreg Roach
10571378461SGreg Roach    /**
1060cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
107961ec755SGreg Roach     *
108961ec755SGreg Roach     * @return string
109961ec755SGreg Roach     */
11049a243cbSGreg Roach    public function title(): string
1111f374598SGreg Roach    {
112bbb76c12SGreg Roach        /* I18N: Name of a module */
113bbb76c12SGreg Roach        return I18N::translate('Pedigree map');
1141f374598SGreg Roach    }
1151f374598SGreg Roach
116961ec755SGreg Roach    /**
117961ec755SGreg Roach     * A sentence describing what this module does.
118961ec755SGreg Roach     *
119961ec755SGreg Roach     * @return string
120961ec755SGreg Roach     */
12149a243cbSGreg Roach    public function description(): string
1221f374598SGreg Roach    {
12371378461SGreg Roach        /* I18N: Description of the “Pedigree map” module */
124bbb76c12SGreg Roach        return I18N::translate('Show the birthplace of ancestors on a map.');
1251f374598SGreg Roach    }
1261f374598SGreg Roach
1271f374598SGreg Roach    /**
128377a2979SGreg Roach     * CSS class for the URL.
129377a2979SGreg Roach     *
130377a2979SGreg Roach     * @return string
131377a2979SGreg Roach     */
132377a2979SGreg Roach    public function chartMenuClass(): string
133377a2979SGreg Roach    {
134377a2979SGreg Roach        return 'menu-chart-pedigreemap';
135377a2979SGreg Roach    }
136377a2979SGreg Roach
137377a2979SGreg Roach    /**
1381f374598SGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1391f374598SGreg Roach     *
1401f374598SGreg Roach     * @param Individual $individual
1411f374598SGreg Roach     *
14249a243cbSGreg Roach     * @return Menu|null
1431f374598SGreg Roach     */
144377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
1451f374598SGreg Roach    {
146e6562982SGreg Roach        return $this->chartMenu($individual);
147e6562982SGreg Roach    }
148e6562982SGreg Roach
149e6562982SGreg Roach    /**
150e6562982SGreg Roach     * The title for a specific instance of this chart.
151e6562982SGreg Roach     *
152e6562982SGreg Roach     * @param Individual $individual
153e6562982SGreg Roach     *
154e6562982SGreg Roach     * @return string
155e6562982SGreg Roach     */
156e6562982SGreg Roach    public function chartTitle(Individual $individual): string
157e6562982SGreg Roach    {
158e6562982SGreg Roach        /* I18N: %s is an individual’s name */
15939ca88baSGreg Roach        return I18N::translate('Pedigree map of %s', $individual->fullName());
160e6562982SGreg Roach    }
161e6562982SGreg Roach
162e6562982SGreg Roach    /**
16371378461SGreg Roach     * The URL for a page showing chart options.
164e6562982SGreg Roach     *
165e6562982SGreg Roach     * @param Individual                                $individual
16676d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
167e6562982SGreg Roach     *
168e6562982SGreg Roach     * @return string
169e6562982SGreg Roach     */
170e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
171e6562982SGreg Roach    {
17272f04adfSGreg Roach        return route(static::class, [
17371378461SGreg Roach                'tree' => $individual->tree()->name(),
174e6562982SGreg Roach                'xref' => $individual->xref(),
17571378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
176e6562982SGreg Roach    }
177e6562982SGreg Roach
178e6562982SGreg Roach    /**
1796ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1801f374598SGreg Roach     *
1816ccdf4f0SGreg Roach     * @return ResponseInterface
1821f374598SGreg Roach     */
18398579324SDavid Drury    public function handle(ServerRequestInterface $request): ResponseInterface
18498579324SDavid Drury    {
185*b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
186*b55cbc6bSGreg Roach        $user        = Validator::attributes($request)->user();
187*b55cbc6bSGreg Roach        $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations');
188*b55cbc6bSGreg Roach        $xref        = Validator::attributes($request)->isXref()->string('xref');
18998579324SDavid Drury
19098579324SDavid Drury        // Convert POST requests into GET requests for pretty URLs.
19198579324SDavid Drury        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
19298579324SDavid Drury            return redirect(route(static::class, [
19398579324SDavid Drury                'tree'        => $tree->name(),
194*b55cbc6bSGreg Roach                'xref'        => Validator::parsedBody($request)->string('xref', ''),
195*b55cbc6bSGreg Roach                'generations' => Validator::parsedBody($request)->string('generations', ''),
19698579324SDavid Drury            ]));
19798579324SDavid Drury        }
19898579324SDavid Drury
199*b55cbc6bSGreg Roach        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
200*b55cbc6bSGreg Roach
201*b55cbc6bSGreg Roach        $individual  = Registry::individualFactory()->make($xref, $tree);
202*b55cbc6bSGreg Roach        $individual  = Auth::checkIndividualAccess($individual, false, true);
203*b55cbc6bSGreg Roach
20498579324SDavid Drury        $map = view('modules/pedigree-map/chart', [
20598579324SDavid Drury            'data'           => $this->getMapData($request),
206c9c6f2ecSGreg Roach            'leaflet_config' => $this->leaflet_js_service->config(),
20798579324SDavid Drury        ]);
20898579324SDavid Drury
20998579324SDavid Drury        return $this->viewResponse('modules/pedigree-map/page', [
21098579324SDavid Drury            'module'         => $this->name(),
21198579324SDavid Drury            /* I18N: %s is an individual’s name */
21298579324SDavid Drury            'title'          => I18N::translate('Pedigree map of %s', $individual->fullName()),
21398579324SDavid Drury            'tree'           => $tree,
21498579324SDavid Drury            'individual'     => $individual,
21598579324SDavid Drury            'generations'    => $generations,
21698579324SDavid Drury            'maxgenerations' => self::MAXIMUM_GENERATIONS,
21798579324SDavid Drury            'map'            => $map,
21898579324SDavid Drury        ]);
21998579324SDavid Drury    }
22098579324SDavid Drury
22198579324SDavid Drury    /**
22298579324SDavid Drury     * @param ServerRequestInterface $request
22398579324SDavid Drury     *
22429eb5762SGreg Roach     * @return array<mixed> $geojson
22598579324SDavid Drury     */
22698579324SDavid Drury    private function getMapData(ServerRequestInterface $request): array
2271f374598SGreg Roach    {
22857ab2231SGreg Roach        $facts = $this->getPedigreeMapFacts($request, $this->chart_service);
2291f374598SGreg Roach
2301f374598SGreg Roach        $geojson = [
2311f374598SGreg Roach            'type'     => 'FeatureCollection',
2321f374598SGreg Roach            'features' => [],
2331f374598SGreg Roach        ];
2347d988ec3SGreg Roach
2357d988ec3SGreg Roach        $sosa_points = [];
2367d988ec3SGreg Roach
237620da733SGreg Roach        foreach ($facts as $sosa => $fact) {
2385333da53SGreg Roach            $location = new PlaceLocation($fact->place()->gedcomName());
2398af6bbf8SGreg Roach
2408af6bbf8SGreg Roach            // Use the co-ordinates from the fact (if they exist).
2418af6bbf8SGreg Roach            $latitude  = $fact->latitude();
2428af6bbf8SGreg Roach            $longitude = $fact->longitude();
2438af6bbf8SGreg Roach
2448af6bbf8SGreg Roach            // Use the co-ordinates from the location otherwise.
24590949315SGreg Roach            if ($latitude === null || $longitude === null) {
2468af6bbf8SGreg Roach                $latitude  = $location->latitude();
2478af6bbf8SGreg Roach                $longitude = $location->longitude();
2488af6bbf8SGreg Roach            }
2498af6bbf8SGreg Roach
25090949315SGreg Roach            if ($latitude !== null && $longitude !== null) {
2511f374598SGreg Roach                $polyline           = null;
252620da733SGreg Roach                $sosa_points[$sosa] = [$latitude, $longitude];
253620da733SGreg Roach                $sosa_child         = intdiv($sosa, 2);
2540ef51d00SGreg Roach                $color              = 'var(--wt-pedigree-map-gen-' . $sosa_child % self::GENERATION_COLORS . ')';
2550d123f04SDavid Drury
2560d123f04SDavid Drury                if (array_key_exists($sosa_child, $sosa_points)) {
2571f374598SGreg Roach                    // Would like to use a GeometryCollection to hold LineStrings
2581f374598SGreg Roach                    // rather than generate polylines but the MarkerCluster library
2591f374598SGreg Roach                    // doesn't seem to like them
2601f374598SGreg Roach                    $polyline = [
2611f374598SGreg Roach                        'points'  => [
2620d123f04SDavid Drury                            $sosa_points[$sosa_child],
2638af6bbf8SGreg Roach                            [$latitude, $longitude],
2641f374598SGreg Roach                        ],
2651f374598SGreg Roach                        'options' => [
2661f374598SGreg Roach                            'color' => $color,
2671f374598SGreg Roach                        ],
2681f374598SGreg Roach                    ];
2691f374598SGreg Roach                }
2701f374598SGreg Roach                $geojson['features'][] = [
2711f374598SGreg Roach                    'type'       => 'Feature',
272620da733SGreg Roach                    'id'         => $sosa,
2731f374598SGreg Roach                    'geometry'   => [
2741f374598SGreg Roach                        'type'        => 'Point',
2758af6bbf8SGreg Roach                        'coordinates' => [$longitude, $latitude],
2761f374598SGreg Roach                    ],
2771f374598SGreg Roach                    'properties' => [
2781f374598SGreg Roach                        'polyline'  => $polyline,
2793130efd4SGreg Roach                        'iconcolor' => $color,
280497231cdSGreg Roach                        'tooltip'   => $fact->place()->gedcomName(),
281620da733SGreg Roach                        'summary'   => view('modules/pedigree-map/events', [
282620da733SGreg Roach                            'fact'         => $fact,
283620da733SGreg Roach                            'relationship' => ucfirst($this->getSosaName($sosa)),
284620da733SGreg Roach                            'sosa'         => $sosa,
285620da733SGreg Roach                        ]),
2861f374598SGreg Roach                    ],
2871f374598SGreg Roach                ];
2881f374598SGreg Roach            }
2891f374598SGreg Roach        }
2907d988ec3SGreg Roach
29198579324SDavid Drury        return $geojson;
29271378461SGreg Roach    }
29371378461SGreg Roach
29471378461SGreg Roach    /**
29571378461SGreg Roach     * @param ServerRequestInterface $request
2966ccdf4f0SGreg Roach     * @param ChartService           $chart_service
2976ccdf4f0SGreg Roach     *
298fc26b4f6SGreg Roach     * @return array<Fact>
2996ccdf4f0SGreg Roach     */
30057ab2231SGreg Roach    private function getPedigreeMapFacts(ServerRequestInterface $request, ChartService $chart_service): array
3016ccdf4f0SGreg Roach    {
302*b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
303*b55cbc6bSGreg Roach        $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations');
304*b55cbc6bSGreg Roach        $xref        = Validator::attributes($request)->isXref()->string('xref');
3056b9cb339SGreg Roach        $individual  = Registry::individualFactory()->make($xref, $tree);
306*b55cbc6bSGreg Roach        $individual  = Auth::checkIndividualAccess($individual, false, true);
3076ccdf4f0SGreg Roach        $ancestors   = $chart_service->sosaStradonitzAncestors($individual, $generations);
3086ccdf4f0SGreg Roach        $facts       = [];
309*b55cbc6bSGreg Roach
3106ccdf4f0SGreg Roach        foreach ($ancestors as $sosa => $person) {
3116ccdf4f0SGreg Roach            if ($person->canShow()) {
312752e4449SGreg Roach                $birth = $person->facts(Gedcom::BIRTH_EVENTS, true)
31319d319e7SGreg Roach                    ->first(static function (Fact $fact): bool {
314752e4449SGreg Roach                        return $fact->place()->gedcomName() !== '';
31519d319e7SGreg Roach                    });
316752e4449SGreg Roach
317752e4449SGreg Roach                if ($birth instanceof Fact) {
3186ccdf4f0SGreg Roach                    $facts[$sosa] = $birth;
3196ccdf4f0SGreg Roach                }
3206ccdf4f0SGreg Roach            }
3216ccdf4f0SGreg Roach        }
3226ccdf4f0SGreg Roach
3236ccdf4f0SGreg Roach        return $facts;
3241f374598SGreg Roach    }
3251f374598SGreg Roach
3261f374598SGreg Roach    /**
32731e26437SGreg Roach     * builds and returns sosa relationship name in the active language
32831e26437SGreg Roach     *
32931e26437SGreg Roach     * @param int $sosa Sosa number
33031e26437SGreg Roach     *
33131e26437SGreg Roach     * @return string
33231e26437SGreg Roach     */
33331e26437SGreg Roach    private function getSosaName(int $sosa): string
33431e26437SGreg Roach    {
33531e26437SGreg Roach        $path = '';
33631e26437SGreg Roach
33731e26437SGreg Roach        while ($sosa > 1) {
33831e26437SGreg Roach            if ($sosa % 2 === 1) {
33931e26437SGreg Roach                $path = 'mot' . $path;
34031e26437SGreg Roach            } else {
34131e26437SGreg Roach                $path = 'fat' . $path;
34231e26437SGreg Roach            }
34331e26437SGreg Roach            $sosa = intdiv($sosa, 2);
34431e26437SGreg Roach        }
34531e26437SGreg Roach
346*b55cbc6bSGreg Roach        $relationship_service = app(RelationshipService::class);
347*b55cbc6bSGreg Roach        assert($relationship_service instanceof RelationshipService);
348*b55cbc6bSGreg Roach
349*b55cbc6bSGreg Roach        return $relationship_service->legacyNameAlgorithm($path);
35031e26437SGreg Roach    }
3511f374598SGreg Roach}
352