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