xref: /webtrees/app/Http/RequestHandlers/FamilyPage.php (revision 79556abe1598fa00d14d0e89167f4f7805a43a3a)
1d7daee59SGreg Roach<?php
2d7daee59SGreg Roach
3d7daee59SGreg Roach/**
4d7daee59SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6d7daee59SGreg Roach * This program is free software: you can redistribute it and/or modify
7d7daee59SGreg Roach * it under the terms of the GNU General Public License as published by
8d7daee59SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9d7daee59SGreg Roach * (at your option) any later version.
10d7daee59SGreg Roach * This program is distributed in the hope that it will be useful,
11d7daee59SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12d7daee59SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13d7daee59SGreg Roach * GNU General Public License for more details.
14d7daee59SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16d7daee59SGreg Roach */
17d7daee59SGreg Roach
18d7daee59SGreg Roachdeclare(strict_types=1);
19d7daee59SGreg Roach
20d7daee59SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21d7daee59SGreg Roach
22e5d858f5SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23d7daee59SGreg Roachuse Fisharebest\Webtrees\Auth;
24d224ae93SGreg Roachuse Fisharebest\Webtrees\Fact;
25d7daee59SGreg Roachuse Fisharebest\Webtrees\Family;
26d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
27ca2c8695SGreg Roachuse Fisharebest\Webtrees\I18N;
28d224ae93SGreg Roachuse Fisharebest\Webtrees\Individual;
296b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
30d7daee59SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
31d7daee59SGreg Roachuse Fisharebest\Webtrees\Tree;
32d7daee59SGreg Roachuse Psr\Http\Message\ResponseInterface;
33d7daee59SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
34d7daee59SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
35d7daee59SGreg Roach
36ca2c8695SGreg Roachuse function array_map;
37d7daee59SGreg Roachuse function assert;
38ca2c8695SGreg Roachuse function e;
39d224ae93SGreg Roachuse function explode;
40ca2c8695SGreg Roachuse function implode;
41d224ae93SGreg Roachuse function in_array;
42d7daee59SGreg Roachuse function is_string;
43d7daee59SGreg Roachuse function redirect;
44ca2c8695SGreg Roachuse function strip_tags;
45ca2c8695SGreg Roachuse function trim;
46d7daee59SGreg Roach
47d7daee59SGreg Roach/**
48d7daee59SGreg Roach * Show a family's page.
49d7daee59SGreg Roach */
50d7daee59SGreg Roachclass FamilyPage implements RequestHandlerInterface
51d7daee59SGreg Roach{
52d7daee59SGreg Roach    use ViewResponseTrait;
53d7daee59SGreg Roach
54c4943cffSGreg Roach    private ClipboardService $clipboard_service;
55d7daee59SGreg Roach
56d7daee59SGreg Roach    /**
57d7daee59SGreg Roach     * FamilyPage constructor.
58d7daee59SGreg Roach     *
59d7daee59SGreg Roach     * @param ClipboardService $clipboard_service
60d7daee59SGreg Roach     */
61*79556abeSGreg Roach    public function __construct(ClipboardService $clipboard_service)
62d7daee59SGreg Roach    {
63d7daee59SGreg Roach        $this->clipboard_service = $clipboard_service;
64d7daee59SGreg Roach    }
65d7daee59SGreg Roach
66d7daee59SGreg Roach    /**
67d7daee59SGreg Roach     * @param ServerRequestInterface $request
68d7daee59SGreg Roach     *
69d7daee59SGreg Roach     * @return ResponseInterface
70d7daee59SGreg Roach     */
71d7daee59SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
72d7daee59SGreg Roach    {
73d7daee59SGreg Roach        $tree = $request->getAttribute('tree');
74d7daee59SGreg Roach        assert($tree instanceof Tree);
75d7daee59SGreg Roach
76d7daee59SGreg Roach        $xref = $request->getAttribute('xref');
77d7daee59SGreg Roach        assert(is_string($xref));
78d7daee59SGreg Roach
796b9cb339SGreg Roach        $family = Registry::familyFactory()->make($xref, $tree);
80d7daee59SGreg Roach        $family = Auth::checkFamilyAccess($family, false);
81d7daee59SGreg Roach
82d7daee59SGreg Roach        // Redirect to correct xref/slug
83194b0938SGreg Roach        $slug = Registry::slugFactory()->make($family);
84194b0938SGreg Roach
85194b0938SGreg Roach        if ($family->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
86e5d858f5SGreg Roach            return redirect($family->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
87d7daee59SGreg Roach        }
88d7daee59SGreg Roach
8969cdf014SGreg Roach        $clipboard_facts = $this->clipboard_service->pastableFacts($family);
90d7daee59SGreg Roach
91d224ae93SGreg Roach        $facts = $family->facts([], true)
92d224ae93SGreg Roach            ->filter(static function (Fact $fact): bool {
93870b663fSGreg Roach                return !in_array($fact->tag(), ['FAM:HUSB', 'FAM:WIFE', 'FAM:CHIL'], true);
94d224ae93SGreg Roach            });
95d224ae93SGreg Roach
96d7daee59SGreg Roach        return $this->viewResponse('family-page', [
97*79556abeSGreg Roach            'can_upload_media' => Auth::canUploadMedia($tree, Auth::user()),
98d7daee59SGreg Roach            'clipboard_facts'  => $clipboard_facts,
99d224ae93SGreg Roach            'facts'            => $facts,
100ca2c8695SGreg Roach            'meta_description' => $this->metaDescription($family),
1012406e0e0SGreg Roach            'meta_robots'      => 'index,follow',
102d7daee59SGreg Roach            'record'           => $family,
103d7daee59SGreg Roach            'significant'      => $this->significant($family),
104d7daee59SGreg Roach            'title'            => $family->fullName(),
105d7daee59SGreg Roach            'tree'             => $tree,
106d7daee59SGreg Roach        ]);
107d7daee59SGreg Roach    }
108d7daee59SGreg Roach
109d7daee59SGreg Roach    /**
110d7daee59SGreg Roach     * What are the significant elements of this page?
111d7daee59SGreg Roach     * The layout will need them to generate URLs for charts and reports.
112d7daee59SGreg Roach     *
113d7daee59SGreg Roach     * @param Family $family
114d7daee59SGreg Roach     *
115ac701fbdSGreg Roach     * @return object
116d7daee59SGreg Roach     */
117ac701fbdSGreg Roach    private function significant(Family $family): object
118d7daee59SGreg Roach    {
119d7daee59SGreg Roach        $significant = (object) [
120d7daee59SGreg Roach            'family'     => $family,
121d7daee59SGreg Roach            'individual' => null,
122d7daee59SGreg Roach            'surname'    => '',
123d7daee59SGreg Roach        ];
124d7daee59SGreg Roach
125d224ae93SGreg Roach        $individual = $family->spouses()->merge($family->children())->first();
126d224ae93SGreg Roach
127d224ae93SGreg Roach        if ($individual instanceof Individual) {
128d7daee59SGreg Roach            $significant->individual = $individual;
129d7daee59SGreg Roach            [$significant->surname] = explode(',', $individual->sortName());
130d7daee59SGreg Roach        }
131d7daee59SGreg Roach
132d7daee59SGreg Roach        return $significant;
133d7daee59SGreg Roach    }
134ca2c8695SGreg Roach
135ca2c8695SGreg Roach    /**
136ca2c8695SGreg Roach     * @param Family $family
137ca2c8695SGreg Roach     *
138ca2c8695SGreg Roach     * @return string
139ca2c8695SGreg Roach     */
140ca2c8695SGreg Roach    private function metaDescription(Family $family): string
141ca2c8695SGreg Roach    {
142ca2c8695SGreg Roach        $meta_facts = [
143ca2c8695SGreg Roach            $family->fullName()
144ca2c8695SGreg Roach        ];
145ca2c8695SGreg Roach
146ca2c8695SGreg Roach        foreach ($family->facts(['MARR', 'DIV'], true) as $fact) {
147ca2c8695SGreg Roach            if ($fact->date()->isOK()) {
148ca2c8695SGreg Roach                $value = strip_tags($fact->date()->display());
149ca2c8695SGreg Roach            } else {
150ca2c8695SGreg Roach                $value = I18N::translate('yes');
151ca2c8695SGreg Roach            }
152ca2c8695SGreg Roach
153ca2c8695SGreg Roach            $meta_facts[] = Registry::elementFactory()->make($fact->tag())->labelValue($value, $family->tree());
154ca2c8695SGreg Roach        }
155ca2c8695SGreg Roach
156ca2c8695SGreg Roach        if ($family->children()->isNotEmpty()) {
157ca2c8695SGreg Roach            $child_names = $family->children()
158ca2c8695SGreg Roach            ->map(static fn (Individual $individual): string => e($individual->getAllNames()[0]['givn']))
159ca2c8695SGreg Roach            ->filter(static fn (string $x): bool => $x !== Individual::PRAENOMEN_NESCIO)
160ca2c8695SGreg Roach            ->implode(', ');
161ca2c8695SGreg Roach
162ca2c8695SGreg Roach            $meta_facts[] = I18N::translate('Children') . ' ' . $child_names;
163ca2c8695SGreg Roach        }
164ca2c8695SGreg Roach
165ca2c8695SGreg Roach        $meta_facts = array_map(static fn (string $x): string => strip_tags($x), $meta_facts);
166ca2c8695SGreg Roach        $meta_facts = array_map(static fn (string $x): string => trim($x), $meta_facts);
167ca2c8695SGreg Roach
168ca2c8695SGreg Roach        return implode(', ', $meta_facts);
169ca2c8695SGreg Roach    }
170d7daee59SGreg Roach}
171