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