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