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