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; 31*ac701fbdSGreg 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; 38e2b8114dSGreg Roachuse stdClass; 39168ff6f3Sric2016 409e18e23bSGreg Roachuse function app; 417c2c99faSGreg Roachuse function array_filter; 427c2c99faSGreg Roachuse function array_intersect; 437c2c99faSGreg Roachuse function array_map; 447c2c99faSGreg Roachuse function array_merge; 457c2c99faSGreg Roachuse function array_reduce; 467c2c99faSGreg Roachuse function array_unique; 479e18e23bSGreg Roachuse function assert; 487c2c99faSGreg Roachuse function count; 497c2c99faSGreg Roachuse function date; 50da616f3dSGreg Roachuse function explode; 51da616f3dSGreg Roachuse function implode; 527c2c99faSGreg Roachuse function intdiv; 537c2c99faSGreg Roachuse function is_array; 547c2c99faSGreg Roachuse function max; 557c2c99faSGreg Roachuse function md5; 567c2c99faSGreg Roachuse function min; 577c2c99faSGreg Roachuse function redirect; 587c2c99faSGreg Roachuse function response; 597c2c99faSGreg Roachuse function route; 607c2c99faSGreg Roachuse function usort; 617c2c99faSGreg Roachuse function view; 627c2c99faSGreg Roach 637c2c99faSGreg Roachuse const PHP_INT_MAX; 649e18e23bSGreg Roach 65168ff6f3Sric2016/** 66168ff6f3Sric2016 * Class LifespansChartModule 67168ff6f3Sric2016 */ 6871378461SGreg Roachclass LifespansChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 69c1010edaSGreg Roach{ 7049a243cbSGreg Roach use ModuleChartTrait; 7149a243cbSGreg Roach 7272f04adfSGreg Roach protected const ROUTE_URL = '/tree/{tree}/lifespans'; 7371378461SGreg Roach 74da616f3dSGreg Roach // In theory, only "@" is a safe separator, but it gives longer and uglier URLs. 75da616f3dSGreg Roach // Unless some other application generates XREFs with a ".", we are safe. 76da616f3dSGreg Roach protected const SEPARATOR = '.'; 77da616f3dSGreg Roach 7871378461SGreg Roach // Defaults 7971378461SGreg Roach protected const DEFAULT_PARAMETERS = []; 8071378461SGreg Roach 81e2b8114dSGreg Roach // Parameters for generating colors 82e2b8114dSGreg Roach protected const RANGE = 120; // degrees 83e2b8114dSGreg Roach protected const SATURATION = 100; // percent 84e2b8114dSGreg Roach protected const LIGHTNESS = 30; // percent 85e2b8114dSGreg Roach protected const ALPHA = 0.25; 86e2b8114dSGreg Roach 87168ff6f3Sric2016 /** 8871378461SGreg Roach * Initialization. 8971378461SGreg Roach * 909e18e23bSGreg Roach * @return void 9171378461SGreg Roach */ 929e18e23bSGreg Roach public function boot(): void 9371378461SGreg Roach { 949e18e23bSGreg Roach $router_container = app(RouterContainer::class); 959e18e23bSGreg Roach assert($router_container instanceof RouterContainer); 969e18e23bSGreg Roach 9771378461SGreg Roach $router_container->getMap() 9872f04adfSGreg Roach ->get(static::class, static::ROUTE_URL, $this) 9971378461SGreg Roach ->allows(RequestMethodInterface::METHOD_POST); 10071378461SGreg Roach } 10171378461SGreg Roach 10271378461SGreg Roach /** 1030cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 104168ff6f3Sric2016 * 105168ff6f3Sric2016 * @return string 106168ff6f3Sric2016 */ 10749a243cbSGreg Roach public function title(): string 108c1010edaSGreg Roach { 109bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 110bbb76c12SGreg Roach return I18N::translate('Lifespans'); 111168ff6f3Sric2016 } 112168ff6f3Sric2016 113168ff6f3Sric2016 /** 114168ff6f3Sric2016 * A sentence describing what this module does. 115168ff6f3Sric2016 * 116168ff6f3Sric2016 * @return string 117168ff6f3Sric2016 */ 11849a243cbSGreg Roach public function description(): string 119c1010edaSGreg Roach { 120bbb76c12SGreg Roach /* I18N: Description of the “LifespansChart” module */ 121bbb76c12SGreg Roach return I18N::translate('A chart of individuals’ lifespans.'); 122168ff6f3Sric2016 } 123168ff6f3Sric2016 124168ff6f3Sric2016 /** 125377a2979SGreg Roach * CSS class for the URL. 126377a2979SGreg Roach * 127377a2979SGreg Roach * @return string 128377a2979SGreg Roach */ 129377a2979SGreg Roach public function chartMenuClass(): string 130377a2979SGreg Roach { 131377a2979SGreg Roach return 'menu-chart-lifespan'; 132377a2979SGreg Roach } 133377a2979SGreg Roach 134377a2979SGreg Roach /** 135e6562982SGreg Roach * The URL for this chart. 136168ff6f3Sric2016 * 13760bc3e3fSGreg Roach * @param Individual $individual 13859597b37SGreg Roach * @param mixed[] $parameters 13960bc3e3fSGreg Roach * 140e6562982SGreg Roach * @return string 141168ff6f3Sric2016 */ 142e6562982SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 143c1010edaSGreg Roach { 14472f04adfSGreg Roach return route(static::class, [ 14571378461SGreg Roach 'tree' => $individual->tree()->name(), 146da616f3dSGreg Roach 'xrefs' => $individual->xref(), 14771378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 148168ff6f3Sric2016 } 149e2b8114dSGreg Roach 150e2b8114dSGreg Roach /** 1516ccdf4f0SGreg Roach * @param ServerRequestInterface $request 152e2b8114dSGreg Roach * 1536ccdf4f0SGreg Roach * @return ResponseInterface 154e2b8114dSGreg Roach */ 15571378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 156e2b8114dSGreg Roach { 15757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 1584ea62551SGreg Roach assert($tree instanceof Tree); 1594ea62551SGreg Roach 16057ab2231SGreg Roach $user = $request->getAttribute('user'); 16171378461SGreg Roach $xrefs = $request->getQueryParams()['xrefs'] ?? []; 1620b93976aSGreg Roach $ajax = $request->getQueryParams()['ajax'] ?? ''; 163b46c87bdSGreg Roach 164da616f3dSGreg Roach // URLs created by older versions may already contain an array. 165da616f3dSGreg Roach if (!is_array($xrefs)) { 166da616f3dSGreg Roach $xrefs = explode(self::SEPARATOR, $xrefs); 167da616f3dSGreg Roach } 168da616f3dSGreg Roach 169b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 170b46c87bdSGreg Roach 171b46c87bdSGreg Roach $addxref = $params['addxref'] ?? ''; 172b46c87bdSGreg Roach $addfam = (bool) ($params['addfam'] ?? false); 173a60f9d1cSGreg Roach $place_id = (int) ($params['place_id'] ?? 0); 174b46c87bdSGreg Roach $start = $params['start'] ?? ''; 175b46c87bdSGreg Roach $end = $params['end'] ?? ''; 176e2b8114dSGreg Roach 177a60f9d1cSGreg Roach $place = Place::find($place_id, $tree); 178e2b8114dSGreg Roach $start_date = new Date($start); 179e2b8114dSGreg Roach $end_date = new Date($end); 180e2b8114dSGreg Roach 181e2b8114dSGreg Roach $xrefs = array_unique($xrefs); 182e2b8114dSGreg Roach 183e2b8114dSGreg Roach // Add an individual, and family members 1846b9cb339SGreg Roach $individual = Registry::individualFactory()->make($addxref, $tree); 185e2b8114dSGreg Roach if ($individual !== null) { 186e2b8114dSGreg Roach $xrefs[] = $addxref; 187e2b8114dSGreg Roach if ($addfam) { 188e2b8114dSGreg Roach $xrefs = array_merge($xrefs, $this->closeFamily($individual)); 189e2b8114dSGreg Roach } 190e2b8114dSGreg Roach } 191e2b8114dSGreg Roach 192e2b8114dSGreg Roach // Select by date and/or place. 193a60f9d1cSGreg Roach if ($place_id !== 0 && $start_date->isOK() && $end_date->isOK()) { 194e2b8114dSGreg Roach $date_xrefs = $this->findIndividualsByDate($start_date, $end_date, $tree); 195e2b8114dSGreg Roach $place_xrefs = $this->findIndividualsByPlace($place, $tree); 196e2b8114dSGreg Roach $xrefs = array_intersect($date_xrefs, $place_xrefs); 197e2b8114dSGreg Roach } elseif ($start_date->isOK() && $end_date->isOK()) { 198e2b8114dSGreg Roach $xrefs = $this->findIndividualsByDate($start_date, $end_date, $tree); 199a60f9d1cSGreg Roach } elseif ($place_id !== 0) { 200e2b8114dSGreg Roach $xrefs = $this->findIndividualsByPlace($place, $tree); 201e2b8114dSGreg Roach } 202e2b8114dSGreg Roach 203e2b8114dSGreg Roach // Filter duplicates and private individuals. 204e2b8114dSGreg Roach $xrefs = array_unique($xrefs); 2050b5fd0a6SGreg Roach $xrefs = array_filter($xrefs, static function (string $xref) use ($tree): bool { 2066b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 207e2b8114dSGreg Roach 208e2b8114dSGreg Roach return $individual !== null && $individual->canShow(); 209e2b8114dSGreg Roach }); 210e2b8114dSGreg Roach 21171378461SGreg Roach // Convert POST requests into GET requests for pretty URLs. 21271378461SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 21372f04adfSGreg Roach return redirect(route(static::class, [ 2144ea62551SGreg Roach 'tree' => $tree->name(), 215da616f3dSGreg Roach 'xrefs' => implode(self::SEPARATOR, $xrefs), 21671378461SGreg Roach ])); 21771378461SGreg Roach } 21871378461SGreg Roach 219ef483801SGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 22071378461SGreg Roach 2210b93976aSGreg Roach if ($ajax === '1') { 22271378461SGreg Roach $this->layout = 'layouts/ajax'; 22371378461SGreg Roach 224a60f9d1cSGreg Roach return $this->chart($tree, $xrefs); 225e2b8114dSGreg Roach } 226e2b8114dSGreg Roach 22772f04adfSGreg Roach $reset_url = route(static::class, ['tree' => $tree->name()]); 228e2b8114dSGreg Roach 22972f04adfSGreg Roach $ajax_url = route(static::class, [ 23071378461SGreg Roach 'ajax' => true, 23171378461SGreg Roach 'tree' => $tree->name(), 232da616f3dSGreg Roach 'xrefs' => implode(self::SEPARATOR, $xrefs), 233e2b8114dSGreg Roach ]); 234e2b8114dSGreg Roach 2359b5537c3SGreg Roach return $this->viewResponse('modules/lifespans-chart/page', [ 236e2b8114dSGreg Roach 'ajax_url' => $ajax_url, 23771378461SGreg Roach 'module' => $this->name(), 238e2b8114dSGreg Roach 'reset_url' => $reset_url, 239e2b8114dSGreg Roach 'title' => $this->title(), 240ef5d23f1SGreg Roach 'tree' => $tree, 241e2b8114dSGreg Roach 'xrefs' => $xrefs, 242e2b8114dSGreg Roach ]); 243e2b8114dSGreg Roach } 244e2b8114dSGreg Roach 245e2b8114dSGreg Roach /** 246e2b8114dSGreg Roach * @param Tree $tree 247*ac701fbdSGreg Roach * @param array<string> $xrefs 248e2b8114dSGreg Roach * 2496ccdf4f0SGreg Roach * @return ResponseInterface 250e2b8114dSGreg Roach */ 251a60f9d1cSGreg Roach protected function chart(Tree $tree, array $xrefs): ResponseInterface 252e2b8114dSGreg Roach { 253e2b8114dSGreg Roach /** @var Individual[] $individuals */ 2540b93976aSGreg Roach $individuals = array_map(static function (string $xref) use ($tree): ?Individual { 2556b9cb339SGreg Roach return Registry::individualFactory()->make($xref, $tree); 256e2b8114dSGreg Roach }, $xrefs); 257e2b8114dSGreg Roach 2580b93976aSGreg Roach $individuals = array_filter($individuals, static function (?Individual $individual): bool { 2590b93976aSGreg Roach return $individual instanceof Individual && $individual->canShow(); 260e2b8114dSGreg Roach }); 261e2b8114dSGreg Roach 262e2b8114dSGreg Roach // Sort the array in order of birth year 2630c1a5edcSGreg Roach usort($individuals, Individual::birthDateComparator()); 264e2b8114dSGreg Roach 265e2b8114dSGreg Roach // Round to whole decades 2667c2c99faSGreg Roach $start_year = intdiv($this->minYear($individuals), 10) * 10; 267e4e7c547SGreg Roach $end_year = intdiv($this->maxYear($individuals) + 9, 10) * 10; 268e2b8114dSGreg Roach 269e2b8114dSGreg Roach $lifespans = $this->layoutIndividuals($individuals); 270e2b8114dSGreg Roach 2710b5fd0a6SGreg Roach $max_rows = array_reduce($lifespans, static function ($carry, stdClass $item) { 272e2b8114dSGreg Roach return max($carry, $item->row); 273e2b8114dSGreg Roach }, 0); 274e2b8114dSGreg Roach 275a60f9d1cSGreg Roach $count = count($xrefs); 276a60f9d1cSGreg Roach $subtitle = I18N::plural('%s individual', '%s individuals', $count, I18N::number($count)); 277a60f9d1cSGreg Roach 278e2b8114dSGreg Roach $html = view('modules/lifespans-chart/chart', [ 279e2b8114dSGreg Roach 'dir' => I18N::direction(), 280e2b8114dSGreg Roach 'end_year' => $end_year, 281e2b8114dSGreg Roach 'lifespans' => $lifespans, 282e2b8114dSGreg Roach 'max_rows' => $max_rows, 283e2b8114dSGreg Roach 'start_year' => $start_year, 284e2b8114dSGreg Roach 'subtitle' => $subtitle, 285e2b8114dSGreg Roach ]); 286e2b8114dSGreg Roach 2876ccdf4f0SGreg Roach return response($html); 288e2b8114dSGreg Roach } 289e2b8114dSGreg Roach 290e2b8114dSGreg Roach /** 291e2b8114dSGreg Roach * Find the latest event year for individuals 292e2b8114dSGreg Roach * 293*ac701fbdSGreg Roach * @param array<Individual> $individuals 294e2b8114dSGreg Roach * 295e2b8114dSGreg Roach * @return int 296e2b8114dSGreg Roach */ 297e2b8114dSGreg Roach protected function maxYear(array $individuals): int 298e2b8114dSGreg Roach { 2990b5fd0a6SGreg Roach $jd = array_reduce($individuals, static function ($carry, Individual $item) { 300e2b8114dSGreg Roach return max($carry, $item->getEstimatedDeathDate()->maximumJulianDay()); 301e2b8114dSGreg Roach }, 0); 302e2b8114dSGreg Roach 303e2b8114dSGreg Roach $year = $this->jdToYear($jd); 304e2b8114dSGreg Roach 305e2b8114dSGreg Roach // Don't show future dates 306e2b8114dSGreg Roach return min($year, (int) date('Y')); 307e2b8114dSGreg Roach } 308e2b8114dSGreg Roach 309e2b8114dSGreg Roach /** 310e2b8114dSGreg Roach * Find the earliest event year for individuals 311e2b8114dSGreg Roach * 312*ac701fbdSGreg Roach * @param array<Individual> $individuals 313e2b8114dSGreg Roach * 314e2b8114dSGreg Roach * @return int 315e2b8114dSGreg Roach */ 316e2b8114dSGreg Roach protected function minYear(array $individuals): int 317e2b8114dSGreg Roach { 3180b5fd0a6SGreg Roach $jd = array_reduce($individuals, static function ($carry, Individual $item) { 319e2b8114dSGreg Roach return min($carry, $item->getEstimatedBirthDate()->minimumJulianDay()); 320e2b8114dSGreg Roach }, PHP_INT_MAX); 321e2b8114dSGreg Roach 322e2b8114dSGreg Roach return $this->jdToYear($jd); 323e2b8114dSGreg Roach } 324e2b8114dSGreg Roach 325e2b8114dSGreg Roach /** 326e2b8114dSGreg Roach * Convert a julian day to a gregorian year 327e2b8114dSGreg Roach * 328e2b8114dSGreg Roach * @param int $jd 329e2b8114dSGreg Roach * 330e2b8114dSGreg Roach * @return int 331e2b8114dSGreg Roach */ 332e2b8114dSGreg Roach protected function jdToYear(int $jd): int 333e2b8114dSGreg Roach { 334e2b8114dSGreg Roach if ($jd === 0) { 335e2b8114dSGreg Roach return 0; 336e2b8114dSGreg Roach } 337e2b8114dSGreg Roach 338e2b8114dSGreg Roach $gregorian = new GregorianCalendar(); 339e2b8114dSGreg Roach [$y] = $gregorian->jdToYmd($jd); 340e2b8114dSGreg Roach 341e2b8114dSGreg Roach return $y; 342e2b8114dSGreg Roach } 343e2b8114dSGreg Roach 344e2b8114dSGreg Roach /** 345e2b8114dSGreg Roach * @param Date $start 346e2b8114dSGreg Roach * @param Date $end 347e2b8114dSGreg Roach * @param Tree $tree 348e2b8114dSGreg Roach * 34924f2a3afSGreg Roach * @return array<string> 350e2b8114dSGreg Roach */ 351e2b8114dSGreg Roach protected function findIndividualsByDate(Date $start, Date $end, Tree $tree): array 352e2b8114dSGreg Roach { 353e2b8114dSGreg Roach return DB::table('individuals') 3540b5fd0a6SGreg Roach ->join('dates', static function (JoinClause $join): void { 355e2b8114dSGreg Roach $join 356e2b8114dSGreg Roach ->on('d_file', '=', 'i_file') 357e2b8114dSGreg Roach ->on('d_gid', '=', 'i_id'); 358e2b8114dSGreg Roach }) 359e2b8114dSGreg Roach ->where('i_file', '=', $tree->id()) 360e2b8114dSGreg Roach ->where('d_julianday1', '<=', $end->maximumJulianDay()) 361e2b8114dSGreg Roach ->where('d_julianday2', '>=', $start->minimumJulianDay()) 362e2b8114dSGreg Roach ->whereNotIn('d_fact', ['BAPL', 'ENDL', 'SLGC', 'SLGS', '_TODO', 'CHAN']) 363e2b8114dSGreg Roach ->pluck('i_id') 364e2b8114dSGreg Roach ->all(); 365e2b8114dSGreg Roach } 366e2b8114dSGreg Roach 367e2b8114dSGreg Roach /** 368e2b8114dSGreg Roach * @param Place $place 369e2b8114dSGreg Roach * @param Tree $tree 370e2b8114dSGreg Roach * 37124f2a3afSGreg Roach * @return array<string> 372e2b8114dSGreg Roach */ 373e2b8114dSGreg Roach protected function findIndividualsByPlace(Place $place, Tree $tree): array 374e2b8114dSGreg Roach { 375e2b8114dSGreg Roach return DB::table('individuals') 3760b5fd0a6SGreg Roach ->join('placelinks', static function (JoinClause $join): void { 377e2b8114dSGreg Roach $join 378e2b8114dSGreg Roach ->on('pl_file', '=', 'i_file') 379e2b8114dSGreg Roach ->on('pl_gid', '=', 'i_id'); 380e2b8114dSGreg Roach }) 381e2b8114dSGreg Roach ->where('i_file', '=', $tree->id()) 382392561bbSGreg Roach ->where('pl_p_id', '=', $place->id()) 383e2b8114dSGreg Roach ->pluck('i_id') 384e2b8114dSGreg Roach ->all(); 385e2b8114dSGreg Roach } 386e2b8114dSGreg Roach 387e2b8114dSGreg Roach /** 388e2b8114dSGreg Roach * Find the close family members of an individual. 389e2b8114dSGreg Roach * 390e2b8114dSGreg Roach * @param Individual $individual 391e2b8114dSGreg Roach * 39224f2a3afSGreg Roach * @return array<string> 393e2b8114dSGreg Roach */ 394e2b8114dSGreg Roach protected function closeFamily(Individual $individual): array 395e2b8114dSGreg Roach { 396e2b8114dSGreg Roach $xrefs = []; 397e2b8114dSGreg Roach 39839ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 39939ca88baSGreg Roach foreach ($family->children() as $child) { 400e2b8114dSGreg Roach $xrefs[] = $child->xref(); 401e2b8114dSGreg Roach } 402e2b8114dSGreg Roach 40339ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 404e2b8114dSGreg Roach $xrefs[] = $spouse->xref(); 405e2b8114dSGreg Roach } 406e2b8114dSGreg Roach } 407e2b8114dSGreg Roach 40839ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 40939ca88baSGreg Roach foreach ($family->children() as $child) { 410e2b8114dSGreg Roach $xrefs[] = $child->xref(); 411e2b8114dSGreg Roach } 412e2b8114dSGreg Roach 41339ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 414e2b8114dSGreg Roach $xrefs[] = $spouse->xref(); 415e2b8114dSGreg Roach } 416e2b8114dSGreg Roach } 417e2b8114dSGreg Roach 418e2b8114dSGreg Roach return $xrefs; 419e2b8114dSGreg Roach } 420e2b8114dSGreg Roach 421e2b8114dSGreg Roach /** 42271378461SGreg Roach * @param Individual[] $individuals 42371378461SGreg Roach * 424870ec5a3SGreg Roach * @return array<stdClass> 42571378461SGreg Roach */ 42671378461SGreg Roach private function layoutIndividuals(array $individuals): array 42771378461SGreg Roach { 42871378461SGreg Roach $colors = [ 42971378461SGreg Roach 'M' => new ColorGenerator(240, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE * -1), 43071378461SGreg Roach 'F' => new ColorGenerator(000, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE), 43171378461SGreg Roach 'U' => new ColorGenerator(120, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE), 43271378461SGreg Roach ]; 43371378461SGreg Roach 43471378461SGreg Roach $current_year = (int) date('Y'); 43571378461SGreg Roach 43671378461SGreg Roach // Latest year used in each row 43771378461SGreg Roach $rows = []; 43871378461SGreg Roach 43971378461SGreg Roach $lifespans = []; 44071378461SGreg Roach 44171378461SGreg Roach foreach ($individuals as $individual) { 44271378461SGreg Roach $birth_jd = $individual->getEstimatedBirthDate()->minimumJulianDay(); 44371378461SGreg Roach $birth_year = $this->jdToYear($birth_jd); 44471378461SGreg Roach $death_jd = $individual->getEstimatedDeathDate()->maximumJulianDay(); 44571378461SGreg Roach $death_year = $this->jdToYear($death_jd); 44671378461SGreg Roach 44771378461SGreg Roach // Died before they were born? Swapping the dates allows them to be shown. 44871378461SGreg Roach if ($death_year < $birth_year) { 44971378461SGreg Roach $death_year = $birth_year; 45071378461SGreg Roach } 45171378461SGreg Roach 45271378461SGreg Roach // Don't show death dates in the future. 45371378461SGreg Roach $death_year = min($death_year, $current_year); 45471378461SGreg Roach 45571378461SGreg Roach // Add this individual to the next row in the chart... 45671378461SGreg Roach $next_row = count($rows); 45771378461SGreg Roach // ...unless we can find an existing row where it fits. 45871378461SGreg Roach foreach ($rows as $row => $year) { 45971378461SGreg Roach if ($year < $birth_year) { 46071378461SGreg Roach $next_row = $row; 46171378461SGreg Roach break; 46271378461SGreg Roach } 46371378461SGreg Roach } 46471378461SGreg Roach 46571378461SGreg Roach // Fill the row up to the year (leaving a small gap) 46671378461SGreg Roach $rows[$next_row] = $death_year; 46771378461SGreg Roach 46871378461SGreg Roach $lifespans[] = (object) [ 46971378461SGreg Roach 'background' => $colors[$individual->sex()]->getNextColor(), 47071378461SGreg Roach 'birth_year' => $birth_year, 47171378461SGreg Roach 'death_year' => $death_year, 47271378461SGreg Roach 'id' => 'individual-' . md5($individual->xref()), 47371378461SGreg Roach 'individual' => $individual, 47471378461SGreg Roach 'row' => $next_row, 47571378461SGreg Roach ]; 47671378461SGreg Roach } 47771378461SGreg Roach 47871378461SGreg Roach return $lifespans; 47971378461SGreg Roach } 480168ff6f3Sric2016} 481