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