xref: /webtrees/app/Module/TimelineChartModule.php (revision 158900c2b212361851fcf0964e99084458ce1b3a)
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;
249867b2f0SGreg Roachuse Fisharebest\Webtrees\Auth;
25eca4a663SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
26580a4d11SGreg Roachuse Fisharebest\Webtrees\Fact;
27eca4a663SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
28168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
29168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
3009482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
31eca4a663SGreg Roachuse Fisharebest\Webtrees\Tree;
32b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
33eca4a663SGreg Roachuse Illuminate\Support\Collection;
346ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
356ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3671378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
37168ff6f3Sric2016
389e18e23bSGreg Roachuse function app;
399e18e23bSGreg Roachuse function assert;
403cfcc809SGreg Roachuse function redirect;
413cfcc809SGreg Roachuse function route;
423cfcc809SGreg Roach
43168ff6f3Sric2016/**
44168ff6f3Sric2016 * Class TimelineChartModule
45168ff6f3Sric2016 */
4671378461SGreg Roachclass TimelineChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
47c1010edaSGreg Roach{
4849a243cbSGreg Roach    use ModuleChartTrait;
4949a243cbSGreg Roach
5072f04adfSGreg Roach    protected const ROUTE_URL = '/tree/{tree}/timeline-{scale}';
5171378461SGreg Roach
5271378461SGreg Roach    // Defaults
5371378461SGreg Roach    protected const DEFAULT_SCALE      = 10;
5471378461SGreg Roach    protected const DEFAULT_PARAMETERS = [
5571378461SGreg Roach        'scale' => self::DEFAULT_SCALE,
5671378461SGreg Roach    ];
5771378461SGreg Roach
5871378461SGreg Roach    // Limits
5971378461SGreg Roach    protected const MINIMUM_SCALE = 1;
6071378461SGreg Roach    protected const MAXIMUM_SCALE = 200;
61eca4a663SGreg Roach
62eca4a663SGreg Roach    // GEDCOM events that may have DATE data, but should not be displayed
63eca4a663SGreg Roach    protected const NON_FACTS = [
64e1052c25SGreg Roach        'FAM:CHAN',
65e1052c25SGreg Roach        'INDI:BAPL',
66e1052c25SGreg Roach        'INDI:CHAN',
67e1052c25SGreg Roach        'INDI:ENDL',
68e1052c25SGreg Roach        'INDI:SLGC',
69e1052c25SGreg Roach        'INDI:SLGS',
70e1052c25SGreg Roach        'INDI:_TODO',
71eca4a663SGreg Roach    ];
723cfcc809SGreg Roach
7315228353SGreg Roach    protected const BOX_HEIGHT = 30;
74eca4a663SGreg Roach
7571378461SGreg Roach    /**
7671378461SGreg Roach     * Initialization.
7771378461SGreg Roach     *
789e18e23bSGreg Roach     * @return void
7971378461SGreg Roach     */
809e18e23bSGreg Roach    public function boot(): void
8171378461SGreg Roach    {
82*158900c2SGreg Roach        Registry::routeFactory()->routeMap()
8372f04adfSGreg Roach            ->get(static::class, static::ROUTE_URL, $this)
8471378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST);
8571378461SGreg Roach    }
8671378461SGreg Roach
87168ff6f3Sric2016    /**
880cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
89168ff6f3Sric2016     *
90168ff6f3Sric2016     * @return string
91168ff6f3Sric2016     */
9249a243cbSGreg Roach    public function title(): string
93c1010edaSGreg Roach    {
94bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
95bbb76c12SGreg Roach        return I18N::translate('Timeline');
96168ff6f3Sric2016    }
97168ff6f3Sric2016
98168ff6f3Sric2016    /**
99168ff6f3Sric2016     * A sentence describing what this module does.
100168ff6f3Sric2016     *
101168ff6f3Sric2016     * @return string
102168ff6f3Sric2016     */
10349a243cbSGreg Roach    public function description(): string
104c1010edaSGreg Roach    {
105bbb76c12SGreg Roach        /* I18N: Description of the “TimelineChart” module */
106bbb76c12SGreg Roach        return I18N::translate('A timeline displaying individual events.');
107168ff6f3Sric2016    }
108168ff6f3Sric2016
109168ff6f3Sric2016    /**
110377a2979SGreg Roach     * CSS class for the URL.
111377a2979SGreg Roach     *
112377a2979SGreg Roach     * @return string
113377a2979SGreg Roach     */
114377a2979SGreg Roach    public function chartMenuClass(): string
115377a2979SGreg Roach    {
116377a2979SGreg Roach        return 'menu-chart-timeline';
117377a2979SGreg Roach    }
118377a2979SGreg Roach
119377a2979SGreg Roach    /**
120e6562982SGreg Roach     * The URL for this chart.
121168ff6f3Sric2016     *
12260bc3e3fSGreg Roach     * @param Individual                                $individual
12376d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
12460bc3e3fSGreg Roach     *
125e6562982SGreg Roach     * @return string
126168ff6f3Sric2016     */
127e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
128c1010edaSGreg Roach    {
12972f04adfSGreg Roach        return route(static::class, [
13071378461SGreg Roach                'tree'  => $individual->tree()->name(),
13136826bd4SGreg Roach                'xrefs' => [$individual->xref()],
13271378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
133168ff6f3Sric2016    }
134eca4a663SGreg Roach
135eca4a663SGreg Roach    /**
1366ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
137eca4a663SGreg Roach     *
1386ccdf4f0SGreg Roach     * @return ResponseInterface
139eca4a663SGreg Roach     */
14071378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
141eca4a663SGreg Roach    {
142b55cbc6bSGreg Roach        $tree  = Validator::attributes($request)->tree();
143b55cbc6bSGreg Roach        $user  = Validator::attributes($request)->user();
144b55cbc6bSGreg Roach        $scale = Validator::attributes($request)->isBetween(self::MINIMUM_SCALE, self::MAXIMUM_SCALE)->integer('scale');
145b55cbc6bSGreg Roach        $xrefs = Validator::queryParams($request)->array('xrefs');
146b55cbc6bSGreg Roach        $ajax  = Validator::queryParams($request)->boolean('ajax', false);
1473cfcc809SGreg Roach        $xrefs = array_filter(array_unique($xrefs));
1483cfcc809SGreg Roach
1493cfcc809SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
1503cfcc809SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
151*158900c2SGreg Roach            $xrefs[] = Validator::parsedBody($request)->isXref()->string('add', '');
152*158900c2SGreg Roach
15372f04adfSGreg Roach            return redirect(route(static::class, [
1543cfcc809SGreg Roach                'tree'  => $tree->name(),
155b55cbc6bSGreg Roach                'scale' => $scale,
1563cfcc809SGreg Roach                'xrefs' => $xrefs,
1573cfcc809SGreg Roach            ]));
1583cfcc809SGreg Roach        }
159eca4a663SGreg Roach
160b55cbc6bSGreg Roach        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
161b55cbc6bSGreg Roach
162eca4a663SGreg Roach        // Find the requested individuals.
163eca4a663SGreg Roach        $individuals = (new Collection($xrefs))
1648c627a69SGreg Roach            ->uniqueStrict()
1650b5fd0a6SGreg Roach            ->map(static function (string $xref) use ($tree): ?Individual {
1666b9cb339SGreg Roach                return Registry::individualFactory()->make($xref, $tree);
167eca4a663SGreg Roach            })
168eca4a663SGreg Roach            ->filter()
169eca4a663SGreg Roach            ->filter(GedcomRecord::accessFilter());
170eca4a663SGreg Roach
171eca4a663SGreg Roach        // Generate URLs omitting each xref.
172eca4a663SGreg Roach        $remove_urls = [];
173eca4a663SGreg Roach
174eca4a663SGreg Roach        foreach ($individuals as $exclude) {
175eca4a663SGreg Roach            $xrefs_1 = $individuals
1760b5fd0a6SGreg Roach                ->filter(static function (Individual $individual) use ($exclude): bool {
177eca4a663SGreg Roach                    return $individual->xref() !== $exclude->xref();
178eca4a663SGreg Roach                })
1790b5fd0a6SGreg Roach                ->map(static function (Individual $individual): string {
180eca4a663SGreg Roach                    return $individual->xref();
181eca4a663SGreg Roach                });
182eca4a663SGreg Roach
18372f04adfSGreg Roach            $remove_urls[$exclude->xref()] = route(static::class, [
18471378461SGreg Roach                'tree'  => $tree->name(),
185eca4a663SGreg Roach                'scale' => $scale,
186eca4a663SGreg Roach                'xrefs' => $xrefs_1->all(),
187eca4a663SGreg Roach            ]);
188eca4a663SGreg Roach        }
189eca4a663SGreg Roach
1900b5fd0a6SGreg Roach        $individuals = array_map(static function (string $xref) use ($tree): ?Individual {
1916b9cb339SGreg Roach            return Registry::individualFactory()->make($xref, $tree);
192eca4a663SGreg Roach        }, $xrefs);
193eca4a663SGreg Roach
1940b5fd0a6SGreg Roach        $individuals = array_filter($individuals, static function (?Individual $individual): bool {
19525d7fe95SGreg Roach            return $individual instanceof Individual && $individual->canShow();
19625d7fe95SGreg Roach        });
19725d7fe95SGreg Roach
198b55cbc6bSGreg Roach        if ($ajax) {
19971378461SGreg Roach            $this->layout = 'layouts/ajax';
20071378461SGreg Roach
201eca4a663SGreg Roach            return $this->chart($tree, $xrefs, $scale);
202eca4a663SGreg Roach        }
203eca4a663SGreg Roach
20472f04adfSGreg Roach        $reset_url = route(static::class, [
20571378461SGreg Roach            'scale' => self::DEFAULT_SCALE,
20671378461SGreg Roach            'tree'  => $tree->name(),
20771378461SGreg Roach        ]);
20871378461SGreg Roach
20972f04adfSGreg Roach        $zoom_in_url = route(static::class, [
21015228353SGreg Roach            'scale' => min(self::MAXIMUM_SCALE, $scale + (int) ($scale * 0.4 + 1)),
21171378461SGreg Roach            'tree'  => $tree->name(),
21271378461SGreg Roach            'xrefs' => $xrefs,
21371378461SGreg Roach        ]);
21471378461SGreg Roach
21572f04adfSGreg Roach        $zoom_out_url = route(static::class, [
21615228353SGreg Roach            'scale' => max(self::MINIMUM_SCALE, $scale - (int) ($scale * 0.4 + 1)),
21771378461SGreg Roach            'tree'  => $tree->name(),
21871378461SGreg Roach            'xrefs' => $xrefs,
21971378461SGreg Roach        ]);
22071378461SGreg Roach
22172f04adfSGreg Roach        $ajax_url = route(static::class, [
2229b5537c3SGreg Roach            'ajax'  => true,
223eca4a663SGreg Roach            'scale' => $scale,
22471378461SGreg Roach            'tree'  => $tree->name(),
225eca4a663SGreg Roach            'xrefs' => $xrefs,
226eca4a663SGreg Roach        ]);
227eca4a663SGreg Roach
2289b5537c3SGreg Roach        return $this->viewResponse('modules/timeline-chart/page', [
229eca4a663SGreg Roach            'ajax_url'     => $ajax_url,
230eca4a663SGreg Roach            'individuals'  => $individuals,
23171378461SGreg Roach            'module'       => $this->name(),
232eca4a663SGreg Roach            'remove_urls'  => $remove_urls,
233eca4a663SGreg Roach            'reset_url'    => $reset_url,
234eca4a663SGreg Roach            'scale'        => $scale,
23571378461SGreg Roach            'title'        => $this->title(),
236ef5d23f1SGreg Roach            'tree'         => $tree,
237eca4a663SGreg Roach            'zoom_in_url'  => $zoom_in_url,
238eca4a663SGreg Roach            'zoom_out_url' => $zoom_out_url,
239eca4a663SGreg Roach        ]);
240eca4a663SGreg Roach    }
241eca4a663SGreg Roach
242eca4a663SGreg Roach    /**
243eca4a663SGreg Roach     * @param Tree          $tree
24409482a55SGreg Roach     * @param array<string> $xrefs
245eca4a663SGreg Roach     * @param int           $scale
246eca4a663SGreg Roach     *
2476ccdf4f0SGreg Roach     * @return ResponseInterface
248eca4a663SGreg Roach     */
2496ccdf4f0SGreg Roach    protected function chart(Tree $tree, array $xrefs, int $scale): ResponseInterface
250eca4a663SGreg Roach    {
251eca4a663SGreg Roach        /** @var Individual[] $individuals */
2520b5fd0a6SGreg Roach        $individuals = array_map(static function (string $xref) use ($tree): ?Individual {
2536b9cb339SGreg Roach            return Registry::individualFactory()->make($xref, $tree);
254eca4a663SGreg Roach        }, $xrefs);
255eca4a663SGreg Roach
2560b5fd0a6SGreg Roach        $individuals = array_filter($individuals, static function (?Individual $individual): bool {
25725d7fe95SGreg Roach            return $individual instanceof Individual && $individual->canShow();
258eca4a663SGreg Roach        });
259eca4a663SGreg Roach
260eca4a663SGreg Roach        $baseyear    = (int) date('Y');
261eca4a663SGreg Roach        $topyear     = 0;
2628af3e5c1SGreg Roach        $indifacts   = new Collection();
263eca4a663SGreg Roach        $birthyears  = [];
264eca4a663SGreg Roach        $birthmonths = [];
265eca4a663SGreg Roach        $birthdays   = [];
266eca4a663SGreg Roach
267eca4a663SGreg Roach        foreach ($individuals as $individual) {
268eca4a663SGreg Roach            $bdate = $individual->getBirthDate();
269eca4a663SGreg Roach            if ($bdate->isOK()) {
270eca4a663SGreg Roach                $date = new GregorianDate($bdate->minimumJulianDay());
271eca4a663SGreg Roach
272eca4a663SGreg Roach                $birthyears [$individual->xref()] = $date->year;
273eca4a663SGreg Roach                $birthmonths[$individual->xref()] = max(1, $date->month);
274eca4a663SGreg Roach                $birthdays  [$individual->xref()] = max(1, $date->day);
275eca4a663SGreg Roach            }
276eca4a663SGreg Roach            // find all the fact information
277eca4a663SGreg Roach            $facts = $individual->facts();
27839ca88baSGreg Roach            foreach ($individual->spouseFamilies() as $family) {
279eca4a663SGreg Roach                foreach ($family->facts() as $fact) {
28039ca88baSGreg Roach                    $facts->push($fact);
281eca4a663SGreg Roach                }
282eca4a663SGreg Roach            }
283e1052c25SGreg Roach
284eca4a663SGreg Roach            foreach ($facts as $event) {
285e1052c25SGreg Roach                if (!in_array($event->tag(), self::NON_FACTS, true)) {
286eca4a663SGreg Roach                    // check for a date
287eca4a663SGreg Roach                    $date = $event->date();
288eca4a663SGreg Roach                    if ($date->isOK()) {
289eca4a663SGreg Roach                        $date     = new GregorianDate($date->minimumJulianDay());
290eca4a663SGreg Roach                        $baseyear = min($baseyear, $date->year);
291eca4a663SGreg Roach                        $topyear  = max($topyear, $date->year);
292eca4a663SGreg Roach
293eca4a663SGreg Roach                        if (!$individual->isDead()) {
294eca4a663SGreg Roach                            $topyear = max($topyear, (int) date('Y'));
295eca4a663SGreg Roach                        }
296eca4a663SGreg Roach
2978af3e5c1SGreg Roach                        $indifacts->push($event);
298eca4a663SGreg Roach                    }
299eca4a663SGreg Roach                }
300eca4a663SGreg Roach            }
301eca4a663SGreg Roach        }
302eca4a663SGreg Roach
3038af3e5c1SGreg Roach        // do not add the same fact twice (prevents marriages from being added multiple times)
3048c627a69SGreg Roach        $indifacts = $indifacts->uniqueStrict(static function (Fact $fact): string {
3058c627a69SGreg Roach            return $fact->id();
3068c627a69SGreg Roach        });
3078af3e5c1SGreg Roach
308eca4a663SGreg Roach        if ($scale === 0) {
3098af3e5c1SGreg Roach            $scale = (int) (($topyear - $baseyear) / 20 * $indifacts->count() / 4);
310eca4a663SGreg Roach            if ($scale < 6) {
311eca4a663SGreg Roach                $scale = 6;
312eca4a663SGreg Roach            }
313eca4a663SGreg Roach        }
314eca4a663SGreg Roach        if ($scale < 2) {
315eca4a663SGreg Roach            $scale = 2;
316eca4a663SGreg Roach        }
317eca4a663SGreg Roach        $baseyear -= 5;
318eca4a663SGreg Roach        $topyear  += 5;
319eca4a663SGreg Roach
320580a4d11SGreg Roach        $indifacts = Fact::sortFacts($indifacts);
321eca4a663SGreg Roach
322eca4a663SGreg Roach        $html = view('modules/timeline-chart/chart', [
323eca4a663SGreg Roach            'baseyear'    => $baseyear,
32415228353SGreg Roach            'bheight'     => self::BOX_HEIGHT,
325eca4a663SGreg Roach            'birthdays'   => $birthdays,
326eca4a663SGreg Roach            'birthmonths' => $birthmonths,
327eca4a663SGreg Roach            'birthyears'  => $birthyears,
328eca4a663SGreg Roach            'indifacts'   => $indifacts,
329eca4a663SGreg Roach            'individuals' => $individuals,
330eca4a663SGreg Roach            'placements'  => [],
331eca4a663SGreg Roach            'scale'       => $scale,
332eca4a663SGreg Roach            'topyear'     => $topyear,
333eca4a663SGreg Roach        ]);
334eca4a663SGreg Roach
3356ccdf4f0SGreg Roach        return response($html);
336eca4a663SGreg Roach    }
337168ff6f3Sric2016}
338