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() 85*f7358520SGreg 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(), 13371378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 134168ff6f3Sric2016 } 135eca4a663SGreg Roach 136eca4a663SGreg Roach /** 1376ccdf4f0SGreg Roach * @param ServerRequestInterface $request 138eca4a663SGreg Roach * 1396ccdf4f0SGreg Roach * @return ResponseInterface 140eca4a663SGreg Roach */ 14171378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 142eca4a663SGreg Roach { 14357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 14457ab2231SGreg Roach $user = $request->getAttribute('user'); 14571378461SGreg Roach $scale = (int) $request->getAttribute('scale'); 14671378461SGreg Roach $xrefs = $request->getQueryParams()['xrefs'] ?? []; 1473cfcc809SGreg Roach $add = $request->getParsedBody()['add'] ?? ''; 14871378461SGreg Roach $ajax = $request->getQueryParams()['ajax'] ?? ''; 14957ab2231SGreg Roach 1509867b2f0SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 1519867b2f0SGreg Roach 15271378461SGreg Roach $scale = min($scale, self::MAXIMUM_SCALE); 15371378461SGreg Roach $scale = max($scale, self::MINIMUM_SCALE); 15471378461SGreg Roach 1553cfcc809SGreg Roach $xrefs[] = $add; 1563cfcc809SGreg Roach $xrefs = array_filter(array_unique($xrefs)); 1573cfcc809SGreg Roach 1583cfcc809SGreg Roach // Convert POST requests into GET requests for pretty URLs. 1593cfcc809SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 1603cfcc809SGreg Roach return redirect(route(self::ROUTE_NAME, [ 1613cfcc809SGreg Roach 'scale' => $scale, 1623cfcc809SGreg Roach 'tree' => $tree->name(), 1633cfcc809SGreg Roach 'xrefs' => $xrefs, 1643cfcc809SGreg Roach ])); 1653cfcc809SGreg Roach } 166eca4a663SGreg Roach 167eca4a663SGreg Roach // Find the requested individuals. 168eca4a663SGreg Roach $individuals = (new Collection($xrefs)) 169eca4a663SGreg Roach ->unique() 1700b5fd0a6SGreg Roach ->map(static function (string $xref) use ($tree): ?Individual { 171eca4a663SGreg Roach return Individual::getInstance($xref, $tree); 172eca4a663SGreg Roach }) 173eca4a663SGreg Roach ->filter() 174eca4a663SGreg Roach ->filter(GedcomRecord::accessFilter()); 175eca4a663SGreg Roach 176eca4a663SGreg Roach // Generate URLs omitting each xref. 177eca4a663SGreg Roach $remove_urls = []; 178eca4a663SGreg Roach 179eca4a663SGreg Roach foreach ($individuals as $exclude) { 180eca4a663SGreg Roach $xrefs_1 = $individuals 1810b5fd0a6SGreg Roach ->filter(static function (Individual $individual) use ($exclude): bool { 182eca4a663SGreg Roach return $individual->xref() !== $exclude->xref(); 183eca4a663SGreg Roach }) 1840b5fd0a6SGreg Roach ->map(static function (Individual $individual): string { 185eca4a663SGreg Roach return $individual->xref(); 186eca4a663SGreg Roach }); 187eca4a663SGreg Roach 18871378461SGreg Roach $remove_urls[$exclude->xref()] = route(self::ROUTE_NAME, [ 18971378461SGreg Roach 'tree' => $tree->name(), 190eca4a663SGreg Roach 'scale' => $scale, 191eca4a663SGreg Roach 'xrefs' => $xrefs_1->all(), 192eca4a663SGreg Roach ]); 193eca4a663SGreg Roach } 194eca4a663SGreg Roach 1950b5fd0a6SGreg Roach $individuals = array_map(static function (string $xref) use ($tree): ?Individual { 196eca4a663SGreg Roach return Individual::getInstance($xref, $tree); 197eca4a663SGreg Roach }, $xrefs); 198eca4a663SGreg Roach 1990b5fd0a6SGreg Roach $individuals = array_filter($individuals, static function (?Individual $individual): bool { 20025d7fe95SGreg Roach return $individual instanceof Individual && $individual->canShow(); 20125d7fe95SGreg Roach }); 20225d7fe95SGreg Roach 20371378461SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 20471378461SGreg Roach 2050b93976aSGreg Roach if ($ajax === '1') { 20671378461SGreg Roach $this->layout = 'layouts/ajax'; 20771378461SGreg Roach 208eca4a663SGreg Roach return $this->chart($tree, $xrefs, $scale); 209eca4a663SGreg Roach } 210eca4a663SGreg Roach 21171378461SGreg Roach $reset_url = route(self::ROUTE_NAME, [ 21271378461SGreg Roach 'scale' => self::DEFAULT_SCALE, 21371378461SGreg Roach 'tree' => $tree->name(), 21471378461SGreg Roach ]); 21571378461SGreg Roach 21671378461SGreg Roach $zoom_in_url = route(self::ROUTE_NAME, [ 21771378461SGreg Roach 'scale' => min(self::MAXIMUM_SCALE, $scale + (int) ($scale * 0.2 + 1)), 21871378461SGreg Roach 'tree' => $tree->name(), 21971378461SGreg Roach 'xrefs' => $xrefs, 22071378461SGreg Roach ]); 22171378461SGreg Roach 22271378461SGreg Roach $zoom_out_url = route(self::ROUTE_NAME, [ 22371378461SGreg Roach 'scale' => max(self::MINIMUM_SCALE, $scale - (int) ($scale * 0.2 + 1)), 22471378461SGreg Roach 'tree' => $tree->name(), 22571378461SGreg Roach 'xrefs' => $xrefs, 22671378461SGreg Roach ]); 22771378461SGreg Roach 22871378461SGreg Roach $ajax_url = route(self::ROUTE_NAME, [ 2299b5537c3SGreg Roach 'ajax' => true, 230eca4a663SGreg Roach 'scale' => $scale, 23171378461SGreg Roach 'tree' => $tree->name(), 232eca4a663SGreg Roach 'xrefs' => $xrefs, 233eca4a663SGreg Roach ]); 234eca4a663SGreg Roach 2359b5537c3SGreg Roach return $this->viewResponse('modules/timeline-chart/page', [ 236eca4a663SGreg Roach 'ajax_url' => $ajax_url, 237eca4a663SGreg Roach 'individuals' => $individuals, 23871378461SGreg Roach 'module' => $this->name(), 239eca4a663SGreg Roach 'remove_urls' => $remove_urls, 240eca4a663SGreg Roach 'reset_url' => $reset_url, 241eca4a663SGreg Roach 'scale' => $scale, 24271378461SGreg Roach 'title' => $this->title(), 243ef5d23f1SGreg Roach 'tree' => $tree, 244eca4a663SGreg Roach 'zoom_in_url' => $zoom_in_url, 245eca4a663SGreg Roach 'zoom_out_url' => $zoom_out_url, 246eca4a663SGreg Roach ]); 247eca4a663SGreg Roach } 248eca4a663SGreg Roach 249eca4a663SGreg Roach /** 250eca4a663SGreg Roach * @param Tree $tree 251eca4a663SGreg Roach * @param array $xrefs 252eca4a663SGreg Roach * @param int $scale 253eca4a663SGreg Roach * 2546ccdf4f0SGreg Roach * @return ResponseInterface 255eca4a663SGreg Roach */ 2566ccdf4f0SGreg Roach protected function chart(Tree $tree, array $xrefs, int $scale): ResponseInterface 257eca4a663SGreg Roach { 258eca4a663SGreg Roach /** @var Individual[] $individuals */ 2590b5fd0a6SGreg Roach $individuals = array_map(static function (string $xref) use ($tree): ?Individual { 260eca4a663SGreg Roach return Individual::getInstance($xref, $tree); 261eca4a663SGreg Roach }, $xrefs); 262eca4a663SGreg Roach 2630b5fd0a6SGreg Roach $individuals = array_filter($individuals, static function (?Individual $individual): bool { 26425d7fe95SGreg Roach return $individual instanceof Individual && $individual->canShow(); 265eca4a663SGreg Roach }); 266eca4a663SGreg Roach 267eca4a663SGreg Roach $baseyear = (int) date('Y'); 268eca4a663SGreg Roach $topyear = 0; 2698af3e5c1SGreg Roach $indifacts = new Collection(); 270eca4a663SGreg Roach $birthyears = []; 271eca4a663SGreg Roach $birthmonths = []; 272eca4a663SGreg Roach $birthdays = []; 273eca4a663SGreg Roach 274eca4a663SGreg Roach foreach ($individuals as $individual) { 275eca4a663SGreg Roach $bdate = $individual->getBirthDate(); 276eca4a663SGreg Roach if ($bdate->isOK()) { 277eca4a663SGreg Roach $date = new GregorianDate($bdate->minimumJulianDay()); 278eca4a663SGreg Roach 279eca4a663SGreg Roach $birthyears [$individual->xref()] = $date->year; 280eca4a663SGreg Roach $birthmonths[$individual->xref()] = max(1, $date->month); 281eca4a663SGreg Roach $birthdays [$individual->xref()] = max(1, $date->day); 282eca4a663SGreg Roach } 283eca4a663SGreg Roach // find all the fact information 284eca4a663SGreg Roach $facts = $individual->facts(); 28539ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 286eca4a663SGreg Roach foreach ($family->facts() as $fact) { 28739ca88baSGreg Roach $facts->push($fact); 288eca4a663SGreg Roach } 289eca4a663SGreg Roach } 290eca4a663SGreg Roach foreach ($facts as $event) { 291eca4a663SGreg Roach // get the fact type 292eca4a663SGreg Roach $fact = $event->getTag(); 29322d65e5aSGreg Roach if (!in_array($fact, self::NON_FACTS, true)) { 294eca4a663SGreg Roach // check for a date 295eca4a663SGreg Roach $date = $event->date(); 296eca4a663SGreg Roach if ($date->isOK()) { 297eca4a663SGreg Roach $date = new GregorianDate($date->minimumJulianDay()); 298eca4a663SGreg Roach $baseyear = min($baseyear, $date->year); 299eca4a663SGreg Roach $topyear = max($topyear, $date->year); 300eca4a663SGreg Roach 301eca4a663SGreg Roach if (!$individual->isDead()) { 302eca4a663SGreg Roach $topyear = max($topyear, (int) date('Y')); 303eca4a663SGreg Roach } 304eca4a663SGreg Roach 3058af3e5c1SGreg Roach $indifacts->push($event); 306eca4a663SGreg Roach } 307eca4a663SGreg Roach } 308eca4a663SGreg Roach } 309eca4a663SGreg Roach } 310eca4a663SGreg Roach 3118af3e5c1SGreg Roach // do not add the same fact twice (prevents marriages from being added multiple times) 3128af3e5c1SGreg Roach $indifacts = $indifacts->unique(); 3138af3e5c1SGreg Roach 314eca4a663SGreg Roach if ($scale === 0) { 3158af3e5c1SGreg Roach $scale = (int) (($topyear - $baseyear) / 20 * $indifacts->count() / 4); 316eca4a663SGreg Roach if ($scale < 6) { 317eca4a663SGreg Roach $scale = 6; 318eca4a663SGreg Roach } 319eca4a663SGreg Roach } 320eca4a663SGreg Roach if ($scale < 2) { 321eca4a663SGreg Roach $scale = 2; 322eca4a663SGreg Roach } 323eca4a663SGreg Roach $baseyear -= 5; 324eca4a663SGreg Roach $topyear += 5; 325eca4a663SGreg Roach 326580a4d11SGreg Roach $indifacts = Fact::sortFacts($indifacts); 327eca4a663SGreg Roach 328eca4a663SGreg Roach $html = view('modules/timeline-chart/chart', [ 329eca4a663SGreg Roach 'baseyear' => $baseyear, 330eca4a663SGreg Roach 'bheight' => self::BHEIGHT, 331eca4a663SGreg Roach 'birthdays' => $birthdays, 332eca4a663SGreg Roach 'birthmonths' => $birthmonths, 333eca4a663SGreg Roach 'birthyears' => $birthyears, 334eca4a663SGreg Roach 'indifacts' => $indifacts, 335eca4a663SGreg Roach 'individuals' => $individuals, 336eca4a663SGreg Roach 'placements' => [], 337eca4a663SGreg Roach 'scale' => $scale, 338eca4a663SGreg Roach 'topyear' => $topyear, 339eca4a663SGreg Roach ]); 340eca4a663SGreg Roach 3416ccdf4f0SGreg Roach return response($html); 342eca4a663SGreg Roach } 343168ff6f3Sric2016} 344