xref: /webtrees/app/Http/RequestHandlers/FamilyPage.php (revision b7e8616f5f3bd6f035d5d40dd804283727b5c140)
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;
34
35use function assert;
36use function explode;
37use function in_array;
38use function is_string;
39use function redirect;
40
41/**
42 * Show a family's page.
43 */
44class FamilyPage implements RequestHandlerInterface
45{
46    use ViewResponseTrait;
47
48    private ClipboardService $clipboard_service;
49
50    /**
51     * FamilyPage constructor.
52     *
53     * @param ClipboardService $clipboard_service
54     */
55    public function __construct(ClipboardService $clipboard_service)
56    {
57        $this->clipboard_service = $clipboard_service;
58    }
59
60    /**
61     * @param ServerRequestInterface $request
62     *
63     * @return ResponseInterface
64     */
65    public function handle(ServerRequestInterface $request): ResponseInterface
66    {
67        $tree = $request->getAttribute('tree');
68        assert($tree instanceof Tree);
69
70        $xref = $request->getAttribute('xref');
71        assert(is_string($xref));
72
73        $family = Registry::familyFactory()->make($xref, $tree);
74        $family = Auth::checkFamilyAccess($family, false);
75
76        // Redirect to correct xref/slug
77        $slug = Registry::slugFactory()->make($family);
78
79        if ($family->xref() !== $xref || $request->getAttribute('slug') !== $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 object
109     */
110    private function significant(Family $family): object
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