xref: /webtrees/app/Http/RequestHandlers/IndividualPage.php (revision 322a4a7419ca3e33c2e67cb5b1860af140a77c7f)
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;
43use stdClass;
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 route;
54use function strtoupper;
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(ClipboardService $clipboard_service, ModuleService $module_service, UserService $user_service)
77    {
78        $this->clipboard_service = $clipboard_service;
79        $this->module_service    = $module_service;
80        $this->user_service      = $user_service;
81    }
82
83    /**
84     * @param ServerRequestInterface $request
85     *
86     * @return ResponseInterface
87     */
88    public function handle(ServerRequestInterface $request): ResponseInterface
89    {
90        $tree = $request->getAttribute('tree');
91        assert($tree instanceof Tree);
92
93        $xref = $request->getAttribute('xref');
94        assert(is_string($xref));
95
96        $individual = Registry::individualFactory()->make($xref, $tree);
97        $individual = Auth::checkIndividualAccess($individual);
98
99        // Redirect to correct xref/slug
100        $slug = Registry::slugFactory()->make($individual);
101
102        if ($individual->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
103            return redirect($individual->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
104        }
105
106        // What images are linked to this individual
107        $individual_media = new Collection();
108        foreach ($individual->facts(['OBJE']) as $fact) {
109            $media_object = $fact->target();
110            if ($media_object instanceof Media) {
111                $media_file = $media_object->firstImageFile();
112                if ($media_file instanceof MediaFile) {
113                    $individual_media->add($media_file);
114                }
115            }
116        }
117
118        // If this individual is linked to a user account, show the link
119        $user_link = '';
120        if (Auth::isAdmin()) {
121            $users = $this->user_service->findByIndividual($individual);
122            foreach ($users as $user) {
123                $user_link = ' —  <a href="' . e(route(UserListPage::class, ['filter' => $user->email()])) . '">' . e($user->userName()) . '</a>';
124            }
125        }
126
127        $shares = $this->module_service->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            'user_link'        => $user_link,
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->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
252            ->filter(static function (ModuleSidebarInterface $sidebar) use ($individual): bool {
253                return $sidebar->hasSidebarContent($individual);
254            });
255    }
256
257    /**
258     * Which tabs should we show on this individual's page.
259     * We don't show empty tabs.
260     *
261     * @param Individual $individual
262     *
263     * @return Collection<ModuleTabInterface>
264     */
265    public function getTabs(Individual $individual): Collection
266    {
267        return $this->module_service->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
268            ->filter(static function (ModuleTabInterface $tab) use ($individual): bool {
269                return $tab->hasTabContent($individual);
270            });
271    }
272
273    /**
274     * What are the significant elements of this page?
275     * The layout will need them to generate URLs for charts and reports.
276     *
277     * @param Individual $individual
278     *
279     * @return stdClass
280     */
281    private function significant(Individual $individual): stdClass
282    {
283        [$surname] = explode(',', $individual->sortName());
284
285        $family = $individual->childFamilies()->merge($individual->spouseFamilies())->first();
286
287        return (object) [
288            'family'     => $family,
289            'individual' => $individual,
290            'surname'    => $surname,
291        ];
292    }
293}
294