xref: /webtrees/app/Elements/AbstractXrefElement.php (revision 45ebc6c6221f90468111ed2015b8449dca331d93)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\Elements;
21
22use Fisharebest\Webtrees\Gedcom;
23use Fisharebest\Webtrees\GedcomRecord;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Tree;
26
27use function e;
28use function preg_match;
29
30/**
31 * Common behaviour for all XREF links
32 */
33class AbstractXrefElement extends AbstractElement
34{
35    /**
36     * Should we collapse the children of this element when editing?
37     *
38     * @return bool
39     */
40    public function collapseChildren(): bool
41    {
42        return true;
43    }
44
45    /**
46     * Escape @ signs in a GEDCOM export.
47     *
48     * @param string $value
49     *
50     * @return string
51     */
52    public function escape(string $value): string
53    {
54        return $value;
55    }
56
57    /**
58     * Display the value of this type of element - convert XREFs to links.
59     *
60     * @param string $value
61     * @param Tree   $tree
62     * @param mixed  $factory We can type-hint this from PHP 7.4
63     *
64     * @return string
65     */
66    protected function valueXrefLink(string $value, Tree $tree, $factory): string
67    {
68        if ($value === '@VOID@') {
69            // I18N: The record was not recorded in this GEDCOM file.
70            return I18N::translate('Not recorded');
71        }
72
73        if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match)) {
74            $record = $factory->make($match[1], $tree);
75
76            if ($record instanceof GedcomRecord) {
77                return '<a href="' . e($record->url()) . '">' . $record->fullName() . '</a>';
78            }
79        }
80
81        return '<span class="error">' . e($value) . '</span>';
82    }
83}
84