1d7daee59SGreg Roach<?php 2d7daee59SGreg Roach 3d7daee59SGreg Roach/** 4d7daee59SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6d7daee59SGreg Roach * This program is free software: you can redistribute it and/or modify 7d7daee59SGreg Roach * it under the terms of the GNU General Public License as published by 8d7daee59SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9d7daee59SGreg Roach * (at your option) any later version. 10d7daee59SGreg Roach * This program is distributed in the hope that it will be useful, 11d7daee59SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12d7daee59SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13d7daee59SGreg Roach * GNU General Public License for more details. 14d7daee59SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16d7daee59SGreg Roach */ 17d7daee59SGreg Roach 18d7daee59SGreg Roachdeclare(strict_types=1); 19d7daee59SGreg Roach 20d7daee59SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21d7daee59SGreg Roach 22e5d858f5SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 23d7daee59SGreg Roachuse Fisharebest\Webtrees\Auth; 24d224ae93SGreg Roachuse Fisharebest\Webtrees\Fact; 25d7daee59SGreg Roachuse Fisharebest\Webtrees\Family; 26d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 27ca2c8695SGreg Roachuse Fisharebest\Webtrees\I18N; 28d224ae93SGreg Roachuse Fisharebest\Webtrees\Individual; 296b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 30d7daee59SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService; 31b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 32d7daee59SGreg Roachuse Psr\Http\Message\ResponseInterface; 33d7daee59SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 34d7daee59SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 35d7daee59SGreg Roach 36ca2c8695SGreg Roachuse function array_map; 37ca2c8695SGreg Roachuse function e; 38d224ae93SGreg Roachuse function explode; 39ca2c8695SGreg Roachuse function implode; 40d224ae93SGreg Roachuse function in_array; 41d7daee59SGreg Roachuse function redirect; 42ca2c8695SGreg Roachuse function strip_tags; 43ca2c8695SGreg Roachuse function trim; 44d7daee59SGreg Roach 45d7daee59SGreg Roach/** 46d7daee59SGreg Roach * Show a family's page. 47d7daee59SGreg Roach */ 48d7daee59SGreg Roachclass FamilyPage implements RequestHandlerInterface 49d7daee59SGreg Roach{ 50d7daee59SGreg Roach use ViewResponseTrait; 51d7daee59SGreg Roach 52c4943cffSGreg Roach private ClipboardService $clipboard_service; 53d7daee59SGreg Roach 54d7daee59SGreg Roach /** 55d7daee59SGreg Roach * @param ClipboardService $clipboard_service 56d7daee59SGreg Roach */ 5779556abeSGreg Roach public function __construct(ClipboardService $clipboard_service) 58d7daee59SGreg Roach { 59d7daee59SGreg Roach $this->clipboard_service = $clipboard_service; 60d7daee59SGreg Roach } 61d7daee59SGreg Roach 62d7daee59SGreg Roach /** 63d7daee59SGreg Roach * @param ServerRequestInterface $request 64d7daee59SGreg Roach * 65d7daee59SGreg Roach * @return ResponseInterface 66d7daee59SGreg Roach */ 67d7daee59SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 68d7daee59SGreg Roach { 69b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 70b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 71b55cbc6bSGreg Roach $slug = Validator::attributes($request)->string('slug', ''); 726b9cb339SGreg Roach $family = Registry::familyFactory()->make($xref, $tree); 73d7daee59SGreg Roach $family = Auth::checkFamilyAccess($family, false); 74d7daee59SGreg Roach 75d7daee59SGreg Roach // Redirect to correct xref/slug 76b55cbc6bSGreg Roach if ($family->xref() !== $xref || Registry::slugFactory()->make($family) !== $slug) { 77e5d858f5SGreg Roach return redirect($family->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 78d7daee59SGreg Roach } 79d7daee59SGreg Roach 8069cdf014SGreg Roach $clipboard_facts = $this->clipboard_service->pastableFacts($family); 81d7daee59SGreg Roach 82d224ae93SGreg Roach $facts = $family->facts([], true) 83*f25fc0f9SGreg Roach ->filter(static fn(Fact $fact): bool => !in_array($fact->tag(), ['FAM:HUSB', 'FAM:WIFE', 'FAM:CHIL'], true)); 84d224ae93SGreg Roach 85d7daee59SGreg Roach return $this->viewResponse('family-page', [ 8679556abeSGreg Roach 'can_upload_media' => Auth::canUploadMedia($tree, Auth::user()), 87d7daee59SGreg Roach 'clipboard_facts' => $clipboard_facts, 88d224ae93SGreg Roach 'facts' => $facts, 89ca2c8695SGreg Roach 'meta_description' => $this->metaDescription($family), 902406e0e0SGreg Roach 'meta_robots' => 'index,follow', 91d7daee59SGreg Roach 'record' => $family, 92d7daee59SGreg Roach 'significant' => $this->significant($family), 93d7daee59SGreg Roach 'title' => $family->fullName(), 94d7daee59SGreg Roach 'tree' => $tree, 9515c4f62cSGreg Roach ])->withHeader('Link', '<' . $family->url() . '>; rel="canonical"'); 96d7daee59SGreg Roach } 97d7daee59SGreg Roach 98d7daee59SGreg Roach /** 99d7daee59SGreg Roach * What are the significant elements of this page? 100d7daee59SGreg Roach * The layout will need them to generate URLs for charts and reports. 101d7daee59SGreg Roach * 102d7daee59SGreg Roach * @param Family $family 103d7daee59SGreg Roach * 104ac701fbdSGreg Roach * @return object 105d7daee59SGreg Roach */ 106ac701fbdSGreg Roach private function significant(Family $family): object 107d7daee59SGreg Roach { 108d7daee59SGreg Roach $significant = (object) [ 109d7daee59SGreg Roach 'family' => $family, 110d7daee59SGreg Roach 'individual' => null, 111d7daee59SGreg Roach 'surname' => '', 112d7daee59SGreg Roach ]; 113d7daee59SGreg Roach 114d224ae93SGreg Roach $individual = $family->spouses()->merge($family->children())->first(); 115d224ae93SGreg Roach 116d224ae93SGreg Roach if ($individual instanceof Individual) { 117d7daee59SGreg Roach $significant->individual = $individual; 118d7daee59SGreg Roach [$significant->surname] = explode(',', $individual->sortName()); 119d7daee59SGreg Roach } 120d7daee59SGreg Roach 121d7daee59SGreg Roach return $significant; 122d7daee59SGreg Roach } 123ca2c8695SGreg Roach 124ca2c8695SGreg Roach /** 125ca2c8695SGreg Roach * @param Family $family 126ca2c8695SGreg Roach * 127ca2c8695SGreg Roach * @return string 128ca2c8695SGreg Roach */ 129ca2c8695SGreg Roach private function metaDescription(Family $family): string 130ca2c8695SGreg Roach { 131ca2c8695SGreg Roach $meta_facts = [ 132ca2c8695SGreg Roach $family->fullName() 133ca2c8695SGreg Roach ]; 134ca2c8695SGreg Roach 135ca2c8695SGreg Roach foreach ($family->facts(['MARR', 'DIV'], true) as $fact) { 136ca2c8695SGreg Roach if ($fact->date()->isOK()) { 137ca2c8695SGreg Roach $value = strip_tags($fact->date()->display()); 138ca2c8695SGreg Roach } else { 139ca2c8695SGreg Roach $value = I18N::translate('yes'); 140ca2c8695SGreg Roach } 141ca2c8695SGreg Roach 142ca2c8695SGreg Roach $meta_facts[] = Registry::elementFactory()->make($fact->tag())->labelValue($value, $family->tree()); 143ca2c8695SGreg Roach } 144ca2c8695SGreg Roach 145ca2c8695SGreg Roach if ($family->children()->isNotEmpty()) { 146ca2c8695SGreg Roach $child_names = $family->children() 147ca2c8695SGreg Roach ->map(static fn (Individual $individual): string => e($individual->getAllNames()[0]['givn'])) 148ca2c8695SGreg Roach ->filter(static fn (string $x): bool => $x !== Individual::PRAENOMEN_NESCIO) 149ca2c8695SGreg Roach ->implode(', '); 150ca2c8695SGreg Roach 151ca2c8695SGreg Roach $meta_facts[] = I18N::translate('Children') . ' ' . $child_names; 152ca2c8695SGreg Roach } 153ca2c8695SGreg Roach 154ca2c8695SGreg Roach $meta_facts = array_map(static fn (string $x): string => strip_tags($x), $meta_facts); 155ca2c8695SGreg Roach $meta_facts = array_map(static fn (string $x): string => trim($x), $meta_facts); 156ca2c8695SGreg Roach 157ca2c8695SGreg Roach return implode(', ', $meta_facts); 158ca2c8695SGreg Roach } 159d7daee59SGreg Roach} 160