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\Fact; 27use Fisharebest\Webtrees\Http\ViewResponseTrait; 28use Fisharebest\Webtrees\I18N; 29use Fisharebest\Webtrees\Individual; 30use Fisharebest\Webtrees\Media; 31use Fisharebest\Webtrees\MediaFile; 32use Fisharebest\Webtrees\Module\ModuleShareInterface; 33use Fisharebest\Webtrees\Module\ModuleSidebarInterface; 34use Fisharebest\Webtrees\Module\ModuleTabInterface; 35use Fisharebest\Webtrees\Registry; 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; 44use stdClass; 45 46use function array_map; 47use function assert; 48use function date; 49use function e; 50use function explode; 51use function implode; 52use function is_string; 53use function redirect; 54use function route; 55use function strtoupper; 56use function view; 57 58/** 59 * Show an individual's page. 60 */ 61class IndividualPage implements RequestHandlerInterface 62{ 63 use ViewResponseTrait; 64 65 private ClipboardService $clipboard_service; 66 67 private ModuleService $module_service; 68 69 private UserService $user_service; 70 71 /** 72 * IndividualPage constructor. 73 * 74 * @param ClipboardService $clipboard_service 75 * @param ModuleService $module_service 76 * @param UserService $user_service 77 */ 78 public function __construct(ClipboardService $clipboard_service, ModuleService $module_service, 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 $user_link = ''; 122 if (Auth::isAdmin()) { 123 $users = $this->user_service->findByIndividual($individual); 124 foreach ($users as $user) { 125 $user_link = ' — <a href="' . e(route(UserListPage::class, ['filter' => $user->email()])) . '">' . e($user->userName()) . '</a>'; 126 } 127 } 128 129 $shares = $this->module_service->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 'user_link' => $user_link, 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->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->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user()) 270 ->filter(static function (ModuleTabInterface $tab) use ($individual): bool { 271 return $tab->hasTabContent($individual); 272 }); 273 } 274 275 /** 276 * What are the significant elements of this page? 277 * The layout will need them to generate URLs for charts and reports. 278 * 279 * @param Individual $individual 280 * 281 * @return stdClass 282 */ 283 private function significant(Individual $individual): stdClass 284 { 285 [$surname] = explode(',', $individual->sortName()); 286 287 $family = $individual->childFamilies()->merge($individual->spouseFamilies())->first(); 288 289 return (object) [ 290 'family' => $family, 291 'individual' => $individual, 292 'surname' => $surname, 293 ]; 294 } 295} 296