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