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