1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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\Auth; 24use Fisharebest\Webtrees\Fact; 25use Fisharebest\Webtrees\Family; 26use Fisharebest\Webtrees\Http\ViewResponseTrait; 27use Fisharebest\Webtrees\I18N; 28use Fisharebest\Webtrees\Individual; 29use Fisharebest\Webtrees\Registry; 30use Fisharebest\Webtrees\Services\ClipboardService; 31use Fisharebest\Webtrees\Validator; 32use Psr\Http\Message\ResponseInterface; 33use Psr\Http\Message\ServerRequestInterface; 34use Psr\Http\Server\RequestHandlerInterface; 35 36use function array_map; 37use function e; 38use function explode; 39use function implode; 40use function in_array; 41use function redirect; 42use function strip_tags; 43use function trim; 44 45/** 46 * Show a family's page. 47 */ 48class FamilyPage implements RequestHandlerInterface 49{ 50 use ViewResponseTrait; 51 52 private ClipboardService $clipboard_service; 53 54 /** 55 * FamilyPage constructor. 56 * 57 * @param ClipboardService $clipboard_service 58 */ 59 public function __construct(ClipboardService $clipboard_service) 60 { 61 $this->clipboard_service = $clipboard_service; 62 } 63 64 /** 65 * @param ServerRequestInterface $request 66 * 67 * @return ResponseInterface 68 */ 69 public function handle(ServerRequestInterface $request): ResponseInterface 70 { 71 $tree = Validator::attributes($request)->tree(); 72 $xref = Validator::attributes($request)->isXref()->string('xref'); 73 $slug = Validator::attributes($request)->string('slug', ''); 74 $family = Registry::familyFactory()->make($xref, $tree); 75 $family = Auth::checkFamilyAccess($family, false); 76 77 // Redirect to correct xref/slug 78 if ($family->xref() !== $xref || Registry::slugFactory()->make($family) !== $slug) { 79 return redirect($family->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 80 } 81 82 $clipboard_facts = $this->clipboard_service->pastableFacts($family); 83 84 $facts = $family->facts([], true) 85 ->filter(static function (Fact $fact): bool { 86 return !in_array($fact->tag(), ['FAM:HUSB', 'FAM:WIFE', 'FAM:CHIL'], true); 87 }); 88 89 return $this->viewResponse('family-page', [ 90 'can_upload_media' => Auth::canUploadMedia($tree, Auth::user()), 91 'clipboard_facts' => $clipboard_facts, 92 'facts' => $facts, 93 'meta_description' => $this->metaDescription($family), 94 'meta_robots' => 'index,follow', 95 'record' => $family, 96 'significant' => $this->significant($family), 97 'title' => $family->fullName(), 98 'tree' => $tree, 99 ]); 100 } 101 102 /** 103 * What are the significant elements of this page? 104 * The layout will need them to generate URLs for charts and reports. 105 * 106 * @param Family $family 107 * 108 * @return object 109 */ 110 private function significant(Family $family): object 111 { 112 $significant = (object) [ 113 'family' => $family, 114 'individual' => null, 115 'surname' => '', 116 ]; 117 118 $individual = $family->spouses()->merge($family->children())->first(); 119 120 if ($individual instanceof Individual) { 121 $significant->individual = $individual; 122 [$significant->surname] = explode(',', $individual->sortName()); 123 } 124 125 return $significant; 126 } 127 128 /** 129 * @param Family $family 130 * 131 * @return string 132 */ 133 private function metaDescription(Family $family): string 134 { 135 $meta_facts = [ 136 $family->fullName() 137 ]; 138 139 foreach ($family->facts(['MARR', 'DIV'], true) as $fact) { 140 if ($fact->date()->isOK()) { 141 $value = strip_tags($fact->date()->display()); 142 } else { 143 $value = I18N::translate('yes'); 144 } 145 146 $meta_facts[] = Registry::elementFactory()->make($fact->tag())->labelValue($value, $family->tree()); 147 } 148 149 if ($family->children()->isNotEmpty()) { 150 $child_names = $family->children() 151 ->map(static fn (Individual $individual): string => e($individual->getAllNames()[0]['givn'])) 152 ->filter(static fn (string $x): bool => $x !== Individual::PRAENOMEN_NESCIO) 153 ->implode(', '); 154 155 $meta_facts[] = I18N::translate('Children') . ' ' . $child_names; 156 } 157 158 $meta_facts = array_map(static fn (string $x): string => strip_tags($x), $meta_facts); 159 $meta_facts = array_map(static fn (string $x): string => trim($x), $meta_facts); 160 161 return implode(', ', $meta_facts); 162 } 163} 164