xref: /webtrees/app/Http/RequestHandlers/IndividualPage.php (revision 194b0938bfa145814c8ade07169d1464fa285e2a)
1852ede8cSGreg Roach<?php
2852ede8cSGreg Roach
3852ede8cSGreg Roach/**
4852ede8cSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6852ede8cSGreg Roach * This program is free software: you can redistribute it and/or modify
7852ede8cSGreg Roach * it under the terms of the GNU General Public License as published by
8852ede8cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9852ede8cSGreg Roach * (at your option) any later version.
10852ede8cSGreg Roach * This program is distributed in the hope that it will be useful,
11852ede8cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12852ede8cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13852ede8cSGreg Roach * GNU General Public License for more details.
14852ede8cSGreg 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/>.
16852ede8cSGreg Roach */
17852ede8cSGreg Roach
18852ede8cSGreg Roachdeclare(strict_types=1);
19852ede8cSGreg Roach
20852ede8cSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21852ede8cSGreg Roach
22e5d858f5SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23054771e9SGreg Roachuse Fisharebest\Webtrees\Age;
24852ede8cSGreg Roachuse Fisharebest\Webtrees\Auth;
25852ede8cSGreg Roachuse Fisharebest\Webtrees\Date;
26852ede8cSGreg Roachuse Fisharebest\Webtrees\Fact;
27852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
28852ede8cSGreg Roachuse Fisharebest\Webtrees\I18N;
29852ede8cSGreg Roachuse Fisharebest\Webtrees\Individual;
30852ede8cSGreg Roachuse Fisharebest\Webtrees\Media;
31852ede8cSGreg Roachuse Fisharebest\Webtrees\MediaFile;
32853f2b8aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleShareInterface;
33852ede8cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleSidebarInterface;
34852ede8cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleTabInterface;
356b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
36852ede8cSGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
37852ede8cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
38852ede8cSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
39852ede8cSGreg Roachuse Fisharebest\Webtrees\Tree;
40852ede8cSGreg Roachuse Illuminate\Support\Collection;
41852ede8cSGreg Roachuse Psr\Http\Message\ResponseInterface;
42852ede8cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
43852ede8cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
44852ede8cSGreg Roachuse stdClass;
45852ede8cSGreg Roach
462406e0e0SGreg Roachuse function array_map;
47852ede8cSGreg Roachuse function assert;
482406e0e0SGreg Roachuse function date;
49852ede8cSGreg Roachuse function e;
50852ede8cSGreg Roachuse function explode;
512406e0e0SGreg Roachuse function implode;
52852ede8cSGreg Roachuse function is_string;
53852ede8cSGreg Roachuse function redirect;
54852ede8cSGreg Roachuse function route;
552406e0e0SGreg Roachuse function strtoupper;
562406e0e0SGreg Roachuse function view;
572406e0e0SGreg Roach
58852ede8cSGreg Roach/**
59852ede8cSGreg Roach * Show an individual's page.
60852ede8cSGreg Roach */
61852ede8cSGreg Roachclass IndividualPage implements RequestHandlerInterface
62852ede8cSGreg Roach{
63852ede8cSGreg Roach    use ViewResponseTrait;
64852ede8cSGreg Roach
65852ede8cSGreg Roach    /** @var ClipboardService */
66852ede8cSGreg Roach    private $clipboard_service;
67852ede8cSGreg Roach
68852ede8cSGreg Roach    /** @var ModuleService */
69852ede8cSGreg Roach    private $module_service;
70852ede8cSGreg Roach
71852ede8cSGreg Roach    /** @var UserService */
72852ede8cSGreg Roach    private $user_service;
73852ede8cSGreg Roach
74852ede8cSGreg Roach    /**
75aba707cdSGreg Roach     * IndividualPage constructor.
76852ede8cSGreg Roach     *
77852ede8cSGreg Roach     * @param ClipboardService $clipboard_service
78852ede8cSGreg Roach     * @param ModuleService    $module_service
79852ede8cSGreg Roach     * @param UserService      $user_service
80852ede8cSGreg Roach     */
81852ede8cSGreg Roach    public function __construct(ClipboardService $clipboard_service, ModuleService $module_service, UserService $user_service)
82852ede8cSGreg Roach    {
83852ede8cSGreg Roach        $this->clipboard_service = $clipboard_service;
84852ede8cSGreg Roach        $this->module_service    = $module_service;
85852ede8cSGreg Roach        $this->user_service      = $user_service;
86852ede8cSGreg Roach    }
87852ede8cSGreg Roach
88852ede8cSGreg Roach    /**
89852ede8cSGreg Roach     * @param ServerRequestInterface $request
90852ede8cSGreg Roach     *
91852ede8cSGreg Roach     * @return ResponseInterface
92852ede8cSGreg Roach     */
93852ede8cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
94852ede8cSGreg Roach    {
95852ede8cSGreg Roach        $tree = $request->getAttribute('tree');
96852ede8cSGreg Roach        assert($tree instanceof Tree);
97852ede8cSGreg Roach
98852ede8cSGreg Roach        $xref = $request->getAttribute('xref');
99852ede8cSGreg Roach        assert(is_string($xref));
100852ede8cSGreg Roach
1016b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
102852ede8cSGreg Roach        $individual = Auth::checkIndividualAccess($individual);
103852ede8cSGreg Roach
104852ede8cSGreg Roach        // Redirect to correct xref/slug
105*194b0938SGreg Roach        $slug = Registry::slugFactory()->make($individual);
106*194b0938SGreg Roach
107*194b0938SGreg Roach        if ($individual->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
108e5d858f5SGreg Roach            return redirect($individual->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
109852ede8cSGreg Roach        }
110852ede8cSGreg Roach
111852ede8cSGreg Roach        // What images are linked to this individual
112852ede8cSGreg Roach        $individual_media = new Collection();
113852ede8cSGreg Roach        foreach ($individual->facts(['OBJE']) as $fact) {
114852ede8cSGreg Roach            $media_object = $fact->target();
115852ede8cSGreg Roach            if ($media_object instanceof Media) {
116852ede8cSGreg Roach                $media_file = $media_object->firstImageFile();
117852ede8cSGreg Roach                if ($media_file instanceof MediaFile) {
118852ede8cSGreg Roach                    $individual_media->add($media_file);
119852ede8cSGreg Roach                }
120852ede8cSGreg Roach            }
121852ede8cSGreg Roach        }
122852ede8cSGreg Roach
123abf37626SGreg Roach        $name_records = $individual->facts(['NAME'])->map(static function (Fact $fact): string {
124abf37626SGreg Roach            return view('individual-name', ['fact' => $fact]);
125abf37626SGreg Roach        });
126852ede8cSGreg Roach
127abf37626SGreg Roach        $sex_records = $individual->facts(['SEX'])->map(static function (Fact $fact): string {
128abf37626SGreg Roach            return view('individual-sex', ['fact' => $fact]);
129abf37626SGreg Roach        });
130852ede8cSGreg Roach
131852ede8cSGreg Roach        // If this individual is linked to a user account, show the link
132852ede8cSGreg Roach        $user_link = '';
133852ede8cSGreg Roach        if (Auth::isAdmin()) {
134852ede8cSGreg Roach            $users = $this->user_service->findByIndividual($individual);
135852ede8cSGreg Roach            foreach ($users as $user) {
1364c3563c0SGreg Roach                $user_link = ' —  <a href="' . e(route(UserListPage::class, ['filter' => $user->email()])) . '">' . e($user->userName()) . '</a>';
137852ede8cSGreg Roach            }
138852ede8cSGreg Roach        }
139852ede8cSGreg Roach
140853f2b8aSGreg Roach        $shares = $this->module_service->findByInterface(ModuleShareInterface::class)
141853f2b8aSGreg Roach            ->map(fn (ModuleShareInterface $module) => $module->share($individual))
142853f2b8aSGreg Roach            ->filter();
143853f2b8aSGreg Roach
144852ede8cSGreg Roach        return $this->viewResponse('individual-page', [
145a5fd6d7cSGreg Roach            'age'              => $this->ageString($individual),
14669cdf014SGreg Roach            'clipboard_facts'  => $this->clipboard_service->pastableFacts($individual),
147852ede8cSGreg Roach            'individual'       => $individual,
148852ede8cSGreg Roach            'individual_media' => $individual_media,
1492406e0e0SGreg Roach            'meta_description' => $this->metaDescription($individual),
150852ede8cSGreg Roach            'meta_robots'      => 'index,follow',
151852ede8cSGreg Roach            'name_records'     => $name_records,
152852ede8cSGreg Roach            'sex_records'      => $sex_records,
153853f2b8aSGreg Roach            'shares'           => $shares,
154852ede8cSGreg Roach            'sidebars'         => $this->getSidebars($individual),
155852ede8cSGreg Roach            'tabs'             => $this->getTabs($individual),
156852ede8cSGreg Roach            'significant'      => $this->significant($individual),
1575e6816beSGreg Roach            'title'            => $individual->fullName() . ' ' . $individual->lifespan(),
158852ede8cSGreg Roach            'tree'             => $tree,
159852ede8cSGreg Roach            'user_link'        => $user_link,
160852ede8cSGreg Roach        ]);
161852ede8cSGreg Roach    }
162852ede8cSGreg Roach
163852ede8cSGreg Roach    /**
1642406e0e0SGreg Roach     * @param Individual $individual
1652406e0e0SGreg Roach     *
1662406e0e0SGreg Roach     * @return string
1672406e0e0SGreg Roach     */
168a5fd6d7cSGreg Roach    private function ageString(Individual $individual): string
169a5fd6d7cSGreg Roach    {
170a5fd6d7cSGreg Roach        if ($individual->isDead()) {
171a5fd6d7cSGreg Roach            // If dead, show age at death
172a5fd6d7cSGreg Roach            $age = (string) new Age($individual->getBirthDate(), $individual->getDeathDate());
173a5fd6d7cSGreg Roach
174a5fd6d7cSGreg Roach            if ($age === '') {
175a5fd6d7cSGreg Roach                return '';
176a5fd6d7cSGreg Roach            }
177a5fd6d7cSGreg Roach
178a5fd6d7cSGreg Roach            switch ($individual->sex()) {
179a5fd6d7cSGreg Roach                case 'M':
180a5fd6d7cSGreg Roach                    /* I18N: The age of an individual at a given date */
181a5fd6d7cSGreg Roach                    return I18N::translateContext('Male', '(aged %s)', $age);
182a5fd6d7cSGreg Roach                case 'F':
183a5fd6d7cSGreg Roach                    /* I18N: The age of an individual at a given date */
184a5fd6d7cSGreg Roach                    return I18N::translateContext('Female', '(aged %s)', $age);
185a5fd6d7cSGreg Roach                default:
186a5fd6d7cSGreg Roach                    /* I18N: The age of an individual at a given date */
187a5fd6d7cSGreg Roach                    return I18N::translate('(aged %s)', $age);
188a5fd6d7cSGreg Roach            }
189a5fd6d7cSGreg Roach        }
190a5fd6d7cSGreg Roach
191a5fd6d7cSGreg Roach        // If living, show age today
192a5fd6d7cSGreg Roach        $today = new Date(strtoupper(date('d M Y')));
193a5fd6d7cSGreg Roach        $age   = (string) new Age($individual->getBirthDate(), $today);
194a5fd6d7cSGreg Roach
195a5fd6d7cSGreg Roach        if ($age === '') {
196a5fd6d7cSGreg Roach            return '';
197a5fd6d7cSGreg Roach        }
198a5fd6d7cSGreg Roach
199a5fd6d7cSGreg Roach        /* I18N: The current age of a living individual */
200a5fd6d7cSGreg Roach        return I18N::translate('(age %s)', $age);
201a5fd6d7cSGreg Roach    }
202a5fd6d7cSGreg Roach
203a5fd6d7cSGreg Roach    /**
204a5fd6d7cSGreg Roach     * @param Individual $individual
205a5fd6d7cSGreg Roach     *
206a5fd6d7cSGreg Roach     * @return string
207a5fd6d7cSGreg Roach     */
2082406e0e0SGreg Roach    private function metaDescription(Individual $individual): string
2092406e0e0SGreg Roach    {
2102406e0e0SGreg Roach        $meta_facts = [];
2112406e0e0SGreg Roach
2122406e0e0SGreg Roach        $birth_date  = $individual->getBirthDate();
2132406e0e0SGreg Roach        $birth_place = $individual->getBirthPlace();
2142406e0e0SGreg Roach
2152406e0e0SGreg Roach        if ($birth_date->isOK() || $birth_place->id() !== 0) {
2162406e0e0SGreg Roach            $meta_facts[] = I18N::translate('Birth') . ' ' .
2172406e0e0SGreg Roach                $birth_date->display(false, null, false) . ' ' .
2182406e0e0SGreg Roach                $birth_place->placeName();
2192406e0e0SGreg Roach        }
2202406e0e0SGreg Roach
2212406e0e0SGreg Roach        $death_date  = $individual->getDeathDate();
2222406e0e0SGreg Roach        $death_place = $individual->getDeathPlace();
2232406e0e0SGreg Roach
2242406e0e0SGreg Roach        if ($death_date->isOK() || $death_place->id() !== 0) {
2252406e0e0SGreg Roach            $meta_facts[] = I18N::translate('Death') . ' ' .
2262406e0e0SGreg Roach                $death_date->display(false, null, false) . ' ' .
2272406e0e0SGreg Roach                $death_place->placeName();
2282406e0e0SGreg Roach        }
2292406e0e0SGreg Roach
2302406e0e0SGreg Roach        foreach ($individual->childFamilies() as $family) {
2312406e0e0SGreg Roach            $meta_facts[] = I18N::translate('Parents') . ' ' . $family->fullName();
2322406e0e0SGreg Roach        }
2332406e0e0SGreg Roach
2342406e0e0SGreg Roach        foreach ($individual->spouseFamilies() as $family) {
2352406e0e0SGreg Roach            $spouse = $family->spouse($individual);
2362406e0e0SGreg Roach            if ($spouse instanceof Individual) {
2372406e0e0SGreg Roach                $meta_facts[] = I18N::translate('Spouse') . ' ' . $spouse->fullName();
2382406e0e0SGreg Roach            }
2392406e0e0SGreg Roach
2402406e0e0SGreg Roach            $child_names = $family->children()->map(static function (Individual $individual): string {
2412406e0e0SGreg Roach                return e($individual->getAllNames()[0]['givn']);
2422406e0e0SGreg Roach            })->implode(', ');
2432406e0e0SGreg Roach
2442406e0e0SGreg Roach
2452406e0e0SGreg Roach            if ($child_names !== '') {
2462406e0e0SGreg Roach                $meta_facts[] = I18N::translate('Children') . ' ' . $child_names;
2472406e0e0SGreg Roach            }
2482406e0e0SGreg Roach        }
2492406e0e0SGreg Roach
2502406e0e0SGreg Roach        $meta_facts = array_map('strip_tags', $meta_facts);
2512406e0e0SGreg Roach        $meta_facts = array_map('trim', $meta_facts);
2522406e0e0SGreg Roach
2532406e0e0SGreg Roach        return implode(', ', $meta_facts);
2542406e0e0SGreg Roach    }
2552406e0e0SGreg Roach
2562406e0e0SGreg Roach    /**
257852ede8cSGreg Roach     * Which tabs should we show on this individual's page.
258852ede8cSGreg Roach     * We don't show empty tabs.
259852ede8cSGreg Roach     *
260852ede8cSGreg Roach     * @param Individual $individual
261852ede8cSGreg Roach     *
262b5c8fd7eSGreg Roach     * @return Collection<ModuleSidebarInterface>
263852ede8cSGreg Roach     */
264852ede8cSGreg Roach    public function getSidebars(Individual $individual): Collection
265852ede8cSGreg Roach    {
266852ede8cSGreg Roach        return $this->module_service->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
267852ede8cSGreg Roach            ->filter(static function (ModuleSidebarInterface $sidebar) use ($individual): bool {
268852ede8cSGreg Roach                return $sidebar->hasSidebarContent($individual);
269852ede8cSGreg Roach            });
270852ede8cSGreg Roach    }
271852ede8cSGreg Roach
272852ede8cSGreg Roach    /**
273852ede8cSGreg Roach     * Which tabs should we show on this individual's page.
274852ede8cSGreg Roach     * We don't show empty tabs.
275852ede8cSGreg Roach     *
276852ede8cSGreg Roach     * @param Individual $individual
277852ede8cSGreg Roach     *
278b5c8fd7eSGreg Roach     * @return Collection<ModuleTabInterface>
279852ede8cSGreg Roach     */
280852ede8cSGreg Roach    public function getTabs(Individual $individual): Collection
281852ede8cSGreg Roach    {
282852ede8cSGreg Roach        return $this->module_service->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
283852ede8cSGreg Roach            ->filter(static function (ModuleTabInterface $tab) use ($individual): bool {
284852ede8cSGreg Roach                return $tab->hasTabContent($individual);
285852ede8cSGreg Roach            });
286852ede8cSGreg Roach    }
287852ede8cSGreg Roach
288852ede8cSGreg Roach    /**
289852ede8cSGreg Roach     * What are the significant elements of this page?
290852ede8cSGreg Roach     * The layout will need them to generate URLs for charts and reports.
291852ede8cSGreg Roach     *
292852ede8cSGreg Roach     * @param Individual $individual
293852ede8cSGreg Roach     *
294852ede8cSGreg Roach     * @return stdClass
295852ede8cSGreg Roach     */
296852ede8cSGreg Roach    private function significant(Individual $individual): stdClass
297852ede8cSGreg Roach    {
298852ede8cSGreg Roach        [$surname] = explode(',', $individual->sortName());
299852ede8cSGreg Roach
300852ede8cSGreg Roach        $family = $individual->childFamilies()->merge($individual->spouseFamilies())->first();
301852ede8cSGreg Roach
302852ede8cSGreg Roach        return (object) [
303852ede8cSGreg Roach            'family'     => $family,
304852ede8cSGreg Roach            'individual' => $individual,
305852ede8cSGreg Roach            'surname'    => $surname,
306852ede8cSGreg Roach        ];
307852ede8cSGreg Roach    }
308852ede8cSGreg Roach}
309