1168ff6f3Sric2016<?php 23976b470SGreg Roach 3168ff6f3Sric2016/** 4168ff6f3Sric2016 * webtrees: online genealogy 5a091ac74SGreg Roach * Copyright (C) 2020 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; 28a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory; 29168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 30168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 31e2b8114dSGreg Roachuse Fisharebest\Webtrees\Place; 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; 419e18e23bSGreg Roachuse function assert; 42*da616f3dSGreg Roachuse function explode; 43*da616f3dSGreg Roachuse function implode; 449e18e23bSGreg Roach 45168ff6f3Sric2016/** 46168ff6f3Sric2016 * Class LifespansChartModule 47168ff6f3Sric2016 */ 4871378461SGreg Roachclass LifespansChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 49c1010edaSGreg Roach{ 5049a243cbSGreg Roach use ModuleChartTrait; 5149a243cbSGreg Roach 5272f04adfSGreg Roach protected const ROUTE_URL = '/tree/{tree}/lifespans'; 5371378461SGreg Roach 54*da616f3dSGreg Roach // In theory, only "@" is a safe separator, but it gives longer and uglier URLs. 55*da616f3dSGreg Roach // Unless some other application generates XREFs with a ".", we are safe. 56*da616f3dSGreg Roach protected const SEPARATOR = '.'; 57*da616f3dSGreg Roach 5871378461SGreg Roach // Defaults 5971378461SGreg Roach protected const DEFAULT_PARAMETERS = []; 6071378461SGreg Roach 61e2b8114dSGreg Roach // Parameters for generating colors 62e2b8114dSGreg Roach protected const RANGE = 120; // degrees 63e2b8114dSGreg Roach protected const SATURATION = 100; // percent 64e2b8114dSGreg Roach protected const LIGHTNESS = 30; // percent 65e2b8114dSGreg Roach protected const ALPHA = 0.25; 66e2b8114dSGreg Roach 67168ff6f3Sric2016 /** 6871378461SGreg Roach * Initialization. 6971378461SGreg Roach * 709e18e23bSGreg Roach * @return void 7171378461SGreg Roach */ 729e18e23bSGreg Roach public function boot(): void 7371378461SGreg Roach { 749e18e23bSGreg Roach $router_container = app(RouterContainer::class); 759e18e23bSGreg Roach assert($router_container instanceof RouterContainer); 769e18e23bSGreg Roach 7771378461SGreg Roach $router_container->getMap() 7872f04adfSGreg Roach ->get(static::class, static::ROUTE_URL, $this) 7971378461SGreg Roach ->allows(RequestMethodInterface::METHOD_POST); 8071378461SGreg Roach } 8171378461SGreg Roach 8271378461SGreg Roach /** 830cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 84168ff6f3Sric2016 * 85168ff6f3Sric2016 * @return string 86168ff6f3Sric2016 */ 8749a243cbSGreg Roach public function title(): string 88c1010edaSGreg Roach { 89bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 90bbb76c12SGreg Roach return I18N::translate('Lifespans'); 91168ff6f3Sric2016 } 92168ff6f3Sric2016 93168ff6f3Sric2016 /** 94168ff6f3Sric2016 * A sentence describing what this module does. 95168ff6f3Sric2016 * 96168ff6f3Sric2016 * @return string 97168ff6f3Sric2016 */ 9849a243cbSGreg Roach public function description(): string 99c1010edaSGreg Roach { 100bbb76c12SGreg Roach /* I18N: Description of the “LifespansChart” module */ 101bbb76c12SGreg Roach return I18N::translate('A chart of individuals’ lifespans.'); 102168ff6f3Sric2016 } 103168ff6f3Sric2016 104168ff6f3Sric2016 /** 105377a2979SGreg Roach * CSS class for the URL. 106377a2979SGreg Roach * 107377a2979SGreg Roach * @return string 108377a2979SGreg Roach */ 109377a2979SGreg Roach public function chartMenuClass(): string 110377a2979SGreg Roach { 111377a2979SGreg Roach return 'menu-chart-lifespan'; 112377a2979SGreg Roach } 113377a2979SGreg Roach 114377a2979SGreg Roach /** 115e6562982SGreg Roach * The URL for this chart. 116168ff6f3Sric2016 * 11760bc3e3fSGreg Roach * @param Individual $individual 11859597b37SGreg Roach * @param mixed[] $parameters 11960bc3e3fSGreg Roach * 120e6562982SGreg Roach * @return string 121168ff6f3Sric2016 */ 122e6562982SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 123c1010edaSGreg Roach { 12472f04adfSGreg Roach return route(static::class, [ 12571378461SGreg Roach 'tree' => $individual->tree()->name(), 126*da616f3dSGreg Roach 'xrefs' => $individual->xref(), 12771378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 128168ff6f3Sric2016 } 129e2b8114dSGreg Roach 130e2b8114dSGreg Roach /** 1316ccdf4f0SGreg Roach * @param ServerRequestInterface $request 132e2b8114dSGreg Roach * 1336ccdf4f0SGreg Roach * @return ResponseInterface 134e2b8114dSGreg Roach */ 13571378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 136e2b8114dSGreg Roach { 13757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 1384ea62551SGreg Roach assert($tree instanceof Tree); 1394ea62551SGreg Roach 14057ab2231SGreg Roach $user = $request->getAttribute('user'); 14171378461SGreg Roach $xrefs = $request->getQueryParams()['xrefs'] ?? []; 1420b93976aSGreg Roach $ajax = $request->getQueryParams()['ajax'] ?? ''; 143b46c87bdSGreg Roach 144*da616f3dSGreg Roach // URLs created by older versions may already contain an array. 145*da616f3dSGreg Roach if (!is_array($xrefs)) { 146*da616f3dSGreg Roach $xrefs = explode(self::SEPARATOR, $xrefs); 147*da616f3dSGreg Roach } 148*da616f3dSGreg Roach 149b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 150b46c87bdSGreg Roach 151b46c87bdSGreg Roach $addxref = $params['addxref'] ?? ''; 152b46c87bdSGreg Roach $addfam = (bool) ($params['addfam'] ?? false); 153a60f9d1cSGreg Roach $place_id = (int) ($params['place_id'] ?? 0); 154b46c87bdSGreg Roach $start = $params['start'] ?? ''; 155b46c87bdSGreg Roach $end = $params['end'] ?? ''; 156e2b8114dSGreg Roach 157a60f9d1cSGreg Roach $place = Place::find($place_id, $tree); 158e2b8114dSGreg Roach $start_date = new Date($start); 159e2b8114dSGreg Roach $end_date = new Date($end); 160e2b8114dSGreg Roach 161e2b8114dSGreg Roach $xrefs = array_unique($xrefs); 162e2b8114dSGreg Roach 163e2b8114dSGreg Roach // Add an individual, and family members 164a091ac74SGreg Roach $individual = Factory::individual()->make($addxref, $tree); 165e2b8114dSGreg Roach if ($individual !== null) { 166e2b8114dSGreg Roach $xrefs[] = $addxref; 167e2b8114dSGreg Roach if ($addfam) { 168e2b8114dSGreg Roach $xrefs = array_merge($xrefs, $this->closeFamily($individual)); 169e2b8114dSGreg Roach } 170e2b8114dSGreg Roach } 171e2b8114dSGreg Roach 172e2b8114dSGreg Roach // Select by date and/or place. 173a60f9d1cSGreg Roach if ($place_id !== 0 && $start_date->isOK() && $end_date->isOK()) { 174e2b8114dSGreg Roach $date_xrefs = $this->findIndividualsByDate($start_date, $end_date, $tree); 175e2b8114dSGreg Roach $place_xrefs = $this->findIndividualsByPlace($place, $tree); 176e2b8114dSGreg Roach $xrefs = array_intersect($date_xrefs, $place_xrefs); 177e2b8114dSGreg Roach } elseif ($start_date->isOK() && $end_date->isOK()) { 178e2b8114dSGreg Roach $xrefs = $this->findIndividualsByDate($start_date, $end_date, $tree); 179a60f9d1cSGreg Roach } elseif ($place_id !== 0) { 180e2b8114dSGreg Roach $xrefs = $this->findIndividualsByPlace($place, $tree); 181e2b8114dSGreg Roach } 182e2b8114dSGreg Roach 183e2b8114dSGreg Roach // Filter duplicates and private individuals. 184e2b8114dSGreg Roach $xrefs = array_unique($xrefs); 1850b5fd0a6SGreg Roach $xrefs = array_filter($xrefs, static function (string $xref) use ($tree): bool { 186a091ac74SGreg Roach $individual = Factory::individual()->make($xref, $tree); 187e2b8114dSGreg Roach 188e2b8114dSGreg Roach return $individual !== null && $individual->canShow(); 189e2b8114dSGreg Roach }); 190e2b8114dSGreg Roach 19171378461SGreg Roach // Convert POST requests into GET requests for pretty URLs. 19271378461SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 19372f04adfSGreg Roach return redirect(route(static::class, [ 1944ea62551SGreg Roach 'tree' => $tree->name(), 195*da616f3dSGreg Roach 'xrefs' => implode(self::SEPARATOR, $xrefs), 19671378461SGreg Roach ])); 19771378461SGreg Roach } 19871378461SGreg Roach 199ef483801SGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 20071378461SGreg Roach 2010b93976aSGreg Roach if ($ajax === '1') { 20271378461SGreg Roach $this->layout = 'layouts/ajax'; 20371378461SGreg Roach 204a60f9d1cSGreg Roach return $this->chart($tree, $xrefs); 205e2b8114dSGreg Roach } 206e2b8114dSGreg Roach 20772f04adfSGreg Roach $reset_url = route(static::class, ['tree' => $tree->name()]); 208e2b8114dSGreg Roach 20972f04adfSGreg Roach $ajax_url = route(static::class, [ 21071378461SGreg Roach 'ajax' => true, 21171378461SGreg Roach 'tree' => $tree->name(), 212*da616f3dSGreg Roach 'xrefs' => implode(self::SEPARATOR, $xrefs), 213e2b8114dSGreg Roach ]); 214e2b8114dSGreg Roach 2159b5537c3SGreg Roach return $this->viewResponse('modules/lifespans-chart/page', [ 216e2b8114dSGreg Roach 'ajax_url' => $ajax_url, 21771378461SGreg Roach 'module' => $this->name(), 218e2b8114dSGreg Roach 'reset_url' => $reset_url, 219e2b8114dSGreg Roach 'title' => $this->title(), 220ef5d23f1SGreg Roach 'tree' => $tree, 221e2b8114dSGreg Roach 'xrefs' => $xrefs, 222e2b8114dSGreg Roach ]); 223e2b8114dSGreg Roach } 224e2b8114dSGreg Roach 225e2b8114dSGreg Roach /** 226e2b8114dSGreg Roach * @param Tree $tree 227e2b8114dSGreg Roach * @param array $xrefs 228e2b8114dSGreg Roach * 2296ccdf4f0SGreg Roach * @return ResponseInterface 230e2b8114dSGreg Roach */ 231a60f9d1cSGreg Roach protected function chart(Tree $tree, array $xrefs): ResponseInterface 232e2b8114dSGreg Roach { 233e2b8114dSGreg Roach /** @var Individual[] $individuals */ 2340b93976aSGreg Roach $individuals = array_map(static function (string $xref) use ($tree): ?Individual { 235a091ac74SGreg Roach return Factory::individual()->make($xref, $tree); 236e2b8114dSGreg Roach }, $xrefs); 237e2b8114dSGreg Roach 2380b93976aSGreg Roach $individuals = array_filter($individuals, static function (?Individual $individual): bool { 2390b93976aSGreg Roach return $individual instanceof Individual && $individual->canShow(); 240e2b8114dSGreg Roach }); 241e2b8114dSGreg Roach 242e2b8114dSGreg Roach // Sort the array in order of birth year 2430c1a5edcSGreg Roach usort($individuals, Individual::birthDateComparator()); 244e2b8114dSGreg Roach 245e2b8114dSGreg Roach // Round to whole decades 246e2b8114dSGreg Roach $start_year = (int) floor($this->minYear($individuals) / 10) * 10; 247e2b8114dSGreg Roach $end_year = (int) ceil($this->maxYear($individuals) / 10) * 10; 248e2b8114dSGreg Roach 249e2b8114dSGreg Roach $lifespans = $this->layoutIndividuals($individuals); 250e2b8114dSGreg Roach 2510b5fd0a6SGreg Roach $max_rows = array_reduce($lifespans, static function ($carry, stdClass $item) { 252e2b8114dSGreg Roach return max($carry, $item->row); 253e2b8114dSGreg Roach }, 0); 254e2b8114dSGreg Roach 255a60f9d1cSGreg Roach $count = count($xrefs); 256a60f9d1cSGreg Roach $subtitle = I18N::plural('%s individual', '%s individuals', $count, I18N::number($count)); 257a60f9d1cSGreg Roach 258e2b8114dSGreg Roach $html = view('modules/lifespans-chart/chart', [ 259e2b8114dSGreg Roach 'dir' => I18N::direction(), 260e2b8114dSGreg Roach 'end_year' => $end_year, 261e2b8114dSGreg Roach 'lifespans' => $lifespans, 262e2b8114dSGreg Roach 'max_rows' => $max_rows, 263e2b8114dSGreg Roach 'start_year' => $start_year, 264e2b8114dSGreg Roach 'subtitle' => $subtitle, 265e2b8114dSGreg Roach ]); 266e2b8114dSGreg Roach 2676ccdf4f0SGreg Roach return response($html); 268e2b8114dSGreg Roach } 269e2b8114dSGreg Roach 270e2b8114dSGreg Roach /** 271e2b8114dSGreg Roach * Find the latest event year for individuals 272e2b8114dSGreg Roach * 273e2b8114dSGreg Roach * @param array $individuals 274e2b8114dSGreg Roach * 275e2b8114dSGreg Roach * @return int 276e2b8114dSGreg Roach */ 277e2b8114dSGreg Roach protected function maxYear(array $individuals): int 278e2b8114dSGreg Roach { 2790b5fd0a6SGreg Roach $jd = array_reduce($individuals, static function ($carry, Individual $item) { 280e2b8114dSGreg Roach return max($carry, $item->getEstimatedDeathDate()->maximumJulianDay()); 281e2b8114dSGreg Roach }, 0); 282e2b8114dSGreg Roach 283e2b8114dSGreg Roach $year = $this->jdToYear($jd); 284e2b8114dSGreg Roach 285e2b8114dSGreg Roach // Don't show future dates 286e2b8114dSGreg Roach return min($year, (int) date('Y')); 287e2b8114dSGreg Roach } 288e2b8114dSGreg Roach 289e2b8114dSGreg Roach /** 290e2b8114dSGreg Roach * Find the earliest event year for individuals 291e2b8114dSGreg Roach * 292e2b8114dSGreg Roach * @param array $individuals 293e2b8114dSGreg Roach * 294e2b8114dSGreg Roach * @return int 295e2b8114dSGreg Roach */ 296e2b8114dSGreg Roach protected function minYear(array $individuals): int 297e2b8114dSGreg Roach { 2980b5fd0a6SGreg Roach $jd = array_reduce($individuals, static function ($carry, Individual $item) { 299e2b8114dSGreg Roach return min($carry, $item->getEstimatedBirthDate()->minimumJulianDay()); 300e2b8114dSGreg Roach }, PHP_INT_MAX); 301e2b8114dSGreg Roach 302e2b8114dSGreg Roach return $this->jdToYear($jd); 303e2b8114dSGreg Roach } 304e2b8114dSGreg Roach 305e2b8114dSGreg Roach /** 306e2b8114dSGreg Roach * Convert a julian day to a gregorian year 307e2b8114dSGreg Roach * 308e2b8114dSGreg Roach * @param int $jd 309e2b8114dSGreg Roach * 310e2b8114dSGreg Roach * @return int 311e2b8114dSGreg Roach */ 312e2b8114dSGreg Roach protected function jdToYear(int $jd): int 313e2b8114dSGreg Roach { 314e2b8114dSGreg Roach if ($jd === 0) { 315e2b8114dSGreg Roach return 0; 316e2b8114dSGreg Roach } 317e2b8114dSGreg Roach 318e2b8114dSGreg Roach $gregorian = new GregorianCalendar(); 319e2b8114dSGreg Roach [$y] = $gregorian->jdToYmd($jd); 320e2b8114dSGreg Roach 321e2b8114dSGreg Roach return $y; 322e2b8114dSGreg Roach } 323e2b8114dSGreg Roach 324e2b8114dSGreg Roach /** 325e2b8114dSGreg Roach * @param Date $start 326e2b8114dSGreg Roach * @param Date $end 327e2b8114dSGreg Roach * @param Tree $tree 328e2b8114dSGreg Roach * 329e2b8114dSGreg Roach * @return string[] 330e2b8114dSGreg Roach */ 331e2b8114dSGreg Roach protected function findIndividualsByDate(Date $start, Date $end, Tree $tree): array 332e2b8114dSGreg Roach { 333e2b8114dSGreg Roach return DB::table('individuals') 3340b5fd0a6SGreg Roach ->join('dates', static function (JoinClause $join): void { 335e2b8114dSGreg Roach $join 336e2b8114dSGreg Roach ->on('d_file', '=', 'i_file') 337e2b8114dSGreg Roach ->on('d_gid', '=', 'i_id'); 338e2b8114dSGreg Roach }) 339e2b8114dSGreg Roach ->where('i_file', '=', $tree->id()) 340e2b8114dSGreg Roach ->where('d_julianday1', '<=', $end->maximumJulianDay()) 341e2b8114dSGreg Roach ->where('d_julianday2', '>=', $start->minimumJulianDay()) 342e2b8114dSGreg Roach ->whereNotIn('d_fact', ['BAPL', 'ENDL', 'SLGC', 'SLGS', '_TODO', 'CHAN']) 343e2b8114dSGreg Roach ->pluck('i_id') 344e2b8114dSGreg Roach ->all(); 345e2b8114dSGreg Roach } 346e2b8114dSGreg Roach 347e2b8114dSGreg Roach /** 348e2b8114dSGreg Roach * @param Place $place 349e2b8114dSGreg Roach * @param Tree $tree 350e2b8114dSGreg Roach * 351e2b8114dSGreg Roach * @return string[] 352e2b8114dSGreg Roach */ 353e2b8114dSGreg Roach protected function findIndividualsByPlace(Place $place, Tree $tree): array 354e2b8114dSGreg Roach { 355e2b8114dSGreg Roach return DB::table('individuals') 3560b5fd0a6SGreg Roach ->join('placelinks', static function (JoinClause $join): void { 357e2b8114dSGreg Roach $join 358e2b8114dSGreg Roach ->on('pl_file', '=', 'i_file') 359e2b8114dSGreg Roach ->on('pl_gid', '=', 'i_id'); 360e2b8114dSGreg Roach }) 361e2b8114dSGreg Roach ->where('i_file', '=', $tree->id()) 362392561bbSGreg Roach ->where('pl_p_id', '=', $place->id()) 363e2b8114dSGreg Roach ->pluck('i_id') 364e2b8114dSGreg Roach ->all(); 365e2b8114dSGreg Roach } 366e2b8114dSGreg Roach 367e2b8114dSGreg Roach /** 368e2b8114dSGreg Roach * Find the close family members of an individual. 369e2b8114dSGreg Roach * 370e2b8114dSGreg Roach * @param Individual $individual 371e2b8114dSGreg Roach * 372e2b8114dSGreg Roach * @return string[] 373e2b8114dSGreg Roach */ 374e2b8114dSGreg Roach protected function closeFamily(Individual $individual): array 375e2b8114dSGreg Roach { 376e2b8114dSGreg Roach $xrefs = []; 377e2b8114dSGreg Roach 37839ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 37939ca88baSGreg Roach foreach ($family->children() as $child) { 380e2b8114dSGreg Roach $xrefs[] = $child->xref(); 381e2b8114dSGreg Roach } 382e2b8114dSGreg Roach 38339ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 384e2b8114dSGreg Roach $xrefs[] = $spouse->xref(); 385e2b8114dSGreg Roach } 386e2b8114dSGreg Roach } 387e2b8114dSGreg Roach 38839ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 38939ca88baSGreg Roach foreach ($family->children() as $child) { 390e2b8114dSGreg Roach $xrefs[] = $child->xref(); 391e2b8114dSGreg Roach } 392e2b8114dSGreg Roach 39339ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 394e2b8114dSGreg Roach $xrefs[] = $spouse->xref(); 395e2b8114dSGreg Roach } 396e2b8114dSGreg Roach } 397e2b8114dSGreg Roach 398e2b8114dSGreg Roach return $xrefs; 399e2b8114dSGreg Roach } 400e2b8114dSGreg Roach 401e2b8114dSGreg Roach /** 40271378461SGreg Roach * @param Individual[] $individuals 40371378461SGreg Roach * 40471378461SGreg Roach * @return stdClass[] 40571378461SGreg Roach */ 40671378461SGreg Roach private function layoutIndividuals(array $individuals): array 40771378461SGreg Roach { 40871378461SGreg Roach $colors = [ 40971378461SGreg Roach 'M' => new ColorGenerator(240, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE * -1), 41071378461SGreg Roach 'F' => new ColorGenerator(000, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE), 41171378461SGreg Roach 'U' => new ColorGenerator(120, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE), 41271378461SGreg Roach ]; 41371378461SGreg Roach 41471378461SGreg Roach $current_year = (int) date('Y'); 41571378461SGreg Roach 41671378461SGreg Roach // Latest year used in each row 41771378461SGreg Roach $rows = []; 41871378461SGreg Roach 41971378461SGreg Roach $lifespans = []; 42071378461SGreg Roach 42171378461SGreg Roach foreach ($individuals as $individual) { 42271378461SGreg Roach $birth_jd = $individual->getEstimatedBirthDate()->minimumJulianDay(); 42371378461SGreg Roach $birth_year = $this->jdToYear($birth_jd); 42471378461SGreg Roach $death_jd = $individual->getEstimatedDeathDate()->maximumJulianDay(); 42571378461SGreg Roach $death_year = $this->jdToYear($death_jd); 42671378461SGreg Roach 42771378461SGreg Roach // Died before they were born? Swapping the dates allows them to be shown. 42871378461SGreg Roach if ($death_year < $birth_year) { 42971378461SGreg Roach $death_year = $birth_year; 43071378461SGreg Roach } 43171378461SGreg Roach 43271378461SGreg Roach // Don't show death dates in the future. 43371378461SGreg Roach $death_year = min($death_year, $current_year); 43471378461SGreg Roach 43571378461SGreg Roach // Add this individual to the next row in the chart... 43671378461SGreg Roach $next_row = count($rows); 43771378461SGreg Roach // ...unless we can find an existing row where it fits. 43871378461SGreg Roach foreach ($rows as $row => $year) { 43971378461SGreg Roach if ($year < $birth_year) { 44071378461SGreg Roach $next_row = $row; 44171378461SGreg Roach break; 44271378461SGreg Roach } 44371378461SGreg Roach } 44471378461SGreg Roach 44571378461SGreg Roach // Fill the row up to the year (leaving a small gap) 44671378461SGreg Roach $rows[$next_row] = $death_year; 44771378461SGreg Roach 44871378461SGreg Roach $lifespans[] = (object) [ 44971378461SGreg Roach 'background' => $colors[$individual->sex()]->getNextColor(), 45071378461SGreg Roach 'birth_year' => $birth_year, 45171378461SGreg Roach 'death_year' => $death_year, 45271378461SGreg Roach 'id' => 'individual-' . md5($individual->xref()), 45371378461SGreg Roach 'individual' => $individual, 45471378461SGreg Roach 'row' => $next_row, 45571378461SGreg Roach ]; 45671378461SGreg Roach } 45771378461SGreg Roach 45871378461SGreg Roach return $lifespans; 45971378461SGreg Roach } 460168ff6f3Sric2016} 461