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