xref: /webtrees/app/Http/RequestHandlers/FamilyPage.php (revision 194b0938bfa145814c8ade07169d1464fa285e2a)
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;
27d224ae93SGreg Roachuse Fisharebest\Webtrees\Individual;
286b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
29d7daee59SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
30d7daee59SGreg Roachuse Fisharebest\Webtrees\Tree;
31d7daee59SGreg Roachuse Psr\Http\Message\ResponseInterface;
32d7daee59SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
33d7daee59SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
34d7daee59SGreg Roachuse stdClass;
35d7daee59SGreg Roach
36d7daee59SGreg Roachuse function assert;
37d224ae93SGreg Roachuse function explode;
38d224ae93SGreg Roachuse function in_array;
39d7daee59SGreg Roachuse function is_string;
40d7daee59SGreg Roachuse function redirect;
41d7daee59SGreg Roach
42d7daee59SGreg Roach/**
43d7daee59SGreg Roach * Show a family's page.
44d7daee59SGreg Roach */
45d7daee59SGreg Roachclass FamilyPage implements RequestHandlerInterface
46d7daee59SGreg Roach{
47d7daee59SGreg Roach    use ViewResponseTrait;
48d7daee59SGreg Roach
49d7daee59SGreg Roach    /** @var ClipboardService */
50d7daee59SGreg Roach    private $clipboard_service;
51d7daee59SGreg Roach
52d7daee59SGreg Roach    /**
53d7daee59SGreg Roach     * FamilyPage constructor.
54d7daee59SGreg Roach     *
55d7daee59SGreg Roach     * @param ClipboardService $clipboard_service
56d7daee59SGreg Roach     */
57d7daee59SGreg Roach    public function __construct(ClipboardService $clipboard_service)
58d7daee59SGreg Roach    {
59d7daee59SGreg Roach        $this->clipboard_service = $clipboard_service;
60d7daee59SGreg Roach    }
61d7daee59SGreg Roach
62d7daee59SGreg Roach    /**
63d7daee59SGreg Roach     * @param ServerRequestInterface $request
64d7daee59SGreg Roach     *
65d7daee59SGreg Roach     * @return ResponseInterface
66d7daee59SGreg Roach     */
67d7daee59SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
68d7daee59SGreg Roach    {
69d7daee59SGreg Roach        $tree = $request->getAttribute('tree');
70d7daee59SGreg Roach        assert($tree instanceof Tree);
71d7daee59SGreg Roach
72d7daee59SGreg Roach        $xref = $request->getAttribute('xref');
73d7daee59SGreg Roach        assert(is_string($xref));
74d7daee59SGreg Roach
756b9cb339SGreg Roach        $family = Registry::familyFactory()->make($xref, $tree);
76d7daee59SGreg Roach        $family = Auth::checkFamilyAccess($family, false);
77d7daee59SGreg Roach
78d7daee59SGreg Roach        // Redirect to correct xref/slug
79*194b0938SGreg Roach        $slug = Registry::slugFactory()->make($family);
80*194b0938SGreg Roach
81*194b0938SGreg Roach        if ($family->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
82e5d858f5SGreg Roach            return redirect($family->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
83d7daee59SGreg Roach        }
84d7daee59SGreg Roach
8569cdf014SGreg Roach        $clipboard_facts = $this->clipboard_service->pastableFacts($family);
86d7daee59SGreg Roach
87d224ae93SGreg Roach        $facts = $family->facts([], true)
88d224ae93SGreg Roach            ->filter(static function (Fact $fact): bool {
89870b663fSGreg Roach                return !in_array($fact->tag(), ['FAM:HUSB', 'FAM:WIFE', 'FAM:CHIL'], true);
90d224ae93SGreg Roach            });
91d224ae93SGreg Roach
92d7daee59SGreg Roach        return $this->viewResponse('family-page', [
93d7daee59SGreg Roach            'clipboard_facts'  => $clipboard_facts,
94d224ae93SGreg Roach            'facts'            => $facts,
952406e0e0SGreg Roach            'meta_description' => '',
962406e0e0SGreg Roach            'meta_robots'      => 'index,follow',
97d7daee59SGreg Roach            'record'           => $family,
98d7daee59SGreg Roach            'significant'      => $this->significant($family),
99d7daee59SGreg Roach            'title'            => $family->fullName(),
100d7daee59SGreg Roach            'tree'             => $tree,
101d7daee59SGreg Roach        ]);
102d7daee59SGreg Roach    }
103d7daee59SGreg Roach
104d7daee59SGreg Roach    /**
105d7daee59SGreg Roach     * What are the significant elements of this page?
106d7daee59SGreg Roach     * The layout will need them to generate URLs for charts and reports.
107d7daee59SGreg Roach     *
108d7daee59SGreg Roach     * @param Family $family
109d7daee59SGreg Roach     *
110d7daee59SGreg Roach     * @return stdClass
111d7daee59SGreg Roach     */
112d7daee59SGreg Roach    private function significant(Family $family): stdClass
113d7daee59SGreg Roach    {
114d7daee59SGreg Roach        $significant = (object) [
115d7daee59SGreg Roach            'family'     => $family,
116d7daee59SGreg Roach            'individual' => null,
117d7daee59SGreg Roach            'surname'    => '',
118d7daee59SGreg Roach        ];
119d7daee59SGreg Roach
120d224ae93SGreg Roach        $individual = $family->spouses()->merge($family->children())->first();
121d224ae93SGreg Roach
122d224ae93SGreg Roach        if ($individual instanceof Individual) {
123d7daee59SGreg Roach            $significant->individual = $individual;
124d7daee59SGreg Roach            [$significant->surname] = explode(',', $individual->sortName());
125d7daee59SGreg Roach        }
126d7daee59SGreg Roach
127d7daee59SGreg Roach        return $significant;
128d7daee59SGreg Roach    }
129d7daee59SGreg Roach}
130