xref: /webtrees/app/Elements/XrefSource.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
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\Elements;
21
22use Fisharebest\Webtrees\Gedcom;
23use Fisharebest\Webtrees\Http\RequestHandlers\CreateSourceModal;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Tree;
27
28use function e;
29use function route;
30use function str_ends_with;
31use function str_starts_with;
32use function trim;
33use function view;
34
35/**
36 * XREF:SOUR := {Size=1:22}
37 * A pointer to, or a cross-reference identifier of, a SOURce record.
38 */
39class XrefSource extends AbstractXrefElement
40{
41    protected const SUBTAGS = [
42        'PAGE' => '0:1',
43        'EVEN' => '0:1',
44        'DATA' => '0:1',
45        'OBJE' => '0:M',
46        'NOTE' => '0:M',
47        'QUAY' => '0:1',
48    ];
49
50    /**
51     * An edit control for this data.
52     *
53     * @param string $id
54     * @param string $name
55     * @param string $value
56     * @param Tree   $tree
57     *
58     * @return string
59     */
60    public function edit(string $id, string $name, string $value, Tree $tree): string
61    {
62        // Other applications create sources with text, rather than XREFs
63        if ($value === '' || preg_match('/^@' . Gedcom::REGEX_XREF . '@$/', $value)) {
64            $select = view('components/select-source', [
65                'id'     => $id,
66                'name'   => $name,
67                'source' => Registry::sourceFactory()->make(trim($value, '@'), $tree),
68                'tree'   => $tree,
69                'at'     => '@',
70            ]);
71
72            return
73                '<div class="input-group">' .
74                '<button class="btn btn-secondary" type="button" data-bs-toggle="modal" data-bs-backdrop="static" data-bs-target="#wt-ajax-modal" data-wt-href="' . e(route(CreateSourceModal::class, ['tree' => $tree->name()])) . '" data-wt-select-id="' . $id . '" title="' . I18N::translate('Create a source') . '">' .
75                view('icons/add') .
76                '</button>' .
77                $select .
78                '</div>';
79        }
80
81        return $this->editTextArea($id, $name, $value);
82    }
83
84    /**
85     * Display the value of this type of element.
86     *
87     * @param string $value
88     * @param Tree   $tree
89     *
90     * @return string
91     */
92    public function value(string $value, Tree $tree): string
93    {
94        if (str_starts_with($value, '@') && str_ends_with($value, '@')) {
95            return $this->valueXrefLink($value, $tree, Registry::sourceFactory());
96        }
97
98        // Inline sources are deprecated - but used by some historic events
99        return $this->valueFormatted($value, $tree);
100    }
101}
102