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