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