xref: /webtrees/app/Module/PedigreeMapModule.php (revision b6e5099191dc075b905b2e79c98d4e83138426ac)
11f374598SGreg Roach<?php
21f374598SGreg Roach/**
31f374598SGreg Roach * webtrees: online genealogy
41f374598SGreg Roach * Copyright (C) 2018 webtrees development team
51f374598SGreg Roach * This program is free software: you can redistribute it and/or modify
61f374598SGreg Roach * it under the terms of the GNU General Public License as published by
71f374598SGreg Roach * the Free Software Foundation, either version 3 of the License, or
81f374598SGreg Roach * (at your option) any later version.
91f374598SGreg Roach * This program is distributed in the hope that it will be useful,
101f374598SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f374598SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
121f374598SGreg Roach * GNU General Public License for more details.
131f374598SGreg Roach * You should have received a copy of the GNU General Public License
141f374598SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
151f374598SGreg Roach */
161f374598SGreg Roachdeclare(strict_types=1);
171f374598SGreg Roach
181f374598SGreg Roachnamespace Fisharebest\Webtrees\Module;
191f374598SGreg Roach
201f374598SGreg Roachuse Exception;
211f374598SGreg Roachuse Fisharebest\Webtrees\Auth;
221f374598SGreg Roachuse Fisharebest\Webtrees\Database;
231f374598SGreg Roachuse Fisharebest\Webtrees\DebugBar;
24*b6e50991SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException;
25*b6e50991SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
261f374598SGreg Roachuse Fisharebest\Webtrees\Fact;
271f374598SGreg Roachuse Fisharebest\Webtrees\FactLocation;
281f374598SGreg Roachuse Fisharebest\Webtrees\I18N;
291f374598SGreg Roachuse Fisharebest\Webtrees\Individual;
301f374598SGreg Roachuse Fisharebest\Webtrees\Log;
311f374598SGreg Roachuse Fisharebest\Webtrees\Menu;
321f374598SGreg Roachuse Fisharebest\Webtrees\Place;
331f374598SGreg Roachuse Fisharebest\Webtrees\Tree;
341f374598SGreg Roachuse Symfony\Component\HttpFoundation\JsonResponse;
351f374598SGreg Roachuse Symfony\Component\HttpFoundation\Request;
361f374598SGreg Roach
371f374598SGreg Roach/**
381f374598SGreg Roach * Class PedigreeMapModule
391f374598SGreg Roach */
401f374598SGreg Roachclass PedigreeMapModule extends AbstractModule implements ModuleChartInterface
411f374598SGreg Roach{
421f374598SGreg Roach    const OSM_MIN_ZOOM = 2;
431f374598SGreg Roach    const LINE_COLORS  = [
441f374598SGreg Roach        '#FF0000',
451f374598SGreg Roach        // Red
461f374598SGreg Roach        '#00FF00',
471f374598SGreg Roach        // Green
481f374598SGreg Roach        '#0000FF',
491f374598SGreg Roach        // Blue
501f374598SGreg Roach        '#FFB300',
511f374598SGreg Roach        // Gold
521f374598SGreg Roach        '#00FFFF',
531f374598SGreg Roach        // Cyan
541f374598SGreg Roach        '#FF00FF',
551f374598SGreg Roach        // Purple
561f374598SGreg Roach        '#7777FF',
571f374598SGreg Roach        // Light blue
581f374598SGreg Roach        '#80FF80'
591f374598SGreg Roach        // Light green
601f374598SGreg Roach    ];
611f374598SGreg Roach
621f374598SGreg Roach    private static $map_providers  = null;
631f374598SGreg Roach    private static $map_selections = null;
641f374598SGreg Roach
651f374598SGreg Roach    /** {@inheritdoc} */
661f374598SGreg Roach    public function getTitle()
671f374598SGreg Roach    {
681f374598SGreg Roach        return /* I18N: Name of a module */
691f374598SGreg Roach            I18N::translate('Pedigree map');
701f374598SGreg Roach    }
711f374598SGreg Roach
721f374598SGreg Roach    /** {@inheritdoc} */
731f374598SGreg Roach    public function getDescription()
741f374598SGreg Roach    {
751f374598SGreg Roach        return /* I18N: Description of the “OSM” module */
761f374598SGreg Roach            I18N::translate('Show the birthplace of ancestors on a map.');
771f374598SGreg Roach    }
781f374598SGreg Roach
791f374598SGreg Roach    /** {@inheritdoc} */
801f374598SGreg Roach    public function defaultAccessLevel()
811f374598SGreg Roach    {
821f374598SGreg Roach        return Auth::PRIV_PRIVATE;
831f374598SGreg Roach    }
841f374598SGreg Roach
851f374598SGreg Roach    /**
861f374598SGreg Roach     * Return a menu item for this chart.
871f374598SGreg Roach     *
881f374598SGreg Roach     * @param Individual $individual
891f374598SGreg Roach     *
901f374598SGreg Roach     * @return Menu
911f374598SGreg Roach     */
921f374598SGreg Roach    public function getChartMenu(Individual $individual)
931f374598SGreg Roach    {
941f374598SGreg Roach        return new Menu(
951f374598SGreg Roach            I18N::translate('Pedigree map'),
961f374598SGreg Roach            route('module', [
971f374598SGreg Roach                'module' => $this->getName(),
981f374598SGreg Roach                'action' => 'PedigreeMap',
991f374598SGreg Roach                'xref'   => $individual->getXref(),
1001f374598SGreg Roach            ]),
1011f374598SGreg Roach            'menu-chart-pedigreemap',
1021f374598SGreg Roach            ['rel' => 'nofollow']
1031f374598SGreg Roach        );
1041f374598SGreg Roach    }
1051f374598SGreg Roach
1061f374598SGreg Roach    /**
1071f374598SGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1081f374598SGreg Roach     *
1091f374598SGreg Roach     * @param Individual $individual
1101f374598SGreg Roach     *
1111f374598SGreg Roach     * @return Menu
1121f374598SGreg Roach     */
1131f374598SGreg Roach    public function getBoxChartMenu(Individual $individual)
1141f374598SGreg Roach    {
1151f374598SGreg Roach        return $this->getChartMenu($individual);
1161f374598SGreg Roach    }
1171f374598SGreg Roach
1181f374598SGreg Roach    /**
1191f374598SGreg Roach     * @param string $type
1201f374598SGreg Roach     *
1211f374598SGreg Roach     * @return array
1221f374598SGreg Roach     */
1231f374598SGreg Roach    public function assets($type = 'user')
1241f374598SGreg Roach    {
1251f374598SGreg Roach        $dir = WT_MODULES_DIR . $this->getName();
1261f374598SGreg Roach        if ($type === 'admin') {
1271f374598SGreg Roach            return [
1281f374598SGreg Roach                'css' => [
1291f374598SGreg Roach                    $dir . '/assets/css/osm-module.css',
1301f374598SGreg Roach                ],
1311f374598SGreg Roach                'js'  => [
1321f374598SGreg Roach                    $dir . '/assets/js/osm-admin.js',
1331f374598SGreg Roach                ],
1341f374598SGreg Roach            ];
1351f374598SGreg Roach        } else {
1361f374598SGreg Roach            return [
1371f374598SGreg Roach                'css' => [
1381f374598SGreg Roach                    $dir . '/assets/css/osm-module.css',
1391f374598SGreg Roach                ],
1401f374598SGreg Roach                'js'  => [
1411f374598SGreg Roach                    $dir . '/assets/js/osm-module.js',
1421f374598SGreg Roach                ],
1431f374598SGreg Roach            ];
1441f374598SGreg Roach        }
1451f374598SGreg Roach    }
1461f374598SGreg Roach
1471f374598SGreg Roach    /**
1481f374598SGreg Roach     * @param Request $request
1491f374598SGreg Roach     *
1501f374598SGreg Roach     * @return JsonResponse
1511f374598SGreg Roach     */
1521f374598SGreg Roach    public function getBaseDataAction(Request $request): JsonResponse
1531f374598SGreg Roach    {
1541f374598SGreg Roach        $provider = $this->getMapProviderData($request);
1551f374598SGreg Roach        $style    = $provider['selectedStyleName'] = '' ? '' : '.' . $provider['selectedStyleName'];
1561f374598SGreg Roach
1571f374598SGreg Roach        switch ($provider['selectedProvIndex']) {
1581f374598SGreg Roach            case 'mapbox':
1591f374598SGreg Roach                $providerOptions = [
1601f374598SGreg Roach                    'id'          => $this->getPreference('mapbox_id'),
1611f374598SGreg Roach                    'accessToken' => $this->getPreference('mapbox_token'),
1621f374598SGreg Roach                ];
1631f374598SGreg Roach                break;
1641f374598SGreg Roach            case 'here':
1651f374598SGreg Roach                $providerOptions = [
1661f374598SGreg Roach                    'app_id'   => $this->getPreference('here_appid'),
1671f374598SGreg Roach                    'app_code' => $this->getPreference('here_appcode'),
1681f374598SGreg Roach                ];
1691f374598SGreg Roach                break;
1701f374598SGreg Roach            default:
1711f374598SGreg Roach                $providerOptions = [];
1721f374598SGreg Roach        };
1731f374598SGreg Roach
1741f374598SGreg Roach        $options = [
1751f374598SGreg Roach            'minZoom'         => self::OSM_MIN_ZOOM,
1761f374598SGreg Roach            'providerName'    => $provider['selectedProvName'] . $style,
1771f374598SGreg Roach            'providerOptions' => $providerOptions,
1781f374598SGreg Roach            'animate'         => $this->getPreference('map_animate', 0),
1791f374598SGreg Roach            'I18N'            => [
1801f374598SGreg Roach                'zoomInTitle'  => I18N::translate('Zoom in'),
1811f374598SGreg Roach                'zoomOutTitle' => I18N::translate('Zoom out'),
1821f374598SGreg Roach                'reset'        => I18N::translate('Reset to initial map state'),
1831f374598SGreg Roach                'noData'       => I18N::translate('No mappable items'),
1841f374598SGreg Roach                'error'        => I18N::translate('An unknown error occurred'),
1851f374598SGreg Roach            ],
1861f374598SGreg Roach        ];
1871f374598SGreg Roach
1881f374598SGreg Roach        return new JsonResponse($options);
1891f374598SGreg Roach    }
1901f374598SGreg Roach
1911f374598SGreg Roach    /**
1921f374598SGreg Roach     * @param Request $request
1931f374598SGreg Roach     *
1941f374598SGreg Roach     * @return JsonResponse
1951f374598SGreg Roach     * @throws Exception
1961f374598SGreg Roach     */
1971f374598SGreg Roach    public function getMapDataAction(Request $request): JsonResponse
1981f374598SGreg Roach    {
1991f374598SGreg Roach        $xref        = $request->get('reference');
2001f374598SGreg Roach        $tree        = $request->attributes->get('tree');
2011f374598SGreg Roach        $indi        = Individual::getInstance($xref, $tree);
2021f374598SGreg Roach        $color_count = count(self::LINE_COLORS);
2031f374598SGreg Roach
2041f374598SGreg Roach        $facts = $this->getPedigreeMapFacts($request);
2051f374598SGreg Roach
2061f374598SGreg Roach        $geojson = [
2071f374598SGreg Roach            'type'     => 'FeatureCollection',
2081f374598SGreg Roach            'features' => [],
2091f374598SGreg Roach        ];
2101f374598SGreg Roach        if (empty($facts)) {
2111f374598SGreg Roach            $code = 204;
2121f374598SGreg Roach        } else {
2131f374598SGreg Roach            $code = 200;
2141f374598SGreg Roach            foreach ($facts as $id => $fact) {
2151f374598SGreg Roach                $event = new FactLocation($fact, $indi);
2161f374598SGreg Roach                $icon  = $event->getIconDetails();
2171f374598SGreg Roach                if ($event->knownLatLon()) {
2181f374598SGreg Roach                    $polyline         = null;
2191f374598SGreg Roach                    $color            = self::LINE_COLORS[log($id, 2) % $color_count];
2201f374598SGreg Roach                    $icon['color']    = $color; //make icon color the same as the line
2211f374598SGreg Roach                    $sosa_points[$id] = $event->getLatLonJSArray();
2221f374598SGreg Roach                    $sosa_parent      = (int)floor($id / 2);
2231f374598SGreg Roach                    if (array_key_exists($sosa_parent, $sosa_points)) {
2241f374598SGreg Roach                        // Would like to use a GeometryCollection to hold LineStrings
2251f374598SGreg Roach                        // rather than generate polylines but the MarkerCluster library
2261f374598SGreg Roach                        // doesn't seem to like them
2271f374598SGreg Roach                        $polyline = [
2281f374598SGreg Roach                            'points'  => [
2291f374598SGreg Roach                                $sosa_points[$sosa_parent],
2301f374598SGreg Roach                                $event->getLatLonJSArray(),
2311f374598SGreg Roach                            ],
2321f374598SGreg Roach                            'options' => [
2331f374598SGreg Roach                                'color' => $color,
2341f374598SGreg Roach                            ],
2351f374598SGreg Roach                        ];
2361f374598SGreg Roach                    }
2371f374598SGreg Roach                    $geojson['features'][] = [
2381f374598SGreg Roach                        'type'       => 'Feature',
2391f374598SGreg Roach                        'id'         => $id,
2401f374598SGreg Roach                        'valid'      => true,
2411f374598SGreg Roach                        'geometry'   => [
2421f374598SGreg Roach                            'type'        => 'Point',
2431f374598SGreg Roach                            'coordinates' => $event->getGeoJsonCoords(),
2441f374598SGreg Roach                        ],
2451f374598SGreg Roach                        'properties' => [
2461f374598SGreg Roach                            'polyline' => $polyline,
2471f374598SGreg Roach                            'icon'     => $icon,
2481f374598SGreg Roach                            'tooltip'  => $event->toolTip(),
2491f374598SGreg Roach                            'summary'  => view(
2501f374598SGreg Roach                                'modules/openstreetmap/event-sidebar',
2511f374598SGreg Roach                                $event->shortSummary('pedigree', $id)
2521f374598SGreg Roach                            ),
2531f374598SGreg Roach                            'zoom'     => (int)$event->getZoom(),
2541f374598SGreg Roach                        ],
2551f374598SGreg Roach                    ];
2561f374598SGreg Roach                }
2571f374598SGreg Roach            }
2581f374598SGreg Roach        }
2591f374598SGreg Roach
2601f374598SGreg Roach        return new JsonResponse($geojson, $code);
2611f374598SGreg Roach    }
2621f374598SGreg Roach
2631f374598SGreg Roach    /**
2641f374598SGreg Roach     * @param Request $request
2651f374598SGreg Roach     *
2661f374598SGreg Roach     * @return array
2671f374598SGreg Roach     * @throws Exception
2681f374598SGreg Roach     */
2691f374598SGreg Roach    private function getPedigreeMapFacts(Request $request)
2701f374598SGreg Roach    {
2711f374598SGreg Roach        $xref        = $request->get('reference');
2721f374598SGreg Roach        $tree        = $request->attributes->get('tree');
2731f374598SGreg Roach        $individual  = Individual::getInstance($xref, $tree);
2741f374598SGreg Roach        $generations = (int)$request->get(
2751f374598SGreg Roach            'generations',
2761f374598SGreg Roach            $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS')
2771f374598SGreg Roach        );
2781f374598SGreg Roach        $ancestors   = $this->sosaStradonitzAncestors($individual, $generations);
2791f374598SGreg Roach        $facts       = [];
2801f374598SGreg Roach        foreach ($ancestors as $sosa => $person) {
2811f374598SGreg Roach            if ($person !== null && $person->canShow()) {
2821f374598SGreg Roach                /** @var Fact $birth */
2831f374598SGreg Roach                $birth = $person->getFirstFact('BIRT');
2841f374598SGreg Roach                if ($birth && !$birth->getPlace()->isEmpty()) {
2851f374598SGreg Roach                    $facts[$sosa] = $birth;
2861f374598SGreg Roach                }
2871f374598SGreg Roach            }
2881f374598SGreg Roach        }
2891f374598SGreg Roach
2901f374598SGreg Roach        return $facts;
2911f374598SGreg Roach    }
2921f374598SGreg Roach
2931f374598SGreg Roach    /**
2941f374598SGreg Roach     * @param Request $request
2951f374598SGreg Roach     *
2961f374598SGreg Roach     * @return JsonResponse
2971f374598SGreg Roach     */
2981f374598SGreg Roach    public function getProviderStylesAction(Request $request): JsonResponse
2991f374598SGreg Roach    {
3001f374598SGreg Roach        $styles = $this->getMapProviderData($request);
3011f374598SGreg Roach
3021f374598SGreg Roach        return new JsonResponse($styles);
3031f374598SGreg Roach    }
3041f374598SGreg Roach
3051f374598SGreg Roach    /**
3061f374598SGreg Roach     * @param Request $request
3071f374598SGreg Roach     *
3081f374598SGreg Roach     * @return array|null
3091f374598SGreg Roach     */
3101f374598SGreg Roach    private function getMapProviderData(Request $request)
3111f374598SGreg Roach    {
3121f374598SGreg Roach        if (self::$map_providers === null) {
3131f374598SGreg Roach            $providersFile        = WT_ROOT . WT_MODULES_DIR . $this->getName() . '/providers/providers.xml';
3141f374598SGreg Roach            self::$map_selections = [
3151f374598SGreg Roach                'provider' => $this->getPreference('provider', 'openstreetmap'),
3161f374598SGreg Roach                'style'    => $this->getPreference('provider_style', 'mapnik'),
3171f374598SGreg Roach            ];
3181f374598SGreg Roach
3191f374598SGreg Roach            try {
3201f374598SGreg Roach                $xml = simplexml_load_file($providersFile);
3211f374598SGreg Roach                // need to convert xml structure into arrays & strings
3221f374598SGreg Roach                foreach ($xml as $provider) {
3231f374598SGreg Roach                    $style_keys = array_map(
3241f374598SGreg Roach                        function ($item) {
3251f374598SGreg Roach                            return preg_replace('/[^a-z\d]/i', '', strtolower($item));
3261f374598SGreg Roach                        },
3271f374598SGreg Roach                        (array)$provider->styles
3281f374598SGreg Roach                    );
3291f374598SGreg Roach
3301f374598SGreg Roach                    $key = preg_replace('/[^a-z\d]/i', '', strtolower((string)$provider->name));
3311f374598SGreg Roach
3321f374598SGreg Roach                    self::$map_providers[$key] = [
3331f374598SGreg Roach                        'name'   => (string)$provider->name,
3341f374598SGreg Roach                        'styles' => array_combine($style_keys, (array)$provider->styles),
3351f374598SGreg Roach                    ];
3361f374598SGreg Roach                }
3371f374598SGreg Roach            } catch (Exception $ex) {
3381f374598SGreg Roach                // Default provider is OpenStreetMap
3391f374598SGreg Roach                self::$map_selections = [
3401f374598SGreg Roach                    'provider' => 'openstreetmap',
3411f374598SGreg Roach                    'style'    => 'mapnik',
3421f374598SGreg Roach                ];
3431f374598SGreg Roach                self::$map_providers  = [
3441f374598SGreg Roach                    'openstreetmap' => [
3451f374598SGreg Roach                        'name'   => 'OpenStreetMap',
3461f374598SGreg Roach                        'styles' => ['mapnik' => 'Mapnik'],
3471f374598SGreg Roach                    ],
3481f374598SGreg Roach                ];
3491f374598SGreg Roach            };
3501f374598SGreg Roach        }
3511f374598SGreg Roach
3521f374598SGreg Roach        //Ugly!!!
3531f374598SGreg Roach        switch ($request->get('action')) {
3541f374598SGreg Roach            case 'BaseData':
3551f374598SGreg Roach                $varName = (self::$map_selections['style'] === '') ? '' : self::$map_providers[self::$map_selections['provider']]['styles'][self::$map_selections['style']];
3561f374598SGreg Roach                $payload = [
3571f374598SGreg Roach                    'selectedProvIndex' => self::$map_selections['provider'],
3581f374598SGreg Roach                    'selectedProvName'  => self::$map_providers[self::$map_selections['provider']]['name'],
3591f374598SGreg Roach                    'selectedStyleName' => $varName,
3601f374598SGreg Roach                ];
3611f374598SGreg Roach                break;
3621f374598SGreg Roach            case 'ProviderStyles':
3631f374598SGreg Roach                $provider = $request->get('provider', 'openstreetmap');
3641f374598SGreg Roach                $payload  = self::$map_providers[$provider]['styles'];
3651f374598SGreg Roach                break;
3661f374598SGreg Roach            case 'AdminConfig':
3671f374598SGreg Roach                $providers = [];
3681f374598SGreg Roach                foreach (self::$map_providers as $key => $provider) {
3691f374598SGreg Roach                    $providers[$key] = $provider['name'];
3701f374598SGreg Roach                }
3711f374598SGreg Roach                $payload = [
3721f374598SGreg Roach                    'providers'     => $providers,
3731f374598SGreg Roach                    'selectedProv'  => self::$map_selections['provider'],
3741f374598SGreg Roach                    'styles'        => self::$map_providers[self::$map_selections['provider']]['styles'],
3751f374598SGreg Roach                    'selectedStyle' => self::$map_selections['style'],
3761f374598SGreg Roach                ];
3771f374598SGreg Roach                break;
3781f374598SGreg Roach            default:
3791f374598SGreg Roach                $payload = null;
3801f374598SGreg Roach        }
3811f374598SGreg Roach
3821f374598SGreg Roach        return $payload;
3831f374598SGreg Roach    }
3841f374598SGreg Roach
3851f374598SGreg Roach    /**
3861f374598SGreg Roach     * @param Request $request
3871f374598SGreg Roach     *
3881f374598SGreg Roach     * @return object
3891f374598SGreg Roach     * @throws Exception
3901f374598SGreg Roach     */
3911f374598SGreg Roach    public function getPedigreeMapAction(Request $request)
3921f374598SGreg Roach    {
3931f374598SGreg Roach        /** @var Tree $tree */
3941f374598SGreg Roach        $tree           = $request->attributes->get('tree');
3951f374598SGreg Roach        $xref           = $request->get('xref');
3961f374598SGreg Roach        $individual     = Individual::getInstance($xref, $tree);
3971f374598SGreg Roach        $maxgenerations = $tree->getPreference('MAX_PEDIGREE_GENERATIONS');
3981f374598SGreg Roach        $generations    = $request->get('generations', $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS'));
3991f374598SGreg Roach
400*b6e50991SGreg Roach        if ($individual === null) {
401*b6e50991SGreg Roach            throw new IndividualNotFoundException;
402*b6e50991SGreg Roach        } elseif (!$individual->canShow()) {
403*b6e50991SGreg Roach            throw new IndividualAccessDeniedException;
404*b6e50991SGreg Roach        }
405*b6e50991SGreg Roach
4061f374598SGreg Roach        return (object)[
4071f374598SGreg Roach            'name' => 'modules/openstreetmap/pedigreemap',
4081f374598SGreg Roach            'data' => [
4091f374598SGreg Roach                'assets'         => $this->assets(),
4101f374598SGreg Roach                'module'         => $this->getName(),
4111f374598SGreg Roach                'title'          => /* I18N: %s is an individual’s name */
4121f374598SGreg Roach                    I18N::translate('Pedigree map of %s', $individual->getFullName()),
4131f374598SGreg Roach                'tree'           => $tree,
4141f374598SGreg Roach                'individual'     => $individual,
4151f374598SGreg Roach                'generations'    => $generations,
4161f374598SGreg Roach                'maxgenerations' => $maxgenerations,
4171f374598SGreg Roach                'map'            => view(
4181f374598SGreg Roach                    'modules/openstreetmap/map',
4191f374598SGreg Roach                    [
4201f374598SGreg Roach                        'assets'      => $this->assets(),
4211f374598SGreg Roach                        'module'      => $this->getName(),
4221f374598SGreg Roach                        'ref'         => $individual->getXref(),
4231f374598SGreg Roach                        'type'        => 'pedigree',
4241f374598SGreg Roach                        'generations' => $generations,
4251f374598SGreg Roach                    ]
4261f374598SGreg Roach                ),
4271f374598SGreg Roach            ],
4281f374598SGreg Roach        ];
4291f374598SGreg Roach    }
4301f374598SGreg Roach
4311f374598SGreg Roach    // @TODO shift the following function to somewhere more appropriate during restructure
4321f374598SGreg Roach
4331f374598SGreg Roach    /**
4341f374598SGreg Roach     * Copied from AbstractChartController.php
4351f374598SGreg Roach     *
4361f374598SGreg Roach     * Find the ancestors of an individual, and generate an array indexed by
4371f374598SGreg Roach     * Sosa-Stradonitz number.
4381f374598SGreg Roach     *
4391f374598SGreg Roach     * @param Individual $individual  Start with this individual
4401f374598SGreg Roach     * @param int        $generations Fetch this number of generations
4411f374598SGreg Roach     *
4421f374598SGreg Roach     * @return Individual[]
4431f374598SGreg Roach     */
4441f374598SGreg Roach    private function sosaStradonitzAncestors(Individual $individual, int $generations): array
4451f374598SGreg Roach    {
4461f374598SGreg Roach        /** @var Individual[] $ancestors */
4471f374598SGreg Roach        $ancestors = [
4481f374598SGreg Roach            1 => $individual,
4491f374598SGreg Roach        ];
4501f374598SGreg Roach
4511f374598SGreg Roach        for ($i = 1, $max = 2 ** ($generations - 1); $i < $max; $i++) {
4521f374598SGreg Roach            $ancestors[$i * 2]     = null;
4531f374598SGreg Roach            $ancestors[$i * 2 + 1] = null;
4541f374598SGreg Roach
4551f374598SGreg Roach            $individual = $ancestors[$i];
4561f374598SGreg Roach
4571f374598SGreg Roach            if ($individual !== null) {
4581f374598SGreg Roach                $family = $individual->getPrimaryChildFamily();
4591f374598SGreg Roach                if ($family !== null) {
4601f374598SGreg Roach                    if ($family->getHusband() !== null) {
4611f374598SGreg Roach                        $ancestors[$i * 2] = $family->getHusband();
4621f374598SGreg Roach                    }
4631f374598SGreg Roach                    if ($family->getWife() !== null) {
4641f374598SGreg Roach                        $ancestors[$i * 2 + 1] = $family->getWife();
4651f374598SGreg Roach                    }
4661f374598SGreg Roach                }
4671f374598SGreg Roach            }
4681f374598SGreg Roach        }
4691f374598SGreg Roach
4701f374598SGreg Roach        return $ancestors;
4711f374598SGreg Roach    }
4721f374598SGreg Roach}
473