xref: /webtrees/app/Module/TimelineChartModule.php (revision b46c87bda4b592cf9252f1db48552a820b1e3d97)
1168ff6f3Sric2016<?php
23976b470SGreg Roach
3168ff6f3Sric2016/**
4168ff6f3Sric2016 * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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;
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;
30eca4a663SGreg Roachuse Fisharebest\Webtrees\Tree;
31eca4a663SGreg Roachuse Illuminate\Support\Collection;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3471378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
35168ff6f3Sric2016
369e18e23bSGreg Roachuse function app;
379e18e23bSGreg Roachuse function assert;
383cfcc809SGreg Roachuse function redirect;
393cfcc809SGreg Roachuse function route;
403cfcc809SGreg Roach
41168ff6f3Sric2016/**
42168ff6f3Sric2016 * Class TimelineChartModule
43168ff6f3Sric2016 */
4471378461SGreg Roachclass TimelineChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
45c1010edaSGreg Roach{
4649a243cbSGreg Roach    use ModuleChartTrait;
4749a243cbSGreg Roach
4871378461SGreg Roach    private const ROUTE_NAME = 'timeline-chart';
4971378461SGreg Roach    private const ROUTE_URL  = '/tree/{tree}/timeline-{scale}';
5071378461SGreg Roach
5171378461SGreg Roach    // Defaults
5271378461SGreg Roach    protected const DEFAULT_SCALE      = 10;
5371378461SGreg Roach    protected const DEFAULT_PARAMETERS = [
5471378461SGreg Roach        'scale' => self::DEFAULT_SCALE,
5571378461SGreg Roach    ];
5671378461SGreg Roach
5771378461SGreg Roach    // Limits
5871378461SGreg Roach    protected const MINIMUM_SCALE = 1;
5971378461SGreg Roach    protected const MAXIMUM_SCALE = 200;
60eca4a663SGreg Roach
61eca4a663SGreg Roach    // GEDCOM events that may have DATE data, but should not be displayed
62eca4a663SGreg Roach    protected const NON_FACTS = [
63eca4a663SGreg Roach        'BAPL',
64eca4a663SGreg Roach        'ENDL',
65eca4a663SGreg Roach        'SLGC',
66eca4a663SGreg Roach        'SLGS',
67eca4a663SGreg Roach        '_TODO',
68eca4a663SGreg Roach        'CHAN',
69eca4a663SGreg Roach    ];
703cfcc809SGreg Roach    protected const BHEIGHT   = 30;
713cfcc809SGreg Roach
723cfcc809SGreg Roach    // Box height
73eca4a663SGreg Roach
7471378461SGreg Roach    /**
7571378461SGreg Roach     * Initialization.
7671378461SGreg Roach     *
779e18e23bSGreg Roach     * @return void
7871378461SGreg Roach     */
799e18e23bSGreg Roach    public function boot(): void
8071378461SGreg Roach    {
819e18e23bSGreg Roach        $router_container = app(RouterContainer::class);
829e18e23bSGreg Roach        assert($router_container instanceof RouterContainer);
839e18e23bSGreg Roach
8471378461SGreg Roach        $router_container->getMap()
85f7358520SGreg Roach            ->get(self::ROUTE_NAME, self::ROUTE_URL, $this)
8671378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST);
8771378461SGreg Roach    }
8871378461SGreg Roach
89168ff6f3Sric2016    /**
900cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
91168ff6f3Sric2016     *
92168ff6f3Sric2016     * @return string
93168ff6f3Sric2016     */
9449a243cbSGreg Roach    public function title(): string
95c1010edaSGreg Roach    {
96bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
97bbb76c12SGreg Roach        return I18N::translate('Timeline');
98168ff6f3Sric2016    }
99168ff6f3Sric2016
100168ff6f3Sric2016    /**
101168ff6f3Sric2016     * A sentence describing what this module does.
102168ff6f3Sric2016     *
103168ff6f3Sric2016     * @return string
104168ff6f3Sric2016     */
10549a243cbSGreg Roach    public function description(): string
106c1010edaSGreg Roach    {
107bbb76c12SGreg Roach        /* I18N: Description of the “TimelineChart” module */
108bbb76c12SGreg Roach        return I18N::translate('A timeline displaying individual events.');
109168ff6f3Sric2016    }
110168ff6f3Sric2016
111168ff6f3Sric2016    /**
112377a2979SGreg Roach     * CSS class for the URL.
113377a2979SGreg Roach     *
114377a2979SGreg Roach     * @return string
115377a2979SGreg Roach     */
116377a2979SGreg Roach    public function chartMenuClass(): string
117377a2979SGreg Roach    {
118377a2979SGreg Roach        return 'menu-chart-timeline';
119377a2979SGreg Roach    }
120377a2979SGreg Roach
121377a2979SGreg Roach    /**
122e6562982SGreg Roach     * The URL for this chart.
123168ff6f3Sric2016     *
12460bc3e3fSGreg Roach     * @param Individual $individual
12559597b37SGreg Roach     * @param mixed[]    $parameters
12660bc3e3fSGreg Roach     *
127e6562982SGreg Roach     * @return string
128168ff6f3Sric2016     */
129e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
130c1010edaSGreg Roach    {
13171378461SGreg Roach        return route(self::ROUTE_NAME, [
13271378461SGreg Roach                'tree'  => $individual->tree()->name(),
13336826bd4SGreg Roach                'xrefs' => [$individual->xref()],
13471378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
135168ff6f3Sric2016    }
136eca4a663SGreg Roach
137eca4a663SGreg Roach    /**
1386ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
139eca4a663SGreg Roach     *
1406ccdf4f0SGreg Roach     * @return ResponseInterface
141eca4a663SGreg Roach     */
14271378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
143eca4a663SGreg Roach    {
14457ab2231SGreg Roach        $tree = $request->getAttribute('tree');
1454ea62551SGreg Roach        assert($tree instanceof Tree);
1464ea62551SGreg Roach
14757ab2231SGreg Roach        $user  = $request->getAttribute('user');
14871378461SGreg Roach        $scale = (int) $request->getAttribute('scale');
14971378461SGreg Roach        $xrefs = $request->getQueryParams()['xrefs'] ?? [];
15071378461SGreg Roach        $ajax  = $request->getQueryParams()['ajax'] ?? '';
15157ab2231SGreg Roach
152*b46c87bdSGreg Roach
153*b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
154*b46c87bdSGreg Roach
155*b46c87bdSGreg Roach        $add  = $params['add'] ?? '';
156*b46c87bdSGreg Roach
1579867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
1589867b2f0SGreg Roach
15971378461SGreg Roach        $scale = min($scale, self::MAXIMUM_SCALE);
16071378461SGreg Roach        $scale = max($scale, self::MINIMUM_SCALE);
16171378461SGreg Roach
1623cfcc809SGreg Roach        $xrefs[] = $add;
1633cfcc809SGreg Roach        $xrefs = array_filter(array_unique($xrefs));
1643cfcc809SGreg Roach
1653cfcc809SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
1663cfcc809SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
1673cfcc809SGreg Roach            return redirect(route(self::ROUTE_NAME, [
1683cfcc809SGreg Roach                'scale' => $scale,
1693cfcc809SGreg Roach                'tree'  => $tree->name(),
1703cfcc809SGreg Roach                'xrefs' => $xrefs,
1713cfcc809SGreg Roach            ]));
1723cfcc809SGreg Roach        }
173eca4a663SGreg Roach
174eca4a663SGreg Roach        // Find the requested individuals.
175eca4a663SGreg Roach        $individuals = (new Collection($xrefs))
176eca4a663SGreg Roach            ->unique()
1770b5fd0a6SGreg Roach            ->map(static function (string $xref) use ($tree): ?Individual {
178eca4a663SGreg Roach                return Individual::getInstance($xref, $tree);
179eca4a663SGreg Roach            })
180eca4a663SGreg Roach            ->filter()
181eca4a663SGreg Roach            ->filter(GedcomRecord::accessFilter());
182eca4a663SGreg Roach
183eca4a663SGreg Roach        // Generate URLs omitting each xref.
184eca4a663SGreg Roach        $remove_urls = [];
185eca4a663SGreg Roach
186eca4a663SGreg Roach        foreach ($individuals as $exclude) {
187eca4a663SGreg Roach            $xrefs_1 = $individuals
1880b5fd0a6SGreg Roach                ->filter(static function (Individual $individual) use ($exclude): bool {
189eca4a663SGreg Roach                    return $individual->xref() !== $exclude->xref();
190eca4a663SGreg Roach                })
1910b5fd0a6SGreg Roach                ->map(static function (Individual $individual): string {
192eca4a663SGreg Roach                    return $individual->xref();
193eca4a663SGreg Roach                });
194eca4a663SGreg Roach
19571378461SGreg Roach            $remove_urls[$exclude->xref()] = route(self::ROUTE_NAME, [
19671378461SGreg Roach                'tree'  => $tree->name(),
197eca4a663SGreg Roach                'scale' => $scale,
198eca4a663SGreg Roach                'xrefs' => $xrefs_1->all(),
199eca4a663SGreg Roach            ]);
200eca4a663SGreg Roach        }
201eca4a663SGreg Roach
2020b5fd0a6SGreg Roach        $individuals = array_map(static function (string $xref) use ($tree): ?Individual {
203eca4a663SGreg Roach            return Individual::getInstance($xref, $tree);
204eca4a663SGreg Roach        }, $xrefs);
205eca4a663SGreg Roach
2060b5fd0a6SGreg Roach        $individuals = array_filter($individuals, static function (?Individual $individual): bool {
20725d7fe95SGreg Roach            return $individual instanceof Individual && $individual->canShow();
20825d7fe95SGreg Roach        });
20925d7fe95SGreg Roach
21071378461SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
21171378461SGreg Roach
2120b93976aSGreg Roach        if ($ajax === '1') {
21371378461SGreg Roach            $this->layout = 'layouts/ajax';
21471378461SGreg Roach
215eca4a663SGreg Roach            return $this->chart($tree, $xrefs, $scale);
216eca4a663SGreg Roach        }
217eca4a663SGreg Roach
21871378461SGreg Roach        $reset_url = route(self::ROUTE_NAME, [
21971378461SGreg Roach            'scale' => self::DEFAULT_SCALE,
22071378461SGreg Roach            'tree'  => $tree->name(),
22171378461SGreg Roach        ]);
22271378461SGreg Roach
22371378461SGreg Roach        $zoom_in_url = route(self::ROUTE_NAME, [
22471378461SGreg Roach            'scale' => min(self::MAXIMUM_SCALE, $scale + (int) ($scale * 0.2 + 1)),
22571378461SGreg Roach            'tree'  => $tree->name(),
22671378461SGreg Roach            'xrefs' => $xrefs,
22771378461SGreg Roach        ]);
22871378461SGreg Roach
22971378461SGreg Roach        $zoom_out_url = route(self::ROUTE_NAME, [
23071378461SGreg Roach            'scale' => max(self::MINIMUM_SCALE, $scale - (int) ($scale * 0.2 + 1)),
23171378461SGreg Roach            'tree'  => $tree->name(),
23271378461SGreg Roach            'xrefs' => $xrefs,
23371378461SGreg Roach        ]);
23471378461SGreg Roach
23571378461SGreg Roach        $ajax_url = route(self::ROUTE_NAME, [
2369b5537c3SGreg Roach            'ajax'  => true,
237eca4a663SGreg Roach            'scale' => $scale,
23871378461SGreg Roach            'tree'  => $tree->name(),
239eca4a663SGreg Roach            'xrefs' => $xrefs,
240eca4a663SGreg Roach        ]);
241eca4a663SGreg Roach
2429b5537c3SGreg Roach        return $this->viewResponse('modules/timeline-chart/page', [
243eca4a663SGreg Roach            'ajax_url'     => $ajax_url,
244eca4a663SGreg Roach            'individuals'  => $individuals,
24571378461SGreg Roach            'module'       => $this->name(),
246eca4a663SGreg Roach            'remove_urls'  => $remove_urls,
247eca4a663SGreg Roach            'reset_url'    => $reset_url,
248eca4a663SGreg Roach            'scale'        => $scale,
24971378461SGreg Roach            'title'        => $this->title(),
250ef5d23f1SGreg Roach            'tree'         => $tree,
251eca4a663SGreg Roach            'zoom_in_url'  => $zoom_in_url,
252eca4a663SGreg Roach            'zoom_out_url' => $zoom_out_url,
253eca4a663SGreg Roach        ]);
254eca4a663SGreg Roach    }
255eca4a663SGreg Roach
256eca4a663SGreg Roach    /**
257eca4a663SGreg Roach     * @param Tree  $tree
258eca4a663SGreg Roach     * @param array $xrefs
259eca4a663SGreg Roach     * @param int   $scale
260eca4a663SGreg Roach     *
2616ccdf4f0SGreg Roach     * @return ResponseInterface
262eca4a663SGreg Roach     */
2636ccdf4f0SGreg Roach    protected function chart(Tree $tree, array $xrefs, int $scale): ResponseInterface
264eca4a663SGreg Roach    {
265eca4a663SGreg Roach        /** @var Individual[] $individuals */
2660b5fd0a6SGreg Roach        $individuals = array_map(static function (string $xref) use ($tree): ?Individual {
267eca4a663SGreg Roach            return Individual::getInstance($xref, $tree);
268eca4a663SGreg Roach        }, $xrefs);
269eca4a663SGreg Roach
2700b5fd0a6SGreg Roach        $individuals = array_filter($individuals, static function (?Individual $individual): bool {
27125d7fe95SGreg Roach            return $individual instanceof Individual && $individual->canShow();
272eca4a663SGreg Roach        });
273eca4a663SGreg Roach
274eca4a663SGreg Roach        $baseyear    = (int) date('Y');
275eca4a663SGreg Roach        $topyear     = 0;
2768af3e5c1SGreg Roach        $indifacts   = new Collection();
277eca4a663SGreg Roach        $birthyears  = [];
278eca4a663SGreg Roach        $birthmonths = [];
279eca4a663SGreg Roach        $birthdays   = [];
280eca4a663SGreg Roach
281eca4a663SGreg Roach        foreach ($individuals as $individual) {
282eca4a663SGreg Roach            $bdate = $individual->getBirthDate();
283eca4a663SGreg Roach            if ($bdate->isOK()) {
284eca4a663SGreg Roach                $date = new GregorianDate($bdate->minimumJulianDay());
285eca4a663SGreg Roach
286eca4a663SGreg Roach                $birthyears [$individual->xref()] = $date->year;
287eca4a663SGreg Roach                $birthmonths[$individual->xref()] = max(1, $date->month);
288eca4a663SGreg Roach                $birthdays  [$individual->xref()] = max(1, $date->day);
289eca4a663SGreg Roach            }
290eca4a663SGreg Roach            // find all the fact information
291eca4a663SGreg Roach            $facts = $individual->facts();
29239ca88baSGreg Roach            foreach ($individual->spouseFamilies() as $family) {
293eca4a663SGreg Roach                foreach ($family->facts() as $fact) {
29439ca88baSGreg Roach                    $facts->push($fact);
295eca4a663SGreg Roach                }
296eca4a663SGreg Roach            }
297eca4a663SGreg Roach            foreach ($facts as $event) {
298eca4a663SGreg Roach                // get the fact type
299eca4a663SGreg Roach                $fact = $event->getTag();
30022d65e5aSGreg Roach                if (!in_array($fact, self::NON_FACTS, true)) {
301eca4a663SGreg Roach                    // check for a date
302eca4a663SGreg Roach                    $date = $event->date();
303eca4a663SGreg Roach                    if ($date->isOK()) {
304eca4a663SGreg Roach                        $date     = new GregorianDate($date->minimumJulianDay());
305eca4a663SGreg Roach                        $baseyear = min($baseyear, $date->year);
306eca4a663SGreg Roach                        $topyear  = max($topyear, $date->year);
307eca4a663SGreg Roach
308eca4a663SGreg Roach                        if (!$individual->isDead()) {
309eca4a663SGreg Roach                            $topyear = max($topyear, (int) date('Y'));
310eca4a663SGreg Roach                        }
311eca4a663SGreg Roach
3128af3e5c1SGreg Roach                        $indifacts->push($event);
313eca4a663SGreg Roach                    }
314eca4a663SGreg Roach                }
315eca4a663SGreg Roach            }
316eca4a663SGreg Roach        }
317eca4a663SGreg Roach
3188af3e5c1SGreg Roach        // do not add the same fact twice (prevents marriages from being added multiple times)
3198af3e5c1SGreg Roach        $indifacts = $indifacts->unique();
3208af3e5c1SGreg Roach
321eca4a663SGreg Roach        if ($scale === 0) {
3228af3e5c1SGreg Roach            $scale = (int) (($topyear - $baseyear) / 20 * $indifacts->count() / 4);
323eca4a663SGreg Roach            if ($scale < 6) {
324eca4a663SGreg Roach                $scale = 6;
325eca4a663SGreg Roach            }
326eca4a663SGreg Roach        }
327eca4a663SGreg Roach        if ($scale < 2) {
328eca4a663SGreg Roach            $scale = 2;
329eca4a663SGreg Roach        }
330eca4a663SGreg Roach        $baseyear -= 5;
331eca4a663SGreg Roach        $topyear  += 5;
332eca4a663SGreg Roach
333580a4d11SGreg Roach        $indifacts = Fact::sortFacts($indifacts);
334eca4a663SGreg Roach
335eca4a663SGreg Roach        $html = view('modules/timeline-chart/chart', [
336eca4a663SGreg Roach            'baseyear'    => $baseyear,
337eca4a663SGreg Roach            'bheight'     => self::BHEIGHT,
338eca4a663SGreg Roach            'birthdays'   => $birthdays,
339eca4a663SGreg Roach            'birthmonths' => $birthmonths,
340eca4a663SGreg Roach            'birthyears'  => $birthyears,
341eca4a663SGreg Roach            'indifacts'   => $indifacts,
342eca4a663SGreg Roach            'individuals' => $individuals,
343eca4a663SGreg Roach            'placements'  => [],
344eca4a663SGreg Roach            'scale'       => $scale,
345eca4a663SGreg Roach            'topyear'     => $topyear,
346eca4a663SGreg Roach        ]);
347eca4a663SGreg Roach
3486ccdf4f0SGreg Roach        return response($html);
349eca4a663SGreg Roach    }
350168ff6f3Sric2016}
351