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 if ($family->xref() !== $xref || $request->getAttribute('slug') !== $family->slug()) { 80 return redirect($family->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 81 } 82 83 $clipboard_facts = $this->clipboard_service->pastableFacts($family); 84 85 $facts = $family->facts([], true) 86 ->filter(static function (Fact $fact): bool { 87 return !in_array($fact->tag(), ['FAM:HUSB', 'FAM:WIFE', 'FAM:CHIL'], true); 88 }); 89 90 return $this->viewResponse('family-page', [ 91 'clipboard_facts' => $clipboard_facts, 92 'facts' => $facts, 93 'meta_description' => '', 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 stdClass 109 */ 110 private function significant(Family $family): stdClass 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