xref: /webtrees/app/Http/RequestHandlers/FamilyPage.php (revision 37f4d1f0a7a5610284a8bb3182a07ec4441c9fc7)
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            'facts'           => $family->facts([], true),
83            'meta_robots'     => 'index,follow',
84            'clipboard_facts' => $clipboard_facts,
85            'record'          => $family,
86            'significant'     => $this->significant($family),
87            'title'           => $family->fullName(),
88            'tree'            => $tree,
89        ]);
90    }
91
92    /**
93     * What are the significant elements of this page?
94     * The layout will need them to generate URLs for charts and reports.
95     *
96     * @param Family $family
97     *
98     * @return stdClass
99     */
100    private function significant(Family $family): stdClass
101    {
102        $significant = (object) [
103            'family'     => $family,
104            'individual' => null,
105            'surname'    => '',
106        ];
107
108        foreach ($family->spouses()->merge($family->children()) as $individual) {
109            $significant->individual = $individual;
110            [$significant->surname] = explode(',', $individual->sortName());
111            break;
112        }
113
114        return $significant;
115    }
116}
117