xref: /webtrees/app/Http/RequestHandlers/IndividualPage.php (revision 2cebb4b438c7c397058c1bb811d1e0ebbdc65e6b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://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\Auth;
24use Fisharebest\Webtrees\Date;
25use Fisharebest\Webtrees\Fact;
26use Fisharebest\Webtrees\Functions\FunctionsDate;
27use Fisharebest\Webtrees\Functions\FunctionsPrint;
28use Fisharebest\Webtrees\Functions\FunctionsPrintFacts;
29use Fisharebest\Webtrees\GedcomCode\GedcomCodeName;
30use Fisharebest\Webtrees\GedcomTag;
31use Fisharebest\Webtrees\Http\ViewResponseTrait;
32use Fisharebest\Webtrees\I18N;
33use Fisharebest\Webtrees\Individual;
34use Fisharebest\Webtrees\Media;
35use Fisharebest\Webtrees\MediaFile;
36use Fisharebest\Webtrees\Module\ModuleSidebarInterface;
37use Fisharebest\Webtrees\Module\ModuleTabInterface;
38use Fisharebest\Webtrees\Services\ClipboardService;
39use Fisharebest\Webtrees\Services\ModuleService;
40use Fisharebest\Webtrees\Services\UserService;
41use Fisharebest\Webtrees\Tree;
42use Illuminate\Support\Collection;
43use Psr\Http\Message\ResponseInterface;
44use Psr\Http\Message\ServerRequestInterface;
45use Psr\Http\Server\RequestHandlerInterface;
46use stdClass;
47
48use function array_map;
49use function assert;
50use function date;
51use function e;
52use function explode;
53use function implode;
54use function is_string;
55use function ob_get_clean;
56use function ob_start;
57use function preg_match_all;
58use function preg_replace;
59use function redirect;
60use function route;
61use function str_replace;
62use function strpos;
63use function strtoupper;
64use function view;
65
66use const PREG_SET_ORDER;
67
68/**
69 * Show an individual's page.
70 */
71class IndividualPage implements RequestHandlerInterface
72{
73    use ViewResponseTrait;
74
75    /** @var ClipboardService */
76    private $clipboard_service;
77
78    /** @var ModuleService */
79    private $module_service;
80
81    /** @var UserService */
82    private $user_service;
83
84    /**
85     * IndividualPage constructor.
86     *
87     * @param ClipboardService $clipboard_service
88     * @param ModuleService    $module_service
89     * @param UserService      $user_service
90     */
91    public function __construct(ClipboardService $clipboard_service, ModuleService $module_service, UserService $user_service)
92    {
93        $this->clipboard_service = $clipboard_service;
94        $this->module_service    = $module_service;
95        $this->user_service      = $user_service;
96    }
97
98    /**
99     * @param ServerRequestInterface $request
100     *
101     * @return ResponseInterface
102     */
103    public function handle(ServerRequestInterface $request): ResponseInterface
104    {
105        $tree = $request->getAttribute('tree');
106        assert($tree instanceof Tree);
107
108        $xref = $request->getAttribute('xref');
109        assert(is_string($xref));
110
111        $individual = Individual::getInstance($xref, $tree);
112        $individual = Auth::checkIndividualAccess($individual);
113
114        // Redirect to correct xref/slug
115        if ($individual->xref() !== $xref || $request->getAttribute('slug') !== $individual->slug()) {
116            return redirect($individual->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
117        }
118
119        // What is (was) the age of the individual
120        $bdate = $individual->getBirthDate();
121        $ddate = $individual->getDeathDate();
122        if ($bdate->isOK() && !$individual->isDead()) {
123            // If living display age
124            $age = ' (' . I18N::translate('age') . ' ' . FunctionsDate::getAgeAtEvent(Date::getAgeGedcom($bdate, new Date(strtoupper(date('d M Y'))))) . ')';
125        } elseif ($bdate->isOK() && $ddate->isOK()) {
126            // If dead, show age at death
127            $age = ' (' . I18N::translate('age') . ' ' . FunctionsDate::getAgeAtEvent(Date::getAgeGedcom($bdate, $ddate)) . ')';
128        } else {
129            $age = '';
130        }
131
132        // What images are linked to this individual
133        $individual_media = new Collection();
134        foreach ($individual->facts(['OBJE']) as $fact) {
135            $media_object = $fact->target();
136            if ($media_object instanceof Media) {
137                $media_file = $media_object->firstImageFile();
138                if ($media_file instanceof MediaFile) {
139                    $individual_media->add($media_file);
140                }
141            }
142        }
143
144        $name_records = new Collection();
145        foreach ($individual->facts(['NAME']) as $n => $name_fact) {
146            $name_records->add($this->formatNameRecord($tree, $n, $name_fact));
147        }
148
149        $sex_records = new Collection();
150        foreach ($individual->facts(['SEX']) as $n => $sex_fact) {
151            $sex_records->add($this->formatSexRecord($sex_fact));
152        }
153
154        // If this individual is linked to a user account, show the link
155        $user_link = '';
156        if (Auth::isAdmin()) {
157            $users = $this->user_service->findByIndividual($individual);
158            foreach ($users as $user) {
159                $user_link = ' —  <a href="' . e(route('admin-users', ['filter' => $user->email()])) . '">' . e($user->userName()) . '</a>';
160            }
161        }
162
163        return $this->viewResponse('individual-page', [
164            'age'              => $age,
165            'clipboard_facts'  => $this->clipboard_service->pastableFacts($individual, new Collection()),
166            'individual'       => $individual,
167            'individual_media' => $individual_media,
168            'meta_description' => $this->metaDescription($individual),
169            'meta_robots'      => 'index,follow',
170            'name_records'     => $name_records,
171            'sex_records'      => $sex_records,
172            'sidebars'         => $this->getSidebars($individual),
173            'tabs'             => $this->getTabs($individual),
174            'significant'      => $this->significant($individual),
175            'title'            => $individual->fullName() . ' ' . $individual->lifespan(),
176            'tree'             => $tree,
177            'user_link'        => $user_link,
178        ]);
179    }
180
181    /**
182     * @param Individual $individual
183     *
184     * @return string
185     */
186    private function metaDescription(Individual $individual): string
187    {
188        $meta_facts = [];
189
190        $birth_date  = $individual->getBirthDate();
191        $birth_place = $individual->getBirthPlace();
192
193        if ($birth_date->isOK() || $birth_place->id() !== 0) {
194            $meta_facts[] = I18N::translate('Birth') . ' ' .
195                $birth_date->display(false, null, false) . ' ' .
196                $birth_place->placeName();
197        }
198
199        $death_date  = $individual->getDeathDate();
200        $death_place = $individual->getDeathPlace();
201
202        if ($death_date->isOK() || $death_place->id() !== 0) {
203            $meta_facts[] = I18N::translate('Death') . ' ' .
204                $death_date->display(false, null, false) . ' ' .
205                $death_place->placeName();
206        }
207
208        foreach ($individual->childFamilies() as $family) {
209            $meta_facts[] = I18N::translate('Parents') . ' ' . $family->fullName();
210        }
211
212        foreach ($individual->spouseFamilies() as $family) {
213            $spouse = $family->spouse($individual);
214            if ($spouse instanceof Individual) {
215                $meta_facts[] = I18N::translate('Spouse') . ' ' . $spouse->fullName();
216            }
217
218            $child_names = $family->children()->map(static function (Individual $individual): string {
219                return e($individual->getAllNames()[0]['givn']);
220            })->implode(', ');
221
222
223            if ($child_names !== '') {
224                $meta_facts[] = I18N::translate('Children') . ' ' . $child_names;
225            }
226        }
227
228        $meta_facts = array_map('strip_tags', $meta_facts);
229        $meta_facts = array_map('trim', $meta_facts);
230
231        return implode(', ', $meta_facts);
232    }
233
234    /**
235     * Format a name record
236     *
237     * @param Tree $tree
238     * @param int  $n
239     * @param Fact $fact
240     *
241     * @return string
242     */
243    private function formatNameRecord(Tree $tree, $n, Fact $fact): string
244    {
245        $individual = $fact->record();
246
247        // Create a dummy record, so we can extract the formatted NAME value from it.
248        $dummy = new Individual(
249            'xref',
250            "0 @xref@ INDI\n1 DEAT Y\n" . $fact->gedcom(),
251            null,
252            $individual->tree()
253        );
254        $dummy->setPrimaryName(0); // Make sure we use the name from "1 NAME"
255
256        $container_class = 'card';
257        $content_class   = 'collapse';
258        $aria            = 'false';
259
260        if ($n === 0) {
261            $content_class = 'collapse show';
262            $aria          = 'true';
263        }
264        if ($fact->isPendingDeletion()) {
265            $container_class .= ' wt-old';
266        } elseif ($fact->isPendingAddition()) {
267            $container_class .= ' wt-new';
268        }
269
270        ob_start();
271        echo '<dl class="row mb-0"><dt class="col-md-4 col-lg-3">', I18N::translate('Name'), '</dt>';
272        echo '<dd class="col-md-8 col-lg-9">', $dummy->fullName(), '</dd>';
273        $ct = preg_match_all('/\n2 (\w+) (.*)/', $fact->gedcom(), $nmatch, PREG_SET_ORDER);
274        for ($i = 0; $i < $ct; $i++) {
275            $tag = $nmatch[$i][1];
276            if ($tag !== 'SOUR' && $tag !== 'NOTE' && $tag !== 'SPFX') {
277                echo '<dt class="col-md-4 col-lg-3">', GedcomTag::getLabel($tag, $individual), '</dt>';
278                echo '<dd class="col-md-8 col-lg-9">'; // Before using dir="auto" on this field, note that Gecko treats this as an inline element but WebKit treats it as a block element
279                if (isset($nmatch[$i][2])) {
280                    $name = e($nmatch[$i][2]);
281                    $name = str_replace('/', '', $name);
282                    $name = preg_replace('/(\S*)\*/', '<span class="starredname">\\1</span>', $name);
283                    switch ($tag) {
284                        case 'TYPE':
285                            echo GedcomCodeName::getValue($name, $individual);
286                            break;
287                        case 'SURN':
288                            // The SURN field is not necessarily the surname.
289                            // Where it is not a substring of the real surname, show it after the real surname.
290                            $surname = e($dummy->getAllNames()[0]['surname']);
291                            $surns   = preg_replace('/, */', ' ', $nmatch[$i][2]);
292                            if (strpos($dummy->getAllNames()[0]['surname'], $surns) !== false) {
293                                echo '<span dir="auto">' . $surname . '</span>';
294                            } else {
295                                echo I18N::translate('%1$s (%2$s)', '<span dir="auto">' . $surname . '</span>', '<span dir="auto">' . $name . '</span>');
296                            }
297                            break;
298                        default:
299                            echo '<span dir="auto">' . $name . '</span>';
300                            break;
301                    }
302                }
303                echo '</dd>';
304            }
305        }
306        echo '</dl>';
307        if (strpos($fact->gedcom(), "\n2 SOUR") !== false) {
308            echo '<div id="indi_sour" class="clearfix">', FunctionsPrintFacts::printFactSources($tree, $fact->gedcom(), 2), '</div>';
309        }
310        if (strpos($fact->gedcom(), "\n2 NOTE") !== false) {
311            echo '<div id="indi_note" class="clearfix">', FunctionsPrint::printFactNotes($tree, $fact->gedcom(), 2), '</div>';
312        }
313        $content = ob_get_clean();
314
315        if ($fact->canEdit()) {
316            $edit_links =
317                '<a class="btn btn-link" href="#" data-confirm="' . I18N::translate('Are you sure you want to delete this fact?') . '" data-post-url="' . e(route(DeleteFact::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref(), 'fact_id' => $fact->id()])) . '" title="' . I18N::translate('Delete this name') . '">' . view('icons/delete') . '<span class="sr-only">' . I18N::translate('Delete this name') . '</span></a>' .
318                '<a class="btn btn-link" href="' . e(route('edit-name', ['xref' => $individual->xref(), 'fact_id' => $fact->id(), 'tree' => $individual->tree()->name()])) . '" title="' . I18N::translate('Edit the name') . '">' . view('icons/edit') . '<span class="sr-only">' . I18N::translate('Edit the name') . '</span></a>';
319        } else {
320            $edit_links = '';
321        }
322
323        return '
324			<div class="' . $container_class . '">
325        <div class="card-header" role="tab" id="name-header-' . $n . '">
326		        <a data-toggle="collapse" data-parent="#individual-names" href="#name-content-' . $n . '" aria-expanded="' . $aria . '" aria-controls="name-content-' . $n . '">' . $dummy->fullName() . '</a>
327		      ' . $edit_links . '
328        </div>
329		    <div id="name-content-' . $n . '" class="' . $content_class . '" role="tabpanel" aria-labelledby="name-header-' . $n . '">
330		      <div class="card-body">' . $content . '</div>
331        </div>
332      </div>';
333    }
334
335    /**
336     * print information for a sex record
337     *
338     * @param Fact $fact
339     *
340     * @return string
341     */
342    private function formatSexRecord(Fact $fact): string
343    {
344        $individual = $fact->record();
345
346        switch ($fact->value()) {
347            case 'M':
348                $sex = I18N::translate('Male');
349                break;
350            case 'F':
351                $sex = I18N::translate('Female');
352                break;
353            default:
354                $sex = I18N::translateContext('unknown gender', 'Unknown');
355                break;
356        }
357
358        $container_class = 'card';
359        if ($fact->isPendingDeletion()) {
360            $container_class .= ' wt-old';
361        } elseif ($fact->isPendingAddition()) {
362            $container_class .= ' wt-new';
363        }
364
365        if ($individual->canEdit()) {
366            $edit_links = '<a class="btn btn-link" href="' . e(route(EditFact::class, ['xref' => $individual->xref(), 'fact_id' => $fact->id(), 'tree' => $individual->tree()->name()])) . '" title="' . I18N::translate('Edit the gender') . '">' . view('icons/edit') . '<span class="sr-only">' . I18N::translate('Edit the gender') . '</span></a>';
367        } else {
368            $edit_links = '';
369        }
370
371        return '
372		<div class="' . $container_class . '">
373			<div class="card-header" role="tab" id="name-header-add">
374				<div class="card-title mb-0">
375					<b>' . I18N::translate('Gender') . '</b> ' . $sex . $edit_links . '
376				</div>
377			</div>
378		</div>';
379    }
380
381    /**
382     * Which tabs should we show on this individual's page.
383     * We don't show empty tabs.
384     *
385     * @param Individual $individual
386     *
387     * @return Collection<ModuleSidebarInterface>
388     */
389    public function getSidebars(Individual $individual): Collection
390    {
391        return $this->module_service->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
392            ->filter(static function (ModuleSidebarInterface $sidebar) use ($individual): bool {
393                return $sidebar->hasSidebarContent($individual);
394            });
395    }
396
397    /**
398     * Which tabs should we show on this individual's page.
399     * We don't show empty tabs.
400     *
401     * @param Individual $individual
402     *
403     * @return Collection<ModuleTabInterface>
404     */
405    public function getTabs(Individual $individual): Collection
406    {
407        return $this->module_service->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
408            ->filter(static function (ModuleTabInterface $tab) use ($individual): bool {
409                return $tab->hasTabContent($individual);
410            });
411    }
412
413    /**
414     * What are the significant elements of this page?
415     * The layout will need them to generate URLs for charts and reports.
416     *
417     * @param Individual $individual
418     *
419     * @return stdClass
420     */
421    private function significant(Individual $individual): stdClass
422    {
423        [$surname] = explode(',', $individual->sortName());
424
425        $family = $individual->childFamilies()->merge($individual->spouseFamilies())->first();
426
427        return (object) [
428            'family'     => $family,
429            'individual' => $individual,
430            'surname'    => $surname,
431        ];
432    }
433}
434