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