xref: /webtrees/app/Module/PedigreeMapModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
11f374598SGreg Roach<?php
23976b470SGreg Roach
31f374598SGreg Roach/**
41f374598SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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 Fig\Http\Message\RequestMethodInterface;
2371378461SGreg Roachuse Fisharebest\Webtrees\Auth;
241f374598SGreg Roachuse Fisharebest\Webtrees\Fact;
25752e4449SGreg Roachuse Fisharebest\Webtrees\Gedcom;
261f374598SGreg Roachuse Fisharebest\Webtrees\I18N;
271f374598SGreg Roachuse Fisharebest\Webtrees\Individual;
281f374598SGreg Roachuse Fisharebest\Webtrees\Menu;
29c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\PlaceLocation;
30c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Registry;
31aca28033SGreg Roachuse Fisharebest\Webtrees\Services\ChartService;
32c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Services\LeafletJsService;
336fcafd02SGreg Roachuse Fisharebest\Webtrees\Services\RelationshipService;
34b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
356ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
366ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3771378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
38752e4449SGreg Roach
39c9a496a6SGreg Roachuse function array_key_exists;
40abaef046SGreg Roachuse function intdiv;
4111b6f9c6SGreg Roachuse function redirect;
4211b6f9c6SGreg Roachuse function route;
43c9a496a6SGreg Roachuse function ucfirst;
4471378461SGreg Roachuse function view;
451f374598SGreg Roach
461f374598SGreg Roach/**
471f374598SGreg Roach * Class PedigreeMapModule
481f374598SGreg Roach */
4971378461SGreg Roachclass PedigreeMapModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
501f374598SGreg Roach{
5149a243cbSGreg Roach    use ModuleChartTrait;
5249a243cbSGreg Roach
5372f04adfSGreg Roach    protected const ROUTE_URL = '/tree/{tree}/pedigree-map-{generations}/{xref}';
5471378461SGreg Roach
55e759aebbSGreg Roach    // Defaults
56e759aebbSGreg Roach    public const DEFAULT_GENERATIONS = '4';
5771378461SGreg Roach    public const DEFAULT_PARAMETERS  = [
5871378461SGreg Roach        'generations' => self::DEFAULT_GENERATIONS,
5971378461SGreg Roach    ];
60e759aebbSGreg Roach
61e759aebbSGreg Roach    // Limits
62b55cbc6bSGreg Roach    public const MINIMUM_GENERATIONS = 1;
63e759aebbSGreg Roach    public const MAXIMUM_GENERATIONS = 10;
64e759aebbSGreg Roach
65b37b2c3dSGreg Roach    // CSS colors for each generation
6650b064dfSRichard Cissée    protected const COUNT_CSS_COLORS = 12;
67c9a496a6SGreg Roach
6850b064dfSRichard Cissée    protected ChartService $chart_service;
69c9c6f2ecSGreg Roach
7050b064dfSRichard Cissée    protected LeafletJsService $leaflet_js_service;
7157ab2231SGreg Roach
7250b064dfSRichard Cissée    protected RelationshipService $relationship_service;
738f9f1cb8SGreg Roach
7457ab2231SGreg Roach    /**
7557ab2231SGreg Roach     * @param ChartService        $chart_service
76c9c6f2ecSGreg Roach     * @param LeafletJsService    $leaflet_js_service
778f9f1cb8SGreg Roach     * @param RelationshipService $relationship_service
7857ab2231SGreg Roach     */
798f9f1cb8SGreg Roach    public function __construct(
808f9f1cb8SGreg Roach        ChartService $chart_service,
818f9f1cb8SGreg Roach        LeafletJsService $leaflet_js_service,
828f9f1cb8SGreg Roach        RelationshipService $relationship_service
838f9f1cb8SGreg Roach    ) {
8457ab2231SGreg Roach        $this->chart_service      = $chart_service;
85c9c6f2ecSGreg Roach        $this->leaflet_js_service = $leaflet_js_service;
868f9f1cb8SGreg Roach        $this->relationship_service = $relationship_service;
8757ab2231SGreg Roach    }
8857ab2231SGreg Roach
89961ec755SGreg Roach    /**
9071378461SGreg Roach     * Initialization.
9171378461SGreg Roach     *
929e18e23bSGreg Roach     * @return void
9371378461SGreg Roach     */
949e18e23bSGreg Roach    public function boot(): void
9571378461SGreg Roach    {
96158900c2SGreg Roach        Registry::routeFactory()->routeMap()
9772f04adfSGreg Roach            ->get(static::class, static::ROUTE_URL, $this)
98158900c2SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST);
9971378461SGreg Roach    }
10071378461SGreg Roach
10171378461SGreg Roach    /**
1020cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
103961ec755SGreg Roach     *
104961ec755SGreg Roach     * @return string
105961ec755SGreg Roach     */
10649a243cbSGreg Roach    public function title(): string
1071f374598SGreg Roach    {
108bbb76c12SGreg Roach        /* I18N: Name of a module */
109bbb76c12SGreg Roach        return I18N::translate('Pedigree map');
1101f374598SGreg Roach    }
1111f374598SGreg Roach
11249a243cbSGreg Roach    public function description(): string
1131f374598SGreg Roach    {
11471378461SGreg Roach        /* I18N: Description of the “Pedigree map” module */
115bbb76c12SGreg Roach        return I18N::translate('Show the birthplace of ancestors on a map.');
1161f374598SGreg Roach    }
1171f374598SGreg Roach
1181f374598SGreg Roach    /**
119377a2979SGreg Roach     * CSS class for the URL.
120377a2979SGreg Roach     *
121377a2979SGreg Roach     * @return string
122377a2979SGreg Roach     */
123377a2979SGreg Roach    public function chartMenuClass(): string
124377a2979SGreg Roach    {
125377a2979SGreg Roach        return 'menu-chart-pedigreemap';
126377a2979SGreg Roach    }
127377a2979SGreg Roach
128377a2979SGreg Roach    /**
1291f374598SGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1301f374598SGreg Roach     *
1311f374598SGreg Roach     * @param Individual $individual
1321f374598SGreg Roach     *
13349a243cbSGreg Roach     * @return Menu|null
1341f374598SGreg Roach     */
135*1ff45046SGreg Roach    public function chartBoxMenu(Individual $individual): Menu|null
1361f374598SGreg Roach    {
137e6562982SGreg Roach        return $this->chartMenu($individual);
138e6562982SGreg Roach    }
139e6562982SGreg Roach
140e6562982SGreg Roach    /**
141e6562982SGreg Roach     * The title for a specific instance of this chart.
142e6562982SGreg Roach     *
143e6562982SGreg Roach     * @param Individual $individual
144e6562982SGreg Roach     *
145e6562982SGreg Roach     * @return string
146e6562982SGreg Roach     */
147e6562982SGreg Roach    public function chartTitle(Individual $individual): string
148e6562982SGreg Roach    {
149e6562982SGreg Roach        /* I18N: %s is an individual’s name */
15039ca88baSGreg Roach        return I18N::translate('Pedigree map of %s', $individual->fullName());
151e6562982SGreg Roach    }
152e6562982SGreg Roach
153e6562982SGreg Roach    /**
15471378461SGreg Roach     * The URL for a page showing chart options.
155e6562982SGreg Roach     *
156e6562982SGreg Roach     * @param Individual                                $individual
15776d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
158e6562982SGreg Roach     *
159e6562982SGreg Roach     * @return string
160e6562982SGreg Roach     */
161e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
162e6562982SGreg Roach    {
16372f04adfSGreg Roach        return route(static::class, [
16471378461SGreg Roach                'tree' => $individual->tree()->name(),
165e6562982SGreg Roach                'xref' => $individual->xref(),
16671378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
167e6562982SGreg Roach    }
168e6562982SGreg Roach
169e6562982SGreg Roach    /**
1706ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1711f374598SGreg Roach     *
1726ccdf4f0SGreg Roach     * @return ResponseInterface
1731f374598SGreg Roach     */
17498579324SDavid Drury    public function handle(ServerRequestInterface $request): ResponseInterface
17598579324SDavid Drury    {
176b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
177b55cbc6bSGreg Roach        $user        = Validator::attributes($request)->user();
178b55cbc6bSGreg Roach        $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations');
179b55cbc6bSGreg Roach        $xref        = Validator::attributes($request)->isXref()->string('xref');
18098579324SDavid Drury
18198579324SDavid Drury        // Convert POST requests into GET requests for pretty URLs.
18298579324SDavid Drury        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
18398579324SDavid Drury            return redirect(route(static::class, [
18498579324SDavid Drury                'tree'        => $tree->name(),
185158900c2SGreg Roach                'xref'        => Validator::parsedBody($request)->isXref()->string('xref'),
186158900c2SGreg Roach                'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'),
18798579324SDavid Drury            ]));
18898579324SDavid Drury        }
18998579324SDavid Drury
190b55cbc6bSGreg Roach        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
191b55cbc6bSGreg Roach
192b55cbc6bSGreg Roach        $individual  = Registry::individualFactory()->make($xref, $tree);
193b55cbc6bSGreg Roach        $individual  = Auth::checkIndividualAccess($individual, false, true);
194b55cbc6bSGreg Roach
19598579324SDavid Drury        $map = view('modules/pedigree-map/chart', [
19698579324SDavid Drury            'data'           => $this->getMapData($request),
197c9c6f2ecSGreg Roach            'leaflet_config' => $this->leaflet_js_service->config(),
19898579324SDavid Drury        ]);
19998579324SDavid Drury
20098579324SDavid Drury        return $this->viewResponse('modules/pedigree-map/page', [
20198579324SDavid Drury            'module'         => $this->name(),
20298579324SDavid Drury            /* I18N: %s is an individual’s name */
20398579324SDavid Drury            'title'          => I18N::translate('Pedigree map of %s', $individual->fullName()),
20498579324SDavid Drury            'tree'           => $tree,
20598579324SDavid Drury            'individual'     => $individual,
20698579324SDavid Drury            'generations'    => $generations,
20798579324SDavid Drury            'maxgenerations' => self::MAXIMUM_GENERATIONS,
20898579324SDavid Drury            'map'            => $map,
20998579324SDavid Drury        ]);
21098579324SDavid Drury    }
21198579324SDavid Drury
21298579324SDavid Drury    /**
21398579324SDavid Drury     * @param ServerRequestInterface $request
21498579324SDavid Drury     *
21529eb5762SGreg Roach     * @return array<mixed> $geojson
21698579324SDavid Drury     */
21750b064dfSRichard Cissée    protected function getMapData(ServerRequestInterface $request): array
2181f374598SGreg Roach    {
21957ab2231SGreg Roach        $facts = $this->getPedigreeMapFacts($request, $this->chart_service);
2201f374598SGreg Roach
2211f374598SGreg Roach        $geojson = [
2221f374598SGreg Roach            'type'     => 'FeatureCollection',
2231f374598SGreg Roach            'features' => [],
2241f374598SGreg Roach        ];
2257d988ec3SGreg Roach
2267d988ec3SGreg Roach        $sosa_points = [];
2277d988ec3SGreg Roach
228620da733SGreg Roach        foreach ($facts as $sosa => $fact) {
2295333da53SGreg Roach            $location = new PlaceLocation($fact->place()->gedcomName());
2308af6bbf8SGreg Roach
2318af6bbf8SGreg Roach            // Use the co-ordinates from the fact (if they exist).
2328af6bbf8SGreg Roach            $latitude  = $fact->latitude();
2338af6bbf8SGreg Roach            $longitude = $fact->longitude();
2348af6bbf8SGreg Roach
2358af6bbf8SGreg Roach            // Use the co-ordinates from the location otherwise.
23690949315SGreg Roach            if ($latitude === null || $longitude === null) {
2378af6bbf8SGreg Roach                $latitude  = $location->latitude();
2388af6bbf8SGreg Roach                $longitude = $location->longitude();
2398af6bbf8SGreg Roach            }
2408af6bbf8SGreg Roach
24190949315SGreg Roach            if ($latitude !== null && $longitude !== null) {
2421f374598SGreg Roach                $polyline           = null;
243620da733SGreg Roach                $sosa_points[$sosa] = [$latitude, $longitude];
244620da733SGreg Roach                $sosa_child         = intdiv($sosa, 2);
2459dbe88c8SGreg Roach                $generation         = (int) log($sosa, 2);
2469dbe88c8SGreg Roach                $color              = 'var(--wt-pedigree-map-gen-' . $generation % self::COUNT_CSS_COLORS . ')';
2479dbe88c8SGreg Roach                $class              = 'wt-pedigree-map-gen-' . $generation % self::COUNT_CSS_COLORS;
2480d123f04SDavid Drury
2490d123f04SDavid Drury                if (array_key_exists($sosa_child, $sosa_points)) {
2501f374598SGreg Roach                    // Would like to use a GeometryCollection to hold LineStrings
2511f374598SGreg Roach                    // rather than generate polylines but the MarkerCluster library
2521f374598SGreg Roach                    // doesn't seem to like them
2531f374598SGreg Roach                    $polyline = [
2541f374598SGreg Roach                        'points'  => [
2550d123f04SDavid Drury                            $sosa_points[$sosa_child],
2568af6bbf8SGreg Roach                            [$latitude, $longitude],
2571f374598SGreg Roach                        ],
2581f374598SGreg Roach                        'options' => [
2591f374598SGreg Roach                            'color' => $color,
2601f374598SGreg Roach                        ],
2611f374598SGreg Roach                    ];
2621f374598SGreg Roach                }
2631f374598SGreg Roach                $geojson['features'][] = [
2641f374598SGreg Roach                    'type'       => 'Feature',
265620da733SGreg Roach                    'id'         => $sosa,
2661f374598SGreg Roach                    'geometry'   => [
2671f374598SGreg Roach                        'type'        => 'Point',
2688af6bbf8SGreg Roach                        'coordinates' => [$longitude, $latitude],
2691f374598SGreg Roach                    ],
2701f374598SGreg Roach                    'properties' => [
2711f374598SGreg Roach                        'polyline'  => $polyline,
2723130efd4SGreg Roach                        'iconcolor' => $color,
2739dbe88c8SGreg Roach                        'tooltip'   => null,
274620da733SGreg Roach                        'summary'   => view('modules/pedigree-map/events', [
275f5fab074SGreg Roach                            'class'        => $class,
276620da733SGreg Roach                            'fact'         => $fact,
2778f9f1cb8SGreg Roach                            'relationship' => $this->getSosaName($sosa),
278620da733SGreg Roach                            'sosa'         => $sosa,
279620da733SGreg Roach                        ]),
2801f374598SGreg Roach                    ],
2811f374598SGreg Roach                ];
2821f374598SGreg Roach            }
2831f374598SGreg Roach        }
2847d988ec3SGreg Roach
28598579324SDavid Drury        return $geojson;
28671378461SGreg Roach    }
28771378461SGreg Roach
28871378461SGreg Roach    /**
28971378461SGreg Roach     * @param ServerRequestInterface $request
2906ccdf4f0SGreg Roach     * @param ChartService           $chart_service
2916ccdf4f0SGreg Roach     *
292fc26b4f6SGreg Roach     * @return array<Fact>
2936ccdf4f0SGreg Roach     */
29450b064dfSRichard Cissée    protected function getPedigreeMapFacts(ServerRequestInterface $request, ChartService $chart_service): array
2956ccdf4f0SGreg Roach    {
296b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
297b55cbc6bSGreg Roach        $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations');
298b55cbc6bSGreg Roach        $xref        = Validator::attributes($request)->isXref()->string('xref');
2996b9cb339SGreg Roach        $individual  = Registry::individualFactory()->make($xref, $tree);
300b55cbc6bSGreg Roach        $individual  = Auth::checkIndividualAccess($individual, false, true);
3016ccdf4f0SGreg Roach        $ancestors   = $chart_service->sosaStradonitzAncestors($individual, $generations);
3026ccdf4f0SGreg Roach        $facts       = [];
303b55cbc6bSGreg Roach
3046ccdf4f0SGreg Roach        foreach ($ancestors as $sosa => $person) {
3056ccdf4f0SGreg Roach            if ($person->canShow()) {
306752e4449SGreg Roach                $birth = $person->facts(Gedcom::BIRTH_EVENTS, true)
307f25fc0f9SGreg Roach                    ->first(static fn (Fact $fact): bool => $fact->place()->gedcomName() !== '');
308752e4449SGreg Roach
309752e4449SGreg Roach                if ($birth instanceof Fact) {
3106ccdf4f0SGreg Roach                    $facts[$sosa] = $birth;
3116ccdf4f0SGreg Roach                }
3126ccdf4f0SGreg Roach            }
3136ccdf4f0SGreg Roach        }
3146ccdf4f0SGreg Roach
3156ccdf4f0SGreg Roach        return $facts;
3161f374598SGreg Roach    }
3171f374598SGreg Roach
3181f374598SGreg Roach    /**
31931e26437SGreg Roach     * builds and returns sosa relationship name in the active language
32031e26437SGreg Roach     *
32131e26437SGreg Roach     * @param int $sosa Sosa number
32231e26437SGreg Roach     *
32331e26437SGreg Roach     * @return string
32431e26437SGreg Roach     */
32550b064dfSRichard Cissée    protected function getSosaName(int $sosa): string
32631e26437SGreg Roach    {
32731e26437SGreg Roach        $path = '';
32831e26437SGreg Roach
32931e26437SGreg Roach        while ($sosa > 1) {
33031e26437SGreg Roach            if ($sosa % 2 === 1) {
33131e26437SGreg Roach                $path = 'mot' . $path;
33231e26437SGreg Roach            } else {
33331e26437SGreg Roach                $path = 'fat' . $path;
33431e26437SGreg Roach            }
33531e26437SGreg Roach            $sosa = intdiv($sosa, 2);
33631e26437SGreg Roach        }
33731e26437SGreg Roach
3388f9f1cb8SGreg Roach        return ucfirst($this->relationship_service->legacyNameAlgorithm($path));
33931e26437SGreg Roach    }
3401f374598SGreg Roach}
341