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\Functions\Functions; 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 labelled on tabs, menus, 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) { 158 return Individual::getInstance($xref, $tree); 159 }, $xrefs); 160 161 if ($ajax) { 162 return $this->chart($tree, $xrefs, $scale); 163 } 164 165 $ajax_url = route('module', [ 166 'ajax' => true, 167 'module' => $this->name(), 168 'action' => 'Chart', 169 'ged' => $tree->name(), 170 'scale' => $scale, 171 'xrefs' => $xrefs, 172 ]); 173 174 $reset_url = route('module', [ 175 'module' => $this->name(), 176 'action' => 'Chart', 177 'ged' => $tree->name(), 178 ]); 179 180 $zoom_in_url = route('module', [ 181 'module' => $this->name(), 182 'action' => 'Chart', 183 'ged' => $tree->name(), 184 'scale' => min(self::SCALE_MAX, $scale + (int) ($scale * 0.2 + 1)), 185 'xrefs' => $xrefs, 186 ]); 187 188 $zoom_out_url = route('module', [ 189 'module' => $this->name(), 190 'action' => 'Chart', 191 'ged' => $tree->name(), 192 'scale' => max(self::SCALE_MIN, $scale - (int) ($scale * 0.2 + 1)), 193 'xrefs' => $xrefs, 194 ]); 195 196 return $this->viewResponse('modules/timeline-chart/page', [ 197 'ajax_url' => $ajax_url, 198 'individuals' => $individuals, 199 'module_name' => $this->name(), 200 'remove_urls' => $remove_urls, 201 'reset_url' => $reset_url, 202 'title' => $this->title(), 203 'scale' => $scale, 204 'zoom_in_url' => $zoom_in_url, 205 'zoom_out_url' => $zoom_out_url, 206 ]); 207 } 208 209 /** 210 * @param Tree $tree 211 * @param array $xrefs 212 * @param int $scale 213 * 214 * @return Response 215 */ 216 protected function chart(Tree $tree, array $xrefs, int $scale): Response 217 { 218 $xrefs = array_unique($xrefs); 219 220 /** @var Individual[] $individuals */ 221 $individuals = array_map(function (string $xref) use ($tree) { 222 return Individual::getInstance($xref, $tree); 223 }, $xrefs); 224 225 $individuals = array_filter($individuals, function (Individual $individual = null): bool { 226 return $individual !== null && $individual->canShow(); 227 }); 228 229 $baseyear = (int) date('Y'); 230 $topyear = 0; 231 $indifacts = []; 232 $birthyears = []; 233 $birthmonths = []; 234 $birthdays = []; 235 236 foreach ($individuals as $individual) { 237 $bdate = $individual->getBirthDate(); 238 if ($bdate->isOK()) { 239 $date = new GregorianDate($bdate->minimumJulianDay()); 240 241 $birthyears [$individual->xref()] = $date->year; 242 $birthmonths[$individual->xref()] = max(1, $date->month); 243 $birthdays [$individual->xref()] = max(1, $date->day); 244 } 245 // find all the fact information 246 $facts = $individual->facts(); 247 foreach ($individual->getSpouseFamilies() as $family) { 248 foreach ($family->facts() as $fact) { 249 $facts[] = $fact; 250 } 251 } 252 foreach ($facts as $event) { 253 // get the fact type 254 $fact = $event->getTag(); 255 if (!in_array($fact, self::NON_FACTS)) { 256 // check for a date 257 $date = $event->date(); 258 if ($date->isOK()) { 259 $date = new GregorianDate($date->minimumJulianDay()); 260 $baseyear = min($baseyear, $date->year); 261 $topyear = max($topyear, $date->year); 262 263 if (!$individual->isDead()) { 264 $topyear = max($topyear, (int) date('Y')); 265 } 266 267 // do not add the same fact twice (prevents marriages from being added multiple times) 268 if (!in_array($event, $indifacts, true)) { 269 $indifacts[] = $event; 270 } 271 } 272 } 273 } 274 } 275 276 if ($scale === 0) { 277 $scale = (int) (($topyear - $baseyear) / 20 * count($indifacts) / 4); 278 if ($scale < 6) { 279 $scale = 6; 280 } 281 } 282 if ($scale < 2) { 283 $scale = 2; 284 } 285 $baseyear -= 5; 286 $topyear += 5; 287 288 Functions::sortFacts($indifacts); 289 290 $html = view('modules/timeline-chart/chart', [ 291 'baseyear' => $baseyear, 292 'bheight' => self::BHEIGHT, 293 'birthdays' => $birthdays, 294 'birthmonths' => $birthmonths, 295 'birthyears' => $birthyears, 296 'indifacts' => $indifacts, 297 'individuals' => $individuals, 298 'placements' => [], 299 'scale' => $scale, 300 'topyear' => $topyear, 301 ]); 302 303 return new Response($html); 304 } 305} 306