1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fig\Http\Message\RequestMethodInterface; 23use Fisharebest\Webtrees\Auth; 24use Fisharebest\Webtrees\Date\GregorianDate; 25use Fisharebest\Webtrees\Fact; 26use Fisharebest\Webtrees\GedcomRecord; 27use Fisharebest\Webtrees\I18N; 28use Fisharebest\Webtrees\Individual; 29use Fisharebest\Webtrees\Registry; 30use Fisharebest\Webtrees\Tree; 31use Fisharebest\Webtrees\Validator; 32use Illuminate\Support\Collection; 33use Psr\Http\Message\ResponseInterface; 34use Psr\Http\Message\ServerRequestInterface; 35use Psr\Http\Server\RequestHandlerInterface; 36 37use function redirect; 38use function route; 39 40/** 41 * Class TimelineChartModule 42 */ 43class TimelineChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 44{ 45 use ModuleChartTrait; 46 47 protected const ROUTE_URL = '/tree/{tree}/timeline-{scale}'; 48 49 // Defaults 50 protected const DEFAULT_SCALE = 10; 51 protected const DEFAULT_PARAMETERS = [ 52 'scale' => self::DEFAULT_SCALE, 53 ]; 54 55 // Limits 56 protected const MINIMUM_SCALE = 1; 57 protected const MAXIMUM_SCALE = 200; 58 59 // GEDCOM events that may have DATE data, but should not be displayed 60 protected const NON_FACTS = [ 61 'FAM:CHAN', 62 'INDI:BAPL', 63 'INDI:CHAN', 64 'INDI:ENDL', 65 'INDI:SLGC', 66 'INDI:SLGS', 67 'INDI:_TODO', 68 ]; 69 70 protected const BOX_HEIGHT = 30; 71 72 /** 73 * Initialization. 74 * 75 * @return void 76 */ 77 public function boot(): void 78 { 79 Registry::routeFactory()->routeMap() 80 ->get(static::class, static::ROUTE_URL, $this) 81 ->allows(RequestMethodInterface::METHOD_POST); 82 } 83 84 /** 85 * How should this module be identified in the control panel, etc.? 86 * 87 * @return string 88 */ 89 public function title(): string 90 { 91 /* I18N: Name of a module/chart */ 92 return I18N::translate('Timeline'); 93 } 94 95 /** 96 * A sentence describing what this module does. 97 * 98 * @return string 99 */ 100 public function description(): string 101 { 102 /* I18N: Description of the “TimelineChart” module */ 103 return I18N::translate('A timeline displaying individual events.'); 104 } 105 106 /** 107 * CSS class for the URL. 108 * 109 * @return string 110 */ 111 public function chartMenuClass(): string 112 { 113 return 'menu-chart-timeline'; 114 } 115 116 /** 117 * The URL for this chart. 118 * 119 * @param Individual $individual 120 * @param array<bool|int|string|array<string>|null> $parameters 121 * 122 * @return string 123 */ 124 public function chartUrl(Individual $individual, array $parameters = []): string 125 { 126 return route(static::class, [ 127 'tree' => $individual->tree()->name(), 128 'xrefs' => [$individual->xref()], 129 ] + $parameters + self::DEFAULT_PARAMETERS); 130 } 131 132 /** 133 * @param ServerRequestInterface $request 134 * 135 * @return ResponseInterface 136 */ 137 public function handle(ServerRequestInterface $request): ResponseInterface 138 { 139 $tree = Validator::attributes($request)->tree(); 140 $user = Validator::attributes($request)->user(); 141 $scale = Validator::attributes($request)->isBetween(self::MINIMUM_SCALE, self::MAXIMUM_SCALE)->integer('scale'); 142 $xrefs = Validator::queryParams($request)->array('xrefs'); 143 $ajax = Validator::queryParams($request)->boolean('ajax', false); 144 $xrefs = array_filter(array_unique($xrefs)); 145 146 // Convert POST requests into GET requests for pretty URLs. 147 if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 148 $xrefs[] = Validator::parsedBody($request)->isXref()->string('add', ''); 149 150 return redirect(route(static::class, [ 151 'tree' => $tree->name(), 152 'scale' => $scale, 153 'xrefs' => $xrefs, 154 ])); 155 } 156 157 Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 158 159 // Find the requested individuals. 160 $individuals = (new Collection($xrefs)) 161 ->uniqueStrict() 162 ->map(static function (string $xref) use ($tree): ?Individual { 163 return Registry::individualFactory()->make($xref, $tree); 164 }) 165 ->filter() 166 ->filter(GedcomRecord::accessFilter()); 167 168 // Generate URLs omitting each xref. 169 $remove_urls = []; 170 171 foreach ($individuals as $exclude) { 172 $xrefs_1 = $individuals 173 ->filter(static function (Individual $individual) use ($exclude): bool { 174 return $individual->xref() !== $exclude->xref(); 175 }) 176 ->map(static function (Individual $individual): string { 177 return $individual->xref(); 178 }); 179 180 $remove_urls[$exclude->xref()] = route(static::class, [ 181 'tree' => $tree->name(), 182 'scale' => $scale, 183 'xrefs' => $xrefs_1->all(), 184 ]); 185 } 186 187 $individuals = array_map(static function (string $xref) use ($tree): ?Individual { 188 return Registry::individualFactory()->make($xref, $tree); 189 }, $xrefs); 190 191 $individuals = array_filter($individuals, static function (?Individual $individual): bool { 192 return $individual instanceof Individual && $individual->canShow(); 193 }); 194 195 if ($ajax) { 196 $this->layout = 'layouts/ajax'; 197 198 return $this->chart($tree, $xrefs, $scale); 199 } 200 201 $reset_url = route(static::class, [ 202 'scale' => self::DEFAULT_SCALE, 203 'tree' => $tree->name(), 204 ]); 205 206 $zoom_in_url = route(static::class, [ 207 'scale' => min(self::MAXIMUM_SCALE, $scale + (int) ($scale * 0.4 + 1)), 208 'tree' => $tree->name(), 209 'xrefs' => $xrefs, 210 ]); 211 212 $zoom_out_url = route(static::class, [ 213 'scale' => max(self::MINIMUM_SCALE, $scale - (int) ($scale * 0.4 + 1)), 214 'tree' => $tree->name(), 215 'xrefs' => $xrefs, 216 ]); 217 218 $ajax_url = route(static::class, [ 219 'ajax' => true, 220 'scale' => $scale, 221 'tree' => $tree->name(), 222 'xrefs' => $xrefs, 223 ]); 224 225 return $this->viewResponse('modules/timeline-chart/page', [ 226 'ajax_url' => $ajax_url, 227 'individuals' => $individuals, 228 'module' => $this->name(), 229 'remove_urls' => $remove_urls, 230 'reset_url' => $reset_url, 231 'scale' => $scale, 232 'title' => $this->title(), 233 'tree' => $tree, 234 'zoom_in_url' => $zoom_in_url, 235 'zoom_out_url' => $zoom_out_url, 236 ]); 237 } 238 239 /** 240 * @param Tree $tree 241 * @param array<string> $xrefs 242 * @param int $scale 243 * 244 * @return ResponseInterface 245 */ 246 protected function chart(Tree $tree, array $xrefs, int $scale): ResponseInterface 247 { 248 /** @var Individual[] $individuals */ 249 $individuals = array_map(static function (string $xref) use ($tree): ?Individual { 250 return Registry::individualFactory()->make($xref, $tree); 251 }, $xrefs); 252 253 $individuals = array_filter($individuals, static function (?Individual $individual): bool { 254 return $individual instanceof Individual && $individual->canShow(); 255 }); 256 257 $baseyear = (int) date('Y'); 258 $topyear = 0; 259 $indifacts = new Collection(); 260 $birthyears = []; 261 $birthmonths = []; 262 $birthdays = []; 263 264 foreach ($individuals as $individual) { 265 $bdate = $individual->getBirthDate(); 266 if ($bdate->isOK()) { 267 $date = new GregorianDate($bdate->minimumJulianDay()); 268 269 $birthyears [$individual->xref()] = $date->year; 270 $birthmonths[$individual->xref()] = max(1, $date->month); 271 $birthdays [$individual->xref()] = max(1, $date->day); 272 } 273 // find all the fact information 274 $facts = $individual->facts(); 275 foreach ($individual->spouseFamilies() as $family) { 276 foreach ($family->facts() as $fact) { 277 $facts->push($fact); 278 } 279 } 280 281 foreach ($facts as $event) { 282 if (!in_array($event->tag(), self::NON_FACTS, true)) { 283 // check for a date 284 $date = $event->date(); 285 if ($date->isOK()) { 286 $date = new GregorianDate($date->minimumJulianDay()); 287 $baseyear = min($baseyear, $date->year); 288 $topyear = max($topyear, $date->year); 289 290 if (!$individual->isDead()) { 291 $topyear = max($topyear, (int) date('Y')); 292 } 293 294 $indifacts->push($event); 295 } 296 } 297 } 298 } 299 300 // do not add the same fact twice (prevents marriages from being added multiple times) 301 $indifacts = $indifacts->uniqueStrict(static function (Fact $fact): string { 302 return $fact->id(); 303 }); 304 305 if ($scale === 0) { 306 $scale = (int) (($topyear - $baseyear) / 20 * $indifacts->count() / 4); 307 if ($scale < 6) { 308 $scale = 6; 309 } 310 } 311 if ($scale < 2) { 312 $scale = 2; 313 } 314 $baseyear -= 5; 315 $topyear += 5; 316 317 $indifacts = Fact::sortFacts($indifacts); 318 319 $html = view('modules/timeline-chart/chart', [ 320 'baseyear' => $baseyear, 321 'bheight' => self::BOX_HEIGHT, 322 'birthdays' => $birthdays, 323 'birthmonths' => $birthmonths, 324 'birthyears' => $birthyears, 325 'indifacts' => $indifacts, 326 'individuals' => $individuals, 327 'placements' => [], 328 'scale' => $scale, 329 'topyear' => $topyear, 330 ]); 331 332 return response($html); 333 } 334} 335