fisharebest /
webtrees
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * webtrees: online genealogy |
||
| 5 | * Copyright (C) 2025 webtrees development team |
||
| 6 | * This program is free software: you can redistribute it and/or modify |
||
| 7 | * it under the terms of the GNU General Public License as published by |
||
| 8 | * the Free Software Foundation, either version 3 of the License, or |
||
| 9 | * (at your option) any later version. |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 14 | * You should have received a copy of the GNU General Public License |
||
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
| 16 | */ |
||
| 17 | |||
| 18 | declare(strict_types=1); |
||
| 19 | |||
| 20 | namespace Fisharebest\Webtrees\Module; |
||
| 21 | |||
| 22 | use Fig\Http\Message\RequestMethodInterface; |
||
| 23 | use Fisharebest\ExtCalendar\GregorianCalendar; |
||
| 24 | use Fisharebest\Webtrees\Auth; |
||
| 25 | use Fisharebest\Webtrees\ColorGenerator; |
||
| 26 | use Fisharebest\Webtrees\Date; |
||
| 27 | use Fisharebest\Webtrees\DB; |
||
| 28 | use Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException; |
||
| 29 | use Fisharebest\Webtrees\Http\Middleware\AuthNotRobot; |
||
| 30 | use Fisharebest\Webtrees\I18N; |
||
| 31 | use Fisharebest\Webtrees\Individual; |
||
| 32 | use Fisharebest\Webtrees\Place; |
||
| 33 | use Fisharebest\Webtrees\Registry; |
||
| 34 | use Fisharebest\Webtrees\Tree; |
||
| 35 | use Fisharebest\Webtrees\Validator; |
||
| 36 | use Illuminate\Database\Query\JoinClause; |
||
| 37 | use Psr\Http\Message\ResponseInterface; |
||
| 38 | use Psr\Http\Message\ServerRequestInterface; |
||
| 39 | use Psr\Http\Server\RequestHandlerInterface; |
||
| 40 | |||
| 41 | use function array_filter; |
||
| 42 | use function array_intersect; |
||
| 43 | use function array_map; |
||
| 44 | use function array_merge; |
||
| 45 | use function array_reduce; |
||
| 46 | use function array_unique; |
||
| 47 | use function count; |
||
| 48 | use function date; |
||
| 49 | use function explode; |
||
| 50 | use function implode; |
||
| 51 | use function intdiv; |
||
| 52 | use function max; |
||
| 53 | use function md5; |
||
| 54 | use function min; |
||
| 55 | use function redirect; |
||
| 56 | use function response; |
||
| 57 | use function route; |
||
| 58 | use function usort; |
||
| 59 | use function view; |
||
| 60 | |||
| 61 | use const PHP_INT_MAX; |
||
| 62 | |||
| 63 | class LifespansChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface |
||
| 64 | { |
||
| 65 | use ModuleChartTrait; |
||
| 66 | |||
| 67 | protected const string ROUTE_URL = '/tree/{tree}/lifespans'; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 68 | |||
| 69 | // In theory, only "@" is a safe separator, but it gives longer and uglier URLs. |
||
| 70 | // Unless some other application generates XREFs with a ".", we are safe. |
||
| 71 | protected const string SEPARATOR = '.'; |
||
| 72 | |||
| 73 | // Defaults |
||
| 74 | protected const array DEFAULT_PARAMETERS = []; |
||
| 75 | |||
| 76 | // Parameters for generating colors |
||
| 77 | protected const int RANGE = 120; // degrees |
||
| 78 | protected const int SATURATION = 100; // percent |
||
| 79 | protected const int LIGHTNESS = 30; // percent |
||
| 80 | protected const float ALPHA = 0.25; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Initialization. |
||
| 84 | * |
||
| 85 | * @return void |
||
| 86 | */ |
||
| 87 | public function boot(): void |
||
| 88 | { |
||
| 89 | Registry::routeFactory()->routeMap() |
||
| 90 | ->get(static::class, static::ROUTE_URL, $this) |
||
| 91 | ->allows(RequestMethodInterface::METHOD_POST) |
||
| 92 | ->extras(['middleware' => [AuthNotRobot::class]]); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function title(): string |
||
| 96 | { |
||
| 97 | /* I18N: Name of a module/chart */ |
||
| 98 | return I18N::translate('Lifespans'); |
||
| 99 | } |
||
| 100 | |||
| 101 | public function description(): string |
||
| 102 | { |
||
| 103 | /* I18N: Description of the “LifespansChart” module */ |
||
| 104 | return I18N::translate('A chart of individuals’ lifespans.'); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * CSS class for the URL. |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function chartMenuClass(): string |
||
| 113 | { |
||
| 114 | return 'menu-chart-lifespan'; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The URL for this chart. |
||
| 119 | * |
||
| 120 | * @param Individual $individual |
||
| 121 | * @param array<bool|int|string|array<string>|null> $parameters |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function chartUrl(Individual $individual, array $parameters = []): string |
||
| 126 | { |
||
| 127 | return route(static::class, [ |
||
| 128 | 'tree' => $individual->tree()->name(), |
||
| 129 | 'xrefs' => $individual->xref(), |
||
| 130 | ] + $parameters + self::DEFAULT_PARAMETERS); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param ServerRequestInterface $request |
||
| 135 | * |
||
| 136 | * @return ResponseInterface |
||
| 137 | */ |
||
| 138 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 139 | { |
||
| 140 | $tree = Validator::attributes($request)->tree(); |
||
| 141 | $user = Validator::attributes($request)->user(); |
||
| 142 | $xrefs = Validator::queryParams($request)->string('xrefs', ''); |
||
| 143 | $ajax = Validator::queryParams($request)->boolean('ajax', false); |
||
| 144 | |||
| 145 | if ($xrefs === '') { |
||
| 146 | try { |
||
| 147 | // URLs created by webtrees 2.0 and earlier used an array. |
||
| 148 | $xrefs = Validator::queryParams($request)->array('xrefs'); |
||
| 149 | } catch (HttpBadRequestException) { |
||
| 150 | // Not a 2.0 request, just an empty parameter. |
||
| 151 | $xrefs = []; |
||
| 152 | } |
||
| 153 | } else { |
||
| 154 | $xrefs = explode(self::SEPARATOR, $xrefs); |
||
| 155 | } |
||
| 156 | |||
| 157 | $addxref = Validator::parsedBody($request)->string('addxref', ''); |
||
| 158 | $addfam = Validator::parsedBody($request)->boolean('addfam', false); |
||
| 159 | $place_id = Validator::parsedBody($request)->integer('place_id', 0); |
||
| 160 | $start = Validator::parsedBody($request)->string('start', ''); |
||
| 161 | $end = Validator::parsedBody($request)->string('end', ''); |
||
| 162 | |||
| 163 | $place = Place::find($place_id, $tree); |
||
| 164 | $start_date = new Date($start); |
||
| 165 | $end_date = new Date($end); |
||
| 166 | |||
| 167 | $xrefs = array_unique($xrefs); |
||
| 168 | |||
| 169 | // Add an individual, and family members |
||
| 170 | $individual = Registry::individualFactory()->make($addxref, $tree); |
||
| 171 | if ($individual !== null) { |
||
| 172 | $xrefs[] = $addxref; |
||
| 173 | if ($addfam) { |
||
| 174 | $xrefs = array_merge($xrefs, $this->closeFamily($individual)); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | // Select by date and/or place. |
||
| 179 | if ($place_id !== 0 && $start_date->isOK() && $end_date->isOK()) { |
||
| 180 | $date_xrefs = $this->findIndividualsByDate($start_date, $end_date, $tree); |
||
| 181 | $place_xrefs = $this->findIndividualsByPlace($place, $tree); |
||
| 182 | $xrefs = array_intersect($date_xrefs, $place_xrefs); |
||
| 183 | } elseif ($start_date->isOK() && $end_date->isOK()) { |
||
| 184 | $xrefs = $this->findIndividualsByDate($start_date, $end_date, $tree); |
||
| 185 | } elseif ($place_id !== 0) { |
||
| 186 | $xrefs = $this->findIndividualsByPlace($place, $tree); |
||
| 187 | } |
||
| 188 | |||
| 189 | // Filter duplicates and private individuals. |
||
| 190 | $xrefs = array_unique($xrefs); |
||
| 191 | $xrefs = array_filter($xrefs, static function (string $xref) use ($tree): bool { |
||
| 192 | $individual = Registry::individualFactory()->make($xref, $tree); |
||
| 193 | |||
| 194 | return $individual !== null && $individual->canShow(); |
||
| 195 | }); |
||
| 196 | |||
| 197 | // Convert POST requests into GET requests for pretty URLs. |
||
| 198 | if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { |
||
| 199 | return redirect(route(static::class, [ |
||
| 200 | 'tree' => $tree->name(), |
||
| 201 | 'xrefs' => implode(self::SEPARATOR, $xrefs), |
||
| 202 | ])); |
||
| 203 | } |
||
| 204 | |||
| 205 | Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); |
||
| 206 | |||
| 207 | if ($ajax) { |
||
| 208 | $this->layout = 'layouts/ajax'; |
||
| 209 | |||
| 210 | return $this->chart($tree, $xrefs); |
||
| 211 | } |
||
| 212 | |||
| 213 | $reset_url = route(static::class, ['tree' => $tree->name()]); |
||
| 214 | |||
| 215 | $ajax_url = route(static::class, [ |
||
| 216 | 'ajax' => true, |
||
| 217 | 'tree' => $tree->name(), |
||
| 218 | 'xrefs' => implode(self::SEPARATOR, $xrefs), |
||
| 219 | ]); |
||
| 220 | |||
| 221 | return $this->viewResponse('modules/lifespans-chart/page', [ |
||
| 222 | 'ajax_url' => $ajax_url, |
||
| 223 | 'module' => $this->name(), |
||
| 224 | 'reset_url' => $reset_url, |
||
| 225 | 'title' => $this->title(), |
||
| 226 | 'tree' => $tree, |
||
| 227 | 'xrefs' => $xrefs, |
||
| 228 | ]); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param Tree $tree |
||
| 233 | * @param array<string> $xrefs |
||
| 234 | * |
||
| 235 | * @return ResponseInterface |
||
| 236 | */ |
||
| 237 | protected function chart(Tree $tree, array $xrefs): ResponseInterface |
||
| 238 | { |
||
| 239 | /** @var Individual[] $individuals */ |
||
| 240 | $individuals = array_map(static fn (string $xref): Individual|null => Registry::individualFactory()->make($xref, $tree), $xrefs); |
||
| 241 | |||
| 242 | $individuals = array_filter($individuals, static fn (Individual|null $individual): bool => $individual instanceof Individual && $individual->canShow()); |
||
| 243 | |||
| 244 | // Sort the array in order of birth year |
||
| 245 | usort($individuals, Individual::birthDateComparator()); |
||
| 246 | |||
| 247 | // Round to whole decades |
||
| 248 | $start_year = intdiv($this->minYear($individuals), 10) * 10; |
||
| 249 | $end_year = intdiv($this->maxYear($individuals) + 9, 10) * 10; |
||
| 250 | |||
| 251 | $lifespans = $this->layoutIndividuals($individuals); |
||
| 252 | |||
| 253 | $callback = static fn (int $carry, object $item): int => max($carry, $item->row); |
||
| 254 | $max_rows = array_reduce($lifespans, $callback, 0); |
||
| 255 | |||
| 256 | $count = count($xrefs); |
||
| 257 | $subtitle = I18N::plural('%s individual', '%s individuals', $count, I18N::number($count)); |
||
| 258 | |||
| 259 | $html = view('modules/lifespans-chart/chart', [ |
||
| 260 | 'dir' => I18N::direction(), |
||
| 261 | 'end_year' => $end_year, |
||
| 262 | 'lifespans' => $lifespans, |
||
| 263 | 'max_rows' => $max_rows, |
||
| 264 | 'start_year' => $start_year, |
||
| 265 | 'subtitle' => $subtitle, |
||
| 266 | ]); |
||
| 267 | |||
| 268 | return response($html); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Find the latest event year for individuals |
||
| 273 | * |
||
| 274 | * @param array<Individual> $individuals |
||
| 275 | * |
||
| 276 | * @return int |
||
| 277 | */ |
||
| 278 | protected function maxYear(array $individuals): int |
||
| 279 | { |
||
| 280 | $jd = array_reduce($individuals, static function (int $carry, Individual $item): int { |
||
| 281 | if ($item->getEstimatedDeathDate()->isOK()) { |
||
| 282 | return max($carry, $item->getEstimatedDeathDate()->maximumJulianDay()); |
||
| 283 | } |
||
| 284 | |||
| 285 | return $carry; |
||
| 286 | }, 0); |
||
| 287 | |||
| 288 | $year = $this->jdToYear($jd); |
||
| 289 | |||
| 290 | // Don't show future dates |
||
| 291 | return min($year, (int) date('Y')); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Find the earliest event year for individuals |
||
| 296 | * |
||
| 297 | * @param array<Individual> $individuals |
||
| 298 | * |
||
| 299 | * @return int |
||
| 300 | */ |
||
| 301 | protected function minYear(array $individuals): int |
||
| 302 | { |
||
| 303 | $jd = array_reduce($individuals, static function (int $carry, Individual $item): int { |
||
| 304 | if ($item->getEstimatedBirthDate()->isOK()) { |
||
| 305 | return min($carry, $item->getEstimatedBirthDate()->minimumJulianDay()); |
||
| 306 | } |
||
| 307 | |||
| 308 | return $carry; |
||
| 309 | }, PHP_INT_MAX); |
||
| 310 | |||
| 311 | return $this->jdToYear($jd); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Convert a julian day to a gregorian year |
||
| 316 | * |
||
| 317 | * @param int $jd |
||
| 318 | * |
||
| 319 | * @return int |
||
| 320 | */ |
||
| 321 | protected function jdToYear(int $jd): int |
||
| 322 | { |
||
| 323 | if ($jd === 0) { |
||
| 324 | return 0; |
||
| 325 | } |
||
| 326 | |||
| 327 | $gregorian = new GregorianCalendar(); |
||
| 328 | [$y] = $gregorian->jdToYmd($jd); |
||
| 329 | |||
| 330 | return $y; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param Date $start |
||
| 335 | * @param Date $end |
||
| 336 | * @param Tree $tree |
||
| 337 | * |
||
| 338 | * @return array<string> |
||
| 339 | */ |
||
| 340 | protected function findIndividualsByDate(Date $start, Date $end, Tree $tree): array |
||
| 341 | { |
||
| 342 | return DB::table('individuals') |
||
| 343 | ->join('dates', static function (JoinClause $join): void { |
||
| 344 | $join |
||
| 345 | ->on('d_file', '=', 'i_file') |
||
| 346 | ->on('d_gid', '=', 'i_id'); |
||
| 347 | }) |
||
| 348 | ->where('i_file', '=', $tree->id()) |
||
| 349 | ->where('d_julianday1', '<=', $end->maximumJulianDay()) |
||
| 350 | ->where('d_julianday2', '>=', $start->minimumJulianDay()) |
||
| 351 | ->whereNotIn('d_fact', ['BAPL', 'ENDL', 'SLGC', 'SLGS', '_TODO', 'CHAN']) |
||
| 352 | ->pluck('i_id') |
||
| 353 | ->all(); |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param Place $place |
||
| 358 | * @param Tree $tree |
||
| 359 | * |
||
| 360 | * @return array<string> |
||
| 361 | */ |
||
| 362 | protected function findIndividualsByPlace(Place $place, Tree $tree): array |
||
| 363 | { |
||
| 364 | return DB::table('individuals') |
||
| 365 | ->join('placelinks', static function (JoinClause $join): void { |
||
| 366 | $join |
||
| 367 | ->on('pl_file', '=', 'i_file') |
||
| 368 | ->on('pl_gid', '=', 'i_id'); |
||
| 369 | }) |
||
| 370 | ->where('i_file', '=', $tree->id()) |
||
| 371 | ->where('pl_p_id', '=', $place->id()) |
||
| 372 | ->pluck('i_id') |
||
| 373 | ->all(); |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Find the close family members of an individual. |
||
| 378 | * |
||
| 379 | * @param Individual $individual |
||
| 380 | * |
||
| 381 | * @return array<string> |
||
| 382 | */ |
||
| 383 | protected function closeFamily(Individual $individual): array |
||
| 384 | { |
||
| 385 | $xrefs = []; |
||
| 386 | |||
| 387 | foreach ($individual->spouseFamilies() as $family) { |
||
| 388 | foreach ($family->children() as $child) { |
||
| 389 | $xrefs[] = $child->xref(); |
||
| 390 | } |
||
| 391 | |||
| 392 | foreach ($family->spouses() as $spouse) { |
||
| 393 | $xrefs[] = $spouse->xref(); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | foreach ($individual->childFamilies() as $family) { |
||
| 398 | foreach ($family->children() as $child) { |
||
| 399 | $xrefs[] = $child->xref(); |
||
| 400 | } |
||
| 401 | |||
| 402 | foreach ($family->spouses() as $spouse) { |
||
| 403 | $xrefs[] = $spouse->xref(); |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | return $xrefs; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param array<Individual> $individuals |
||
| 412 | * |
||
| 413 | * @return array<object{ |
||
| 414 | * background: string, |
||
| 415 | * birth_year: int, |
||
| 416 | * death_year: int, |
||
| 417 | * id: string, |
||
| 418 | * individual: Individual, |
||
| 419 | * row: int |
||
| 420 | * }> |
||
| 421 | */ |
||
| 422 | private function layoutIndividuals(array $individuals): array |
||
| 423 | { |
||
| 424 | $color_generators = [ |
||
| 425 | 'M' => new ColorGenerator(240, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE * -1), |
||
| 426 | 'F' => new ColorGenerator(000, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE), |
||
| 427 | 'U' => new ColorGenerator(120, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE), |
||
| 428 | ]; |
||
| 429 | |||
| 430 | $current_year = (int) date('Y'); |
||
| 431 | |||
| 432 | // Latest year used in each row |
||
| 433 | $rows = []; |
||
| 434 | |||
| 435 | $lifespans = []; |
||
| 436 | |||
| 437 | foreach ($individuals as $individual) { |
||
| 438 | $birth_jd = $individual->getEstimatedBirthDate()->minimumJulianDay(); |
||
| 439 | $birth_year = $this->jdToYear($birth_jd); |
||
| 440 | $death_jd = $individual->getEstimatedDeathDate()->maximumJulianDay(); |
||
| 441 | $death_year = $this->jdToYear($death_jd); |
||
| 442 | |||
| 443 | // Died before they were born? Swapping the dates allows them to be shown. |
||
| 444 | if ($death_year < $birth_year) { |
||
| 445 | $death_year = $birth_year; |
||
| 446 | } |
||
| 447 | |||
| 448 | // Don't show death dates in the future. |
||
| 449 | $death_year = min($death_year, $current_year); |
||
| 450 | |||
| 451 | // Add this individual to the next row in the chart... |
||
| 452 | $next_row = count($rows); |
||
| 453 | // ...unless we can find an existing row where it fits. |
||
| 454 | foreach ($rows as $row => $year) { |
||
| 455 | if ($year < $birth_year) { |
||
| 456 | $next_row = $row; |
||
| 457 | break; |
||
| 458 | } |
||
| 459 | } |
||
| 460 | |||
| 461 | // Fill the row up to the year (leaving a small gap) |
||
| 462 | $rows[$next_row] = $death_year; |
||
| 463 | |||
| 464 | $color_generator = $color_generators[$individual->sex()] ?? $color_generators['U']; |
||
| 465 | |||
| 466 | $lifespans[] = (object) [ |
||
| 467 | 'background' => $color_generator->getNextColor(), |
||
| 468 | 'birth_year' => $birth_year, |
||
| 469 | 'death_year' => $death_year, |
||
| 470 | 'id' => 'individual-' . md5($individual->xref()), |
||
| 471 | 'individual' => $individual, |
||
| 472 | 'row' => $next_row, |
||
| 473 | ]; |
||
| 474 | } |
||
| 475 | |||
| 476 | return $lifespans; |
||
| 477 | } |
||
| 478 | } |
||
| 479 |