xref: /webtrees/app/Http/RequestHandlers/FamilyPage.php (revision 1c6adce825f16611bd8b75a22114302de4b41cfe)
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\I18N;
28use Fisharebest\Webtrees\Individual;
29use Fisharebest\Webtrees\Registry;
30use Fisharebest\Webtrees\Services\ClipboardService;
31use Fisharebest\Webtrees\Tree;
32use Psr\Http\Message\ResponseInterface;
33use Psr\Http\Message\ServerRequestInterface;
34use Psr\Http\Server\RequestHandlerInterface;
35
36use function array_map;
37use function assert;
38use function e;
39use function explode;
40use function implode;
41use function in_array;
42use function is_string;
43use function redirect;
44use function strip_tags;
45use function trim;
46
47/**
48 * Show a family's page.
49 */
50class FamilyPage implements RequestHandlerInterface
51{
52    use ViewResponseTrait;
53
54    private ClipboardService $clipboard_service;
55
56    /**
57     * FamilyPage constructor.
58     *
59     * @param ClipboardService $clipboard_service
60     */
61    public function __construct(ClipboardService $clipboard_service)
62    {
63        $this->clipboard_service = $clipboard_service;
64    }
65
66    /**
67     * @param ServerRequestInterface $request
68     *
69     * @return ResponseInterface
70     */
71    public function handle(ServerRequestInterface $request): ResponseInterface
72    {
73        $tree = $request->getAttribute('tree');
74        assert($tree instanceof Tree);
75
76        $xref = $request->getAttribute('xref');
77        assert(is_string($xref));
78
79        $family = Registry::familyFactory()->make($xref, $tree);
80        $family = Auth::checkFamilyAccess($family, false);
81
82        // Redirect to correct xref/slug
83        $slug = Registry::slugFactory()->make($family);
84
85        if ($family->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
86            return redirect($family->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
87        }
88
89        $clipboard_facts = $this->clipboard_service->pastableFacts($family);
90
91        $facts = $family->facts([], true)
92            ->filter(static function (Fact $fact): bool {
93                return !in_array($fact->tag(), ['FAM:HUSB', 'FAM:WIFE', 'FAM:CHIL'], true);
94            });
95
96        return $this->viewResponse('family-page', [
97            'can_upload_media' => Auth::canUploadMedia($tree, Auth::user()),
98            'clipboard_facts'  => $clipboard_facts,
99            'facts'            => $facts,
100            'meta_description' => $this->metaDescription($family),
101            'meta_robots'      => 'index,follow',
102            'record'           => $family,
103            'significant'      => $this->significant($family),
104            'title'            => $family->fullName(),
105            'tree'             => $tree,
106        ]);
107    }
108
109    /**
110     * What are the significant elements of this page?
111     * The layout will need them to generate URLs for charts and reports.
112     *
113     * @param Family $family
114     *
115     * @return object
116     */
117    private function significant(Family $family): object
118    {
119        $significant = (object) [
120            'family'     => $family,
121            'individual' => null,
122            'surname'    => '',
123        ];
124
125        $individual = $family->spouses()->merge($family->children())->first();
126
127        if ($individual instanceof Individual) {
128            $significant->individual = $individual;
129            [$significant->surname] = explode(',', $individual->sortName());
130        }
131
132        return $significant;
133    }
134
135    /**
136     * @param Family $family
137     *
138     * @return string
139     */
140    private function metaDescription(Family $family): string
141    {
142        $meta_facts = [
143            $family->fullName()
144        ];
145
146        foreach ($family->facts(['MARR', 'DIV'], true) as $fact) {
147            if ($fact->date()->isOK()) {
148                $value = strip_tags($fact->date()->display());
149            } else {
150                $value = I18N::translate('yes');
151            }
152
153            $meta_facts[] = Registry::elementFactory()->make($fact->tag())->labelValue($value, $family->tree());
154        }
155
156        if ($family->children()->isNotEmpty()) {
157            $child_names = $family->children()
158            ->map(static fn (Individual $individual): string => e($individual->getAllNames()[0]['givn']))
159            ->filter(static fn (string $x): bool => $x !== Individual::PRAENOMEN_NESCIO)
160            ->implode(', ');
161
162            $meta_facts[] = I18N::translate('Children') . ' ' . $child_names;
163        }
164
165        $meta_facts = array_map(static fn (string $x): string => strip_tags($x), $meta_facts);
166        $meta_facts = array_map(static fn (string $x): string => trim($x), $meta_facts);
167
168        return implode(', ', $meta_facts);
169    }
170}
171