xref: /webtrees/app/Module/LifespansChartModule.php (revision f70bcff589628705e50ed72a6fc6cbad68558677)
1168ff6f3Sric2016<?php
23976b470SGreg Roach
3168ff6f3Sric2016/**
4168ff6f3Sric2016 * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6168ff6f3Sric2016 * This program is free software: you can redistribute it and/or modify
7168ff6f3Sric2016 * it under the terms of the GNU General Public License as published by
8168ff6f3Sric2016 * the Free Software Foundation, either version 3 of the License, or
9168ff6f3Sric2016 * (at your option) any later version.
10168ff6f3Sric2016 * This program is distributed in the hope that it will be useful,
11168ff6f3Sric2016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12168ff6f3Sric2016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13168ff6f3Sric2016 * GNU General Public License for more details.
14168ff6f3Sric2016 * 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/>.
16168ff6f3Sric2016 */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
20168ff6f3Sric2016namespace Fisharebest\Webtrees\Module;
21168ff6f3Sric2016
2271378461SGreg Roachuse Aura\Router\RouterContainer;
2371378461SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
24e2b8114dSGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
259867b2f0SGreg Roachuse Fisharebest\Webtrees\Auth;
26e2b8114dSGreg Roachuse Fisharebest\Webtrees\ColorGenerator;
27e2b8114dSGreg Roachuse Fisharebest\Webtrees\Date;
28168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
29168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
30e2b8114dSGreg Roachuse Fisharebest\Webtrees\Place;
31ac701fbdSGreg Roachuse Fisharebest\Webtrees\Registry;
32e2b8114dSGreg Roachuse Fisharebest\Webtrees\Tree;
33e2b8114dSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
34e2b8114dSGreg Roachuse Illuminate\Database\Query\JoinClause;
356ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
366ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3771378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
38168ff6f3Sric2016
399e18e23bSGreg Roachuse function app;
407c2c99faSGreg Roachuse function array_filter;
417c2c99faSGreg Roachuse function array_intersect;
427c2c99faSGreg Roachuse function array_map;
437c2c99faSGreg Roachuse function array_merge;
447c2c99faSGreg Roachuse function array_reduce;
457c2c99faSGreg Roachuse function array_unique;
469e18e23bSGreg Roachuse function assert;
477c2c99faSGreg Roachuse function count;
487c2c99faSGreg Roachuse function date;
49da616f3dSGreg Roachuse function explode;
50da616f3dSGreg Roachuse function implode;
517c2c99faSGreg Roachuse function intdiv;
527c2c99faSGreg Roachuse function is_array;
537c2c99faSGreg Roachuse function max;
547c2c99faSGreg Roachuse function md5;
557c2c99faSGreg Roachuse function min;
567c2c99faSGreg Roachuse function redirect;
577c2c99faSGreg Roachuse function response;
587c2c99faSGreg Roachuse function route;
597c2c99faSGreg Roachuse function usort;
607c2c99faSGreg Roachuse function view;
617c2c99faSGreg Roach
627c2c99faSGreg Roachuse const PHP_INT_MAX;
639e18e23bSGreg Roach
64168ff6f3Sric2016/**
65168ff6f3Sric2016 * Class LifespansChartModule
66168ff6f3Sric2016 */
6771378461SGreg Roachclass LifespansChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
68c1010edaSGreg Roach{
6949a243cbSGreg Roach    use ModuleChartTrait;
7049a243cbSGreg Roach
7172f04adfSGreg Roach    protected const ROUTE_URL = '/tree/{tree}/lifespans';
7271378461SGreg Roach
73da616f3dSGreg Roach    // In theory, only "@" is a safe separator, but it gives longer and uglier URLs.
74da616f3dSGreg Roach    // Unless some other application generates XREFs with a ".", we are safe.
75da616f3dSGreg Roach    protected const SEPARATOR = '.';
76da616f3dSGreg Roach
7771378461SGreg Roach    // Defaults
7871378461SGreg Roach    protected const DEFAULT_PARAMETERS = [];
7971378461SGreg Roach
80e2b8114dSGreg Roach    // Parameters for generating colors
81e2b8114dSGreg Roach    protected const RANGE      = 120; // degrees
82e2b8114dSGreg Roach    protected const SATURATION = 100; // percent
83e2b8114dSGreg Roach    protected const LIGHTNESS  = 30; // percent
84e2b8114dSGreg Roach    protected const ALPHA      = 0.25;
85e2b8114dSGreg Roach
86168ff6f3Sric2016    /**
8771378461SGreg Roach     * Initialization.
8871378461SGreg Roach     *
899e18e23bSGreg Roach     * @return void
9071378461SGreg Roach     */
919e18e23bSGreg Roach    public function boot(): void
9271378461SGreg Roach    {
939e18e23bSGreg Roach        $router_container = app(RouterContainer::class);
949e18e23bSGreg Roach        assert($router_container instanceof RouterContainer);
959e18e23bSGreg Roach
9671378461SGreg Roach        $router_container->getMap()
9772f04adfSGreg Roach            ->get(static::class, static::ROUTE_URL, $this)
9871378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST);
9971378461SGreg Roach    }
10071378461SGreg Roach
10171378461SGreg Roach    /**
1020cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
103168ff6f3Sric2016     *
104168ff6f3Sric2016     * @return string
105168ff6f3Sric2016     */
10649a243cbSGreg Roach    public function title(): string
107c1010edaSGreg Roach    {
108bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
109bbb76c12SGreg Roach        return I18N::translate('Lifespans');
110168ff6f3Sric2016    }
111168ff6f3Sric2016
112168ff6f3Sric2016    /**
113168ff6f3Sric2016     * A sentence describing what this module does.
114168ff6f3Sric2016     *
115168ff6f3Sric2016     * @return string
116168ff6f3Sric2016     */
11749a243cbSGreg Roach    public function description(): string
118c1010edaSGreg Roach    {
119bbb76c12SGreg Roach        /* I18N: Description of the “LifespansChart” module */
120bbb76c12SGreg Roach        return I18N::translate('A chart of individuals’ lifespans.');
121168ff6f3Sric2016    }
122168ff6f3Sric2016
123168ff6f3Sric2016    /**
124377a2979SGreg Roach     * CSS class for the URL.
125377a2979SGreg Roach     *
126377a2979SGreg Roach     * @return string
127377a2979SGreg Roach     */
128377a2979SGreg Roach    public function chartMenuClass(): string
129377a2979SGreg Roach    {
130377a2979SGreg Roach        return 'menu-chart-lifespan';
131377a2979SGreg Roach    }
132377a2979SGreg Roach
133377a2979SGreg Roach    /**
134e6562982SGreg Roach     * The URL for this chart.
135168ff6f3Sric2016     *
13660bc3e3fSGreg Roach     * @param Individual                        $individual
13709482a55SGreg Roach     * @param array<bool|int|string|array|null> $parameters
13860bc3e3fSGreg Roach     *
139e6562982SGreg Roach     * @return string
140168ff6f3Sric2016     */
141e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
142c1010edaSGreg Roach    {
14372f04adfSGreg Roach        return route(static::class, [
14471378461SGreg Roach                'tree'  => $individual->tree()->name(),
145da616f3dSGreg Roach                'xrefs' => $individual->xref(),
14671378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
147168ff6f3Sric2016    }
148e2b8114dSGreg Roach
149e2b8114dSGreg Roach    /**
1506ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
151e2b8114dSGreg Roach     *
1526ccdf4f0SGreg Roach     * @return ResponseInterface
153e2b8114dSGreg Roach     */
15471378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
155e2b8114dSGreg Roach    {
15657ab2231SGreg Roach        $tree = $request->getAttribute('tree');
1574ea62551SGreg Roach        assert($tree instanceof Tree);
1584ea62551SGreg Roach
15957ab2231SGreg Roach        $user      = $request->getAttribute('user');
16071378461SGreg Roach        $xrefs     = $request->getQueryParams()['xrefs'] ?? [];
1610b93976aSGreg Roach        $ajax      = $request->getQueryParams()['ajax'] ?? '';
162b46c87bdSGreg Roach
163da616f3dSGreg Roach        // URLs created by older versions may already contain an array.
164da616f3dSGreg Roach        if (!is_array($xrefs)) {
165da616f3dSGreg Roach            $xrefs = explode(self::SEPARATOR, $xrefs);
166da616f3dSGreg Roach        }
167da616f3dSGreg Roach
168b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
169b46c87bdSGreg Roach
170b46c87bdSGreg Roach        $addxref   = $params['addxref'] ?? '';
171b46c87bdSGreg Roach        $addfam    = (bool) ($params['addfam'] ?? false);
172a60f9d1cSGreg Roach        $place_id  = (int) ($params['place_id'] ?? 0);
173b46c87bdSGreg Roach        $start     = $params['start'] ?? '';
174b46c87bdSGreg Roach        $end       = $params['end'] ?? '';
175e2b8114dSGreg Roach
176a60f9d1cSGreg Roach        $place      = Place::find($place_id, $tree);
177e2b8114dSGreg Roach        $start_date = new Date($start);
178e2b8114dSGreg Roach        $end_date   = new Date($end);
179e2b8114dSGreg Roach
180e2b8114dSGreg Roach        $xrefs = array_unique($xrefs);
181e2b8114dSGreg Roach
182e2b8114dSGreg Roach        // Add an individual, and family members
1836b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($addxref, $tree);
184e2b8114dSGreg Roach        if ($individual !== null) {
185e2b8114dSGreg Roach            $xrefs[] = $addxref;
186e2b8114dSGreg Roach            if ($addfam) {
187e2b8114dSGreg Roach                $xrefs = array_merge($xrefs, $this->closeFamily($individual));
188e2b8114dSGreg Roach            }
189e2b8114dSGreg Roach        }
190e2b8114dSGreg Roach
191e2b8114dSGreg Roach        // Select by date and/or place.
192a60f9d1cSGreg Roach        if ($place_id !== 0 && $start_date->isOK() && $end_date->isOK()) {
193e2b8114dSGreg Roach            $date_xrefs  = $this->findIndividualsByDate($start_date, $end_date, $tree);
194e2b8114dSGreg Roach            $place_xrefs = $this->findIndividualsByPlace($place, $tree);
195e2b8114dSGreg Roach            $xrefs       = array_intersect($date_xrefs, $place_xrefs);
196e2b8114dSGreg Roach        } elseif ($start_date->isOK() && $end_date->isOK()) {
197e2b8114dSGreg Roach            $xrefs = $this->findIndividualsByDate($start_date, $end_date, $tree);
198a60f9d1cSGreg Roach        } elseif ($place_id !== 0) {
199e2b8114dSGreg Roach            $xrefs = $this->findIndividualsByPlace($place, $tree);
200e2b8114dSGreg Roach        }
201e2b8114dSGreg Roach
202e2b8114dSGreg Roach        // Filter duplicates and private individuals.
203e2b8114dSGreg Roach        $xrefs = array_unique($xrefs);
2040b5fd0a6SGreg Roach        $xrefs = array_filter($xrefs, static function (string $xref) use ($tree): bool {
2056b9cb339SGreg Roach            $individual = Registry::individualFactory()->make($xref, $tree);
206e2b8114dSGreg Roach
207e2b8114dSGreg Roach            return $individual !== null && $individual->canShow();
208e2b8114dSGreg Roach        });
209e2b8114dSGreg Roach
21071378461SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
21171378461SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
21272f04adfSGreg Roach            return redirect(route(static::class, [
2134ea62551SGreg Roach                'tree'  => $tree->name(),
214da616f3dSGreg Roach                'xrefs' => implode(self::SEPARATOR, $xrefs),
21571378461SGreg Roach            ]));
21671378461SGreg Roach        }
21771378461SGreg Roach
218ef483801SGreg Roach        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
21971378461SGreg Roach
2200b93976aSGreg Roach        if ($ajax === '1') {
22171378461SGreg Roach            $this->layout = 'layouts/ajax';
22271378461SGreg Roach
223a60f9d1cSGreg Roach            return $this->chart($tree, $xrefs);
224e2b8114dSGreg Roach        }
225e2b8114dSGreg Roach
22672f04adfSGreg Roach        $reset_url = route(static::class, ['tree' => $tree->name()]);
227e2b8114dSGreg Roach
22872f04adfSGreg Roach        $ajax_url = route(static::class, [
22971378461SGreg Roach            'ajax'  => true,
23071378461SGreg Roach            'tree'  => $tree->name(),
231da616f3dSGreg Roach            'xrefs' => implode(self::SEPARATOR, $xrefs),
232e2b8114dSGreg Roach        ]);
233e2b8114dSGreg Roach
2349b5537c3SGreg Roach        return $this->viewResponse('modules/lifespans-chart/page', [
235e2b8114dSGreg Roach            'ajax_url'  => $ajax_url,
23671378461SGreg Roach            'module'    => $this->name(),
237e2b8114dSGreg Roach            'reset_url' => $reset_url,
238e2b8114dSGreg Roach            'title'     => $this->title(),
239ef5d23f1SGreg Roach            'tree'      => $tree,
240e2b8114dSGreg Roach            'xrefs'     => $xrefs,
241e2b8114dSGreg Roach        ]);
242e2b8114dSGreg Roach    }
243e2b8114dSGreg Roach
244e2b8114dSGreg Roach    /**
245e2b8114dSGreg Roach     * @param Tree          $tree
246ac701fbdSGreg Roach     * @param array<string> $xrefs
247e2b8114dSGreg Roach     *
2486ccdf4f0SGreg Roach     * @return ResponseInterface
249e2b8114dSGreg Roach     */
250a60f9d1cSGreg Roach    protected function chart(Tree $tree, array $xrefs): ResponseInterface
251e2b8114dSGreg Roach    {
252e2b8114dSGreg Roach        /** @var Individual[] $individuals */
2530b93976aSGreg Roach        $individuals = array_map(static function (string $xref) use ($tree): ?Individual {
2546b9cb339SGreg Roach            return Registry::individualFactory()->make($xref, $tree);
255e2b8114dSGreg Roach        }, $xrefs);
256e2b8114dSGreg Roach
2570b93976aSGreg Roach        $individuals = array_filter($individuals, static function (?Individual $individual): bool {
2580b93976aSGreg Roach            return $individual instanceof Individual && $individual->canShow();
259e2b8114dSGreg Roach        });
260e2b8114dSGreg Roach
261e2b8114dSGreg Roach        // Sort the array in order of birth year
2620c1a5edcSGreg Roach        usort($individuals, Individual::birthDateComparator());
263e2b8114dSGreg Roach
264e2b8114dSGreg Roach        // Round to whole decades
2657c2c99faSGreg Roach        $start_year = intdiv($this->minYear($individuals), 10) * 10;
266e4e7c547SGreg Roach        $end_year   = intdiv($this->maxYear($individuals) + 9, 10) * 10;
267e2b8114dSGreg Roach
268e2b8114dSGreg Roach        $lifespans = $this->layoutIndividuals($individuals);
269e2b8114dSGreg Roach
270*f70bcff5SGreg Roach        $callback = static fn (int $carry, object $item): int => max($carry, $item->row);
2714c78e066SGreg Roach        $max_rows = array_reduce($lifespans, $callback, 0);
272e2b8114dSGreg Roach
273a60f9d1cSGreg Roach        $count    = count($xrefs);
274a60f9d1cSGreg Roach        $subtitle = I18N::plural('%s individual', '%s individuals', $count, I18N::number($count));
275a60f9d1cSGreg Roach
276e2b8114dSGreg Roach        $html = view('modules/lifespans-chart/chart', [
277e2b8114dSGreg Roach            'dir'        => I18N::direction(),
278e2b8114dSGreg Roach            'end_year'   => $end_year,
279e2b8114dSGreg Roach            'lifespans'  => $lifespans,
280e2b8114dSGreg Roach            'max_rows'   => $max_rows,
281e2b8114dSGreg Roach            'start_year' => $start_year,
282e2b8114dSGreg Roach            'subtitle'   => $subtitle,
283e2b8114dSGreg Roach        ]);
284e2b8114dSGreg Roach
2856ccdf4f0SGreg Roach        return response($html);
286e2b8114dSGreg Roach    }
287e2b8114dSGreg Roach
288e2b8114dSGreg Roach    /**
289e2b8114dSGreg Roach     * Find the latest event year for individuals
290e2b8114dSGreg Roach     *
291ac701fbdSGreg Roach     * @param array<Individual> $individuals
292e2b8114dSGreg Roach     *
293e2b8114dSGreg Roach     * @return int
294e2b8114dSGreg Roach     */
295e2b8114dSGreg Roach    protected function maxYear(array $individuals): int
296e2b8114dSGreg Roach    {
2974c78e066SGreg Roach        $jd = array_reduce($individuals, static function (int $carry, Individual $item): int {
298c09a4a40SGreg Roach            if ($item->getEstimatedDeathDate()->isOK()) {
299c09a4a40SGreg Roach                return max($carry, $item->getEstimatedDeathDate()->maximumJulianDay());
300c09a4a40SGreg Roach            }
301c09a4a40SGreg Roach
302c09a4a40SGreg Roach            return $carry;
303e2b8114dSGreg Roach        }, 0);
304e2b8114dSGreg Roach
305e2b8114dSGreg Roach        $year = $this->jdToYear($jd);
306e2b8114dSGreg Roach
307e2b8114dSGreg Roach        // Don't show future dates
308e2b8114dSGreg Roach        return min($year, (int) date('Y'));
309e2b8114dSGreg Roach    }
310e2b8114dSGreg Roach
311e2b8114dSGreg Roach    /**
312e2b8114dSGreg Roach     * Find the earliest event year for individuals
313e2b8114dSGreg Roach     *
314ac701fbdSGreg Roach     * @param array<Individual> $individuals
315e2b8114dSGreg Roach     *
316e2b8114dSGreg Roach     * @return int
317e2b8114dSGreg Roach     */
318e2b8114dSGreg Roach    protected function minYear(array $individuals): int
319e2b8114dSGreg Roach    {
3204c78e066SGreg Roach        $jd = array_reduce($individuals, static function (int $carry, Individual $item): int {
321c09a4a40SGreg Roach            if ($item->getEstimatedBirthDate()->isOK()) {
322c09a4a40SGreg Roach                return min($carry, $item->getEstimatedBirthDate()->minimumJulianDay());
323c09a4a40SGreg Roach            }
324c09a4a40SGreg Roach
325c09a4a40SGreg Roach            return $carry;
326e2b8114dSGreg Roach        }, PHP_INT_MAX);
327e2b8114dSGreg Roach
328e2b8114dSGreg Roach        return $this->jdToYear($jd);
329e2b8114dSGreg Roach    }
330e2b8114dSGreg Roach
331e2b8114dSGreg Roach    /**
332e2b8114dSGreg Roach     * Convert a julian day to a gregorian year
333e2b8114dSGreg Roach     *
334e2b8114dSGreg Roach     * @param int $jd
335e2b8114dSGreg Roach     *
336e2b8114dSGreg Roach     * @return int
337e2b8114dSGreg Roach     */
338e2b8114dSGreg Roach    protected function jdToYear(int $jd): int
339e2b8114dSGreg Roach    {
340e2b8114dSGreg Roach        if ($jd === 0) {
341e2b8114dSGreg Roach            return 0;
342e2b8114dSGreg Roach        }
343e2b8114dSGreg Roach
344e2b8114dSGreg Roach        $gregorian = new GregorianCalendar();
345e2b8114dSGreg Roach        [$y] = $gregorian->jdToYmd($jd);
346e2b8114dSGreg Roach
347e2b8114dSGreg Roach        return $y;
348e2b8114dSGreg Roach    }
349e2b8114dSGreg Roach
350e2b8114dSGreg Roach    /**
351e2b8114dSGreg Roach     * @param Date $start
352e2b8114dSGreg Roach     * @param Date $end
353e2b8114dSGreg Roach     * @param Tree $tree
354e2b8114dSGreg Roach     *
35524f2a3afSGreg Roach     * @return array<string>
356e2b8114dSGreg Roach     */
357e2b8114dSGreg Roach    protected function findIndividualsByDate(Date $start, Date $end, Tree $tree): array
358e2b8114dSGreg Roach    {
359e2b8114dSGreg Roach        return DB::table('individuals')
3600b5fd0a6SGreg Roach            ->join('dates', static function (JoinClause $join): void {
361e2b8114dSGreg Roach                $join
362e2b8114dSGreg Roach                    ->on('d_file', '=', 'i_file')
363e2b8114dSGreg Roach                    ->on('d_gid', '=', 'i_id');
364e2b8114dSGreg Roach            })
365e2b8114dSGreg Roach            ->where('i_file', '=', $tree->id())
366e2b8114dSGreg Roach            ->where('d_julianday1', '<=', $end->maximumJulianDay())
367e2b8114dSGreg Roach            ->where('d_julianday2', '>=', $start->minimumJulianDay())
368e2b8114dSGreg Roach            ->whereNotIn('d_fact', ['BAPL', 'ENDL', 'SLGC', 'SLGS', '_TODO', 'CHAN'])
369e2b8114dSGreg Roach            ->pluck('i_id')
370e2b8114dSGreg Roach            ->all();
371e2b8114dSGreg Roach    }
372e2b8114dSGreg Roach
373e2b8114dSGreg Roach    /**
374e2b8114dSGreg Roach     * @param Place $place
375e2b8114dSGreg Roach     * @param Tree  $tree
376e2b8114dSGreg Roach     *
37724f2a3afSGreg Roach     * @return array<string>
378e2b8114dSGreg Roach     */
379e2b8114dSGreg Roach    protected function findIndividualsByPlace(Place $place, Tree $tree): array
380e2b8114dSGreg Roach    {
381e2b8114dSGreg Roach        return DB::table('individuals')
3820b5fd0a6SGreg Roach            ->join('placelinks', static function (JoinClause $join): void {
383e2b8114dSGreg Roach                $join
384e2b8114dSGreg Roach                    ->on('pl_file', '=', 'i_file')
385e2b8114dSGreg Roach                    ->on('pl_gid', '=', 'i_id');
386e2b8114dSGreg Roach            })
387e2b8114dSGreg Roach            ->where('i_file', '=', $tree->id())
388392561bbSGreg Roach            ->where('pl_p_id', '=', $place->id())
389e2b8114dSGreg Roach            ->pluck('i_id')
390e2b8114dSGreg Roach            ->all();
391e2b8114dSGreg Roach    }
392e2b8114dSGreg Roach
393e2b8114dSGreg Roach    /**
394e2b8114dSGreg Roach     * Find the close family members of an individual.
395e2b8114dSGreg Roach     *
396e2b8114dSGreg Roach     * @param Individual $individual
397e2b8114dSGreg Roach     *
39824f2a3afSGreg Roach     * @return array<string>
399e2b8114dSGreg Roach     */
400e2b8114dSGreg Roach    protected function closeFamily(Individual $individual): array
401e2b8114dSGreg Roach    {
402e2b8114dSGreg Roach        $xrefs = [];
403e2b8114dSGreg Roach
40439ca88baSGreg Roach        foreach ($individual->spouseFamilies() as $family) {
40539ca88baSGreg Roach            foreach ($family->children() as $child) {
406e2b8114dSGreg Roach                $xrefs[] = $child->xref();
407e2b8114dSGreg Roach            }
408e2b8114dSGreg Roach
40939ca88baSGreg Roach            foreach ($family->spouses() as $spouse) {
410e2b8114dSGreg Roach                $xrefs[] = $spouse->xref();
411e2b8114dSGreg Roach            }
412e2b8114dSGreg Roach        }
413e2b8114dSGreg Roach
41439ca88baSGreg Roach        foreach ($individual->childFamilies() as $family) {
41539ca88baSGreg Roach            foreach ($family->children() as $child) {
416e2b8114dSGreg Roach                $xrefs[] = $child->xref();
417e2b8114dSGreg Roach            }
418e2b8114dSGreg Roach
41939ca88baSGreg Roach            foreach ($family->spouses() as $spouse) {
420e2b8114dSGreg Roach                $xrefs[] = $spouse->xref();
421e2b8114dSGreg Roach            }
422e2b8114dSGreg Roach        }
423e2b8114dSGreg Roach
424e2b8114dSGreg Roach        return $xrefs;
425e2b8114dSGreg Roach    }
426e2b8114dSGreg Roach
427e2b8114dSGreg Roach    /**
42809482a55SGreg Roach     * @param array<Individual> $individuals
42971378461SGreg Roach     *
430*f70bcff5SGreg Roach     * @return array<object>
43171378461SGreg Roach     */
43271378461SGreg Roach    private function layoutIndividuals(array $individuals): array
43371378461SGreg Roach    {
43471378461SGreg Roach        $colors = [
43571378461SGreg Roach            'M' => new ColorGenerator(240, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE * -1),
43671378461SGreg Roach            'F' => new ColorGenerator(000, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE),
43771378461SGreg Roach            'U' => new ColorGenerator(120, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE),
43871378461SGreg Roach        ];
43971378461SGreg Roach
44071378461SGreg Roach        $current_year = (int) date('Y');
44171378461SGreg Roach
44271378461SGreg Roach        // Latest year used in each row
44371378461SGreg Roach        $rows = [];
44471378461SGreg Roach
44571378461SGreg Roach        $lifespans = [];
44671378461SGreg Roach
44771378461SGreg Roach        foreach ($individuals as $individual) {
44871378461SGreg Roach            $birth_jd   = $individual->getEstimatedBirthDate()->minimumJulianDay();
44971378461SGreg Roach            $birth_year = $this->jdToYear($birth_jd);
45071378461SGreg Roach            $death_jd   = $individual->getEstimatedDeathDate()->maximumJulianDay();
45171378461SGreg Roach            $death_year = $this->jdToYear($death_jd);
45271378461SGreg Roach
45371378461SGreg Roach            // Died before they were born?  Swapping the dates allows them to be shown.
45471378461SGreg Roach            if ($death_year < $birth_year) {
45571378461SGreg Roach                $death_year = $birth_year;
45671378461SGreg Roach            }
45771378461SGreg Roach
45871378461SGreg Roach            // Don't show death dates in the future.
45971378461SGreg Roach            $death_year = min($death_year, $current_year);
46071378461SGreg Roach
46171378461SGreg Roach            // Add this individual to the next row in the chart...
46271378461SGreg Roach            $next_row = count($rows);
46371378461SGreg Roach            // ...unless we can find an existing row where it fits.
46471378461SGreg Roach            foreach ($rows as $row => $year) {
46571378461SGreg Roach                if ($year < $birth_year) {
46671378461SGreg Roach                    $next_row = $row;
46771378461SGreg Roach                    break;
46871378461SGreg Roach                }
46971378461SGreg Roach            }
47071378461SGreg Roach
47171378461SGreg Roach            // Fill the row up to the year (leaving a small gap)
47271378461SGreg Roach            $rows[$next_row] = $death_year;
47371378461SGreg Roach
47471378461SGreg Roach            $lifespans[] = (object) [
47571378461SGreg Roach                'background' => $colors[$individual->sex()]->getNextColor(),
47671378461SGreg Roach                'birth_year' => $birth_year,
47771378461SGreg Roach                'death_year' => $death_year,
47871378461SGreg Roach                'id'         => 'individual-' . md5($individual->xref()),
47971378461SGreg Roach                'individual' => $individual,
48071378461SGreg Roach                'row'        => $next_row,
48171378461SGreg Roach            ];
48271378461SGreg Roach        }
48371378461SGreg Roach
48471378461SGreg Roach        return $lifespans;
48571378461SGreg Roach    }
486168ff6f3Sric2016}
487