xref: /webtrees/app/Module/TimelineChartModule.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
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 */
17*fcfa147eSGreg 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
363cfcc809SGreg Roachuse function redirect;
373cfcc809SGreg Roachuse function route;
383cfcc809SGreg Roach
39168ff6f3Sric2016/**
40168ff6f3Sric2016 * Class TimelineChartModule
41168ff6f3Sric2016 */
4271378461SGreg Roachclass TimelineChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
43c1010edaSGreg Roach{
4449a243cbSGreg Roach    use ModuleChartTrait;
4549a243cbSGreg Roach
4671378461SGreg Roach    private const ROUTE_NAME = 'timeline-chart';
4771378461SGreg Roach    private const ROUTE_URL  = '/tree/{tree}/timeline-{scale}';
4871378461SGreg Roach
4971378461SGreg Roach    // Defaults
5071378461SGreg Roach    protected const DEFAULT_SCALE      = 10;
5171378461SGreg Roach    protected const DEFAULT_PARAMETERS = [
5271378461SGreg Roach        'scale' => self::DEFAULT_SCALE,
5371378461SGreg Roach    ];
5471378461SGreg Roach
5571378461SGreg Roach    // Limits
5671378461SGreg Roach    protected const MINIMUM_SCALE = 1;
5771378461SGreg Roach    protected const MAXIMUM_SCALE = 200;
58eca4a663SGreg Roach
59eca4a663SGreg Roach    // GEDCOM events that may have DATE data, but should not be displayed
60eca4a663SGreg Roach    protected const NON_FACTS = [
61eca4a663SGreg Roach        'BAPL',
62eca4a663SGreg Roach        'ENDL',
63eca4a663SGreg Roach        'SLGC',
64eca4a663SGreg Roach        'SLGS',
65eca4a663SGreg Roach        '_TODO',
66eca4a663SGreg Roach        'CHAN',
67eca4a663SGreg Roach    ];
683cfcc809SGreg Roach    protected const BHEIGHT   = 30;
693cfcc809SGreg Roach
703cfcc809SGreg Roach    // Box height
71eca4a663SGreg Roach
7271378461SGreg Roach    /**
7371378461SGreg Roach     * Initialization.
7471378461SGreg Roach     *
7571378461SGreg Roach     * @param RouterContainer $router_container
7671378461SGreg Roach     */
7771378461SGreg Roach    public function boot(RouterContainer $router_container)
7871378461SGreg Roach    {
7971378461SGreg Roach        $router_container->getMap()
8071378461SGreg Roach            ->get(self::ROUTE_NAME, self::ROUTE_URL, self::class)
8171378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST);
8271378461SGreg Roach    }
8371378461SGreg Roach
84168ff6f3Sric2016    /**
850cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
86168ff6f3Sric2016     *
87168ff6f3Sric2016     * @return string
88168ff6f3Sric2016     */
8949a243cbSGreg Roach    public function title(): string
90c1010edaSGreg Roach    {
91bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
92bbb76c12SGreg Roach        return I18N::translate('Timeline');
93168ff6f3Sric2016    }
94168ff6f3Sric2016
95168ff6f3Sric2016    /**
96168ff6f3Sric2016     * A sentence describing what this module does.
97168ff6f3Sric2016     *
98168ff6f3Sric2016     * @return string
99168ff6f3Sric2016     */
10049a243cbSGreg Roach    public function description(): string
101c1010edaSGreg Roach    {
102bbb76c12SGreg Roach        /* I18N: Description of the “TimelineChart” module */
103bbb76c12SGreg Roach        return I18N::translate('A timeline displaying individual events.');
104168ff6f3Sric2016    }
105168ff6f3Sric2016
106168ff6f3Sric2016    /**
107377a2979SGreg Roach     * CSS class for the URL.
108377a2979SGreg Roach     *
109377a2979SGreg Roach     * @return string
110377a2979SGreg Roach     */
111377a2979SGreg Roach    public function chartMenuClass(): string
112377a2979SGreg Roach    {
113377a2979SGreg Roach        return 'menu-chart-timeline';
114377a2979SGreg Roach    }
115377a2979SGreg Roach
116377a2979SGreg Roach    /**
117e6562982SGreg Roach     * The URL for this chart.
118168ff6f3Sric2016     *
11960bc3e3fSGreg Roach     * @param Individual $individual
120e6562982SGreg Roach     * @param string[]   $parameters
12160bc3e3fSGreg Roach     *
122e6562982SGreg Roach     * @return string
123168ff6f3Sric2016     */
124e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
125c1010edaSGreg Roach    {
12671378461SGreg Roach        return route(self::ROUTE_NAME, [
12771378461SGreg Roach                'tree' => $individual->tree()->name(),
12871378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
129168ff6f3Sric2016    }
130eca4a663SGreg Roach
131eca4a663SGreg Roach    /**
1326ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
133eca4a663SGreg Roach     *
1346ccdf4f0SGreg Roach     * @return ResponseInterface
135eca4a663SGreg Roach     */
13671378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
137eca4a663SGreg Roach    {
13857ab2231SGreg Roach        $tree  = $request->getAttribute('tree');
13957ab2231SGreg Roach        $user  = $request->getAttribute('user');
14071378461SGreg Roach        $scale = (int) $request->getAttribute('scale');
14171378461SGreg Roach        $xrefs = $request->getQueryParams()['xrefs'] ?? [];
1423cfcc809SGreg Roach        $add   = $request->getParsedBody()['add'] ?? '';
14371378461SGreg Roach        $ajax  = $request->getQueryParams()['ajax'] ?? '';
14457ab2231SGreg Roach
1459867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
1469867b2f0SGreg Roach
14771378461SGreg Roach        $scale = min($scale, self::MAXIMUM_SCALE);
14871378461SGreg Roach        $scale = max($scale, self::MINIMUM_SCALE);
14971378461SGreg Roach
1503cfcc809SGreg Roach        $xrefs[] = $add;
1513cfcc809SGreg Roach        $xrefs = array_filter(array_unique($xrefs));
1523cfcc809SGreg Roach
1533cfcc809SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
1543cfcc809SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
1553cfcc809SGreg Roach            return redirect(route(self::ROUTE_NAME, [
1563cfcc809SGreg Roach                'scale' => $scale,
1573cfcc809SGreg Roach                'tree'  => $tree->name(),
1583cfcc809SGreg Roach                'xrefs' => $xrefs,
1593cfcc809SGreg Roach            ]));
1603cfcc809SGreg Roach        }
161eca4a663SGreg Roach
162eca4a663SGreg Roach        // Find the requested individuals.
163eca4a663SGreg Roach        $individuals = (new Collection($xrefs))
164eca4a663SGreg Roach            ->unique()
1650b5fd0a6SGreg Roach            ->map(static function (string $xref) use ($tree): ?Individual {
166eca4a663SGreg Roach                return Individual::getInstance($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
18371378461SGreg Roach            $remove_urls[$exclude->xref()] = route(self::ROUTE_NAME, [
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 {
191eca4a663SGreg Roach            return Individual::getInstance($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
19871378461SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
19971378461SGreg Roach
2000b93976aSGreg Roach        if ($ajax === '1') {
20171378461SGreg Roach            $this->layout = 'layouts/ajax';
20271378461SGreg Roach
203eca4a663SGreg Roach            return $this->chart($tree, $xrefs, $scale);
204eca4a663SGreg Roach        }
205eca4a663SGreg Roach
20671378461SGreg Roach        $reset_url = route(self::ROUTE_NAME, [
20771378461SGreg Roach            'scale' => self::DEFAULT_SCALE,
20871378461SGreg Roach            'tree'  => $tree->name(),
20971378461SGreg Roach        ]);
21071378461SGreg Roach
21171378461SGreg Roach        $zoom_in_url = route(self::ROUTE_NAME, [
21271378461SGreg Roach            'scale' => min(self::MAXIMUM_SCALE, $scale + (int) ($scale * 0.2 + 1)),
21371378461SGreg Roach            'tree'  => $tree->name(),
21471378461SGreg Roach            'xrefs' => $xrefs,
21571378461SGreg Roach        ]);
21671378461SGreg Roach
21771378461SGreg Roach        $zoom_out_url = route(self::ROUTE_NAME, [
21871378461SGreg Roach            'scale' => max(self::MINIMUM_SCALE, $scale - (int) ($scale * 0.2 + 1)),
21971378461SGreg Roach            'tree'  => $tree->name(),
22071378461SGreg Roach            'xrefs' => $xrefs,
22171378461SGreg Roach        ]);
22271378461SGreg Roach
22371378461SGreg Roach        $ajax_url = route(self::ROUTE_NAME, [
2249b5537c3SGreg Roach            'ajax'  => true,
225eca4a663SGreg Roach            'scale' => $scale,
22671378461SGreg Roach            'tree'  => $tree->name(),
227eca4a663SGreg Roach            'xrefs' => $xrefs,
228eca4a663SGreg Roach        ]);
229eca4a663SGreg Roach
2309b5537c3SGreg Roach        return $this->viewResponse('modules/timeline-chart/page', [
231eca4a663SGreg Roach            'ajax_url'     => $ajax_url,
232eca4a663SGreg Roach            'individuals'  => $individuals,
23371378461SGreg Roach            'module'       => $this->name(),
234eca4a663SGreg Roach            'remove_urls'  => $remove_urls,
235eca4a663SGreg Roach            'reset_url'    => $reset_url,
236eca4a663SGreg Roach            'scale'        => $scale,
23771378461SGreg Roach            'title'        => $this->title(),
238eca4a663SGreg Roach            'zoom_in_url'  => $zoom_in_url,
239eca4a663SGreg Roach            'zoom_out_url' => $zoom_out_url,
240eca4a663SGreg Roach        ]);
241eca4a663SGreg Roach    }
242eca4a663SGreg Roach
243eca4a663SGreg Roach    /**
244eca4a663SGreg Roach     * @param Tree  $tree
245eca4a663SGreg Roach     * @param array $xrefs
246eca4a663SGreg Roach     * @param int   $scale
247eca4a663SGreg Roach     *
2486ccdf4f0SGreg Roach     * @return ResponseInterface
249eca4a663SGreg Roach     */
2506ccdf4f0SGreg Roach    protected function chart(Tree $tree, array $xrefs, int $scale): ResponseInterface
251eca4a663SGreg Roach    {
252eca4a663SGreg Roach        /** @var Individual[] $individuals */
2530b5fd0a6SGreg Roach        $individuals = array_map(static function (string $xref) use ($tree): ?Individual {
254eca4a663SGreg Roach            return Individual::getInstance($xref, $tree);
255eca4a663SGreg Roach        }, $xrefs);
256eca4a663SGreg Roach
2570b5fd0a6SGreg Roach        $individuals = array_filter($individuals, static function (?Individual $individual): bool {
25825d7fe95SGreg Roach            return $individual instanceof Individual && $individual->canShow();
259eca4a663SGreg Roach        });
260eca4a663SGreg Roach
261eca4a663SGreg Roach        $baseyear    = (int) date('Y');
262eca4a663SGreg Roach        $topyear     = 0;
2638af3e5c1SGreg Roach        $indifacts   = new Collection();
264eca4a663SGreg Roach        $birthyears  = [];
265eca4a663SGreg Roach        $birthmonths = [];
266eca4a663SGreg Roach        $birthdays   = [];
267eca4a663SGreg Roach
268eca4a663SGreg Roach        foreach ($individuals as $individual) {
269eca4a663SGreg Roach            $bdate = $individual->getBirthDate();
270eca4a663SGreg Roach            if ($bdate->isOK()) {
271eca4a663SGreg Roach                $date = new GregorianDate($bdate->minimumJulianDay());
272eca4a663SGreg Roach
273eca4a663SGreg Roach                $birthyears [$individual->xref()] = $date->year;
274eca4a663SGreg Roach                $birthmonths[$individual->xref()] = max(1, $date->month);
275eca4a663SGreg Roach                $birthdays  [$individual->xref()] = max(1, $date->day);
276eca4a663SGreg Roach            }
277eca4a663SGreg Roach            // find all the fact information
278eca4a663SGreg Roach            $facts = $individual->facts();
27939ca88baSGreg Roach            foreach ($individual->spouseFamilies() as $family) {
280eca4a663SGreg Roach                foreach ($family->facts() as $fact) {
28139ca88baSGreg Roach                    $facts->push($fact);
282eca4a663SGreg Roach                }
283eca4a663SGreg Roach            }
284eca4a663SGreg Roach            foreach ($facts as $event) {
285eca4a663SGreg Roach                // get the fact type
286eca4a663SGreg Roach                $fact = $event->getTag();
28722d65e5aSGreg Roach                if (!in_array($fact, self::NON_FACTS, true)) {
288eca4a663SGreg Roach                    // check for a date
289eca4a663SGreg Roach                    $date = $event->date();
290eca4a663SGreg Roach                    if ($date->isOK()) {
291eca4a663SGreg Roach                        $date     = new GregorianDate($date->minimumJulianDay());
292eca4a663SGreg Roach                        $baseyear = min($baseyear, $date->year);
293eca4a663SGreg Roach                        $topyear  = max($topyear, $date->year);
294eca4a663SGreg Roach
295eca4a663SGreg Roach                        if (!$individual->isDead()) {
296eca4a663SGreg Roach                            $topyear = max($topyear, (int) date('Y'));
297eca4a663SGreg Roach                        }
298eca4a663SGreg Roach
2998af3e5c1SGreg Roach                        $indifacts->push($event);
300eca4a663SGreg Roach                    }
301eca4a663SGreg Roach                }
302eca4a663SGreg Roach            }
303eca4a663SGreg Roach        }
304eca4a663SGreg Roach
3058af3e5c1SGreg Roach        // do not add the same fact twice (prevents marriages from being added multiple times)
3068af3e5c1SGreg Roach        $indifacts = $indifacts->unique();
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,
324eca4a663SGreg Roach            'bheight'     => self::BHEIGHT,
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