xref: /webtrees/app/Http/RequestHandlers/IndividualPage.php (revision 6fcafd028e511367c3fcdbfaa31aa05a85bf85c0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fig\Http\Message\StatusCodeInterface;
23use Fisharebest\Webtrees\Age;
24use Fisharebest\Webtrees\Auth;
25use Fisharebest\Webtrees\Date;
26use Fisharebest\Webtrees\Fact;
27use Fisharebest\Webtrees\Http\ViewResponseTrait;
28use Fisharebest\Webtrees\I18N;
29use Fisharebest\Webtrees\Individual;
30use Fisharebest\Webtrees\Media;
31use Fisharebest\Webtrees\MediaFile;
32use Fisharebest\Webtrees\Module\ModuleShareInterface;
33use Fisharebest\Webtrees\Module\ModuleSidebarInterface;
34use Fisharebest\Webtrees\Module\ModuleTabInterface;
35use Fisharebest\Webtrees\Registry;
36use Fisharebest\Webtrees\Services\ClipboardService;
37use Fisharebest\Webtrees\Services\ModuleService;
38use Fisharebest\Webtrees\Services\UserService;
39use Fisharebest\Webtrees\Tree;
40use Illuminate\Support\Collection;
41use Psr\Http\Message\ResponseInterface;
42use Psr\Http\Message\ServerRequestInterface;
43use Psr\Http\Server\RequestHandlerInterface;
44use stdClass;
45
46use function array_map;
47use function assert;
48use function date;
49use function e;
50use function explode;
51use function implode;
52use function is_string;
53use function redirect;
54use function route;
55use function strtoupper;
56use function view;
57
58/**
59 * Show an individual's page.
60 */
61class IndividualPage implements RequestHandlerInterface
62{
63    use ViewResponseTrait;
64
65    /** @var ClipboardService */
66    private $clipboard_service;
67
68    /** @var ModuleService */
69    private $module_service;
70
71    /** @var UserService */
72    private $user_service;
73
74    /**
75     * IndividualPage constructor.
76     *
77     * @param ClipboardService $clipboard_service
78     * @param ModuleService    $module_service
79     * @param UserService      $user_service
80     */
81    public function __construct(ClipboardService $clipboard_service, ModuleService $module_service, UserService $user_service)
82    {
83        $this->clipboard_service = $clipboard_service;
84        $this->module_service    = $module_service;
85        $this->user_service      = $user_service;
86    }
87
88    /**
89     * @param ServerRequestInterface $request
90     *
91     * @return ResponseInterface
92     */
93    public function handle(ServerRequestInterface $request): ResponseInterface
94    {
95        $tree = $request->getAttribute('tree');
96        assert($tree instanceof Tree);
97
98        $xref = $request->getAttribute('xref');
99        assert(is_string($xref));
100
101        $individual = Registry::individualFactory()->make($xref, $tree);
102        $individual = Auth::checkIndividualAccess($individual);
103
104        // Redirect to correct xref/slug
105        $slug = Registry::slugFactory()->make($individual);
106
107        if ($individual->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
108            return redirect($individual->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
109        }
110
111        // What images are linked to this individual
112        $individual_media = new Collection();
113        foreach ($individual->facts(['OBJE']) as $fact) {
114            $media_object = $fact->target();
115            if ($media_object instanceof Media) {
116                $media_file = $media_object->firstImageFile();
117                if ($media_file instanceof MediaFile) {
118                    $individual_media->add($media_file);
119                }
120            }
121        }
122
123        $name_records = $individual->facts(['NAME'])->map(static function (Fact $fact): string {
124            return view('individual-name', ['fact' => $fact]);
125        });
126
127        $sex_records = $individual->facts(['SEX'])->map(static function (Fact $fact): string {
128            return view('individual-sex', ['fact' => $fact]);
129        });
130
131        // If this individual is linked to a user account, show the link
132        $user_link = '';
133        if (Auth::isAdmin()) {
134            $users = $this->user_service->findByIndividual($individual);
135            foreach ($users as $user) {
136                $user_link = ' —  <a href="' . e(route(UserListPage::class, ['filter' => $user->email()])) . '">' . e($user->userName()) . '</a>';
137            }
138        }
139
140        $shares = $this->module_service->findByInterface(ModuleShareInterface::class)
141            ->map(fn (ModuleShareInterface $module) => $module->share($individual))
142            ->filter();
143
144        return $this->viewResponse('individual-page', [
145            'age'              => $this->ageString($individual),
146            'clipboard_facts'  => $this->clipboard_service->pastableFacts($individual),
147            'individual'       => $individual,
148            'individual_media' => $individual_media,
149            'meta_description' => $this->metaDescription($individual),
150            'meta_robots'      => 'index,follow',
151            'name_records'     => $name_records,
152            'sex_records'      => $sex_records,
153            'shares'           => $shares,
154            'sidebars'         => $this->getSidebars($individual),
155            'tabs'             => $this->getTabs($individual),
156            'significant'      => $this->significant($individual),
157            'title'            => $individual->fullName() . ' ' . $individual->lifespan(),
158            'tree'             => $tree,
159            'user_link'        => $user_link,
160        ]);
161    }
162
163    /**
164     * @param Individual $individual
165     *
166     * @return string
167     */
168    private function ageString(Individual $individual): string
169    {
170        if ($individual->isDead()) {
171            // If dead, show age at death
172            $age = (string) new Age($individual->getBirthDate(), $individual->getDeathDate());
173
174            if ($age === '') {
175                return '';
176            }
177
178            switch ($individual->sex()) {
179                case 'M':
180                    /* I18N: The age of an individual at a given date */
181                    return I18N::translateContext('Male', '(aged %s)', $age);
182                case 'F':
183                    /* I18N: The age of an individual at a given date */
184                    return I18N::translateContext('Female', '(aged %s)', $age);
185                default:
186                    /* I18N: The age of an individual at a given date */
187                    return I18N::translate('(aged %s)', $age);
188            }
189        }
190
191        // If living, show age today
192        $today = new Date(strtoupper(date('d M Y')));
193        $age   = (string) new Age($individual->getBirthDate(), $today);
194
195        if ($age === '') {
196            return '';
197        }
198
199        /* I18N: The current age of a living individual */
200        return I18N::translate('(age %s)', $age);
201    }
202
203    /**
204     * @param Individual $individual
205     *
206     * @return string
207     */
208    private function metaDescription(Individual $individual): string
209    {
210        $meta_facts = [];
211
212        $birth_date  = $individual->getBirthDate();
213        $birth_place = $individual->getBirthPlace();
214
215        if ($birth_date->isOK() || $birth_place->id() !== 0) {
216            $meta_facts[] = I18N::translate('Birth') . ' ' .
217                $birth_date->display(false, null, false) . ' ' .
218                $birth_place->placeName();
219        }
220
221        $death_date  = $individual->getDeathDate();
222        $death_place = $individual->getDeathPlace();
223
224        if ($death_date->isOK() || $death_place->id() !== 0) {
225            $meta_facts[] = I18N::translate('Death') . ' ' .
226                $death_date->display(false, null, false) . ' ' .
227                $death_place->placeName();
228        }
229
230        foreach ($individual->childFamilies() as $family) {
231            $meta_facts[] = I18N::translate('Parents') . ' ' . $family->fullName();
232        }
233
234        foreach ($individual->spouseFamilies() as $family) {
235            $spouse = $family->spouse($individual);
236            if ($spouse instanceof Individual) {
237                $meta_facts[] = I18N::translate('Spouse') . ' ' . $spouse->fullName();
238            }
239
240            $child_names = $family->children()->map(static function (Individual $individual): string {
241                return e($individual->getAllNames()[0]['givn']);
242            })->implode(', ');
243
244
245            if ($child_names !== '') {
246                $meta_facts[] = I18N::translate('Children') . ' ' . $child_names;
247            }
248        }
249
250        $meta_facts = array_map('strip_tags', $meta_facts);
251        $meta_facts = array_map('trim', $meta_facts);
252
253        return implode(', ', $meta_facts);
254    }
255
256    /**
257     * Which tabs should we show on this individual's page.
258     * We don't show empty tabs.
259     *
260     * @param Individual $individual
261     *
262     * @return Collection<ModuleSidebarInterface>
263     */
264    public function getSidebars(Individual $individual): Collection
265    {
266        return $this->module_service->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
267            ->filter(static function (ModuleSidebarInterface $sidebar) use ($individual): bool {
268                return $sidebar->hasSidebarContent($individual);
269            });
270    }
271
272    /**
273     * Which tabs should we show on this individual's page.
274     * We don't show empty tabs.
275     *
276     * @param Individual $individual
277     *
278     * @return Collection<ModuleTabInterface>
279     */
280    public function getTabs(Individual $individual): Collection
281    {
282        return $this->module_service->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
283            ->filter(static function (ModuleTabInterface $tab) use ($individual): bool {
284                return $tab->hasTabContent($individual);
285            });
286    }
287
288    /**
289     * What are the significant elements of this page?
290     * The layout will need them to generate URLs for charts and reports.
291     *
292     * @param Individual $individual
293     *
294     * @return stdClass
295     */
296    private function significant(Individual $individual): stdClass
297    {
298        [$surname] = explode(',', $individual->sortName());
299
300        $family = $individual->childFamilies()->merge($individual->spouseFamilies())->first();
301
302        return (object) [
303            'family'     => $family,
304            'individual' => $individual,
305            'surname'    => $surname,
306        ];
307    }
308}
309