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\Http\RequestHandlers; 21 22use Fig\Http\Message\StatusCodeInterface; 23use Fisharebest\Webtrees\Age; 24use Fisharebest\Webtrees\Auth; 25use Fisharebest\Webtrees\Date; 26use Fisharebest\Webtrees\Http\ViewResponseTrait; 27use Fisharebest\Webtrees\I18N; 28use Fisharebest\Webtrees\Individual; 29use Fisharebest\Webtrees\Media; 30use Fisharebest\Webtrees\MediaFile; 31use Fisharebest\Webtrees\Module\ModuleShareInterface; 32use Fisharebest\Webtrees\Module\ModuleSidebarInterface; 33use Fisharebest\Webtrees\Module\ModuleTabInterface; 34use Fisharebest\Webtrees\Registry; 35use Fisharebest\Webtrees\Services\ClipboardService; 36use Fisharebest\Webtrees\Services\ModuleService; 37use Fisharebest\Webtrees\Services\UserService; 38use Fisharebest\Webtrees\Validator; 39use Illuminate\Support\Collection; 40use Psr\Http\Message\ResponseInterface; 41use Psr\Http\Message\ServerRequestInterface; 42use Psr\Http\Server\RequestHandlerInterface; 43 44use function array_map; 45use function date; 46use function e; 47use function explode; 48use function implode; 49use function redirect; 50use function strip_tags; 51use function strtoupper; 52use function trim; 53 54/** 55 * Show an individual's page. 56 */ 57class IndividualPage implements RequestHandlerInterface 58{ 59 use ViewResponseTrait; 60 61 private ClipboardService $clipboard_service; 62 63 private ModuleService $module_service; 64 65 private UserService $user_service; 66 67 /** 68 * IndividualPage constructor. 69 * 70 * @param ClipboardService $clipboard_service 71 * @param ModuleService $module_service 72 * @param UserService $user_service 73 */ 74 public function __construct( 75 ClipboardService $clipboard_service, 76 ModuleService $module_service, 77 UserService $user_service 78 ) { 79 $this->clipboard_service = $clipboard_service; 80 $this->module_service = $module_service; 81 $this->user_service = $user_service; 82 } 83 84 /** 85 * @param ServerRequestInterface $request 86 * 87 * @return ResponseInterface 88 */ 89 public function handle(ServerRequestInterface $request): ResponseInterface 90 { 91 $tree = Validator::attributes($request)->tree(); 92 $xref = Validator::attributes($request)->isXref()->string('xref'); 93 $slug = Validator::attributes($request)->string('slug', ''); 94 $individual = Registry::individualFactory()->make($xref, $tree); 95 $individual = Auth::checkIndividualAccess($individual); 96 97 // Redirect to correct xref/slug 98 if ($individual->xref() !== $xref || Registry::slugFactory()->make($individual) !== $slug) { 99 return redirect($individual->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 100 } 101 102 // What images are linked to this individual 103 $individual_media = new Collection(); 104 foreach ($individual->facts(['OBJE']) as $fact) { 105 $media_object = $fact->target(); 106 if ($media_object instanceof Media) { 107 $media_file = $media_object->firstImageFile(); 108 if ($media_file instanceof MediaFile) { 109 $individual_media->add($media_file); 110 } 111 } 112 } 113 114 // If this individual is linked to a user account, show the link 115 if (Auth::isAdmin()) { 116 $users = $this->user_service->findByIndividual($individual); 117 } else { 118 $users = new Collection(); 119 } 120 121 $shares = $this->module_service 122 ->findByInterface(ModuleShareInterface::class) 123 ->map(fn (ModuleShareInterface $module) => $module->share($individual)) 124 ->filter(); 125 126 return $this->viewResponse('individual-page', [ 127 'age' => $this->ageString($individual), 128 'can_upload_media' => Auth::canUploadMedia($tree, Auth::user()), 129 'clipboard_facts' => $this->clipboard_service->pastableFacts($individual), 130 'individual_media' => $individual_media, 131 'meta_description' => $this->metaDescription($individual), 132 'meta_robots' => 'index,follow', 133 'record' => $individual, 134 'shares' => $shares, 135 'sidebars' => $this->getSidebars($individual), 136 'tabs' => $this->getTabs($individual), 137 'significant' => $this->significant($individual), 138 'title' => $individual->fullName() . ' ' . $individual->lifespan(), 139 'tree' => $tree, 140 'users' => $users, 141 ]); 142 } 143 144 /** 145 * @param Individual $individual 146 * 147 * @return string 148 */ 149 private function ageString(Individual $individual): string 150 { 151 if ($individual->isDead()) { 152 // If dead, show age at death 153 $age = (string) new Age($individual->getBirthDate(), $individual->getDeathDate()); 154 155 if ($age === '') { 156 return ''; 157 } 158 159 switch ($individual->sex()) { 160 case 'M': 161 /* I18N: The age of an individual at a given date */ 162 return I18N::translateContext('Male', '(aged %s)', $age); 163 case 'F': 164 /* I18N: The age of an individual at a given date */ 165 return I18N::translateContext('Female', '(aged %s)', $age); 166 default: 167 /* I18N: The age of an individual at a given date */ 168 return I18N::translate('(aged %s)', $age); 169 } 170 } 171 172 // If living, show age today 173 $today = new Date(strtoupper(date('d M Y'))); 174 $age = (string) new Age($individual->getBirthDate(), $today); 175 176 if ($age === '') { 177 return ''; 178 } 179 180 /* I18N: The current age of a living individual */ 181 return I18N::translate('(age %s)', $age); 182 } 183 184 /** 185 * @param Individual $individual 186 * 187 * @return string 188 */ 189 private function metaDescription(Individual $individual): string 190 { 191 $meta_facts = []; 192 193 $birth_date = $individual->getBirthDate(); 194 $birth_place = $individual->getBirthPlace(); 195 196 if ($birth_date->isOK() || $birth_place->id() !== 0) { 197 $meta_facts[] = I18N::translate('Birth') . ' ' . 198 $birth_date->display() . ' ' . 199 $birth_place->placeName(); 200 } 201 202 $death_date = $individual->getDeathDate(); 203 $death_place = $individual->getDeathPlace(); 204 205 if ($death_date->isOK() || $death_place->id() !== 0) { 206 $meta_facts[] = I18N::translate('Death') . ' ' . 207 $death_date->display() . ' ' . 208 $death_place->placeName(); 209 } 210 211 foreach ($individual->childFamilies() as $family) { 212 $meta_facts[] = I18N::translate('Parents') . ' ' . $family->fullName(); 213 } 214 215 foreach ($individual->spouseFamilies() as $family) { 216 $spouse = $family->spouse($individual); 217 if ($spouse instanceof Individual) { 218 $meta_facts[] = I18N::translate('Spouse') . ' ' . $spouse->fullName(); 219 } 220 221 $child_names = $family->children()->map(static function (Individual $individual): string { 222 return e($individual->getAllNames()[0]['givn']); 223 })->implode(', '); 224 225 226 if ($child_names !== '') { 227 $meta_facts[] = I18N::translate('Children') . ' ' . $child_names; 228 } 229 } 230 231 $meta_facts = array_map(static fn (string $x): string => strip_tags($x), $meta_facts); 232 $meta_facts = array_map(static fn (string $x): string => trim($x), $meta_facts); 233 234 return implode(', ', $meta_facts); 235 } 236 237 /** 238 * Which tabs should we show on this individual's page. 239 * We don't show empty tabs. 240 * 241 * @param Individual $individual 242 * 243 * @return Collection<int,ModuleSidebarInterface> 244 */ 245 public function getSidebars(Individual $individual): Collection 246 { 247 return $this->module_service 248 ->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user()) 249 ->filter(static function (ModuleSidebarInterface $sidebar) use ($individual): bool { 250 return $sidebar->hasSidebarContent($individual); 251 }); 252 } 253 254 /** 255 * Which tabs should we show on this individual's page. 256 * We don't show empty tabs. 257 * 258 * @param Individual $individual 259 * 260 * @return Collection<int,ModuleTabInterface> 261 */ 262 public function getTabs(Individual $individual): Collection 263 { 264 return $this->module_service 265 ->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user()) 266 ->filter(static function (ModuleTabInterface $tab) use ($individual): bool { 267 return $tab->hasTabContent($individual); 268 }); 269 } 270 271 /** 272 * What are the significant elements of this page? 273 * The layout will need them to generate URLs for charts and reports. 274 * 275 * @param Individual $individual 276 * 277 * @return object 278 */ 279 private function significant(Individual $individual): object 280 { 281 [$surname] = explode(',', $individual->sortName()); 282 283 $family = $individual->childFamilies()->merge($individual->spouseFamilies())->first(); 284 285 return (object) [ 286 'family' => $family, 287 'individual' => $individual, 288 'surname' => $surname, 289 ]; 290 } 291} 292