xref: /webtrees/app/Http/RequestHandlers/FamilyPage.php (revision 24931b29a0237a5f5f1b8620af661ea530451af0)
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 function (Fact $fact): bool {
84                return !in_array($fact->tag(), ['FAM:HUSB', 'FAM:WIFE', 'FAM:CHIL'], true);
85            });
86
87        return $this->viewResponse('family-page', [
88            'can_upload_media' => Auth::canUploadMedia($tree, Auth::user()),
89            'clipboard_facts'  => $clipboard_facts,
90            'facts'            => $facts,
91            'meta_description' => $this->metaDescription($family),
92            'meta_robots'      => 'index,follow',
93            'record'           => $family,
94            'significant'      => $this->significant($family),
95            'title'            => $family->fullName(),
96            'tree'             => $tree,
97        ])->withHeader('Link', '<' . $family->url() . '>; rel="canonical"');
98    }
99
100    /**
101     * What are the significant elements of this page?
102     * The layout will need them to generate URLs for charts and reports.
103     *
104     * @param Family $family
105     *
106     * @return object
107     */
108    private function significant(Family $family): object
109    {
110        $significant = (object) [
111            'family'     => $family,
112            'individual' => null,
113            'surname'    => '',
114        ];
115
116        $individual = $family->spouses()->merge($family->children())->first();
117
118        if ($individual instanceof Individual) {
119            $significant->individual = $individual;
120            [$significant->surname] = explode(',', $individual->sortName());
121        }
122
123        return $significant;
124    }
125
126    /**
127     * @param Family $family
128     *
129     * @return string
130     */
131    private function metaDescription(Family $family): string
132    {
133        $meta_facts = [
134            $family->fullName()
135        ];
136
137        foreach ($family->facts(['MARR', 'DIV'], true) as $fact) {
138            if ($fact->date()->isOK()) {
139                $value = strip_tags($fact->date()->display());
140            } else {
141                $value = I18N::translate('yes');
142            }
143
144            $meta_facts[] = Registry::elementFactory()->make($fact->tag())->labelValue($value, $family->tree());
145        }
146
147        if ($family->children()->isNotEmpty()) {
148            $child_names = $family->children()
149            ->map(static fn (Individual $individual): string => e($individual->getAllNames()[0]['givn']))
150            ->filter(static fn (string $x): bool => $x !== Individual::PRAENOMEN_NESCIO)
151            ->implode(', ');
152
153            $meta_facts[] = I18N::translate('Children') . ' ' . $child_names;
154        }
155
156        $meta_facts = array_map(static fn (string $x): string => strip_tags($x), $meta_facts);
157        $meta_facts = array_map(static fn (string $x): string => trim($x), $meta_facts);
158
159        return implode(', ', $meta_facts);
160    }
161}
162