xref: /webtrees/app/Module/LifespansChartModule.php (revision 59597b37d69e8147c3f4a27643e9c8edaa2a0592)
1168ff6f3Sric2016<?php
23976b470SGreg Roach
3168ff6f3Sric2016/**
4168ff6f3Sric2016 * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
15168ff6f3Sric2016 * along with this program. If not, see <http://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;
31e2b8114dSGreg Roachuse Fisharebest\Webtrees\Tree;
32e2b8114dSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
33e2b8114dSGreg Roachuse Illuminate\Database\Query\JoinClause;
346ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
356ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3671378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
37e2b8114dSGreg Roachuse stdClass;
38168ff6f3Sric2016
39168ff6f3Sric2016/**
40168ff6f3Sric2016 * Class LifespansChartModule
41168ff6f3Sric2016 */
4271378461SGreg Roachclass LifespansChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
43c1010edaSGreg Roach{
4449a243cbSGreg Roach    use ModuleChartTrait;
4549a243cbSGreg Roach
4671378461SGreg Roach    private const ROUTE_NAME = 'lifespans-chart';
4771378461SGreg Roach    private const ROUTE_URL  = '/tree/{tree}/lifespans';
4871378461SGreg Roach
4971378461SGreg Roach    // Defaults
5071378461SGreg Roach    protected const DEFAULT_PARAMETERS = [];
5171378461SGreg Roach
52e2b8114dSGreg Roach    // Parameters for generating colors
53e2b8114dSGreg Roach    protected const RANGE      = 120; // degrees
54e2b8114dSGreg Roach    protected const SATURATION = 100; // percent
55e2b8114dSGreg Roach    protected const LIGHTNESS  = 30; // percent
56e2b8114dSGreg Roach    protected const ALPHA      = 0.25;
57e2b8114dSGreg Roach
58168ff6f3Sric2016    /**
5971378461SGreg Roach     * Initialization.
6071378461SGreg Roach     *
6171378461SGreg Roach     * @param RouterContainer $router_container
6271378461SGreg Roach     */
6371378461SGreg Roach    public function boot(RouterContainer $router_container)
6471378461SGreg Roach    {
6571378461SGreg Roach        $router_container->getMap()
6671378461SGreg Roach            ->get(self::ROUTE_NAME, self::ROUTE_URL, self::class)
6771378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST);
6871378461SGreg Roach    }
6971378461SGreg Roach
7071378461SGreg Roach    /**
710cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
72168ff6f3Sric2016     *
73168ff6f3Sric2016     * @return string
74168ff6f3Sric2016     */
7549a243cbSGreg Roach    public function title(): string
76c1010edaSGreg Roach    {
77bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
78bbb76c12SGreg Roach        return I18N::translate('Lifespans');
79168ff6f3Sric2016    }
80168ff6f3Sric2016
81168ff6f3Sric2016    /**
82168ff6f3Sric2016     * A sentence describing what this module does.
83168ff6f3Sric2016     *
84168ff6f3Sric2016     * @return string
85168ff6f3Sric2016     */
8649a243cbSGreg Roach    public function description(): string
87c1010edaSGreg Roach    {
88bbb76c12SGreg Roach        /* I18N: Description of the “LifespansChart” module */
89bbb76c12SGreg Roach        return I18N::translate('A chart of individuals’ lifespans.');
90168ff6f3Sric2016    }
91168ff6f3Sric2016
92168ff6f3Sric2016    /**
93377a2979SGreg Roach     * CSS class for the URL.
94377a2979SGreg Roach     *
95377a2979SGreg Roach     * @return string
96377a2979SGreg Roach     */
97377a2979SGreg Roach    public function chartMenuClass(): string
98377a2979SGreg Roach    {
99377a2979SGreg Roach        return 'menu-chart-lifespan';
100377a2979SGreg Roach    }
101377a2979SGreg Roach
102377a2979SGreg Roach    /**
103e6562982SGreg Roach     * The URL for this chart.
104168ff6f3Sric2016     *
10560bc3e3fSGreg Roach     * @param Individual $individual
106*59597b37SGreg Roach     * @param mixed[]    $parameters
10760bc3e3fSGreg Roach     *
108e6562982SGreg Roach     * @return string
109168ff6f3Sric2016     */
110e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
111c1010edaSGreg Roach    {
11271378461SGreg Roach        return route(self::ROUTE_NAME, [
11371378461SGreg Roach                'tree' => $individual->tree()->name(),
11471378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
115168ff6f3Sric2016    }
116e2b8114dSGreg Roach
117e2b8114dSGreg Roach    /**
1186ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
119e2b8114dSGreg Roach     *
1206ccdf4f0SGreg Roach     * @return ResponseInterface
121e2b8114dSGreg Roach     */
12271378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
123e2b8114dSGreg Roach    {
12457ab2231SGreg Roach        $tree      = $request->getAttribute('tree');
12557ab2231SGreg Roach        $user      = $request->getAttribute('user');
12671378461SGreg Roach        $xrefs     = $request->getQueryParams()['xrefs'] ?? [];
1270b93976aSGreg Roach        $ajax      = $request->getQueryParams()['ajax'] ?? '';
12871378461SGreg Roach        $addxref   = $request->getParsedBody()['addxref'] ?? '';
12971378461SGreg Roach        $addfam    = (bool) ($request->getParsedBody()['addfam'] ?? false);
13071378461SGreg Roach        $placename = $request->getParsedBody()['placename'] ?? '';
13171378461SGreg Roach        $start     = $request->getParsedBody()['start'] ?? '';
13271378461SGreg Roach        $end       = $request->getParsedBody()['end'] ?? '';
133e2b8114dSGreg Roach
134e2b8114dSGreg Roach        $place      = new Place($placename, $tree);
135e2b8114dSGreg Roach        $start_date = new Date($start);
136e2b8114dSGreg Roach        $end_date   = new Date($end);
137e2b8114dSGreg Roach
138e2b8114dSGreg Roach        $xrefs = array_unique($xrefs);
139e2b8114dSGreg Roach
140e2b8114dSGreg Roach        // Add an individual, and family members
141e2b8114dSGreg Roach        $individual = Individual::getInstance($addxref, $tree);
142e2b8114dSGreg Roach        if ($individual !== null) {
143e2b8114dSGreg Roach            $xrefs[] = $addxref;
144e2b8114dSGreg Roach            if ($addfam) {
145e2b8114dSGreg Roach                $xrefs = array_merge($xrefs, $this->closeFamily($individual));
146e2b8114dSGreg Roach            }
147e2b8114dSGreg Roach        }
148e2b8114dSGreg Roach
149e2b8114dSGreg Roach        // Select by date and/or place.
15071378461SGreg Roach        if ($placename !== '' && $start_date->isOK() && $end_date->isOK()) {
151e2b8114dSGreg Roach            $date_xrefs  = $this->findIndividualsByDate($start_date, $end_date, $tree);
152e2b8114dSGreg Roach            $place_xrefs = $this->findIndividualsByPlace($place, $tree);
153e2b8114dSGreg Roach            $xrefs       = array_intersect($date_xrefs, $place_xrefs);
154e2b8114dSGreg Roach        } elseif ($start_date->isOK() && $end_date->isOK()) {
155e2b8114dSGreg Roach            $xrefs = $this->findIndividualsByDate($start_date, $end_date, $tree);
156e2b8114dSGreg Roach        } elseif ($placename !== '') {
157e2b8114dSGreg Roach            $xrefs = $this->findIndividualsByPlace($place, $tree);
158e2b8114dSGreg Roach        }
159e2b8114dSGreg Roach
160e2b8114dSGreg Roach        // Filter duplicates and private individuals.
161e2b8114dSGreg Roach        $xrefs = array_unique($xrefs);
1620b5fd0a6SGreg Roach        $xrefs = array_filter($xrefs, static function (string $xref) use ($tree): bool {
163e2b8114dSGreg Roach            $individual = Individual::getInstance($xref, $tree);
164e2b8114dSGreg Roach
165e2b8114dSGreg Roach            return $individual !== null && $individual->canShow();
166e2b8114dSGreg Roach        });
167e2b8114dSGreg Roach
16871378461SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
16971378461SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
17071378461SGreg Roach            return redirect(route(self::ROUTE_NAME, [
17171378461SGreg Roach                'tree'  => $request->getAttribute('tree')->name(),
17271378461SGreg Roach                'xrefs' => $xrefs,
17371378461SGreg Roach            ]));
17471378461SGreg Roach        }
17571378461SGreg Roach
17671378461SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
17771378461SGreg Roach
1780b93976aSGreg Roach        if ($ajax === '1') {
17971378461SGreg Roach            $this->layout = 'layouts/ajax';
18071378461SGreg Roach
181e2b8114dSGreg Roach            $subtitle = $this->subtitle(count($xrefs), $start_date, $end_date, $placename);
182e2b8114dSGreg Roach
183e2b8114dSGreg Roach            return $this->chart($tree, $xrefs, $subtitle);
184e2b8114dSGreg Roach        }
185e2b8114dSGreg Roach
18671378461SGreg Roach        $reset_url = route(self::ROUTE_NAME, ['tree' => $tree->name()]);
187e2b8114dSGreg Roach
18871378461SGreg Roach        $ajax_url = route(self::ROUTE_NAME, [
18971378461SGreg Roach            'ajax'  => true,
19071378461SGreg Roach            'tree'  => $tree->name(),
19171378461SGreg Roach            'xrefs' => $xrefs,
192e2b8114dSGreg Roach        ]);
193e2b8114dSGreg Roach
1949b5537c3SGreg Roach        return $this->viewResponse('modules/lifespans-chart/page', [
195e2b8114dSGreg Roach            'ajax_url'  => $ajax_url,
19671378461SGreg Roach            'module'    => $this->name(),
197e2b8114dSGreg Roach            'reset_url' => $reset_url,
198e2b8114dSGreg Roach            'title'     => $this->title(),
199e2b8114dSGreg Roach            'xrefs'     => $xrefs,
200e2b8114dSGreg Roach        ]);
201e2b8114dSGreg Roach    }
202e2b8114dSGreg Roach
203e2b8114dSGreg Roach    /**
204e2b8114dSGreg Roach     * @param Tree   $tree
205e2b8114dSGreg Roach     * @param array  $xrefs
206e2b8114dSGreg Roach     * @param string $subtitle
207e2b8114dSGreg Roach     *
2086ccdf4f0SGreg Roach     * @return ResponseInterface
209e2b8114dSGreg Roach     */
2106ccdf4f0SGreg Roach    protected function chart(Tree $tree, array $xrefs, string $subtitle): ResponseInterface
211e2b8114dSGreg Roach    {
212e2b8114dSGreg Roach        /** @var Individual[] $individuals */
2130b93976aSGreg Roach        $individuals = array_map(static function (string $xref) use ($tree): ?Individual {
214e2b8114dSGreg Roach            return Individual::getInstance($xref, $tree);
215e2b8114dSGreg Roach        }, $xrefs);
216e2b8114dSGreg Roach
2170b93976aSGreg Roach        $individuals = array_filter($individuals, static function (?Individual $individual): bool {
2180b93976aSGreg Roach            return $individual instanceof Individual && $individual->canShow();
219e2b8114dSGreg Roach        });
220e2b8114dSGreg Roach
221e2b8114dSGreg Roach        // Sort the array in order of birth year
2220c1a5edcSGreg Roach        usort($individuals, Individual::birthDateComparator());
223e2b8114dSGreg Roach
224e2b8114dSGreg Roach        // Round to whole decades
225e2b8114dSGreg Roach        $start_year = (int) floor($this->minYear($individuals) / 10) * 10;
226e2b8114dSGreg Roach        $end_year   = (int) ceil($this->maxYear($individuals) / 10) * 10;
227e2b8114dSGreg Roach
228e2b8114dSGreg Roach        $lifespans = $this->layoutIndividuals($individuals);
229e2b8114dSGreg Roach
2300b5fd0a6SGreg Roach        $max_rows = array_reduce($lifespans, static function ($carry, stdClass $item) {
231e2b8114dSGreg Roach            return max($carry, $item->row);
232e2b8114dSGreg Roach        }, 0);
233e2b8114dSGreg Roach
234e2b8114dSGreg Roach        $html = view('modules/lifespans-chart/chart', [
235e2b8114dSGreg Roach            'dir'        => I18N::direction(),
236e2b8114dSGreg Roach            'end_year'   => $end_year,
237e2b8114dSGreg Roach            'lifespans'  => $lifespans,
238e2b8114dSGreg Roach            'max_rows'   => $max_rows,
239e2b8114dSGreg Roach            'start_year' => $start_year,
240e2b8114dSGreg Roach            'subtitle'   => $subtitle,
241e2b8114dSGreg Roach        ]);
242e2b8114dSGreg Roach
2436ccdf4f0SGreg Roach        return response($html);
244e2b8114dSGreg Roach    }
245e2b8114dSGreg Roach
246e2b8114dSGreg Roach    /**
247e2b8114dSGreg Roach     * Find the latest event year for individuals
248e2b8114dSGreg Roach     *
249e2b8114dSGreg Roach     * @param array $individuals
250e2b8114dSGreg Roach     *
251e2b8114dSGreg Roach     * @return int
252e2b8114dSGreg Roach     */
253e2b8114dSGreg Roach    protected function maxYear(array $individuals): int
254e2b8114dSGreg Roach    {
2550b5fd0a6SGreg Roach        $jd = array_reduce($individuals, static function ($carry, Individual $item) {
256e2b8114dSGreg Roach            return max($carry, $item->getEstimatedDeathDate()->maximumJulianDay());
257e2b8114dSGreg Roach        }, 0);
258e2b8114dSGreg Roach
259e2b8114dSGreg Roach        $year = $this->jdToYear($jd);
260e2b8114dSGreg Roach
261e2b8114dSGreg Roach        // Don't show future dates
262e2b8114dSGreg Roach        return min($year, (int) date('Y'));
263e2b8114dSGreg Roach    }
264e2b8114dSGreg Roach
265e2b8114dSGreg Roach    /**
266e2b8114dSGreg Roach     * Find the earliest event year for individuals
267e2b8114dSGreg Roach     *
268e2b8114dSGreg Roach     * @param array $individuals
269e2b8114dSGreg Roach     *
270e2b8114dSGreg Roach     * @return int
271e2b8114dSGreg Roach     */
272e2b8114dSGreg Roach    protected function minYear(array $individuals): int
273e2b8114dSGreg Roach    {
2740b5fd0a6SGreg Roach        $jd = array_reduce($individuals, static function ($carry, Individual $item) {
275e2b8114dSGreg Roach            return min($carry, $item->getEstimatedBirthDate()->minimumJulianDay());
276e2b8114dSGreg Roach        }, PHP_INT_MAX);
277e2b8114dSGreg Roach
278e2b8114dSGreg Roach        return $this->jdToYear($jd);
279e2b8114dSGreg Roach    }
280e2b8114dSGreg Roach
281e2b8114dSGreg Roach    /**
282e2b8114dSGreg Roach     * Convert a julian day to a gregorian year
283e2b8114dSGreg Roach     *
284e2b8114dSGreg Roach     * @param int $jd
285e2b8114dSGreg Roach     *
286e2b8114dSGreg Roach     * @return int
287e2b8114dSGreg Roach     */
288e2b8114dSGreg Roach    protected function jdToYear(int $jd): int
289e2b8114dSGreg Roach    {
290e2b8114dSGreg Roach        if ($jd === 0) {
291e2b8114dSGreg Roach            return 0;
292e2b8114dSGreg Roach        }
293e2b8114dSGreg Roach
294e2b8114dSGreg Roach        $gregorian = new GregorianCalendar();
295e2b8114dSGreg Roach        [$y] = $gregorian->jdToYmd($jd);
296e2b8114dSGreg Roach
297e2b8114dSGreg Roach        return $y;
298e2b8114dSGreg Roach    }
299e2b8114dSGreg Roach
300e2b8114dSGreg Roach    /**
301e2b8114dSGreg Roach     * @param Date $start
302e2b8114dSGreg Roach     * @param Date $end
303e2b8114dSGreg Roach     * @param Tree $tree
304e2b8114dSGreg Roach     *
305e2b8114dSGreg Roach     * @return string[]
306e2b8114dSGreg Roach     */
307e2b8114dSGreg Roach    protected function findIndividualsByDate(Date $start, Date $end, Tree $tree): array
308e2b8114dSGreg Roach    {
309e2b8114dSGreg Roach        return DB::table('individuals')
3100b5fd0a6SGreg Roach            ->join('dates', static function (JoinClause $join): void {
311e2b8114dSGreg Roach                $join
312e2b8114dSGreg Roach                    ->on('d_file', '=', 'i_file')
313e2b8114dSGreg Roach                    ->on('d_gid', '=', 'i_id');
314e2b8114dSGreg Roach            })
315e2b8114dSGreg Roach            ->where('i_file', '=', $tree->id())
316e2b8114dSGreg Roach            ->where('d_julianday1', '<=', $end->maximumJulianDay())
317e2b8114dSGreg Roach            ->where('d_julianday2', '>=', $start->minimumJulianDay())
318e2b8114dSGreg Roach            ->whereNotIn('d_fact', ['BAPL', 'ENDL', 'SLGC', 'SLGS', '_TODO', 'CHAN'])
319e2b8114dSGreg Roach            ->pluck('i_id')
320e2b8114dSGreg Roach            ->all();
321e2b8114dSGreg Roach    }
322e2b8114dSGreg Roach
323e2b8114dSGreg Roach    /**
324e2b8114dSGreg Roach     * @param Place $place
325e2b8114dSGreg Roach     * @param Tree  $tree
326e2b8114dSGreg Roach     *
327e2b8114dSGreg Roach     * @return string[]
328e2b8114dSGreg Roach     */
329e2b8114dSGreg Roach    protected function findIndividualsByPlace(Place $place, Tree $tree): array
330e2b8114dSGreg Roach    {
331e2b8114dSGreg Roach        return DB::table('individuals')
3320b5fd0a6SGreg Roach            ->join('placelinks', static function (JoinClause $join): void {
333e2b8114dSGreg Roach                $join
334e2b8114dSGreg Roach                    ->on('pl_file', '=', 'i_file')
335e2b8114dSGreg Roach                    ->on('pl_gid', '=', 'i_id');
336e2b8114dSGreg Roach            })
337e2b8114dSGreg Roach            ->where('i_file', '=', $tree->id())
338392561bbSGreg Roach            ->where('pl_p_id', '=', $place->id())
339e2b8114dSGreg Roach            ->pluck('i_id')
340e2b8114dSGreg Roach            ->all();
341e2b8114dSGreg Roach    }
342e2b8114dSGreg Roach
343e2b8114dSGreg Roach    /**
344e2b8114dSGreg Roach     * Find the close family members of an individual.
345e2b8114dSGreg Roach     *
346e2b8114dSGreg Roach     * @param Individual $individual
347e2b8114dSGreg Roach     *
348e2b8114dSGreg Roach     * @return string[]
349e2b8114dSGreg Roach     */
350e2b8114dSGreg Roach    protected function closeFamily(Individual $individual): array
351e2b8114dSGreg Roach    {
352e2b8114dSGreg Roach        $xrefs = [];
353e2b8114dSGreg Roach
35439ca88baSGreg Roach        foreach ($individual->spouseFamilies() as $family) {
35539ca88baSGreg Roach            foreach ($family->children() as $child) {
356e2b8114dSGreg Roach                $xrefs[] = $child->xref();
357e2b8114dSGreg Roach            }
358e2b8114dSGreg Roach
35939ca88baSGreg Roach            foreach ($family->spouses() as $spouse) {
360e2b8114dSGreg Roach                $xrefs[] = $spouse->xref();
361e2b8114dSGreg Roach            }
362e2b8114dSGreg Roach        }
363e2b8114dSGreg Roach
36439ca88baSGreg Roach        foreach ($individual->childFamilies() as $family) {
36539ca88baSGreg Roach            foreach ($family->children() as $child) {
366e2b8114dSGreg Roach                $xrefs[] = $child->xref();
367e2b8114dSGreg Roach            }
368e2b8114dSGreg Roach
36939ca88baSGreg Roach            foreach ($family->spouses() as $spouse) {
370e2b8114dSGreg Roach                $xrefs[] = $spouse->xref();
371e2b8114dSGreg Roach            }
372e2b8114dSGreg Roach        }
373e2b8114dSGreg Roach
374e2b8114dSGreg Roach        return $xrefs;
375e2b8114dSGreg Roach    }
376e2b8114dSGreg Roach
377e2b8114dSGreg Roach    /**
378e2b8114dSGreg Roach     * Generate a subtitle, based on filter parameters
379e2b8114dSGreg Roach     *
380e2b8114dSGreg Roach     * @param int    $count
381e2b8114dSGreg Roach     * @param Date   $start
382e2b8114dSGreg Roach     * @param Date   $end
383e2b8114dSGreg Roach     * @param string $placename
384e2b8114dSGreg Roach     *
385e2b8114dSGreg Roach     * @return string
386e2b8114dSGreg Roach     */
387e2b8114dSGreg Roach    protected function subtitle(int $count, Date $start, Date $end, string $placename): string
388e2b8114dSGreg Roach    {
38971378461SGreg Roach        if ($placename !== '' && $start->isOK() && $end->isOK()) {
390e2b8114dSGreg Roach            return I18N::plural(
391e2b8114dSGreg Roach                '%s individual with events in %s between %s and %s',
392e2b8114dSGreg Roach                '%s individuals with events in %s between %s and %s',
393e2b8114dSGreg Roach                $count,
394e2b8114dSGreg Roach                I18N::number($count),
395e2b8114dSGreg Roach                $placename,
396e2b8114dSGreg Roach                $start->display(false, '%Y'),
397e2b8114dSGreg Roach                $end->display(false, '%Y')
398e2b8114dSGreg Roach            );
399e2b8114dSGreg Roach        }
400e2b8114dSGreg Roach
401e2b8114dSGreg Roach        if ($placename !== '') {
402e2b8114dSGreg Roach            return I18N::plural(
403e2b8114dSGreg Roach                '%s individual with events in %s',
404e2b8114dSGreg Roach                '%s individuals with events in %s',
405e2b8114dSGreg Roach                $count,
406e2b8114dSGreg Roach                I18N::number($count),
407e2b8114dSGreg Roach                $placename
408e2b8114dSGreg Roach            );
409e2b8114dSGreg Roach        }
410e2b8114dSGreg Roach
411e2b8114dSGreg Roach        if ($start->isOK() && $end->isOK()) {
412e2b8114dSGreg Roach            return I18N::plural(
413e2b8114dSGreg Roach                '%s individual with events between %s and %s',
414e2b8114dSGreg Roach                '%s individuals with events between %s and %s',
415e2b8114dSGreg Roach                $count,
416e2b8114dSGreg Roach                I18N::number($count),
417e2b8114dSGreg Roach                $start->display(false, '%Y'),
418e2b8114dSGreg Roach                $end->display(false, '%Y')
419e2b8114dSGreg Roach            );
420e2b8114dSGreg Roach        }
421e2b8114dSGreg Roach
422e2b8114dSGreg Roach        return I18N::plural('%s individual', '%s individuals', $count, I18N::number($count));
423e2b8114dSGreg Roach    }
42471378461SGreg Roach
42571378461SGreg Roach    /**
42671378461SGreg Roach     * @param Individual[] $individuals
42771378461SGreg Roach     *
42871378461SGreg Roach     * @return stdClass[]
42971378461SGreg Roach     */
43071378461SGreg Roach    private function layoutIndividuals(array $individuals): array
43171378461SGreg Roach    {
43271378461SGreg Roach        $colors = [
43371378461SGreg Roach            'M' => new ColorGenerator(240, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE * -1),
43471378461SGreg Roach            'F' => new ColorGenerator(000, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE),
43571378461SGreg Roach            'U' => new ColorGenerator(120, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE),
43671378461SGreg Roach        ];
43771378461SGreg Roach
43871378461SGreg Roach        $current_year = (int) date('Y');
43971378461SGreg Roach
44071378461SGreg Roach        // Latest year used in each row
44171378461SGreg Roach        $rows = [];
44271378461SGreg Roach
44371378461SGreg Roach        $lifespans = [];
44471378461SGreg Roach
44571378461SGreg Roach        foreach ($individuals as $individual) {
44671378461SGreg Roach            $birth_jd   = $individual->getEstimatedBirthDate()->minimumJulianDay();
44771378461SGreg Roach            $birth_year = $this->jdToYear($birth_jd);
44871378461SGreg Roach            $death_jd   = $individual->getEstimatedDeathDate()->maximumJulianDay();
44971378461SGreg Roach            $death_year = $this->jdToYear($death_jd);
45071378461SGreg Roach
45171378461SGreg Roach            // Died before they were born?  Swapping the dates allows them to be shown.
45271378461SGreg Roach            if ($death_year < $birth_year) {
45371378461SGreg Roach                $death_year = $birth_year;
45471378461SGreg Roach            }
45571378461SGreg Roach
45671378461SGreg Roach            // Don't show death dates in the future.
45771378461SGreg Roach            $death_year = min($death_year, $current_year);
45871378461SGreg Roach
45971378461SGreg Roach            // Add this individual to the next row in the chart...
46071378461SGreg Roach            $next_row = count($rows);
46171378461SGreg Roach            // ...unless we can find an existing row where it fits.
46271378461SGreg Roach            foreach ($rows as $row => $year) {
46371378461SGreg Roach                if ($year < $birth_year) {
46471378461SGreg Roach                    $next_row = $row;
46571378461SGreg Roach                    break;
46671378461SGreg Roach                }
46771378461SGreg Roach            }
46871378461SGreg Roach
46971378461SGreg Roach            // Fill the row up to the year (leaving a small gap)
47071378461SGreg Roach            $rows[$next_row] = $death_year;
47171378461SGreg Roach
47271378461SGreg Roach            $lifespans[] = (object) [
47371378461SGreg Roach                'background' => $colors[$individual->sex()]->getNextColor(),
47471378461SGreg Roach                'birth_year' => $birth_year,
47571378461SGreg Roach                'death_year' => $death_year,
47671378461SGreg Roach                'id'         => 'individual-' . md5($individual->xref()),
47771378461SGreg Roach                'individual' => $individual,
47871378461SGreg Roach                'row'        => $next_row,
47971378461SGreg Roach            ];
48071378461SGreg Roach        }
48171378461SGreg Roach
48271378461SGreg Roach        return $lifespans;
48371378461SGreg Roach    }
484168ff6f3Sric2016}
485