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