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(), 133*36826bd4SGreg 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'] ?? []; 1503cfcc809SGreg Roach $add = $request->getParsedBody()['add'] ?? ''; 15171378461SGreg Roach $ajax = $request->getQueryParams()['ajax'] ?? ''; 15257ab2231SGreg Roach 1539867b2f0SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 1549867b2f0SGreg Roach 15571378461SGreg Roach $scale = min($scale, self::MAXIMUM_SCALE); 15671378461SGreg Roach $scale = max($scale, self::MINIMUM_SCALE); 15771378461SGreg Roach 1583cfcc809SGreg Roach $xrefs[] = $add; 1593cfcc809SGreg Roach $xrefs = array_filter(array_unique($xrefs)); 1603cfcc809SGreg Roach 1613cfcc809SGreg Roach // Convert POST requests into GET requests for pretty URLs. 1623cfcc809SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 1633cfcc809SGreg Roach return redirect(route(self::ROUTE_NAME, [ 1643cfcc809SGreg Roach 'scale' => $scale, 1653cfcc809SGreg Roach 'tree' => $tree->name(), 1663cfcc809SGreg Roach 'xrefs' => $xrefs, 1673cfcc809SGreg Roach ])); 1683cfcc809SGreg Roach } 169eca4a663SGreg Roach 170eca4a663SGreg Roach // Find the requested individuals. 171eca4a663SGreg Roach $individuals = (new Collection($xrefs)) 172eca4a663SGreg Roach ->unique() 1730b5fd0a6SGreg Roach ->map(static function (string $xref) use ($tree): ?Individual { 174eca4a663SGreg Roach return Individual::getInstance($xref, $tree); 175eca4a663SGreg Roach }) 176eca4a663SGreg Roach ->filter() 177eca4a663SGreg Roach ->filter(GedcomRecord::accessFilter()); 178eca4a663SGreg Roach 179eca4a663SGreg Roach // Generate URLs omitting each xref. 180eca4a663SGreg Roach $remove_urls = []; 181eca4a663SGreg Roach 182eca4a663SGreg Roach foreach ($individuals as $exclude) { 183eca4a663SGreg Roach $xrefs_1 = $individuals 1840b5fd0a6SGreg Roach ->filter(static function (Individual $individual) use ($exclude): bool { 185eca4a663SGreg Roach return $individual->xref() !== $exclude->xref(); 186eca4a663SGreg Roach }) 1870b5fd0a6SGreg Roach ->map(static function (Individual $individual): string { 188eca4a663SGreg Roach return $individual->xref(); 189eca4a663SGreg Roach }); 190eca4a663SGreg Roach 19171378461SGreg Roach $remove_urls[$exclude->xref()] = route(self::ROUTE_NAME, [ 19271378461SGreg Roach 'tree' => $tree->name(), 193eca4a663SGreg Roach 'scale' => $scale, 194eca4a663SGreg Roach 'xrefs' => $xrefs_1->all(), 195eca4a663SGreg Roach ]); 196eca4a663SGreg Roach } 197eca4a663SGreg Roach 1980b5fd0a6SGreg Roach $individuals = array_map(static function (string $xref) use ($tree): ?Individual { 199eca4a663SGreg Roach return Individual::getInstance($xref, $tree); 200eca4a663SGreg Roach }, $xrefs); 201eca4a663SGreg Roach 2020b5fd0a6SGreg Roach $individuals = array_filter($individuals, static function (?Individual $individual): bool { 20325d7fe95SGreg Roach return $individual instanceof Individual && $individual->canShow(); 20425d7fe95SGreg Roach }); 20525d7fe95SGreg Roach 20671378461SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 20771378461SGreg Roach 2080b93976aSGreg Roach if ($ajax === '1') { 20971378461SGreg Roach $this->layout = 'layouts/ajax'; 21071378461SGreg Roach 211eca4a663SGreg Roach return $this->chart($tree, $xrefs, $scale); 212eca4a663SGreg Roach } 213eca4a663SGreg Roach 21471378461SGreg Roach $reset_url = route(self::ROUTE_NAME, [ 21571378461SGreg Roach 'scale' => self::DEFAULT_SCALE, 21671378461SGreg Roach 'tree' => $tree->name(), 21771378461SGreg Roach ]); 21871378461SGreg Roach 21971378461SGreg Roach $zoom_in_url = route(self::ROUTE_NAME, [ 22071378461SGreg Roach 'scale' => min(self::MAXIMUM_SCALE, $scale + (int) ($scale * 0.2 + 1)), 22171378461SGreg Roach 'tree' => $tree->name(), 22271378461SGreg Roach 'xrefs' => $xrefs, 22371378461SGreg Roach ]); 22471378461SGreg Roach 22571378461SGreg Roach $zoom_out_url = route(self::ROUTE_NAME, [ 22671378461SGreg Roach 'scale' => max(self::MINIMUM_SCALE, $scale - (int) ($scale * 0.2 + 1)), 22771378461SGreg Roach 'tree' => $tree->name(), 22871378461SGreg Roach 'xrefs' => $xrefs, 22971378461SGreg Roach ]); 23071378461SGreg Roach 23171378461SGreg Roach $ajax_url = route(self::ROUTE_NAME, [ 2329b5537c3SGreg Roach 'ajax' => true, 233eca4a663SGreg Roach 'scale' => $scale, 23471378461SGreg Roach 'tree' => $tree->name(), 235eca4a663SGreg Roach 'xrefs' => $xrefs, 236eca4a663SGreg Roach ]); 237eca4a663SGreg Roach 2389b5537c3SGreg Roach return $this->viewResponse('modules/timeline-chart/page', [ 239eca4a663SGreg Roach 'ajax_url' => $ajax_url, 240eca4a663SGreg Roach 'individuals' => $individuals, 24171378461SGreg Roach 'module' => $this->name(), 242eca4a663SGreg Roach 'remove_urls' => $remove_urls, 243eca4a663SGreg Roach 'reset_url' => $reset_url, 244eca4a663SGreg Roach 'scale' => $scale, 24571378461SGreg Roach 'title' => $this->title(), 246ef5d23f1SGreg Roach 'tree' => $tree, 247eca4a663SGreg Roach 'zoom_in_url' => $zoom_in_url, 248eca4a663SGreg Roach 'zoom_out_url' => $zoom_out_url, 249eca4a663SGreg Roach ]); 250eca4a663SGreg Roach } 251eca4a663SGreg Roach 252eca4a663SGreg Roach /** 253eca4a663SGreg Roach * @param Tree $tree 254eca4a663SGreg Roach * @param array $xrefs 255eca4a663SGreg Roach * @param int $scale 256eca4a663SGreg Roach * 2576ccdf4f0SGreg Roach * @return ResponseInterface 258eca4a663SGreg Roach */ 2596ccdf4f0SGreg Roach protected function chart(Tree $tree, array $xrefs, int $scale): ResponseInterface 260eca4a663SGreg Roach { 261eca4a663SGreg Roach /** @var Individual[] $individuals */ 2620b5fd0a6SGreg Roach $individuals = array_map(static function (string $xref) use ($tree): ?Individual { 263eca4a663SGreg Roach return Individual::getInstance($xref, $tree); 264eca4a663SGreg Roach }, $xrefs); 265eca4a663SGreg Roach 2660b5fd0a6SGreg Roach $individuals = array_filter($individuals, static function (?Individual $individual): bool { 26725d7fe95SGreg Roach return $individual instanceof Individual && $individual->canShow(); 268eca4a663SGreg Roach }); 269eca4a663SGreg Roach 270eca4a663SGreg Roach $baseyear = (int) date('Y'); 271eca4a663SGreg Roach $topyear = 0; 2728af3e5c1SGreg Roach $indifacts = new Collection(); 273eca4a663SGreg Roach $birthyears = []; 274eca4a663SGreg Roach $birthmonths = []; 275eca4a663SGreg Roach $birthdays = []; 276eca4a663SGreg Roach 277eca4a663SGreg Roach foreach ($individuals as $individual) { 278eca4a663SGreg Roach $bdate = $individual->getBirthDate(); 279eca4a663SGreg Roach if ($bdate->isOK()) { 280eca4a663SGreg Roach $date = new GregorianDate($bdate->minimumJulianDay()); 281eca4a663SGreg Roach 282eca4a663SGreg Roach $birthyears [$individual->xref()] = $date->year; 283eca4a663SGreg Roach $birthmonths[$individual->xref()] = max(1, $date->month); 284eca4a663SGreg Roach $birthdays [$individual->xref()] = max(1, $date->day); 285eca4a663SGreg Roach } 286eca4a663SGreg Roach // find all the fact information 287eca4a663SGreg Roach $facts = $individual->facts(); 28839ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 289eca4a663SGreg Roach foreach ($family->facts() as $fact) { 29039ca88baSGreg Roach $facts->push($fact); 291eca4a663SGreg Roach } 292eca4a663SGreg Roach } 293eca4a663SGreg Roach foreach ($facts as $event) { 294eca4a663SGreg Roach // get the fact type 295eca4a663SGreg Roach $fact = $event->getTag(); 29622d65e5aSGreg Roach if (!in_array($fact, self::NON_FACTS, true)) { 297eca4a663SGreg Roach // check for a date 298eca4a663SGreg Roach $date = $event->date(); 299eca4a663SGreg Roach if ($date->isOK()) { 300eca4a663SGreg Roach $date = new GregorianDate($date->minimumJulianDay()); 301eca4a663SGreg Roach $baseyear = min($baseyear, $date->year); 302eca4a663SGreg Roach $topyear = max($topyear, $date->year); 303eca4a663SGreg Roach 304eca4a663SGreg Roach if (!$individual->isDead()) { 305eca4a663SGreg Roach $topyear = max($topyear, (int) date('Y')); 306eca4a663SGreg Roach } 307eca4a663SGreg Roach 3088af3e5c1SGreg Roach $indifacts->push($event); 309eca4a663SGreg Roach } 310eca4a663SGreg Roach } 311eca4a663SGreg Roach } 312eca4a663SGreg Roach } 313eca4a663SGreg Roach 3148af3e5c1SGreg Roach // do not add the same fact twice (prevents marriages from being added multiple times) 3158af3e5c1SGreg Roach $indifacts = $indifacts->unique(); 3168af3e5c1SGreg Roach 317eca4a663SGreg Roach if ($scale === 0) { 3188af3e5c1SGreg Roach $scale = (int) (($topyear - $baseyear) / 20 * $indifacts->count() / 4); 319eca4a663SGreg Roach if ($scale < 6) { 320eca4a663SGreg Roach $scale = 6; 321eca4a663SGreg Roach } 322eca4a663SGreg Roach } 323eca4a663SGreg Roach if ($scale < 2) { 324eca4a663SGreg Roach $scale = 2; 325eca4a663SGreg Roach } 326eca4a663SGreg Roach $baseyear -= 5; 327eca4a663SGreg Roach $topyear += 5; 328eca4a663SGreg Roach 329580a4d11SGreg Roach $indifacts = Fact::sortFacts($indifacts); 330eca4a663SGreg Roach 331eca4a663SGreg Roach $html = view('modules/timeline-chart/chart', [ 332eca4a663SGreg Roach 'baseyear' => $baseyear, 333eca4a663SGreg Roach 'bheight' => self::BHEIGHT, 334eca4a663SGreg Roach 'birthdays' => $birthdays, 335eca4a663SGreg Roach 'birthmonths' => $birthmonths, 336eca4a663SGreg Roach 'birthyears' => $birthyears, 337eca4a663SGreg Roach 'indifacts' => $indifacts, 338eca4a663SGreg Roach 'individuals' => $individuals, 339eca4a663SGreg Roach 'placements' => [], 340eca4a663SGreg Roach 'scale' => $scale, 341eca4a663SGreg Roach 'topyear' => $topyear, 342eca4a663SGreg Roach ]); 343eca4a663SGreg Roach 3446ccdf4f0SGreg Roach return response($html); 345eca4a663SGreg Roach } 346168ff6f3Sric2016} 347