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