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