xref: /webtrees/app/Http/RequestHandlers/IndividualPage.php (revision 4c3563c0d95e22997e247ca4944dff997416f927)
1852ede8cSGreg Roach<?php
2852ede8cSGreg Roach
3852ede8cSGreg Roach/**
4852ede8cSGreg Roach * webtrees: online genealogy
5a091ac74SGreg Roach * Copyright (C) 2020 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
15852ede8cSGreg Roach * along with this program. If not, see <http://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;
32852ede8cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleSidebarInterface;
33852ede8cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleTabInterface;
346b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
35852ede8cSGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
36852ede8cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
37852ede8cSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
38852ede8cSGreg Roachuse Fisharebest\Webtrees\Tree;
39852ede8cSGreg Roachuse Illuminate\Support\Collection;
40852ede8cSGreg Roachuse Psr\Http\Message\ResponseInterface;
41852ede8cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
42852ede8cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
43852ede8cSGreg Roachuse stdClass;
44852ede8cSGreg Roach
452406e0e0SGreg Roachuse function array_map;
46852ede8cSGreg Roachuse function assert;
472406e0e0SGreg Roachuse function date;
48852ede8cSGreg Roachuse function e;
49852ede8cSGreg Roachuse function explode;
502406e0e0SGreg Roachuse function implode;
51852ede8cSGreg Roachuse function is_string;
52852ede8cSGreg Roachuse function redirect;
53852ede8cSGreg Roachuse function route;
542406e0e0SGreg Roachuse function strtoupper;
552406e0e0SGreg Roachuse function view;
562406e0e0SGreg Roach
57852ede8cSGreg Roach/**
58852ede8cSGreg Roach * Show an individual's page.
59852ede8cSGreg Roach */
60852ede8cSGreg Roachclass IndividualPage implements RequestHandlerInterface
61852ede8cSGreg Roach{
62852ede8cSGreg Roach    use ViewResponseTrait;
63852ede8cSGreg Roach
64852ede8cSGreg Roach    /** @var ClipboardService */
65852ede8cSGreg Roach    private $clipboard_service;
66852ede8cSGreg Roach
67852ede8cSGreg Roach    /** @var ModuleService */
68852ede8cSGreg Roach    private $module_service;
69852ede8cSGreg Roach
70852ede8cSGreg Roach    /** @var UserService */
71852ede8cSGreg Roach    private $user_service;
72852ede8cSGreg Roach
73852ede8cSGreg Roach    /**
74aba707cdSGreg Roach     * IndividualPage constructor.
75852ede8cSGreg Roach     *
76852ede8cSGreg Roach     * @param ClipboardService $clipboard_service
77852ede8cSGreg Roach     * @param ModuleService    $module_service
78852ede8cSGreg Roach     * @param UserService      $user_service
79852ede8cSGreg Roach     */
80852ede8cSGreg Roach    public function __construct(ClipboardService $clipboard_service, ModuleService $module_service, UserService $user_service)
81852ede8cSGreg Roach    {
82852ede8cSGreg Roach        $this->clipboard_service = $clipboard_service;
83852ede8cSGreg Roach        $this->module_service    = $module_service;
84852ede8cSGreg Roach        $this->user_service      = $user_service;
85852ede8cSGreg Roach    }
86852ede8cSGreg Roach
87852ede8cSGreg Roach    /**
88852ede8cSGreg Roach     * @param ServerRequestInterface $request
89852ede8cSGreg Roach     *
90852ede8cSGreg Roach     * @return ResponseInterface
91852ede8cSGreg Roach     */
92852ede8cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
93852ede8cSGreg Roach    {
94852ede8cSGreg Roach        $tree = $request->getAttribute('tree');
95852ede8cSGreg Roach        assert($tree instanceof Tree);
96852ede8cSGreg Roach
97852ede8cSGreg Roach        $xref = $request->getAttribute('xref');
98852ede8cSGreg Roach        assert(is_string($xref));
99852ede8cSGreg Roach
1006b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
101852ede8cSGreg Roach        $individual = Auth::checkIndividualAccess($individual);
102852ede8cSGreg Roach
103852ede8cSGreg Roach        // Redirect to correct xref/slug
104852ede8cSGreg Roach        if ($individual->xref() !== $xref || $request->getAttribute('slug') !== $individual->slug()) {
105e5d858f5SGreg Roach            return redirect($individual->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
106852ede8cSGreg Roach        }
107852ede8cSGreg Roach
108852ede8cSGreg Roach        // What images are linked to this individual
109852ede8cSGreg Roach        $individual_media = new Collection();
110852ede8cSGreg Roach        foreach ($individual->facts(['OBJE']) as $fact) {
111852ede8cSGreg Roach            $media_object = $fact->target();
112852ede8cSGreg Roach            if ($media_object instanceof Media) {
113852ede8cSGreg Roach                $media_file = $media_object->firstImageFile();
114852ede8cSGreg Roach                if ($media_file instanceof MediaFile) {
115852ede8cSGreg Roach                    $individual_media->add($media_file);
116852ede8cSGreg Roach                }
117852ede8cSGreg Roach            }
118852ede8cSGreg Roach        }
119852ede8cSGreg Roach
120abf37626SGreg Roach        $name_records = $individual->facts(['NAME'])->map(static function (Fact $fact): string {
121abf37626SGreg Roach            return view('individual-name', ['fact' => $fact]);
122abf37626SGreg Roach        });
123852ede8cSGreg Roach
124abf37626SGreg Roach        $sex_records = $individual->facts(['SEX'])->map(static function (Fact $fact): string {
125abf37626SGreg Roach            return view('individual-sex', ['fact' => $fact]);
126abf37626SGreg Roach        });
127852ede8cSGreg Roach
128852ede8cSGreg Roach        // If this individual is linked to a user account, show the link
129852ede8cSGreg Roach        $user_link = '';
130852ede8cSGreg Roach        if (Auth::isAdmin()) {
131852ede8cSGreg Roach            $users = $this->user_service->findByIndividual($individual);
132852ede8cSGreg Roach            foreach ($users as $user) {
133*4c3563c0SGreg Roach                $user_link = ' —  <a href="' . e(route(UserListPage::class, ['filter' => $user->email()])) . '">' . e($user->userName()) . '</a>';
134852ede8cSGreg Roach            }
135852ede8cSGreg Roach        }
136852ede8cSGreg Roach
137852ede8cSGreg Roach        return $this->viewResponse('individual-page', [
138a5fd6d7cSGreg Roach            'age'              => $this->ageString($individual),
139852ede8cSGreg Roach            'clipboard_facts'  => $this->clipboard_service->pastableFacts($individual, new Collection()),
140852ede8cSGreg Roach            'individual'       => $individual,
141852ede8cSGreg Roach            'individual_media' => $individual_media,
1422406e0e0SGreg Roach            'meta_description' => $this->metaDescription($individual),
143852ede8cSGreg Roach            'meta_robots'      => 'index,follow',
144852ede8cSGreg Roach            'name_records'     => $name_records,
145852ede8cSGreg Roach            'sex_records'      => $sex_records,
146852ede8cSGreg Roach            'sidebars'         => $this->getSidebars($individual),
147852ede8cSGreg Roach            'tabs'             => $this->getTabs($individual),
148852ede8cSGreg Roach            'significant'      => $this->significant($individual),
1495e6816beSGreg Roach            'title'            => $individual->fullName() . ' ' . $individual->lifespan(),
150852ede8cSGreg Roach            'tree'             => $tree,
151852ede8cSGreg Roach            'user_link'        => $user_link,
152852ede8cSGreg Roach        ]);
153852ede8cSGreg Roach    }
154852ede8cSGreg Roach
155852ede8cSGreg Roach    /**
1562406e0e0SGreg Roach     * @param Individual $individual
1572406e0e0SGreg Roach     *
1582406e0e0SGreg Roach     * @return string
1592406e0e0SGreg Roach     */
160a5fd6d7cSGreg Roach    private function ageString(Individual $individual): string
161a5fd6d7cSGreg Roach    {
162a5fd6d7cSGreg Roach        if ($individual->isDead()) {
163a5fd6d7cSGreg Roach            // If dead, show age at death
164a5fd6d7cSGreg Roach            $age = (string) new Age($individual->getBirthDate(), $individual->getDeathDate());
165a5fd6d7cSGreg Roach
166a5fd6d7cSGreg Roach            if ($age === '') {
167a5fd6d7cSGreg Roach                return '';
168a5fd6d7cSGreg Roach            }
169a5fd6d7cSGreg Roach
170a5fd6d7cSGreg Roach            switch ($individual->sex()) {
171a5fd6d7cSGreg Roach                case 'M':
172a5fd6d7cSGreg Roach                    /* I18N: The age of an individual at a given date */
173a5fd6d7cSGreg Roach                    return I18N::translateContext('Male', '(aged %s)', $age);
174a5fd6d7cSGreg Roach                case 'F':
175a5fd6d7cSGreg Roach                    /* I18N: The age of an individual at a given date */
176a5fd6d7cSGreg Roach                    return I18N::translateContext('Female', '(aged %s)', $age);
177a5fd6d7cSGreg Roach                default:
178a5fd6d7cSGreg Roach                    /* I18N: The age of an individual at a given date */
179a5fd6d7cSGreg Roach                    return I18N::translate('(aged %s)', $age);
180a5fd6d7cSGreg Roach            }
181a5fd6d7cSGreg Roach        }
182a5fd6d7cSGreg Roach
183a5fd6d7cSGreg Roach        // If living, show age today
184a5fd6d7cSGreg Roach        $today = new Date(strtoupper(date('d M Y')));
185a5fd6d7cSGreg Roach        $age   = (string) new Age($individual->getBirthDate(), $today);
186a5fd6d7cSGreg Roach
187a5fd6d7cSGreg Roach        if ($age === '') {
188a5fd6d7cSGreg Roach            return '';
189a5fd6d7cSGreg Roach        }
190a5fd6d7cSGreg Roach
191a5fd6d7cSGreg Roach        /* I18N: The current age of a living individual */
192a5fd6d7cSGreg Roach        return I18N::translate('(age %s)', $age);
193a5fd6d7cSGreg Roach    }
194a5fd6d7cSGreg Roach
195a5fd6d7cSGreg Roach    /**
196a5fd6d7cSGreg Roach     * @param Individual $individual
197a5fd6d7cSGreg Roach     *
198a5fd6d7cSGreg Roach     * @return string
199a5fd6d7cSGreg Roach     */
2002406e0e0SGreg Roach    private function metaDescription(Individual $individual): string
2012406e0e0SGreg Roach    {
2022406e0e0SGreg Roach        $meta_facts = [];
2032406e0e0SGreg Roach
2042406e0e0SGreg Roach        $birth_date  = $individual->getBirthDate();
2052406e0e0SGreg Roach        $birth_place = $individual->getBirthPlace();
2062406e0e0SGreg Roach
2072406e0e0SGreg Roach        if ($birth_date->isOK() || $birth_place->id() !== 0) {
2082406e0e0SGreg Roach            $meta_facts[] = I18N::translate('Birth') . ' ' .
2092406e0e0SGreg Roach                $birth_date->display(false, null, false) . ' ' .
2102406e0e0SGreg Roach                $birth_place->placeName();
2112406e0e0SGreg Roach        }
2122406e0e0SGreg Roach
2132406e0e0SGreg Roach        $death_date  = $individual->getDeathDate();
2142406e0e0SGreg Roach        $death_place = $individual->getDeathPlace();
2152406e0e0SGreg Roach
2162406e0e0SGreg Roach        if ($death_date->isOK() || $death_place->id() !== 0) {
2172406e0e0SGreg Roach            $meta_facts[] = I18N::translate('Death') . ' ' .
2182406e0e0SGreg Roach                $death_date->display(false, null, false) . ' ' .
2192406e0e0SGreg Roach                $death_place->placeName();
2202406e0e0SGreg Roach        }
2212406e0e0SGreg Roach
2222406e0e0SGreg Roach        foreach ($individual->childFamilies() as $family) {
2232406e0e0SGreg Roach            $meta_facts[] = I18N::translate('Parents') . ' ' . $family->fullName();
2242406e0e0SGreg Roach        }
2252406e0e0SGreg Roach
2262406e0e0SGreg Roach        foreach ($individual->spouseFamilies() as $family) {
2272406e0e0SGreg Roach            $spouse = $family->spouse($individual);
2282406e0e0SGreg Roach            if ($spouse instanceof Individual) {
2292406e0e0SGreg Roach                $meta_facts[] = I18N::translate('Spouse') . ' ' . $spouse->fullName();
2302406e0e0SGreg Roach            }
2312406e0e0SGreg Roach
2322406e0e0SGreg Roach            $child_names = $family->children()->map(static function (Individual $individual): string {
2332406e0e0SGreg Roach                return e($individual->getAllNames()[0]['givn']);
2342406e0e0SGreg Roach            })->implode(', ');
2352406e0e0SGreg Roach
2362406e0e0SGreg Roach
2372406e0e0SGreg Roach            if ($child_names !== '') {
2382406e0e0SGreg Roach                $meta_facts[] = I18N::translate('Children') . ' ' . $child_names;
2392406e0e0SGreg Roach            }
2402406e0e0SGreg Roach        }
2412406e0e0SGreg Roach
2422406e0e0SGreg Roach        $meta_facts = array_map('strip_tags', $meta_facts);
2432406e0e0SGreg Roach        $meta_facts = array_map('trim', $meta_facts);
2442406e0e0SGreg Roach
2452406e0e0SGreg Roach        return implode(', ', $meta_facts);
2462406e0e0SGreg Roach    }
2472406e0e0SGreg Roach
2482406e0e0SGreg Roach    /**
249852ede8cSGreg Roach     * Which tabs should we show on this individual's page.
250852ede8cSGreg Roach     * We don't show empty tabs.
251852ede8cSGreg Roach     *
252852ede8cSGreg Roach     * @param Individual $individual
253852ede8cSGreg Roach     *
254b5c8fd7eSGreg Roach     * @return Collection<ModuleSidebarInterface>
255852ede8cSGreg Roach     */
256852ede8cSGreg Roach    public function getSidebars(Individual $individual): Collection
257852ede8cSGreg Roach    {
258852ede8cSGreg Roach        return $this->module_service->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
259852ede8cSGreg Roach            ->filter(static function (ModuleSidebarInterface $sidebar) use ($individual): bool {
260852ede8cSGreg Roach                return $sidebar->hasSidebarContent($individual);
261852ede8cSGreg Roach            });
262852ede8cSGreg Roach    }
263852ede8cSGreg Roach
264852ede8cSGreg Roach    /**
265852ede8cSGreg Roach     * Which tabs should we show on this individual's page.
266852ede8cSGreg Roach     * We don't show empty tabs.
267852ede8cSGreg Roach     *
268852ede8cSGreg Roach     * @param Individual $individual
269852ede8cSGreg Roach     *
270b5c8fd7eSGreg Roach     * @return Collection<ModuleTabInterface>
271852ede8cSGreg Roach     */
272852ede8cSGreg Roach    public function getTabs(Individual $individual): Collection
273852ede8cSGreg Roach    {
274852ede8cSGreg Roach        return $this->module_service->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
275852ede8cSGreg Roach            ->filter(static function (ModuleTabInterface $tab) use ($individual): bool {
276852ede8cSGreg Roach                return $tab->hasTabContent($individual);
277852ede8cSGreg Roach            });
278852ede8cSGreg Roach    }
279852ede8cSGreg Roach
280852ede8cSGreg Roach    /**
281852ede8cSGreg Roach     * What are the significant elements of this page?
282852ede8cSGreg Roach     * The layout will need them to generate URLs for charts and reports.
283852ede8cSGreg Roach     *
284852ede8cSGreg Roach     * @param Individual $individual
285852ede8cSGreg Roach     *
286852ede8cSGreg Roach     * @return stdClass
287852ede8cSGreg Roach     */
288852ede8cSGreg Roach    private function significant(Individual $individual): stdClass
289852ede8cSGreg Roach    {
290852ede8cSGreg Roach        [$surname] = explode(',', $individual->sortName());
291852ede8cSGreg Roach
292852ede8cSGreg Roach        $family = $individual->childFamilies()->merge($individual->spouseFamilies())->first();
293852ede8cSGreg Roach
294852ede8cSGreg Roach        return (object) [
295852ede8cSGreg Roach            'family'     => $family,
296852ede8cSGreg Roach            'individual' => $individual,
297852ede8cSGreg Roach            'surname'    => $surname,
298852ede8cSGreg Roach        ];
299852ede8cSGreg Roach    }
300852ede8cSGreg Roach}
301