Issues (2559)

app/Elements/Uid.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Elements;
21
22
use Fisharebest\Webtrees\I18N;
23
use Fisharebest\Webtrees\Registry;
24
use Fisharebest\Webtrees\Tree;
25
26
use function e;
27
use function preg_match;
28
29
/**
30
 * UID fields
31
 */
32
class Uid extends AbstractElement
33
{
34
    protected const int MAXIMUM_LENGTH = 36;
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 34 at column 24
Loading history...
35
36
    public function canonical(string $value): string
37
    {
38
        $value = strtolower(parent::canonical($value));
39
40
        if (preg_match('/([0-9a-f]{8})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{12})/', $value, $match) === 1) {
41
            return $match[1] . '-' . $match[2] . '-' . $match [3] . '-' . $match[4] . '-' . $match[5];
42
        }
43
44
        return Registry::idFactory()->uuid();
45
    }
46
47
48
    public function edit(string $id, string $name, string $value, Tree $tree): string
49
    {
50
        return
51
            '<div class="input-group mb-3">' .
52
            parent::edit($id, $name, $value, $tree) .
53
            '<button type="button" class="input-group-text btn btn-primary" id="create-' . e($id) . '">' .
54
            I18N::translate('create') .
55
            '</button>' .
56
            '</div>' .
57
            '<script>' .
58
            'document.getElementById("create-' . e($id) . '").addEventListener("click", function(event) {' .
59
            ' document.getElementById("' . e($id) . '").value="' . e(Registry::idFactory()->uuid()) . '";' .
60
            '})' .
61
            '</script>';
62
    }
63
}
64