xref: /webtrees/app/Elements/AbstractEventElement.php (revision 8634febf6487fcb3ee7b59e88f828071a22cba22)
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\I18N;
23use Fisharebest\Webtrees\Tree;
24
25use function e;
26
27/**
28 * Events which can take "Y" to indicate that they occurred, but date/place are unknown.
29 */
30class AbstractEventElement extends AbstractElement
31{
32    /**
33     * Convert a value to a canonical form.
34     *
35     * @param string $value
36     *
37     * @return string
38     */
39    public function canonical(string $value): string
40    {
41        $value = parent::canonical($value);
42
43        if ($value === 'y') {
44            return 'Y';
45        }
46
47        return $value;
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        return
63            '<div class="form-check">' .
64            $this->editHidden($id, $name, $value ? 'Y' : '') .
65            '<input class="form-check-input" type="checkbox" value="Y" id="' . e($id) . '-check" ' . ($value ? 'checked="checked"' : '') . ' />' .
66            '<label class="form-check-label" for="' . e($id) . '-check">' .
67            I18N::translate('This event occurred, but the details are unknown.') .
68            '</label>' .
69            '</div>' .
70            '<script>' .
71            'document.getElementById("' . e($id) . '-check").addEventListener("change", function () {' .
72            'document.getElementById("' . e($id) . '").value = this.checked ? "Y" : "";' .
73            '})' .
74            '</script>';
75    }
76
77    /**
78     * Display the value of this type of element.
79     *
80     * @param string $value
81     * @param Tree   $tree
82     *
83     * @return string
84     */
85    public function value(string $value, Tree $tree): string
86    {
87        if ($value === 'Y') {
88            return I18N::translate('yes');
89        }
90
91        return parent::value($value, $tree);
92    }
93}
94