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