1852ede8cSGreg Roach<?php 2852ede8cSGreg Roach 3852ede8cSGreg Roach/** 4852ede8cSGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team 6852ede8cSGreg Roach * This program is free software: you can redistribute it and/or modify 7852ede8cSGreg Roach * it under the terms of the GNU General Public License as published by 8852ede8cSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9852ede8cSGreg Roach * (at your option) any later version. 10852ede8cSGreg Roach * This program is distributed in the hope that it will be useful, 11852ede8cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12852ede8cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13852ede8cSGreg Roach * GNU General Public License for more details. 14852ede8cSGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16852ede8cSGreg Roach */ 17852ede8cSGreg Roach 18852ede8cSGreg Roachdeclare(strict_types=1); 19852ede8cSGreg Roach 20852ede8cSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21852ede8cSGreg Roach 22e5d858f5SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 23054771e9SGreg Roachuse Fisharebest\Webtrees\Age; 24852ede8cSGreg Roachuse Fisharebest\Webtrees\Auth; 25852ede8cSGreg Roachuse Fisharebest\Webtrees\Date; 26852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 27852ede8cSGreg Roachuse Fisharebest\Webtrees\I18N; 28852ede8cSGreg Roachuse Fisharebest\Webtrees\Individual; 29852ede8cSGreg Roachuse Fisharebest\Webtrees\Media; 30852ede8cSGreg Roachuse Fisharebest\Webtrees\MediaFile; 31853f2b8aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleShareInterface; 32852ede8cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleSidebarInterface; 33852ede8cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleTabInterface; 346b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 35852ede8cSGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService; 36852ede8cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 37852ede8cSGreg Roachuse Fisharebest\Webtrees\Services\UserService; 38852ede8cSGreg Roachuse Fisharebest\Webtrees\Tree; 39852ede8cSGreg Roachuse Illuminate\Support\Collection; 40852ede8cSGreg Roachuse Psr\Http\Message\ResponseInterface; 41852ede8cSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 42852ede8cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 43852ede8cSGreg Roach 442406e0e0SGreg Roachuse function array_map; 45852ede8cSGreg Roachuse function assert; 462406e0e0SGreg Roachuse function date; 47852ede8cSGreg Roachuse function e; 48852ede8cSGreg Roachuse function explode; 492406e0e0SGreg Roachuse function implode; 50852ede8cSGreg Roachuse function is_string; 51852ede8cSGreg Roachuse function redirect; 52852ede8cSGreg Roachuse function route; 532406e0e0SGreg Roachuse function strtoupper; 542406e0e0SGreg Roach 55852ede8cSGreg Roach/** 56852ede8cSGreg Roach * Show an individual's page. 57852ede8cSGreg Roach */ 58852ede8cSGreg Roachclass IndividualPage implements RequestHandlerInterface 59852ede8cSGreg Roach{ 60852ede8cSGreg Roach use ViewResponseTrait; 61852ede8cSGreg Roach 62c4943cffSGreg Roach private ClipboardService $clipboard_service; 63852ede8cSGreg Roach 64c4943cffSGreg Roach private ModuleService $module_service; 65852ede8cSGreg Roach 66c4943cffSGreg Roach private UserService $user_service; 67852ede8cSGreg Roach 68852ede8cSGreg Roach /** 69aba707cdSGreg Roach * IndividualPage constructor. 70852ede8cSGreg Roach * 71852ede8cSGreg Roach * @param ClipboardService $clipboard_service 72852ede8cSGreg Roach * @param ModuleService $module_service 73852ede8cSGreg Roach * @param UserService $user_service 74852ede8cSGreg Roach */ 75828e3b20SGreg Roach public function __construct( 76828e3b20SGreg Roach ClipboardService $clipboard_service, 77828e3b20SGreg Roach ModuleService $module_service, 78828e3b20SGreg Roach UserService $user_service 79828e3b20SGreg Roach ) { 80852ede8cSGreg Roach $this->clipboard_service = $clipboard_service; 81852ede8cSGreg Roach $this->module_service = $module_service; 82852ede8cSGreg Roach $this->user_service = $user_service; 83852ede8cSGreg Roach } 84852ede8cSGreg Roach 85852ede8cSGreg Roach /** 86852ede8cSGreg Roach * @param ServerRequestInterface $request 87852ede8cSGreg Roach * 88852ede8cSGreg Roach * @return ResponseInterface 89852ede8cSGreg Roach */ 90852ede8cSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 91852ede8cSGreg Roach { 92852ede8cSGreg Roach $tree = $request->getAttribute('tree'); 93852ede8cSGreg Roach assert($tree instanceof Tree); 94852ede8cSGreg Roach 95852ede8cSGreg Roach $xref = $request->getAttribute('xref'); 96852ede8cSGreg Roach assert(is_string($xref)); 97852ede8cSGreg Roach 986b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 99852ede8cSGreg Roach $individual = Auth::checkIndividualAccess($individual); 100852ede8cSGreg Roach 101852ede8cSGreg Roach // Redirect to correct xref/slug 102194b0938SGreg Roach $slug = Registry::slugFactory()->make($individual); 103194b0938SGreg Roach 104194b0938SGreg Roach if ($individual->xref() !== $xref || $request->getAttribute('slug') !== $slug) { 105e5d858f5SGreg Roach return redirect($individual->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 106852ede8cSGreg Roach } 107852ede8cSGreg Roach 108852ede8cSGreg Roach // What images are linked to this individual 109852ede8cSGreg Roach $individual_media = new Collection(); 110852ede8cSGreg Roach foreach ($individual->facts(['OBJE']) as $fact) { 111852ede8cSGreg Roach $media_object = $fact->target(); 112852ede8cSGreg Roach if ($media_object instanceof Media) { 113852ede8cSGreg Roach $media_file = $media_object->firstImageFile(); 114852ede8cSGreg Roach if ($media_file instanceof MediaFile) { 115852ede8cSGreg Roach $individual_media->add($media_file); 116852ede8cSGreg Roach } 117852ede8cSGreg Roach } 118852ede8cSGreg Roach } 119852ede8cSGreg Roach 120852ede8cSGreg Roach // If this individual is linked to a user account, show the link 121852ede8cSGreg Roach if (Auth::isAdmin()) { 122852ede8cSGreg Roach $users = $this->user_service->findByIndividual($individual); 123828e3b20SGreg Roach } else { 124828e3b20SGreg Roach $users = new Collection(); 125852ede8cSGreg Roach } 126852ede8cSGreg Roach 127828e3b20SGreg Roach $shares = $this->module_service 128828e3b20SGreg Roach ->findByInterface(ModuleShareInterface::class) 129853f2b8aSGreg Roach ->map(fn (ModuleShareInterface $module) => $module->share($individual)) 130853f2b8aSGreg Roach ->filter(); 131853f2b8aSGreg Roach 132852ede8cSGreg Roach return $this->viewResponse('individual-page', [ 133a5fd6d7cSGreg Roach 'age' => $this->ageString($individual), 13469cdf014SGreg Roach 'clipboard_facts' => $this->clipboard_service->pastableFacts($individual), 135852ede8cSGreg Roach 'individual_media' => $individual_media, 1362406e0e0SGreg Roach 'meta_description' => $this->metaDescription($individual), 137852ede8cSGreg Roach 'meta_robots' => 'index,follow', 1380f5fd22fSGreg Roach 'record' => $individual, 139853f2b8aSGreg Roach 'shares' => $shares, 140852ede8cSGreg Roach 'sidebars' => $this->getSidebars($individual), 141852ede8cSGreg Roach 'tabs' => $this->getTabs($individual), 142852ede8cSGreg Roach 'significant' => $this->significant($individual), 1435e6816beSGreg Roach 'title' => $individual->fullName() . ' ' . $individual->lifespan(), 144852ede8cSGreg Roach 'tree' => $tree, 145828e3b20SGreg Roach 'users' => $users, 146852ede8cSGreg Roach ]); 147852ede8cSGreg Roach } 148852ede8cSGreg Roach 149852ede8cSGreg Roach /** 1502406e0e0SGreg Roach * @param Individual $individual 1512406e0e0SGreg Roach * 1522406e0e0SGreg Roach * @return string 1532406e0e0SGreg Roach */ 154a5fd6d7cSGreg Roach private function ageString(Individual $individual): string 155a5fd6d7cSGreg Roach { 156a5fd6d7cSGreg Roach if ($individual->isDead()) { 157a5fd6d7cSGreg Roach // If dead, show age at death 158a5fd6d7cSGreg Roach $age = (string) new Age($individual->getBirthDate(), $individual->getDeathDate()); 159a5fd6d7cSGreg Roach 160a5fd6d7cSGreg Roach if ($age === '') { 161a5fd6d7cSGreg Roach return ''; 162a5fd6d7cSGreg Roach } 163a5fd6d7cSGreg Roach 164a5fd6d7cSGreg Roach switch ($individual->sex()) { 165a5fd6d7cSGreg Roach case 'M': 166a5fd6d7cSGreg Roach /* I18N: The age of an individual at a given date */ 167a5fd6d7cSGreg Roach return I18N::translateContext('Male', '(aged %s)', $age); 168a5fd6d7cSGreg Roach case 'F': 169a5fd6d7cSGreg Roach /* I18N: The age of an individual at a given date */ 170a5fd6d7cSGreg Roach return I18N::translateContext('Female', '(aged %s)', $age); 171a5fd6d7cSGreg Roach default: 172a5fd6d7cSGreg Roach /* I18N: The age of an individual at a given date */ 173a5fd6d7cSGreg Roach return I18N::translate('(aged %s)', $age); 174a5fd6d7cSGreg Roach } 175a5fd6d7cSGreg Roach } 176a5fd6d7cSGreg Roach 177a5fd6d7cSGreg Roach // If living, show age today 178a5fd6d7cSGreg Roach $today = new Date(strtoupper(date('d M Y'))); 179a5fd6d7cSGreg Roach $age = (string) new Age($individual->getBirthDate(), $today); 180a5fd6d7cSGreg Roach 181a5fd6d7cSGreg Roach if ($age === '') { 182a5fd6d7cSGreg Roach return ''; 183a5fd6d7cSGreg Roach } 184a5fd6d7cSGreg Roach 185a5fd6d7cSGreg Roach /* I18N: The current age of a living individual */ 186a5fd6d7cSGreg Roach return I18N::translate('(age %s)', $age); 187a5fd6d7cSGreg Roach } 188a5fd6d7cSGreg Roach 189a5fd6d7cSGreg Roach /** 190a5fd6d7cSGreg Roach * @param Individual $individual 191a5fd6d7cSGreg Roach * 192a5fd6d7cSGreg Roach * @return string 193a5fd6d7cSGreg Roach */ 1942406e0e0SGreg Roach private function metaDescription(Individual $individual): string 1952406e0e0SGreg Roach { 1962406e0e0SGreg Roach $meta_facts = []; 1972406e0e0SGreg Roach 1982406e0e0SGreg Roach $birth_date = $individual->getBirthDate(); 1992406e0e0SGreg Roach $birth_place = $individual->getBirthPlace(); 2002406e0e0SGreg Roach 2012406e0e0SGreg Roach if ($birth_date->isOK() || $birth_place->id() !== 0) { 2022406e0e0SGreg Roach $meta_facts[] = I18N::translate('Birth') . ' ' . 2032406e0e0SGreg Roach $birth_date->display(false, null, false) . ' ' . 2042406e0e0SGreg Roach $birth_place->placeName(); 2052406e0e0SGreg Roach } 2062406e0e0SGreg Roach 2072406e0e0SGreg Roach $death_date = $individual->getDeathDate(); 2082406e0e0SGreg Roach $death_place = $individual->getDeathPlace(); 2092406e0e0SGreg Roach 2102406e0e0SGreg Roach if ($death_date->isOK() || $death_place->id() !== 0) { 2112406e0e0SGreg Roach $meta_facts[] = I18N::translate('Death') . ' ' . 2122406e0e0SGreg Roach $death_date->display(false, null, false) . ' ' . 2132406e0e0SGreg Roach $death_place->placeName(); 2142406e0e0SGreg Roach } 2152406e0e0SGreg Roach 2162406e0e0SGreg Roach foreach ($individual->childFamilies() as $family) { 2172406e0e0SGreg Roach $meta_facts[] = I18N::translate('Parents') . ' ' . $family->fullName(); 2182406e0e0SGreg Roach } 2192406e0e0SGreg Roach 2202406e0e0SGreg Roach foreach ($individual->spouseFamilies() as $family) { 2212406e0e0SGreg Roach $spouse = $family->spouse($individual); 2222406e0e0SGreg Roach if ($spouse instanceof Individual) { 2232406e0e0SGreg Roach $meta_facts[] = I18N::translate('Spouse') . ' ' . $spouse->fullName(); 2242406e0e0SGreg Roach } 2252406e0e0SGreg Roach 2262406e0e0SGreg Roach $child_names = $family->children()->map(static function (Individual $individual): string { 2272406e0e0SGreg Roach return e($individual->getAllNames()[0]['givn']); 2282406e0e0SGreg Roach })->implode(', '); 2292406e0e0SGreg Roach 2302406e0e0SGreg Roach 2312406e0e0SGreg Roach if ($child_names !== '') { 2322406e0e0SGreg Roach $meta_facts[] = I18N::translate('Children') . ' ' . $child_names; 2332406e0e0SGreg Roach } 2342406e0e0SGreg Roach } 2352406e0e0SGreg Roach 2362406e0e0SGreg Roach $meta_facts = array_map('strip_tags', $meta_facts); 2372406e0e0SGreg Roach $meta_facts = array_map('trim', $meta_facts); 2382406e0e0SGreg Roach 2392406e0e0SGreg Roach return implode(', ', $meta_facts); 2402406e0e0SGreg Roach } 2412406e0e0SGreg Roach 2422406e0e0SGreg Roach /** 243852ede8cSGreg Roach * Which tabs should we show on this individual's page. 244852ede8cSGreg Roach * We don't show empty tabs. 245852ede8cSGreg Roach * 246852ede8cSGreg Roach * @param Individual $individual 247852ede8cSGreg Roach * 248b5c8fd7eSGreg Roach * @return Collection<ModuleSidebarInterface> 249852ede8cSGreg Roach */ 250852ede8cSGreg Roach public function getSidebars(Individual $individual): Collection 251852ede8cSGreg Roach { 252828e3b20SGreg Roach return $this->module_service 253828e3b20SGreg Roach ->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user()) 254852ede8cSGreg Roach ->filter(static function (ModuleSidebarInterface $sidebar) use ($individual): bool { 255852ede8cSGreg Roach return $sidebar->hasSidebarContent($individual); 256852ede8cSGreg Roach }); 257852ede8cSGreg Roach } 258852ede8cSGreg Roach 259852ede8cSGreg Roach /** 260852ede8cSGreg Roach * Which tabs should we show on this individual's page. 261852ede8cSGreg Roach * We don't show empty tabs. 262852ede8cSGreg Roach * 263852ede8cSGreg Roach * @param Individual $individual 264852ede8cSGreg Roach * 265b5c8fd7eSGreg Roach * @return Collection<ModuleTabInterface> 266852ede8cSGreg Roach */ 267852ede8cSGreg Roach public function getTabs(Individual $individual): Collection 268852ede8cSGreg Roach { 269828e3b20SGreg Roach return $this->module_service 270828e3b20SGreg Roach ->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user()) 271852ede8cSGreg Roach ->filter(static function (ModuleTabInterface $tab) use ($individual): bool { 272852ede8cSGreg Roach return $tab->hasTabContent($individual); 273852ede8cSGreg Roach }); 274852ede8cSGreg Roach } 275852ede8cSGreg Roach 276852ede8cSGreg Roach /** 277852ede8cSGreg Roach * What are the significant elements of this page? 278852ede8cSGreg Roach * The layout will need them to generate URLs for charts and reports. 279852ede8cSGreg Roach * 280852ede8cSGreg Roach * @param Individual $individual 281852ede8cSGreg Roach * 282*ac701fbdSGreg Roach * @return object 283852ede8cSGreg Roach */ 284*ac701fbdSGreg Roach private function significant(Individual $individual): object 285852ede8cSGreg Roach { 286852ede8cSGreg Roach [$surname] = explode(',', $individual->sortName()); 287852ede8cSGreg Roach 288852ede8cSGreg Roach $family = $individual->childFamilies()->merge($individual->spouseFamilies())->first(); 289852ede8cSGreg Roach 290852ede8cSGreg Roach return (object) [ 291852ede8cSGreg Roach 'family' => $family, 292852ede8cSGreg Roach 'individual' => $individual, 293852ede8cSGreg Roach 'surname' => $surname, 294852ede8cSGreg Roach ]; 295852ede8cSGreg Roach } 296852ede8cSGreg Roach} 297