1168ff6f3Sric2016<?php 23976b470SGreg Roach 3168ff6f3Sric2016/** 4168ff6f3Sric2016 * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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 Fig\Http\Message\RequestMethodInterface; 239867b2f0SGreg Roachuse Fisharebest\Webtrees\Auth; 24eca4a663SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate; 25580a4d11SGreg Roachuse Fisharebest\Webtrees\Fact; 26eca4a663SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 27168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 28168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 2909482a55SGreg Roachuse Fisharebest\Webtrees\Registry; 30eca4a663SGreg Roachuse Fisharebest\Webtrees\Tree; 31b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 32eca4a663SGreg Roachuse Illuminate\Support\Collection; 336ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 346ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3571378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 36168ff6f3Sric2016 3710e06497SGreg Roachuse function in_array; 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 4872f04adfSGreg Roach protected const ROUTE_URL = '/tree/{tree}/timeline-{scale}'; 4971378461SGreg Roach 5071378461SGreg Roach // Defaults 5171378461SGreg Roach protected const DEFAULT_SCALE = 10; 5271378461SGreg Roach protected const DEFAULT_PARAMETERS = [ 5371378461SGreg Roach 'scale' => self::DEFAULT_SCALE, 5471378461SGreg Roach ]; 5571378461SGreg Roach 5671378461SGreg Roach // Limits 5771378461SGreg Roach protected const MINIMUM_SCALE = 1; 5871378461SGreg Roach protected const MAXIMUM_SCALE = 200; 59eca4a663SGreg Roach 60eca4a663SGreg Roach // GEDCOM events that may have DATE data, but should not be displayed 61eca4a663SGreg Roach protected const NON_FACTS = [ 62e1052c25SGreg Roach 'FAM:CHAN', 63e1052c25SGreg Roach 'INDI:BAPL', 64e1052c25SGreg Roach 'INDI:CHAN', 65e1052c25SGreg Roach 'INDI:ENDL', 66e1052c25SGreg Roach 'INDI:SLGC', 67e1052c25SGreg Roach 'INDI:SLGS', 68e1052c25SGreg Roach 'INDI:_TODO', 69eca4a663SGreg Roach ]; 703cfcc809SGreg Roach 7115228353SGreg Roach protected const BOX_HEIGHT = 30; 72eca4a663SGreg Roach 7371378461SGreg Roach /** 7471378461SGreg Roach * Initialization. 7571378461SGreg Roach * 769e18e23bSGreg Roach * @return void 7771378461SGreg Roach */ 789e18e23bSGreg Roach public function boot(): void 7971378461SGreg Roach { 80158900c2SGreg Roach Registry::routeFactory()->routeMap() 8172f04adfSGreg Roach ->get(static::class, static::ROUTE_URL, $this) 8271378461SGreg Roach ->allows(RequestMethodInterface::METHOD_POST); 8371378461SGreg Roach } 8471378461SGreg Roach 85168ff6f3Sric2016 /** 860cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 87168ff6f3Sric2016 * 88168ff6f3Sric2016 * @return string 89168ff6f3Sric2016 */ 9049a243cbSGreg Roach public function title(): string 91c1010edaSGreg Roach { 92bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 93bbb76c12SGreg Roach return I18N::translate('Timeline'); 94168ff6f3Sric2016 } 95168ff6f3Sric2016 9649a243cbSGreg Roach public function description(): string 97c1010edaSGreg Roach { 98bbb76c12SGreg Roach /* I18N: Description of the “TimelineChart” module */ 99bbb76c12SGreg Roach return I18N::translate('A timeline displaying individual events.'); 100168ff6f3Sric2016 } 101168ff6f3Sric2016 102168ff6f3Sric2016 /** 103377a2979SGreg Roach * CSS class for the URL. 104377a2979SGreg Roach * 105377a2979SGreg Roach * @return string 106377a2979SGreg Roach */ 107377a2979SGreg Roach public function chartMenuClass(): string 108377a2979SGreg Roach { 109377a2979SGreg Roach return 'menu-chart-timeline'; 110377a2979SGreg Roach } 111377a2979SGreg Roach 112377a2979SGreg Roach /** 113e6562982SGreg Roach * The URL for this chart. 114168ff6f3Sric2016 * 11560bc3e3fSGreg Roach * @param Individual $individual 11676d39c55SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters 11760bc3e3fSGreg Roach * 118e6562982SGreg Roach * @return string 119168ff6f3Sric2016 */ 120e6562982SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 121c1010edaSGreg Roach { 12272f04adfSGreg Roach return route(static::class, [ 12371378461SGreg Roach 'tree' => $individual->tree()->name(), 12436826bd4SGreg Roach 'xrefs' => [$individual->xref()], 12571378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 126168ff6f3Sric2016 } 127eca4a663SGreg Roach 128eca4a663SGreg Roach /** 1296ccdf4f0SGreg Roach * @param ServerRequestInterface $request 130eca4a663SGreg Roach * 1316ccdf4f0SGreg Roach * @return ResponseInterface 132eca4a663SGreg Roach */ 13371378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 134eca4a663SGreg Roach { 135b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 136b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 137b55cbc6bSGreg Roach $scale = Validator::attributes($request)->isBetween(self::MINIMUM_SCALE, self::MAXIMUM_SCALE)->integer('scale'); 138b55cbc6bSGreg Roach $xrefs = Validator::queryParams($request)->array('xrefs'); 139b55cbc6bSGreg Roach $ajax = Validator::queryParams($request)->boolean('ajax', false); 1403cfcc809SGreg Roach $xrefs = array_filter(array_unique($xrefs)); 1413cfcc809SGreg Roach 1423cfcc809SGreg Roach // Convert POST requests into GET requests for pretty URLs. 1433cfcc809SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 144158900c2SGreg Roach $xrefs[] = Validator::parsedBody($request)->isXref()->string('add', ''); 145158900c2SGreg Roach 14672f04adfSGreg Roach return redirect(route(static::class, [ 1473cfcc809SGreg Roach 'tree' => $tree->name(), 148b55cbc6bSGreg Roach 'scale' => $scale, 1493cfcc809SGreg Roach 'xrefs' => $xrefs, 1503cfcc809SGreg Roach ])); 1513cfcc809SGreg Roach } 152eca4a663SGreg Roach 153b55cbc6bSGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 154b55cbc6bSGreg Roach 155eca4a663SGreg Roach // Find the requested individuals. 156eca4a663SGreg Roach $individuals = (new Collection($xrefs)) 1578c627a69SGreg Roach ->uniqueStrict() 158*1ff45046SGreg Roach ->map(static fn (string $xref): Individual|null => Registry::individualFactory()->make($xref, $tree)) 159eca4a663SGreg Roach ->filter() 160eca4a663SGreg Roach ->filter(GedcomRecord::accessFilter()); 161eca4a663SGreg Roach 162eca4a663SGreg Roach // Generate URLs omitting each xref. 163eca4a663SGreg Roach $remove_urls = []; 164eca4a663SGreg Roach 165eca4a663SGreg Roach foreach ($individuals as $exclude) { 166eca4a663SGreg Roach $xrefs_1 = $individuals 167f25fc0f9SGreg Roach ->filter(static fn (Individual $individual): bool => $individual->xref() !== $exclude->xref()) 168f25fc0f9SGreg Roach ->map(static fn (Individual $individual): string => $individual->xref()); 169eca4a663SGreg Roach 17072f04adfSGreg Roach $remove_urls[$exclude->xref()] = route(static::class, [ 17171378461SGreg Roach 'tree' => $tree->name(), 172eca4a663SGreg Roach 'scale' => $scale, 173eca4a663SGreg Roach 'xrefs' => $xrefs_1->all(), 174eca4a663SGreg Roach ]); 175eca4a663SGreg Roach } 176eca4a663SGreg Roach 177*1ff45046SGreg Roach $individuals = array_map(static fn (string $xref): Individual|null => Registry::individualFactory()->make($xref, $tree), $xrefs); 178eca4a663SGreg Roach 179*1ff45046SGreg Roach $individuals = array_filter($individuals, static fn (Individual|null $individual): bool => $individual instanceof Individual && $individual->canShow()); 18025d7fe95SGreg Roach 181b55cbc6bSGreg Roach if ($ajax) { 18271378461SGreg Roach $this->layout = 'layouts/ajax'; 18371378461SGreg Roach 184eca4a663SGreg Roach return $this->chart($tree, $xrefs, $scale); 185eca4a663SGreg Roach } 186eca4a663SGreg Roach 18772f04adfSGreg Roach $reset_url = route(static::class, [ 18871378461SGreg Roach 'scale' => self::DEFAULT_SCALE, 18971378461SGreg Roach 'tree' => $tree->name(), 19071378461SGreg Roach ]); 19171378461SGreg Roach 19272f04adfSGreg Roach $zoom_in_url = route(static::class, [ 19315228353SGreg Roach 'scale' => min(self::MAXIMUM_SCALE, $scale + (int) ($scale * 0.4 + 1)), 19471378461SGreg Roach 'tree' => $tree->name(), 19571378461SGreg Roach 'xrefs' => $xrefs, 19671378461SGreg Roach ]); 19771378461SGreg Roach 19872f04adfSGreg Roach $zoom_out_url = route(static::class, [ 19915228353SGreg Roach 'scale' => max(self::MINIMUM_SCALE, $scale - (int) ($scale * 0.4 + 1)), 20071378461SGreg Roach 'tree' => $tree->name(), 20171378461SGreg Roach 'xrefs' => $xrefs, 20271378461SGreg Roach ]); 20371378461SGreg Roach 20472f04adfSGreg Roach $ajax_url = route(static::class, [ 2059b5537c3SGreg Roach 'ajax' => true, 206eca4a663SGreg Roach 'scale' => $scale, 20771378461SGreg Roach 'tree' => $tree->name(), 208eca4a663SGreg Roach 'xrefs' => $xrefs, 209eca4a663SGreg Roach ]); 210eca4a663SGreg Roach 2119b5537c3SGreg Roach return $this->viewResponse('modules/timeline-chart/page', [ 212eca4a663SGreg Roach 'ajax_url' => $ajax_url, 213eca4a663SGreg Roach 'individuals' => $individuals, 21471378461SGreg Roach 'module' => $this->name(), 215eca4a663SGreg Roach 'remove_urls' => $remove_urls, 216eca4a663SGreg Roach 'reset_url' => $reset_url, 217eca4a663SGreg Roach 'scale' => $scale, 21871378461SGreg Roach 'title' => $this->title(), 219ef5d23f1SGreg Roach 'tree' => $tree, 220eca4a663SGreg Roach 'zoom_in_url' => $zoom_in_url, 221eca4a663SGreg Roach 'zoom_out_url' => $zoom_out_url, 222eca4a663SGreg Roach ]); 223eca4a663SGreg Roach } 224eca4a663SGreg Roach 225eca4a663SGreg Roach /** 226eca4a663SGreg Roach * @param Tree $tree 22709482a55SGreg Roach * @param array<string> $xrefs 228eca4a663SGreg Roach * @param int $scale 229eca4a663SGreg Roach * 2306ccdf4f0SGreg Roach * @return ResponseInterface 231eca4a663SGreg Roach */ 2326ccdf4f0SGreg Roach protected function chart(Tree $tree, array $xrefs, int $scale): ResponseInterface 233eca4a663SGreg Roach { 234eca4a663SGreg Roach /** @var Individual[] $individuals */ 235*1ff45046SGreg Roach $individuals = array_map(static fn (string $xref): Individual|null => Registry::individualFactory()->make($xref, $tree), $xrefs); 236eca4a663SGreg Roach 237*1ff45046SGreg Roach $individuals = array_filter($individuals, static fn (Individual|null $individual): bool => $individual instanceof Individual && $individual->canShow()); 238eca4a663SGreg Roach 239eca4a663SGreg Roach $baseyear = (int) date('Y'); 240eca4a663SGreg Roach $topyear = 0; 2418af3e5c1SGreg Roach $indifacts = new Collection(); 242eca4a663SGreg Roach $birthyears = []; 243eca4a663SGreg Roach $birthmonths = []; 244eca4a663SGreg Roach $birthdays = []; 245eca4a663SGreg Roach 246eca4a663SGreg Roach foreach ($individuals as $individual) { 247eca4a663SGreg Roach $bdate = $individual->getBirthDate(); 248eca4a663SGreg Roach if ($bdate->isOK()) { 249eca4a663SGreg Roach $date = new GregorianDate($bdate->minimumJulianDay()); 250eca4a663SGreg Roach 251eca4a663SGreg Roach $birthyears [$individual->xref()] = $date->year; 252eca4a663SGreg Roach $birthmonths[$individual->xref()] = max(1, $date->month); 253eca4a663SGreg Roach $birthdays [$individual->xref()] = max(1, $date->day); 254eca4a663SGreg Roach } 255eca4a663SGreg Roach // find all the fact information 256eca4a663SGreg Roach $facts = $individual->facts(); 25739ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 258eca4a663SGreg Roach foreach ($family->facts() as $fact) { 25939ca88baSGreg Roach $facts->push($fact); 260eca4a663SGreg Roach } 261eca4a663SGreg Roach } 262e1052c25SGreg Roach 263eca4a663SGreg Roach foreach ($facts as $event) { 264e1052c25SGreg Roach if (!in_array($event->tag(), self::NON_FACTS, true)) { 265eca4a663SGreg Roach // check for a date 266eca4a663SGreg Roach $date = $event->date(); 267eca4a663SGreg Roach if ($date->isOK()) { 268eca4a663SGreg Roach $date = new GregorianDate($date->minimumJulianDay()); 269eca4a663SGreg Roach $baseyear = min($baseyear, $date->year); 270eca4a663SGreg Roach $topyear = max($topyear, $date->year); 271eca4a663SGreg Roach 272eca4a663SGreg Roach if (!$individual->isDead()) { 273eca4a663SGreg Roach $topyear = max($topyear, (int) date('Y')); 274eca4a663SGreg Roach } 275eca4a663SGreg Roach 2768af3e5c1SGreg Roach $indifacts->push($event); 277eca4a663SGreg Roach } 278eca4a663SGreg Roach } 279eca4a663SGreg Roach } 280eca4a663SGreg Roach } 281eca4a663SGreg Roach 2828af3e5c1SGreg Roach // do not add the same fact twice (prevents marriages from being added multiple times) 283f25fc0f9SGreg Roach $indifacts = $indifacts->uniqueStrict(static fn (Fact $fact): string => $fact->id()); 2848af3e5c1SGreg Roach 285eca4a663SGreg Roach if ($scale === 0) { 2868af3e5c1SGreg Roach $scale = (int) (($topyear - $baseyear) / 20 * $indifacts->count() / 4); 287eca4a663SGreg Roach if ($scale < 6) { 288eca4a663SGreg Roach $scale = 6; 289eca4a663SGreg Roach } 290eca4a663SGreg Roach } 291eca4a663SGreg Roach if ($scale < 2) { 292eca4a663SGreg Roach $scale = 2; 293eca4a663SGreg Roach } 294eca4a663SGreg Roach $baseyear -= 5; 295eca4a663SGreg Roach $topyear += 5; 296eca4a663SGreg Roach 297580a4d11SGreg Roach $indifacts = Fact::sortFacts($indifacts); 298eca4a663SGreg Roach 299eca4a663SGreg Roach $html = view('modules/timeline-chart/chart', [ 300eca4a663SGreg Roach 'baseyear' => $baseyear, 30115228353SGreg Roach 'bheight' => self::BOX_HEIGHT, 302eca4a663SGreg Roach 'birthdays' => $birthdays, 303eca4a663SGreg Roach 'birthmonths' => $birthmonths, 304eca4a663SGreg Roach 'birthyears' => $birthyears, 305eca4a663SGreg Roach 'indifacts' => $indifacts, 306eca4a663SGreg Roach 'individuals' => $individuals, 307eca4a663SGreg Roach 'placements' => [], 308eca4a663SGreg Roach 'scale' => $scale, 309eca4a663SGreg Roach 'topyear' => $topyear, 310eca4a663SGreg Roach ]); 311eca4a663SGreg Roach 3126ccdf4f0SGreg Roach return response($html); 313eca4a663SGreg Roach } 314168ff6f3Sric2016} 315