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