xref: /webtrees/app/Http/RequestHandlers/IndividualPage.php (revision 992ad95a3b71cdc2194e7663a57b01db9e5e7719)
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\Http\ViewResponseTrait;
27use Fisharebest\Webtrees\I18N;
28use Fisharebest\Webtrees\Individual;
29use Fisharebest\Webtrees\Media;
30use Fisharebest\Webtrees\MediaFile;
31use Fisharebest\Webtrees\Module\ModuleShareInterface;
32use Fisharebest\Webtrees\Module\ModuleSidebarInterface;
33use Fisharebest\Webtrees\Module\ModuleTabInterface;
34use Fisharebest\Webtrees\Registry;
35use Fisharebest\Webtrees\Services\ClipboardService;
36use Fisharebest\Webtrees\Services\ModuleService;
37use Fisharebest\Webtrees\Services\UserService;
38use Fisharebest\Webtrees\Tree;
39use Illuminate\Support\Collection;
40use Psr\Http\Message\ResponseInterface;
41use Psr\Http\Message\ServerRequestInterface;
42use Psr\Http\Server\RequestHandlerInterface;
43
44use function array_map;
45use function assert;
46use function date;
47use function e;
48use function explode;
49use function implode;
50use function is_string;
51use function redirect;
52use function strtoupper;
53
54/**
55 * Show an individual's page.
56 */
57class IndividualPage implements RequestHandlerInterface
58{
59    use ViewResponseTrait;
60
61    private ClipboardService $clipboard_service;
62
63    private ModuleService $module_service;
64
65    private UserService $user_service;
66
67    /**
68     * IndividualPage constructor.
69     *
70     * @param ClipboardService $clipboard_service
71     * @param ModuleService    $module_service
72     * @param UserService      $user_service
73     */
74    public function __construct(
75        ClipboardService $clipboard_service,
76        ModuleService $module_service,
77        UserService $user_service
78    ) {
79        $this->clipboard_service = $clipboard_service;
80        $this->module_service    = $module_service;
81        $this->user_service      = $user_service;
82    }
83
84    /**
85     * @param ServerRequestInterface $request
86     *
87     * @return ResponseInterface
88     */
89    public function handle(ServerRequestInterface $request): ResponseInterface
90    {
91        $tree = $request->getAttribute('tree');
92        assert($tree instanceof Tree);
93
94        $xref = $request->getAttribute('xref');
95        assert(is_string($xref));
96
97        $individual = Registry::individualFactory()->make($xref, $tree);
98        $individual = Auth::checkIndividualAccess($individual);
99
100        // Redirect to correct xref/slug
101        $slug = Registry::slugFactory()->make($individual);
102
103        if ($individual->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
104            return redirect($individual->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
105        }
106
107        // What images are linked to this individual
108        $individual_media = new Collection();
109        foreach ($individual->facts(['OBJE']) as $fact) {
110            $media_object = $fact->target();
111            if ($media_object instanceof Media) {
112                $media_file = $media_object->firstImageFile();
113                if ($media_file instanceof MediaFile) {
114                    $individual_media->add($media_file);
115                }
116            }
117        }
118
119        // If this individual is linked to a user account, show the link
120        if (Auth::isAdmin()) {
121            $users = $this->user_service->findByIndividual($individual);
122        } else {
123            $users = new Collection();
124        }
125
126        $shares = $this->module_service
127            ->findByInterface(ModuleShareInterface::class)
128            ->map(fn (ModuleShareInterface $module) => $module->share($individual))
129            ->filter();
130
131        return $this->viewResponse('individual-page', [
132            'age'              => $this->ageString($individual),
133            'clipboard_facts'  => $this->clipboard_service->pastableFacts($individual),
134            'individual_media' => $individual_media,
135            'meta_description' => $this->metaDescription($individual),
136            'meta_robots'      => 'index,follow',
137            'record'           => $individual,
138            'shares'           => $shares,
139            'sidebars'         => $this->getSidebars($individual),
140            'tabs'             => $this->getTabs($individual),
141            'significant'      => $this->significant($individual),
142            'title'            => $individual->fullName() . ' ' . $individual->lifespan(),
143            'tree'             => $tree,
144            'users'            => $users,
145        ]);
146    }
147
148    /**
149     * @param Individual $individual
150     *
151     * @return string
152     */
153    private function ageString(Individual $individual): string
154    {
155        if ($individual->isDead()) {
156            // If dead, show age at death
157            $age = (string) new Age($individual->getBirthDate(), $individual->getDeathDate());
158
159            if ($age === '') {
160                return '';
161            }
162
163            switch ($individual->sex()) {
164                case 'M':
165                    /* I18N: The age of an individual at a given date */
166                    return I18N::translateContext('Male', '(aged %s)', $age);
167                case 'F':
168                    /* I18N: The age of an individual at a given date */
169                    return I18N::translateContext('Female', '(aged %s)', $age);
170                default:
171                    /* I18N: The age of an individual at a given date */
172                    return I18N::translate('(aged %s)', $age);
173            }
174        }
175
176        // If living, show age today
177        $today = new Date(strtoupper(date('d M Y')));
178        $age   = (string) new Age($individual->getBirthDate(), $today);
179
180        if ($age === '') {
181            return '';
182        }
183
184        /* I18N: The current age of a living individual */
185        return I18N::translate('(age %s)', $age);
186    }
187
188    /**
189     * @param Individual $individual
190     *
191     * @return string
192     */
193    private function metaDescription(Individual $individual): string
194    {
195        $meta_facts = [];
196
197        $birth_date  = $individual->getBirthDate();
198        $birth_place = $individual->getBirthPlace();
199
200        if ($birth_date->isOK() || $birth_place->id() !== 0) {
201            $meta_facts[] = I18N::translate('Birth') . ' ' .
202                $birth_date->display(false, null, false) . ' ' .
203                $birth_place->placeName();
204        }
205
206        $death_date  = $individual->getDeathDate();
207        $death_place = $individual->getDeathPlace();
208
209        if ($death_date->isOK() || $death_place->id() !== 0) {
210            $meta_facts[] = I18N::translate('Death') . ' ' .
211                $death_date->display(false, null, false) . ' ' .
212                $death_place->placeName();
213        }
214
215        foreach ($individual->childFamilies() as $family) {
216            $meta_facts[] = I18N::translate('Parents') . ' ' . $family->fullName();
217        }
218
219        foreach ($individual->spouseFamilies() as $family) {
220            $spouse = $family->spouse($individual);
221            if ($spouse instanceof Individual) {
222                $meta_facts[] = I18N::translate('Spouse') . ' ' . $spouse->fullName();
223            }
224
225            $child_names = $family->children()->map(static function (Individual $individual): string {
226                return e($individual->getAllNames()[0]['givn']);
227            })->implode(', ');
228
229
230            if ($child_names !== '') {
231                $meta_facts[] = I18N::translate('Children') . ' ' . $child_names;
232            }
233        }
234
235        $meta_facts = array_map('strip_tags', $meta_facts);
236        $meta_facts = array_map('trim', $meta_facts);
237
238        return implode(', ', $meta_facts);
239    }
240
241    /**
242     * Which tabs should we show on this individual's page.
243     * We don't show empty tabs.
244     *
245     * @param Individual $individual
246     *
247     * @return Collection<ModuleSidebarInterface>
248     */
249    public function getSidebars(Individual $individual): Collection
250    {
251        return $this->module_service
252            ->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
253            ->filter(static function (ModuleSidebarInterface $sidebar) use ($individual): bool {
254                return $sidebar->hasSidebarContent($individual);
255            });
256    }
257
258    /**
259     * Which tabs should we show on this individual's page.
260     * We don't show empty tabs.
261     *
262     * @param Individual $individual
263     *
264     * @return Collection<ModuleTabInterface>
265     */
266    public function getTabs(Individual $individual): Collection
267    {
268        return $this->module_service
269            ->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
270            ->filter(static function (ModuleTabInterface $tab) use ($individual): bool {
271                return $tab->hasTabContent($individual);
272            });
273    }
274
275    /**
276     * What are the significant elements of this page?
277     * The layout will need them to generate URLs for charts and reports.
278     *
279     * @param Individual $individual
280     *
281     * @return object
282     */
283    private function significant(Individual $individual): object
284    {
285        [$surname] = explode(',', $individual->sortName());
286
287        $family = $individual->childFamilies()->merge($individual->spouseFamilies())->first();
288
289        return (object) [
290            'family'     => $family,
291            'individual' => $individual,
292            'surname'    => $surname,
293        ];
294    }
295}
296