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\Fact; 27852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 28852ede8cSGreg Roachuse Fisharebest\Webtrees\I18N; 29852ede8cSGreg Roachuse Fisharebest\Webtrees\Individual; 30852ede8cSGreg Roachuse Fisharebest\Webtrees\Media; 31852ede8cSGreg Roachuse Fisharebest\Webtrees\MediaFile; 32853f2b8aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleShareInterface; 33852ede8cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleSidebarInterface; 34852ede8cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleTabInterface; 356b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 36852ede8cSGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService; 37852ede8cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 38852ede8cSGreg Roachuse Fisharebest\Webtrees\Services\UserService; 39852ede8cSGreg Roachuse Fisharebest\Webtrees\Tree; 40852ede8cSGreg Roachuse Illuminate\Support\Collection; 41852ede8cSGreg Roachuse Psr\Http\Message\ResponseInterface; 42852ede8cSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 43852ede8cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 44852ede8cSGreg Roachuse stdClass; 45852ede8cSGreg Roach 462406e0e0SGreg Roachuse function array_map; 47852ede8cSGreg Roachuse function assert; 482406e0e0SGreg Roachuse function date; 49852ede8cSGreg Roachuse function e; 50852ede8cSGreg Roachuse function explode; 512406e0e0SGreg Roachuse function implode; 52852ede8cSGreg Roachuse function is_string; 53852ede8cSGreg Roachuse function redirect; 54852ede8cSGreg Roachuse function route; 552406e0e0SGreg Roachuse function strtoupper; 562406e0e0SGreg Roachuse function view; 572406e0e0SGreg Roach 58852ede8cSGreg Roach/** 59852ede8cSGreg Roach * Show an individual's page. 60852ede8cSGreg Roach */ 61852ede8cSGreg Roachclass IndividualPage implements RequestHandlerInterface 62852ede8cSGreg Roach{ 63852ede8cSGreg Roach use ViewResponseTrait; 64852ede8cSGreg Roach 65*c4943cffSGreg Roach private ClipboardService $clipboard_service; 66852ede8cSGreg Roach 67*c4943cffSGreg Roach private ModuleService $module_service; 68852ede8cSGreg Roach 69*c4943cffSGreg Roach private UserService $user_service; 70852ede8cSGreg Roach 71852ede8cSGreg Roach /** 72aba707cdSGreg Roach * IndividualPage constructor. 73852ede8cSGreg Roach * 74852ede8cSGreg Roach * @param ClipboardService $clipboard_service 75852ede8cSGreg Roach * @param ModuleService $module_service 76852ede8cSGreg Roach * @param UserService $user_service 77852ede8cSGreg Roach */ 78852ede8cSGreg Roach public function __construct(ClipboardService $clipboard_service, ModuleService $module_service, UserService $user_service) 79852ede8cSGreg 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 120abf37626SGreg Roach $name_records = $individual->facts(['NAME'])->map(static function (Fact $fact): string { 121abf37626SGreg Roach return view('individual-name', ['fact' => $fact]); 122abf37626SGreg Roach }); 123852ede8cSGreg Roach 124abf37626SGreg Roach $sex_records = $individual->facts(['SEX'])->map(static function (Fact $fact): string { 125abf37626SGreg Roach return view('individual-sex', ['fact' => $fact]); 126abf37626SGreg Roach }); 127852ede8cSGreg Roach 128852ede8cSGreg Roach // If this individual is linked to a user account, show the link 129852ede8cSGreg Roach $user_link = ''; 130852ede8cSGreg Roach if (Auth::isAdmin()) { 131852ede8cSGreg Roach $users = $this->user_service->findByIndividual($individual); 132852ede8cSGreg Roach foreach ($users as $user) { 1334c3563c0SGreg Roach $user_link = ' — <a href="' . e(route(UserListPage::class, ['filter' => $user->email()])) . '">' . e($user->userName()) . '</a>'; 134852ede8cSGreg Roach } 135852ede8cSGreg Roach } 136852ede8cSGreg Roach 137853f2b8aSGreg Roach $shares = $this->module_service->findByInterface(ModuleShareInterface::class) 138853f2b8aSGreg Roach ->map(fn (ModuleShareInterface $module) => $module->share($individual)) 139853f2b8aSGreg Roach ->filter(); 140853f2b8aSGreg Roach 141852ede8cSGreg Roach return $this->viewResponse('individual-page', [ 142a5fd6d7cSGreg Roach 'age' => $this->ageString($individual), 14369cdf014SGreg Roach 'clipboard_facts' => $this->clipboard_service->pastableFacts($individual), 144852ede8cSGreg Roach 'individual' => $individual, 145852ede8cSGreg Roach 'individual_media' => $individual_media, 1462406e0e0SGreg Roach 'meta_description' => $this->metaDescription($individual), 147852ede8cSGreg Roach 'meta_robots' => 'index,follow', 148852ede8cSGreg Roach 'name_records' => $name_records, 149852ede8cSGreg Roach 'sex_records' => $sex_records, 150853f2b8aSGreg Roach 'shares' => $shares, 151852ede8cSGreg Roach 'sidebars' => $this->getSidebars($individual), 152852ede8cSGreg Roach 'tabs' => $this->getTabs($individual), 153852ede8cSGreg Roach 'significant' => $this->significant($individual), 1545e6816beSGreg Roach 'title' => $individual->fullName() . ' ' . $individual->lifespan(), 155852ede8cSGreg Roach 'tree' => $tree, 156852ede8cSGreg Roach 'user_link' => $user_link, 157852ede8cSGreg Roach ]); 158852ede8cSGreg Roach } 159852ede8cSGreg Roach 160852ede8cSGreg Roach /** 1612406e0e0SGreg Roach * @param Individual $individual 1622406e0e0SGreg Roach * 1632406e0e0SGreg Roach * @return string 1642406e0e0SGreg Roach */ 165a5fd6d7cSGreg Roach private function ageString(Individual $individual): string 166a5fd6d7cSGreg Roach { 167a5fd6d7cSGreg Roach if ($individual->isDead()) { 168a5fd6d7cSGreg Roach // If dead, show age at death 169a5fd6d7cSGreg Roach $age = (string) new Age($individual->getBirthDate(), $individual->getDeathDate()); 170a5fd6d7cSGreg Roach 171a5fd6d7cSGreg Roach if ($age === '') { 172a5fd6d7cSGreg Roach return ''; 173a5fd6d7cSGreg Roach } 174a5fd6d7cSGreg Roach 175a5fd6d7cSGreg Roach switch ($individual->sex()) { 176a5fd6d7cSGreg Roach case 'M': 177a5fd6d7cSGreg Roach /* I18N: The age of an individual at a given date */ 178a5fd6d7cSGreg Roach return I18N::translateContext('Male', '(aged %s)', $age); 179a5fd6d7cSGreg Roach case 'F': 180a5fd6d7cSGreg Roach /* I18N: The age of an individual at a given date */ 181a5fd6d7cSGreg Roach return I18N::translateContext('Female', '(aged %s)', $age); 182a5fd6d7cSGreg Roach default: 183a5fd6d7cSGreg Roach /* I18N: The age of an individual at a given date */ 184a5fd6d7cSGreg Roach return I18N::translate('(aged %s)', $age); 185a5fd6d7cSGreg Roach } 186a5fd6d7cSGreg Roach } 187a5fd6d7cSGreg Roach 188a5fd6d7cSGreg Roach // If living, show age today 189a5fd6d7cSGreg Roach $today = new Date(strtoupper(date('d M Y'))); 190a5fd6d7cSGreg Roach $age = (string) new Age($individual->getBirthDate(), $today); 191a5fd6d7cSGreg Roach 192a5fd6d7cSGreg Roach if ($age === '') { 193a5fd6d7cSGreg Roach return ''; 194a5fd6d7cSGreg Roach } 195a5fd6d7cSGreg Roach 196a5fd6d7cSGreg Roach /* I18N: The current age of a living individual */ 197a5fd6d7cSGreg Roach return I18N::translate('(age %s)', $age); 198a5fd6d7cSGreg Roach } 199a5fd6d7cSGreg Roach 200a5fd6d7cSGreg Roach /** 201a5fd6d7cSGreg Roach * @param Individual $individual 202a5fd6d7cSGreg Roach * 203a5fd6d7cSGreg Roach * @return string 204a5fd6d7cSGreg Roach */ 2052406e0e0SGreg Roach private function metaDescription(Individual $individual): string 2062406e0e0SGreg Roach { 2072406e0e0SGreg Roach $meta_facts = []; 2082406e0e0SGreg Roach 2092406e0e0SGreg Roach $birth_date = $individual->getBirthDate(); 2102406e0e0SGreg Roach $birth_place = $individual->getBirthPlace(); 2112406e0e0SGreg Roach 2122406e0e0SGreg Roach if ($birth_date->isOK() || $birth_place->id() !== 0) { 2132406e0e0SGreg Roach $meta_facts[] = I18N::translate('Birth') . ' ' . 2142406e0e0SGreg Roach $birth_date->display(false, null, false) . ' ' . 2152406e0e0SGreg Roach $birth_place->placeName(); 2162406e0e0SGreg Roach } 2172406e0e0SGreg Roach 2182406e0e0SGreg Roach $death_date = $individual->getDeathDate(); 2192406e0e0SGreg Roach $death_place = $individual->getDeathPlace(); 2202406e0e0SGreg Roach 2212406e0e0SGreg Roach if ($death_date->isOK() || $death_place->id() !== 0) { 2222406e0e0SGreg Roach $meta_facts[] = I18N::translate('Death') . ' ' . 2232406e0e0SGreg Roach $death_date->display(false, null, false) . ' ' . 2242406e0e0SGreg Roach $death_place->placeName(); 2252406e0e0SGreg Roach } 2262406e0e0SGreg Roach 2272406e0e0SGreg Roach foreach ($individual->childFamilies() as $family) { 2282406e0e0SGreg Roach $meta_facts[] = I18N::translate('Parents') . ' ' . $family->fullName(); 2292406e0e0SGreg Roach } 2302406e0e0SGreg Roach 2312406e0e0SGreg Roach foreach ($individual->spouseFamilies() as $family) { 2322406e0e0SGreg Roach $spouse = $family->spouse($individual); 2332406e0e0SGreg Roach if ($spouse instanceof Individual) { 2342406e0e0SGreg Roach $meta_facts[] = I18N::translate('Spouse') . ' ' . $spouse->fullName(); 2352406e0e0SGreg Roach } 2362406e0e0SGreg Roach 2372406e0e0SGreg Roach $child_names = $family->children()->map(static function (Individual $individual): string { 2382406e0e0SGreg Roach return e($individual->getAllNames()[0]['givn']); 2392406e0e0SGreg Roach })->implode(', '); 2402406e0e0SGreg Roach 2412406e0e0SGreg Roach 2422406e0e0SGreg Roach if ($child_names !== '') { 2432406e0e0SGreg Roach $meta_facts[] = I18N::translate('Children') . ' ' . $child_names; 2442406e0e0SGreg Roach } 2452406e0e0SGreg Roach } 2462406e0e0SGreg Roach 2472406e0e0SGreg Roach $meta_facts = array_map('strip_tags', $meta_facts); 2482406e0e0SGreg Roach $meta_facts = array_map('trim', $meta_facts); 2492406e0e0SGreg Roach 2502406e0e0SGreg Roach return implode(', ', $meta_facts); 2512406e0e0SGreg Roach } 2522406e0e0SGreg Roach 2532406e0e0SGreg Roach /** 254852ede8cSGreg Roach * Which tabs should we show on this individual's page. 255852ede8cSGreg Roach * We don't show empty tabs. 256852ede8cSGreg Roach * 257852ede8cSGreg Roach * @param Individual $individual 258852ede8cSGreg Roach * 259b5c8fd7eSGreg Roach * @return Collection<ModuleSidebarInterface> 260852ede8cSGreg Roach */ 261852ede8cSGreg Roach public function getSidebars(Individual $individual): Collection 262852ede8cSGreg Roach { 263852ede8cSGreg Roach return $this->module_service->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user()) 264852ede8cSGreg Roach ->filter(static function (ModuleSidebarInterface $sidebar) use ($individual): bool { 265852ede8cSGreg Roach return $sidebar->hasSidebarContent($individual); 266852ede8cSGreg Roach }); 267852ede8cSGreg Roach } 268852ede8cSGreg Roach 269852ede8cSGreg Roach /** 270852ede8cSGreg Roach * Which tabs should we show on this individual's page. 271852ede8cSGreg Roach * We don't show empty tabs. 272852ede8cSGreg Roach * 273852ede8cSGreg Roach * @param Individual $individual 274852ede8cSGreg Roach * 275b5c8fd7eSGreg Roach * @return Collection<ModuleTabInterface> 276852ede8cSGreg Roach */ 277852ede8cSGreg Roach public function getTabs(Individual $individual): Collection 278852ede8cSGreg Roach { 279852ede8cSGreg Roach return $this->module_service->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user()) 280852ede8cSGreg Roach ->filter(static function (ModuleTabInterface $tab) use ($individual): bool { 281852ede8cSGreg Roach return $tab->hasTabContent($individual); 282852ede8cSGreg Roach }); 283852ede8cSGreg Roach } 284852ede8cSGreg Roach 285852ede8cSGreg Roach /** 286852ede8cSGreg Roach * What are the significant elements of this page? 287852ede8cSGreg Roach * The layout will need them to generate URLs for charts and reports. 288852ede8cSGreg Roach * 289852ede8cSGreg Roach * @param Individual $individual 290852ede8cSGreg Roach * 291852ede8cSGreg Roach * @return stdClass 292852ede8cSGreg Roach */ 293852ede8cSGreg Roach private function significant(Individual $individual): stdClass 294852ede8cSGreg Roach { 295852ede8cSGreg Roach [$surname] = explode(',', $individual->sortName()); 296852ede8cSGreg Roach 297852ede8cSGreg Roach $family = $individual->childFamilies()->merge($individual->spouseFamilies())->first(); 298852ede8cSGreg Roach 299852ede8cSGreg Roach return (object) [ 300852ede8cSGreg Roach 'family' => $family, 301852ede8cSGreg Roach 'individual' => $individual, 302852ede8cSGreg Roach 'surname' => $surname, 303852ede8cSGreg Roach ]; 304852ede8cSGreg Roach } 305852ede8cSGreg Roach} 306