xref: /webtrees/app/Module/PedigreeMapModule.php (revision 4ea6255165d5776629956d29cdf24b33b104cfa6)
11f374598SGreg Roach<?php
23976b470SGreg Roach
31f374598SGreg Roach/**
41f374598SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
151f374598SGreg Roach * along with this program. If not, see <http://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;
246ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
2571378461SGreg Roachuse Fisharebest\Webtrees\Auth;
261f374598SGreg Roachuse Fisharebest\Webtrees\Fact;
278af6bbf8SGreg Roachuse Fisharebest\Webtrees\Family;
2831e26437SGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
298af6bbf8SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
301f374598SGreg Roachuse Fisharebest\Webtrees\I18N;
311f374598SGreg Roachuse Fisharebest\Webtrees\Individual;
328af6bbf8SGreg Roachuse Fisharebest\Webtrees\Location;
331f374598SGreg Roachuse Fisharebest\Webtrees\Menu;
34aca28033SGreg Roachuse Fisharebest\Webtrees\Services\ChartService;
35*4ea62551SGreg Roachuse Fisharebest\Webtrees\Tree;
366ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
376ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3871378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
393976b470SGreg Roach
409e18e23bSGreg Roachuse function app;
419e18e23bSGreg Roachuse function assert;
42abaef046SGreg Roachuse function intdiv;
4311b6f9c6SGreg Roachuse function redirect;
4411b6f9c6SGreg Roachuse function route;
4571378461SGreg Roachuse function view;
461f374598SGreg Roach
471f374598SGreg Roach/**
481f374598SGreg Roach * Class PedigreeMapModule
491f374598SGreg Roach */
5071378461SGreg Roachclass PedigreeMapModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
511f374598SGreg Roach{
5249a243cbSGreg Roach    use ModuleChartTrait;
5349a243cbSGreg Roach
5471378461SGreg Roach    private const ROUTE_NAME = 'pedigree-map';
5571378461SGreg Roach    private const ROUTE_URL  = '/tree/{tree}/pedigree-map-{generations}/{xref}';
5671378461SGreg Roach
57e759aebbSGreg Roach    // Defaults
58e759aebbSGreg Roach    public const DEFAULT_GENERATIONS = '4';
5971378461SGreg Roach    public const DEFAULT_PARAMETERS  = [
6071378461SGreg Roach        'generations' => self::DEFAULT_GENERATIONS,
6171378461SGreg Roach    ];
62e759aebbSGreg Roach
63e759aebbSGreg Roach    // Limits
64e759aebbSGreg Roach    public const MAXIMUM_GENERATIONS = 10;
65e759aebbSGreg Roach
6616d6367aSGreg Roach    private const LINE_COLORS = [
671f374598SGreg Roach        '#FF0000',
681f374598SGreg Roach        // Red
691f374598SGreg Roach        '#00FF00',
701f374598SGreg Roach        // Green
711f374598SGreg Roach        '#0000FF',
721f374598SGreg Roach        // Blue
731f374598SGreg Roach        '#FFB300',
741f374598SGreg Roach        // Gold
751f374598SGreg Roach        '#00FFFF',
761f374598SGreg Roach        // Cyan
771f374598SGreg Roach        '#FF00FF',
781f374598SGreg Roach        // Purple
791f374598SGreg Roach        '#7777FF',
801f374598SGreg Roach        // Light blue
811f374598SGreg Roach        '#80FF80'
821f374598SGreg Roach        // Light green
831f374598SGreg Roach    ];
841f374598SGreg Roach
8557ab2231SGreg Roach    /** @var ChartService */
8657ab2231SGreg Roach    private $chart_service;
8757ab2231SGreg Roach
8857ab2231SGreg Roach    /**
8957ab2231SGreg Roach     * PedigreeMapModule constructor.
9057ab2231SGreg Roach     *
9157ab2231SGreg Roach     * @param ChartService $chart_service
9257ab2231SGreg Roach     */
933976b470SGreg Roach    public function __construct(ChartService $chart_service)
943976b470SGreg Roach    {
9557ab2231SGreg Roach        $this->chart_service = $chart_service;
9657ab2231SGreg Roach    }
9757ab2231SGreg Roach
98961ec755SGreg Roach    /**
9971378461SGreg Roach     * Initialization.
10071378461SGreg Roach     *
1019e18e23bSGreg Roach     * @return void
10271378461SGreg Roach     */
1039e18e23bSGreg Roach    public function boot(): void
10471378461SGreg Roach    {
1059e18e23bSGreg Roach        $router_container = app(RouterContainer::class);
1069e18e23bSGreg Roach        assert($router_container instanceof RouterContainer);
1079e18e23bSGreg Roach
10871378461SGreg Roach        $router_container->getMap()
109f7358520SGreg Roach            ->get(self::ROUTE_NAME, self::ROUTE_URL, $this)
11071378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST)
11171378461SGreg Roach            ->tokens([
11271378461SGreg Roach                'generations' => '\d+',
11371378461SGreg Roach            ]);
11471378461SGreg Roach    }
11571378461SGreg Roach
11671378461SGreg Roach    /**
1170cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
118961ec755SGreg Roach     *
119961ec755SGreg Roach     * @return string
120961ec755SGreg Roach     */
12149a243cbSGreg Roach    public function title(): string
1221f374598SGreg Roach    {
123bbb76c12SGreg Roach        /* I18N: Name of a module */
124bbb76c12SGreg Roach        return I18N::translate('Pedigree map');
1251f374598SGreg Roach    }
1261f374598SGreg Roach
127961ec755SGreg Roach    /**
128961ec755SGreg Roach     * A sentence describing what this module does.
129961ec755SGreg Roach     *
130961ec755SGreg Roach     * @return string
131961ec755SGreg Roach     */
13249a243cbSGreg Roach    public function description(): string
1331f374598SGreg Roach    {
13471378461SGreg Roach        /* I18N: Description of the “Pedigree map” module */
135bbb76c12SGreg Roach        return I18N::translate('Show the birthplace of ancestors on a map.');
1361f374598SGreg Roach    }
1371f374598SGreg Roach
1381f374598SGreg Roach    /**
139377a2979SGreg Roach     * CSS class for the URL.
140377a2979SGreg Roach     *
141377a2979SGreg Roach     * @return string
142377a2979SGreg Roach     */
143377a2979SGreg Roach    public function chartMenuClass(): string
144377a2979SGreg Roach    {
145377a2979SGreg Roach        return 'menu-chart-pedigreemap';
146377a2979SGreg Roach    }
147377a2979SGreg Roach
148377a2979SGreg Roach    /**
1491f374598SGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1501f374598SGreg Roach     *
1511f374598SGreg Roach     * @param Individual $individual
1521f374598SGreg Roach     *
15349a243cbSGreg Roach     * @return Menu|null
1541f374598SGreg Roach     */
155377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
1561f374598SGreg Roach    {
157e6562982SGreg Roach        return $this->chartMenu($individual);
158e6562982SGreg Roach    }
159e6562982SGreg Roach
160e6562982SGreg Roach    /**
161e6562982SGreg Roach     * The title for a specific instance of this chart.
162e6562982SGreg Roach     *
163e6562982SGreg Roach     * @param Individual $individual
164e6562982SGreg Roach     *
165e6562982SGreg Roach     * @return string
166e6562982SGreg Roach     */
167e6562982SGreg Roach    public function chartTitle(Individual $individual): string
168e6562982SGreg Roach    {
169e6562982SGreg Roach        /* I18N: %s is an individual’s name */
17039ca88baSGreg Roach        return I18N::translate('Pedigree map of %s', $individual->fullName());
171e6562982SGreg Roach    }
172e6562982SGreg Roach
173e6562982SGreg Roach    /**
17471378461SGreg Roach     * The URL for a page showing chart options.
175e6562982SGreg Roach     *
176e6562982SGreg Roach     * @param Individual $individual
17759597b37SGreg Roach     * @param mixed[]    $parameters
178e6562982SGreg Roach     *
179e6562982SGreg Roach     * @return string
180e6562982SGreg Roach     */
181e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
182e6562982SGreg Roach    {
18371378461SGreg Roach        return route(self::ROUTE_NAME, [
18471378461SGreg Roach                'tree' => $individual->tree()->name(),
185e6562982SGreg Roach                'xref' => $individual->xref(),
18671378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
187e6562982SGreg Roach    }
188e6562982SGreg Roach
189e6562982SGreg Roach    /**
1906ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1911f374598SGreg Roach     *
1926ccdf4f0SGreg Roach     * @return ResponseInterface
1931f374598SGreg Roach     */
19457ab2231SGreg Roach    public function getMapDataAction(ServerRequestInterface $request): ResponseInterface
1951f374598SGreg Roach    {
19657ab2231SGreg Roach        $tree = $request->getAttribute('tree');
197*4ea62551SGreg Roach        assert($tree instanceof Tree);
198*4ea62551SGreg Roach
199353080f8SGreg Roach        $xref        = $request->getQueryParams()['xref'];
20071378461SGreg Roach        $individual  = Individual::getInstance($xref, $tree);
2011f374598SGreg Roach        $color_count = count(self::LINE_COLORS);
2021f374598SGreg Roach
20357ab2231SGreg Roach        $facts = $this->getPedigreeMapFacts($request, $this->chart_service);
2041f374598SGreg Roach
2051f374598SGreg Roach        $geojson = [
2061f374598SGreg Roach            'type'     => 'FeatureCollection',
2071f374598SGreg Roach            'features' => [],
2081f374598SGreg Roach        ];
2097d988ec3SGreg Roach
2107d988ec3SGreg Roach        $sosa_points = [];
2117d988ec3SGreg Roach
2121f374598SGreg Roach        foreach ($facts as $id => $fact) {
2138af6bbf8SGreg Roach            $location = new Location($fact->place()->gedcomName());
2148af6bbf8SGreg Roach
2158af6bbf8SGreg Roach            // Use the co-ordinates from the fact (if they exist).
2168af6bbf8SGreg Roach            $latitude  = $fact->latitude();
2178af6bbf8SGreg Roach            $longitude = $fact->longitude();
2188af6bbf8SGreg Roach
2198af6bbf8SGreg Roach            // Use the co-ordinates from the location otherwise.
2208af6bbf8SGreg Roach            if ($latitude === 0.0 && $longitude === 0.0) {
2218af6bbf8SGreg Roach                $latitude  = $location->latitude();
2228af6bbf8SGreg Roach                $longitude = $location->longitude();
2238af6bbf8SGreg Roach            }
2248af6bbf8SGreg Roach
2258af6bbf8SGreg Roach            $icon = ['color' => 'Gold', 'name' => 'bullseye '];
2268af6bbf8SGreg Roach            if ($latitude !== 0.0 || $longitude !== 0.0) {
2271f374598SGreg Roach                $polyline         = null;
2281f374598SGreg Roach                $color            = self::LINE_COLORS[log($id, 2) % $color_count];
2291f374598SGreg Roach                $icon['color']    = $color; //make icon color the same as the line
2308af6bbf8SGreg Roach                $sosa_points[$id] = [$latitude, $longitude];
231cdaafeeeSGreg Roach                $sosa_parent      = intdiv($id, 2);
2321f374598SGreg Roach                if (array_key_exists($sosa_parent, $sosa_points)) {
2331f374598SGreg Roach                    // Would like to use a GeometryCollection to hold LineStrings
2341f374598SGreg Roach                    // rather than generate polylines but the MarkerCluster library
2351f374598SGreg Roach                    // doesn't seem to like them
2361f374598SGreg Roach                    $polyline = [
2371f374598SGreg Roach                        'points'  => [
2381f374598SGreg Roach                            $sosa_points[$sosa_parent],
2398af6bbf8SGreg Roach                            [$latitude, $longitude],
2401f374598SGreg Roach                        ],
2411f374598SGreg Roach                        'options' => [
2421f374598SGreg Roach                            'color' => $color,
2431f374598SGreg Roach                        ],
2441f374598SGreg Roach                    ];
2451f374598SGreg Roach                }
2461f374598SGreg Roach                $geojson['features'][] = [
2471f374598SGreg Roach                    'type'       => 'Feature',
2481f374598SGreg Roach                    'id'         => $id,
2491f374598SGreg Roach                    'valid'      => true,
2501f374598SGreg Roach                    'geometry'   => [
2511f374598SGreg Roach                        'type'        => 'Point',
2528af6bbf8SGreg Roach                        'coordinates' => [$longitude, $latitude],
2531f374598SGreg Roach                    ],
2541f374598SGreg Roach                    'properties' => [
2551f374598SGreg Roach                        'polyline' => $polyline,
2561f374598SGreg Roach                        'icon'     => $icon,
2578af6bbf8SGreg Roach                        'tooltip'  => strip_tags($fact->place()->fullName()),
25871378461SGreg Roach                        'summary'  => view('modules/pedigree-map/events', $this->summaryData($individual, $fact, $id)),
2598af6bbf8SGreg Roach                        'zoom'     => $location->zoom() ?: 2,
2601f374598SGreg Roach                    ],
2611f374598SGreg Roach                ];
2621f374598SGreg Roach            }
2631f374598SGreg Roach        }
2647d988ec3SGreg Roach
2656ccdf4f0SGreg Roach        $code = empty($facts) ? StatusCodeInterface::STATUS_NO_CONTENT : StatusCodeInterface::STATUS_OK;
2661f374598SGreg Roach
2676ccdf4f0SGreg Roach        return response($geojson, $code);
2686ccdf4f0SGreg Roach    }
2696ccdf4f0SGreg Roach
2706ccdf4f0SGreg Roach    /**
2716ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
27271378461SGreg Roach     *
27371378461SGreg Roach     * @return ResponseInterface
27471378461SGreg Roach     */
27571378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
27671378461SGreg Roach    {
277*4ea62551SGreg Roach        $tree = $request->getAttribute('tree');
278*4ea62551SGreg Roach        assert($tree instanceof Tree);
279*4ea62551SGreg Roach
28071378461SGreg Roach        $tree        = $request->getAttribute('tree');
28171378461SGreg Roach        $user        = $request->getAttribute('user');
28271378461SGreg Roach        $xref        = $request->getAttribute('xref');
28371378461SGreg Roach        $individual  = Individual::getInstance($xref, $tree);
28471378461SGreg Roach
28511b6f9c6SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
28611b6f9c6SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
28711b6f9c6SGreg Roach            return redirect(route(self::ROUTE_NAME, [
28811b6f9c6SGreg Roach                'tree'        => $tree->name(),
28911b6f9c6SGreg Roach                'xref'        => $request->getParsedBody()['xref'],
29011b6f9c6SGreg Roach                'generations' => $request->getParsedBody()['generations'],
29111b6f9c6SGreg Roach            ]));
29211b6f9c6SGreg Roach        }
29311b6f9c6SGreg Roach
29471378461SGreg Roach        Auth::checkIndividualAccess($individual);
29571378461SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
29671378461SGreg Roach
29771378461SGreg Roach        $map = view('modules/pedigree-map/chart', [
29871378461SGreg Roach            'module'      => $this->name(),
29971378461SGreg Roach            'individual'  => $individual,
30071378461SGreg Roach            'type'        => 'pedigree',
30171378461SGreg Roach            'generations' => $generations,
30271378461SGreg Roach        ]);
30371378461SGreg Roach
30471378461SGreg Roach        return $this->viewResponse('modules/pedigree-map/page', [
30571378461SGreg Roach            'module'         => $this->name(),
30671378461SGreg Roach            /* I18N: %s is an individual’s name */
30771378461SGreg Roach            'title'          => I18N::translate('Pedigree map of %s', $individual->fullName()),
30871378461SGreg Roach            'tree'           => $tree,
30971378461SGreg Roach            'individual'     => $individual,
31071378461SGreg Roach            'generations'    => $generations,
31171378461SGreg Roach            'maxgenerations' => self::MAXIMUM_GENERATIONS,
31271378461SGreg Roach            'map'            => $map,
31371378461SGreg Roach        ]);
31471378461SGreg Roach    }
31571378461SGreg Roach
31671378461SGreg Roach    /**
31771378461SGreg Roach     * @param ServerRequestInterface $request
3186ccdf4f0SGreg Roach     * @param ChartService           $chart_service
3196ccdf4f0SGreg Roach     *
3206ccdf4f0SGreg Roach     * @return array
3216ccdf4f0SGreg Roach     */
32257ab2231SGreg Roach    private function getPedigreeMapFacts(ServerRequestInterface $request, ChartService $chart_service): array
3236ccdf4f0SGreg Roach    {
32457ab2231SGreg Roach        $tree = $request->getAttribute('tree');
325*4ea62551SGreg Roach        assert($tree instanceof Tree);
326*4ea62551SGreg Roach
327*4ea62551SGreg Roach        $generations = (int) $request->getQueryParams()['generations'];
328353080f8SGreg Roach        $xref        = $request->getQueryParams()['xref'];
3296ccdf4f0SGreg Roach        $individual  = Individual::getInstance($xref, $tree);
3306ccdf4f0SGreg Roach        $ancestors   = $chart_service->sosaStradonitzAncestors($individual, $generations);
3316ccdf4f0SGreg Roach        $facts       = [];
3326ccdf4f0SGreg Roach        foreach ($ancestors as $sosa => $person) {
3336ccdf4f0SGreg Roach            if ($person->canShow()) {
3346ccdf4f0SGreg Roach                $birth = $person->facts(['BIRT'])->first();
3356ccdf4f0SGreg Roach                if ($birth instanceof Fact && $birth->place()->gedcomName() !== '') {
3366ccdf4f0SGreg Roach                    $facts[$sosa] = $birth;
3376ccdf4f0SGreg Roach                }
3386ccdf4f0SGreg Roach            }
3396ccdf4f0SGreg Roach        }
3406ccdf4f0SGreg Roach
3416ccdf4f0SGreg Roach        return $facts;
3421f374598SGreg Roach    }
3431f374598SGreg Roach
3441f374598SGreg Roach    /**
3458af6bbf8SGreg Roach     * @param Individual $individual
3468af6bbf8SGreg Roach     * @param Fact       $fact
3478af6bbf8SGreg Roach     * @param int        $sosa
3488af6bbf8SGreg Roach     *
3498af6bbf8SGreg Roach     * @return array
3508af6bbf8SGreg Roach     */
3518af6bbf8SGreg Roach    private function summaryData(Individual $individual, Fact $fact, int $sosa): array
3528af6bbf8SGreg Roach    {
3538af6bbf8SGreg Roach        $record      = $fact->record();
3548af6bbf8SGreg Roach        $name        = '';
3558af6bbf8SGreg Roach        $url         = '';
3568af6bbf8SGreg Roach        $tag         = $fact->label();
3578af6bbf8SGreg Roach        $addbirthtag = false;
3588af6bbf8SGreg Roach
3598af6bbf8SGreg Roach        if ($record instanceof Family) {
3608af6bbf8SGreg Roach            // Marriage
36139ca88baSGreg Roach            $spouse = $record->spouse($individual);
3628af6bbf8SGreg Roach            if ($spouse) {
3638af6bbf8SGreg Roach                $url  = $spouse->url();
36439ca88baSGreg Roach                $name = $spouse->fullName();
3658af6bbf8SGreg Roach            }
3668af6bbf8SGreg Roach        } elseif ($record !== $individual) {
3678af6bbf8SGreg Roach            // Birth of a child
3688af6bbf8SGreg Roach            $url  = $record->url();
36939ca88baSGreg Roach            $name = $record->fullName();
3708af6bbf8SGreg Roach            $tag  = GedcomTag::getLabel('_BIRT_CHIL', $record);
3718af6bbf8SGreg Roach        }
3728af6bbf8SGreg Roach
3738af6bbf8SGreg Roach        if ($sosa > 1) {
3748af6bbf8SGreg Roach            $addbirthtag = true;
37531e26437SGreg Roach            $tag         = ucfirst($this->getSosaName($sosa));
3768af6bbf8SGreg Roach        }
3778af6bbf8SGreg Roach
3788af6bbf8SGreg Roach        return [
3798af6bbf8SGreg Roach            'tag'    => $tag,
3808af6bbf8SGreg Roach            'url'    => $url,
3818af6bbf8SGreg Roach            'name'   => $name,
3828af6bbf8SGreg Roach            'value'  => $fact->value(),
3838af6bbf8SGreg Roach            'date'   => $fact->date()->display(true),
3848af6bbf8SGreg Roach            'place'  => $fact->place(),
3858af6bbf8SGreg Roach            'addtag' => $addbirthtag,
3868af6bbf8SGreg Roach        ];
3878af6bbf8SGreg Roach    }
3888af6bbf8SGreg Roach
3898af6bbf8SGreg Roach    /**
39031e26437SGreg Roach     * builds and returns sosa relationship name in the active language
39131e26437SGreg Roach     *
39231e26437SGreg Roach     * @param int $sosa Sosa number
39331e26437SGreg Roach     *
39431e26437SGreg Roach     * @return string
39531e26437SGreg Roach     */
39631e26437SGreg Roach    private function getSosaName(int $sosa): string
39731e26437SGreg Roach    {
39831e26437SGreg Roach        $path = '';
39931e26437SGreg Roach
40031e26437SGreg Roach        while ($sosa > 1) {
40131e26437SGreg Roach            if ($sosa % 2 === 1) {
40231e26437SGreg Roach                $path = 'mot' . $path;
40331e26437SGreg Roach            } else {
40431e26437SGreg Roach                $path = 'fat' . $path;
40531e26437SGreg Roach            }
40631e26437SGreg Roach            $sosa = intdiv($sosa, 2);
40731e26437SGreg Roach        }
40831e26437SGreg Roach
40931e26437SGreg Roach        return Functions::getRelationshipNameFromPath($path);
41031e26437SGreg Roach    }
4111f374598SGreg Roach}
412