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