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