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