1d7daee59SGreg Roach<?php 2d7daee59SGreg Roach 3d7daee59SGreg Roach/** 4d7daee59SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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; 27d224ae93SGreg Roachuse Fisharebest\Webtrees\Individual; 286b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 29d7daee59SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService; 30d7daee59SGreg Roachuse Fisharebest\Webtrees\Tree; 31d7daee59SGreg Roachuse Psr\Http\Message\ResponseInterface; 32d7daee59SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 33d7daee59SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 34d7daee59SGreg Roachuse stdClass; 35d7daee59SGreg Roach 36d7daee59SGreg Roachuse function assert; 37d224ae93SGreg Roachuse function explode; 38d224ae93SGreg Roachuse function in_array; 39d7daee59SGreg Roachuse function is_string; 40d7daee59SGreg Roachuse function redirect; 41d7daee59SGreg Roach 42d7daee59SGreg Roach/** 43d7daee59SGreg Roach * Show a family's page. 44d7daee59SGreg Roach */ 45d7daee59SGreg Roachclass FamilyPage implements RequestHandlerInterface 46d7daee59SGreg Roach{ 47d7daee59SGreg Roach use ViewResponseTrait; 48d7daee59SGreg Roach 49*c4943cffSGreg Roach private ClipboardService $clipboard_service; 50d7daee59SGreg Roach 51d7daee59SGreg Roach /** 52d7daee59SGreg Roach * FamilyPage constructor. 53d7daee59SGreg Roach * 54d7daee59SGreg Roach * @param ClipboardService $clipboard_service 55d7daee59SGreg Roach */ 56d7daee59SGreg Roach public function __construct(ClipboardService $clipboard_service) 57d7daee59SGreg Roach { 58d7daee59SGreg Roach $this->clipboard_service = $clipboard_service; 59d7daee59SGreg Roach } 60d7daee59SGreg Roach 61d7daee59SGreg Roach /** 62d7daee59SGreg Roach * @param ServerRequestInterface $request 63d7daee59SGreg Roach * 64d7daee59SGreg Roach * @return ResponseInterface 65d7daee59SGreg Roach */ 66d7daee59SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 67d7daee59SGreg Roach { 68d7daee59SGreg Roach $tree = $request->getAttribute('tree'); 69d7daee59SGreg Roach assert($tree instanceof Tree); 70d7daee59SGreg Roach 71d7daee59SGreg Roach $xref = $request->getAttribute('xref'); 72d7daee59SGreg Roach assert(is_string($xref)); 73d7daee59SGreg Roach 746b9cb339SGreg Roach $family = Registry::familyFactory()->make($xref, $tree); 75d7daee59SGreg Roach $family = Auth::checkFamilyAccess($family, false); 76d7daee59SGreg Roach 77d7daee59SGreg Roach // Redirect to correct xref/slug 78194b0938SGreg Roach $slug = Registry::slugFactory()->make($family); 79194b0938SGreg Roach 80194b0938SGreg Roach if ($family->xref() !== $xref || $request->getAttribute('slug') !== $slug) { 81e5d858f5SGreg Roach return redirect($family->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 82d7daee59SGreg Roach } 83d7daee59SGreg Roach 8469cdf014SGreg Roach $clipboard_facts = $this->clipboard_service->pastableFacts($family); 85d7daee59SGreg Roach 86d224ae93SGreg Roach $facts = $family->facts([], true) 87d224ae93SGreg Roach ->filter(static function (Fact $fact): bool { 88870b663fSGreg Roach return !in_array($fact->tag(), ['FAM:HUSB', 'FAM:WIFE', 'FAM:CHIL'], true); 89d224ae93SGreg Roach }); 90d224ae93SGreg Roach 91d7daee59SGreg Roach return $this->viewResponse('family-page', [ 92d7daee59SGreg Roach 'clipboard_facts' => $clipboard_facts, 93d224ae93SGreg Roach 'facts' => $facts, 942406e0e0SGreg Roach 'meta_description' => '', 952406e0e0SGreg Roach 'meta_robots' => 'index,follow', 96d7daee59SGreg Roach 'record' => $family, 97d7daee59SGreg Roach 'significant' => $this->significant($family), 98d7daee59SGreg Roach 'title' => $family->fullName(), 99d7daee59SGreg Roach 'tree' => $tree, 100d7daee59SGreg Roach ]); 101d7daee59SGreg Roach } 102d7daee59SGreg Roach 103d7daee59SGreg Roach /** 104d7daee59SGreg Roach * What are the significant elements of this page? 105d7daee59SGreg Roach * The layout will need them to generate URLs for charts and reports. 106d7daee59SGreg Roach * 107d7daee59SGreg Roach * @param Family $family 108d7daee59SGreg Roach * 109d7daee59SGreg Roach * @return stdClass 110d7daee59SGreg Roach */ 111d7daee59SGreg Roach private function significant(Family $family): stdClass 112d7daee59SGreg Roach { 113d7daee59SGreg Roach $significant = (object) [ 114d7daee59SGreg Roach 'family' => $family, 115d7daee59SGreg Roach 'individual' => null, 116d7daee59SGreg Roach 'surname' => '', 117d7daee59SGreg Roach ]; 118d7daee59SGreg Roach 119d224ae93SGreg Roach $individual = $family->spouses()->merge($family->children())->first(); 120d224ae93SGreg Roach 121d224ae93SGreg Roach if ($individual instanceof Individual) { 122d7daee59SGreg Roach $significant->individual = $individual; 123d7daee59SGreg Roach [$significant->surname] = explode(',', $individual->sortName()); 124d7daee59SGreg Roach } 125d7daee59SGreg Roach 126d7daee59SGreg Roach return $significant; 127d7daee59SGreg Roach } 128d7daee59SGreg Roach} 129