xref: /webtrees/app/Http/RequestHandlers/FamilyPage.php (revision 99125a335d0be11b975e213c69fc4ede0c5303f1)
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    private ClipboardService $clipboard_service;
50
51    /**
52     * FamilyPage constructor.
53     *
54     * @param ClipboardService $clipboard_service
55     */
56    public function __construct(ClipboardService $clipboard_service)
57    {
58        $this->clipboard_service = $clipboard_service;
59    }
60
61    /**
62     * @param ServerRequestInterface $request
63     *
64     * @return ResponseInterface
65     */
66    public function handle(ServerRequestInterface $request): ResponseInterface
67    {
68        $tree = $request->getAttribute('tree');
69        assert($tree instanceof Tree);
70
71        $xref = $request->getAttribute('xref');
72        assert(is_string($xref));
73
74        $family = Registry::familyFactory()->make($xref, $tree);
75        $family = Auth::checkFamilyAccess($family, false);
76
77        // Redirect to correct xref/slug
78        $slug = Registry::slugFactory()->make($family);
79
80        if ($family->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
81            return redirect($family->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
82        }
83
84        $clipboard_facts = $this->clipboard_service->pastableFacts($family);
85
86        $facts = $family->facts([], true)
87            ->filter(static function (Fact $fact): bool {
88                return !in_array($fact->tag(), ['FAM:HUSB', 'FAM:WIFE', 'FAM: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