xref: /webtrees/app/Module/PedigreeMapModule.php (revision 59597b37d69e8147c3f4a27643e9c8edaa2a0592)
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;
356ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
366ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3771378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
383976b470SGreg Roach
39abaef046SGreg Roachuse function intdiv;
4071378461SGreg Roachuse function view;
411f374598SGreg Roach
421f374598SGreg Roach/**
431f374598SGreg Roach * Class PedigreeMapModule
441f374598SGreg Roach */
4571378461SGreg Roachclass PedigreeMapModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
461f374598SGreg Roach{
4749a243cbSGreg Roach    use ModuleChartTrait;
4849a243cbSGreg Roach
4971378461SGreg Roach    private const ROUTE_NAME = 'pedigree-map';
5071378461SGreg Roach    private const ROUTE_URL  = '/tree/{tree}/pedigree-map-{generations}/{xref}';
5171378461SGreg Roach
52e759aebbSGreg Roach    // Defaults
53e759aebbSGreg Roach    public const DEFAULT_GENERATIONS = '4';
5471378461SGreg Roach    public const DEFAULT_PARAMETERS  = [
5571378461SGreg Roach        'generations' => self::DEFAULT_GENERATIONS,
5671378461SGreg Roach    ];
57e759aebbSGreg Roach
58e759aebbSGreg Roach    // Limits
59e759aebbSGreg Roach    public const MAXIMUM_GENERATIONS = 10;
60e759aebbSGreg Roach
6116d6367aSGreg Roach    private const LINE_COLORS = [
621f374598SGreg Roach        '#FF0000',
631f374598SGreg Roach        // Red
641f374598SGreg Roach        '#00FF00',
651f374598SGreg Roach        // Green
661f374598SGreg Roach        '#0000FF',
671f374598SGreg Roach        // Blue
681f374598SGreg Roach        '#FFB300',
691f374598SGreg Roach        // Gold
701f374598SGreg Roach        '#00FFFF',
711f374598SGreg Roach        // Cyan
721f374598SGreg Roach        '#FF00FF',
731f374598SGreg Roach        // Purple
741f374598SGreg Roach        '#7777FF',
751f374598SGreg Roach        // Light blue
761f374598SGreg Roach        '#80FF80'
771f374598SGreg Roach        // Light green
781f374598SGreg Roach    ];
791f374598SGreg Roach
8057ab2231SGreg Roach    /** @var ChartService */
8157ab2231SGreg Roach    private $chart_service;
8257ab2231SGreg Roach
8357ab2231SGreg Roach    /**
8457ab2231SGreg Roach     * PedigreeMapModule constructor.
8557ab2231SGreg Roach     *
8657ab2231SGreg Roach     * @param ChartService $chart_service
8757ab2231SGreg Roach     */
883976b470SGreg Roach    public function __construct(ChartService $chart_service)
893976b470SGreg Roach    {
9057ab2231SGreg Roach        $this->chart_service = $chart_service;
9157ab2231SGreg Roach    }
9257ab2231SGreg Roach
93961ec755SGreg Roach    /**
9471378461SGreg Roach     * Initialization.
9571378461SGreg Roach     *
9671378461SGreg Roach     * @param RouterContainer $router_container
9771378461SGreg Roach     */
9871378461SGreg Roach    public function boot(RouterContainer $router_container)
9971378461SGreg Roach    {
10071378461SGreg Roach        $router_container->getMap()
10171378461SGreg Roach            ->get(self::ROUTE_NAME, self::ROUTE_URL, self::class)
10271378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST)
10371378461SGreg Roach            ->tokens([
10471378461SGreg Roach                'generations' => '\d+',
10571378461SGreg Roach            ]);
10671378461SGreg Roach    }
10771378461SGreg Roach
10871378461SGreg Roach    /**
1090cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
110961ec755SGreg Roach     *
111961ec755SGreg Roach     * @return string
112961ec755SGreg Roach     */
11349a243cbSGreg Roach    public function title(): string
1141f374598SGreg Roach    {
115bbb76c12SGreg Roach        /* I18N: Name of a module */
116bbb76c12SGreg Roach        return I18N::translate('Pedigree map');
1171f374598SGreg Roach    }
1181f374598SGreg Roach
119961ec755SGreg Roach    /**
120961ec755SGreg Roach     * A sentence describing what this module does.
121961ec755SGreg Roach     *
122961ec755SGreg Roach     * @return string
123961ec755SGreg Roach     */
12449a243cbSGreg Roach    public function description(): string
1251f374598SGreg Roach    {
12671378461SGreg Roach        /* I18N: Description of the “Pedigree map” module */
127bbb76c12SGreg Roach        return I18N::translate('Show the birthplace of ancestors on a map.');
1281f374598SGreg Roach    }
1291f374598SGreg Roach
1301f374598SGreg Roach    /**
131377a2979SGreg Roach     * CSS class for the URL.
132377a2979SGreg Roach     *
133377a2979SGreg Roach     * @return string
134377a2979SGreg Roach     */
135377a2979SGreg Roach    public function chartMenuClass(): string
136377a2979SGreg Roach    {
137377a2979SGreg Roach        return 'menu-chart-pedigreemap';
138377a2979SGreg Roach    }
139377a2979SGreg Roach
140377a2979SGreg Roach    /**
1411f374598SGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1421f374598SGreg Roach     *
1431f374598SGreg Roach     * @param Individual $individual
1441f374598SGreg Roach     *
14549a243cbSGreg Roach     * @return Menu|null
1461f374598SGreg Roach     */
147377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
1481f374598SGreg Roach    {
149e6562982SGreg Roach        return $this->chartMenu($individual);
150e6562982SGreg Roach    }
151e6562982SGreg Roach
152e6562982SGreg Roach    /**
153e6562982SGreg Roach     * The title for a specific instance of this chart.
154e6562982SGreg Roach     *
155e6562982SGreg Roach     * @param Individual $individual
156e6562982SGreg Roach     *
157e6562982SGreg Roach     * @return string
158e6562982SGreg Roach     */
159e6562982SGreg Roach    public function chartTitle(Individual $individual): string
160e6562982SGreg Roach    {
161e6562982SGreg Roach        /* I18N: %s is an individual’s name */
16239ca88baSGreg Roach        return I18N::translate('Pedigree map of %s', $individual->fullName());
163e6562982SGreg Roach    }
164e6562982SGreg Roach
165e6562982SGreg Roach    /**
16671378461SGreg Roach     * The URL for a page showing chart options.
167e6562982SGreg Roach     *
168e6562982SGreg Roach     * @param Individual $individual
169*59597b37SGreg Roach     * @param mixed[]    $parameters
170e6562982SGreg Roach     *
171e6562982SGreg Roach     * @return string
172e6562982SGreg Roach     */
173e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
174e6562982SGreg Roach    {
17571378461SGreg Roach        return route(self::ROUTE_NAME, [
17671378461SGreg Roach                'tree' => $individual->tree()->name(),
177e6562982SGreg Roach                'xref' => $individual->xref(),
17871378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
179e6562982SGreg Roach    }
180e6562982SGreg Roach
181e6562982SGreg Roach    /**
1826ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1831f374598SGreg Roach     *
1846ccdf4f0SGreg Roach     * @return ResponseInterface
1851f374598SGreg Roach     */
18657ab2231SGreg Roach    public function getMapDataAction(ServerRequestInterface $request): ResponseInterface
1871f374598SGreg Roach    {
18871378461SGreg Roach        $ajax        = $request->getQueryParams()['ajax'] ?? '';
18971378461SGreg Roach        $generations = (int) $request->getAttribute('generations');
19057ab2231SGreg Roach        $tree        = $request->getAttribute('tree');
19171378461SGreg Roach        $user        = $request->getAttribute('user');
19271378461SGreg Roach        $xref        = $request->getAttribute('xref');
19371378461SGreg Roach        $individual  = Individual::getInstance($xref, $tree);
1941f374598SGreg Roach        $color_count = count(self::LINE_COLORS);
1951f374598SGreg Roach
19657ab2231SGreg Roach        $facts = $this->getPedigreeMapFacts($request, $this->chart_service);
1971f374598SGreg Roach
1981f374598SGreg Roach        $geojson = [
1991f374598SGreg Roach            'type'     => 'FeatureCollection',
2001f374598SGreg Roach            'features' => [],
2011f374598SGreg Roach        ];
2027d988ec3SGreg Roach
2037d988ec3SGreg Roach        $sosa_points = [];
2047d988ec3SGreg Roach
2051f374598SGreg Roach        foreach ($facts as $id => $fact) {
2068af6bbf8SGreg Roach            $location = new Location($fact->place()->gedcomName());
2078af6bbf8SGreg Roach
2088af6bbf8SGreg Roach            // Use the co-ordinates from the fact (if they exist).
2098af6bbf8SGreg Roach            $latitude  = $fact->latitude();
2108af6bbf8SGreg Roach            $longitude = $fact->longitude();
2118af6bbf8SGreg Roach
2128af6bbf8SGreg Roach            // Use the co-ordinates from the location otherwise.
2138af6bbf8SGreg Roach            if ($latitude === 0.0 && $longitude === 0.0) {
2148af6bbf8SGreg Roach                $latitude  = $location->latitude();
2158af6bbf8SGreg Roach                $longitude = $location->longitude();
2168af6bbf8SGreg Roach            }
2178af6bbf8SGreg Roach
2188af6bbf8SGreg Roach            $icon = ['color' => 'Gold', 'name' => 'bullseye '];
2198af6bbf8SGreg Roach            if ($latitude !== 0.0 || $longitude !== 0.0) {
2201f374598SGreg Roach                $polyline         = null;
2211f374598SGreg Roach                $color            = self::LINE_COLORS[log($id, 2) % $color_count];
2221f374598SGreg Roach                $icon['color']    = $color; //make icon color the same as the line
2238af6bbf8SGreg Roach                $sosa_points[$id] = [$latitude, $longitude];
224cdaafeeeSGreg Roach                $sosa_parent      = intdiv($id, 2);
2251f374598SGreg Roach                if (array_key_exists($sosa_parent, $sosa_points)) {
2261f374598SGreg Roach                    // Would like to use a GeometryCollection to hold LineStrings
2271f374598SGreg Roach                    // rather than generate polylines but the MarkerCluster library
2281f374598SGreg Roach                    // doesn't seem to like them
2291f374598SGreg Roach                    $polyline = [
2301f374598SGreg Roach                        'points'  => [
2311f374598SGreg Roach                            $sosa_points[$sosa_parent],
2328af6bbf8SGreg Roach                            [$latitude, $longitude],
2331f374598SGreg Roach                        ],
2341f374598SGreg Roach                        'options' => [
2351f374598SGreg Roach                            'color' => $color,
2361f374598SGreg Roach                        ],
2371f374598SGreg Roach                    ];
2381f374598SGreg Roach                }
2391f374598SGreg Roach                $geojson['features'][] = [
2401f374598SGreg Roach                    'type'       => 'Feature',
2411f374598SGreg Roach                    'id'         => $id,
2421f374598SGreg Roach                    'valid'      => true,
2431f374598SGreg Roach                    'geometry'   => [
2441f374598SGreg Roach                        'type'        => 'Point',
2458af6bbf8SGreg Roach                        'coordinates' => [$longitude, $latitude],
2461f374598SGreg Roach                    ],
2471f374598SGreg Roach                    'properties' => [
2481f374598SGreg Roach                        'polyline' => $polyline,
2491f374598SGreg Roach                        'icon'     => $icon,
2508af6bbf8SGreg Roach                        'tooltip'  => strip_tags($fact->place()->fullName()),
25171378461SGreg Roach                        'summary'  => view('modules/pedigree-map/events', $this->summaryData($individual, $fact, $id)),
2528af6bbf8SGreg Roach                        'zoom'     => $location->zoom() ?: 2,
2531f374598SGreg Roach                    ],
2541f374598SGreg Roach                ];
2551f374598SGreg Roach            }
2561f374598SGreg Roach        }
2577d988ec3SGreg Roach
2586ccdf4f0SGreg Roach        $code = empty($facts) ? StatusCodeInterface::STATUS_NO_CONTENT : StatusCodeInterface::STATUS_OK;
2591f374598SGreg Roach
2606ccdf4f0SGreg Roach        return response($geojson, $code);
2616ccdf4f0SGreg Roach    }
2626ccdf4f0SGreg Roach
2636ccdf4f0SGreg Roach    /**
2646ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
26571378461SGreg Roach     *
26671378461SGreg Roach     * @return ResponseInterface
26771378461SGreg Roach     */
26871378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
26971378461SGreg Roach    {
27071378461SGreg Roach        $generations = (int) $request->getAttribute('generations');
27171378461SGreg Roach        $tree        = $request->getAttribute('tree');
27271378461SGreg Roach        $user        = $request->getAttribute('user');
27371378461SGreg Roach        $xref        = $request->getAttribute('xref');
27471378461SGreg Roach        $individual  = Individual::getInstance($xref, $tree);
27571378461SGreg Roach
27671378461SGreg Roach        Auth::checkIndividualAccess($individual);
27771378461SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
27871378461SGreg Roach
27971378461SGreg Roach        $map = view('modules/pedigree-map/chart', [
28071378461SGreg Roach            'module'      => $this->name(),
28171378461SGreg Roach            'individual'  => $individual,
28271378461SGreg Roach            'type'        => 'pedigree',
28371378461SGreg Roach            'generations' => $generations,
28471378461SGreg Roach        ]);
28571378461SGreg Roach
28671378461SGreg Roach        return $this->viewResponse('modules/pedigree-map/page', [
28771378461SGreg Roach            'module'         => $this->name(),
28871378461SGreg Roach            /* I18N: %s is an individual’s name */
28971378461SGreg Roach            'title'          => I18N::translate('Pedigree map of %s', $individual->fullName()),
29071378461SGreg Roach            'tree'           => $tree,
29171378461SGreg Roach            'individual'     => $individual,
29271378461SGreg Roach            'generations'    => $generations,
29371378461SGreg Roach            'maxgenerations' => self::MAXIMUM_GENERATIONS,
29471378461SGreg Roach            'map'            => $map,
29571378461SGreg Roach        ]);
29671378461SGreg Roach    }
29771378461SGreg Roach
29871378461SGreg Roach    /**
29971378461SGreg Roach     * @param ServerRequestInterface $request
3006ccdf4f0SGreg Roach     * @param ChartService           $chart_service
3016ccdf4f0SGreg Roach     *
3026ccdf4f0SGreg Roach     * @return array
3036ccdf4f0SGreg Roach     */
30457ab2231SGreg Roach    private function getPedigreeMapFacts(ServerRequestInterface $request, ChartService $chart_service): array
3056ccdf4f0SGreg Roach    {
30671378461SGreg Roach        $generations = (int) $request->getAttribute('generations');
30757ab2231SGreg Roach        $tree        = $request->getAttribute('tree');
30871378461SGreg Roach        $xref        = $request->getAttribute('xref');
3096ccdf4f0SGreg Roach        $individual  = Individual::getInstance($xref, $tree);
3106ccdf4f0SGreg Roach        $ancestors   = $chart_service->sosaStradonitzAncestors($individual, $generations);
3116ccdf4f0SGreg Roach        $facts       = [];
3126ccdf4f0SGreg Roach        foreach ($ancestors as $sosa => $person) {
3136ccdf4f0SGreg Roach            if ($person->canShow()) {
3146ccdf4f0SGreg Roach                $birth = $person->facts(['BIRT'])->first();
3156ccdf4f0SGreg Roach                if ($birth instanceof Fact && $birth->place()->gedcomName() !== '') {
3166ccdf4f0SGreg Roach                    $facts[$sosa] = $birth;
3176ccdf4f0SGreg Roach                }
3186ccdf4f0SGreg Roach            }
3196ccdf4f0SGreg Roach        }
3206ccdf4f0SGreg Roach
3216ccdf4f0SGreg Roach        return $facts;
3221f374598SGreg Roach    }
3231f374598SGreg Roach
3241f374598SGreg Roach    /**
3258af6bbf8SGreg Roach     * @param Individual $individual
3268af6bbf8SGreg Roach     * @param Fact       $fact
3278af6bbf8SGreg Roach     * @param int        $sosa
3288af6bbf8SGreg Roach     *
3298af6bbf8SGreg Roach     * @return array
3308af6bbf8SGreg Roach     */
3318af6bbf8SGreg Roach    private function summaryData(Individual $individual, Fact $fact, int $sosa): array
3328af6bbf8SGreg Roach    {
3338af6bbf8SGreg Roach        $record      = $fact->record();
3348af6bbf8SGreg Roach        $name        = '';
3358af6bbf8SGreg Roach        $url         = '';
3368af6bbf8SGreg Roach        $tag         = $fact->label();
3378af6bbf8SGreg Roach        $addbirthtag = false;
3388af6bbf8SGreg Roach
3398af6bbf8SGreg Roach        if ($record instanceof Family) {
3408af6bbf8SGreg Roach            // Marriage
34139ca88baSGreg Roach            $spouse = $record->spouse($individual);
3428af6bbf8SGreg Roach            if ($spouse) {
3438af6bbf8SGreg Roach                $url  = $spouse->url();
34439ca88baSGreg Roach                $name = $spouse->fullName();
3458af6bbf8SGreg Roach            }
3468af6bbf8SGreg Roach        } elseif ($record !== $individual) {
3478af6bbf8SGreg Roach            // Birth of a child
3488af6bbf8SGreg Roach            $url  = $record->url();
34939ca88baSGreg Roach            $name = $record->fullName();
3508af6bbf8SGreg Roach            $tag  = GedcomTag::getLabel('_BIRT_CHIL', $record);
3518af6bbf8SGreg Roach        }
3528af6bbf8SGreg Roach
3538af6bbf8SGreg Roach        if ($sosa > 1) {
3548af6bbf8SGreg Roach            $addbirthtag = true;
35531e26437SGreg Roach            $tag         = ucfirst($this->getSosaName($sosa));
3568af6bbf8SGreg Roach        }
3578af6bbf8SGreg Roach
3588af6bbf8SGreg Roach        return [
3598af6bbf8SGreg Roach            'tag'    => $tag,
3608af6bbf8SGreg Roach            'url'    => $url,
3618af6bbf8SGreg Roach            'name'   => $name,
3628af6bbf8SGreg Roach            'value'  => $fact->value(),
3638af6bbf8SGreg Roach            'date'   => $fact->date()->display(true),
3648af6bbf8SGreg Roach            'place'  => $fact->place(),
3658af6bbf8SGreg Roach            'addtag' => $addbirthtag,
3668af6bbf8SGreg Roach        ];
3678af6bbf8SGreg Roach    }
3688af6bbf8SGreg Roach
3698af6bbf8SGreg Roach    /**
37031e26437SGreg Roach     * builds and returns sosa relationship name in the active language
37131e26437SGreg Roach     *
37231e26437SGreg Roach     * @param int $sosa Sosa number
37331e26437SGreg Roach     *
37431e26437SGreg Roach     * @return string
37531e26437SGreg Roach     */
37631e26437SGreg Roach    private function getSosaName(int $sosa): string
37731e26437SGreg Roach    {
37831e26437SGreg Roach        $path = '';
37931e26437SGreg Roach
38031e26437SGreg Roach        while ($sosa > 1) {
38131e26437SGreg Roach            if ($sosa % 2 === 1) {
38231e26437SGreg Roach                $path = 'mot' . $path;
38331e26437SGreg Roach            } else {
38431e26437SGreg Roach                $path = 'fat' . $path;
38531e26437SGreg Roach            }
38631e26437SGreg Roach            $sosa = intdiv($sosa, 2);
38731e26437SGreg Roach        }
38831e26437SGreg Roach
38931e26437SGreg Roach        return Functions::getRelationshipNameFromPath($path);
39031e26437SGreg Roach    }
3911f374598SGreg Roach}
392