Issues (165)

app/Elements/EventsRecorded.php (1 issue)

Labels
Severity
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
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
use Illuminate\Support\Collection;
26
27
use function array_map;
28
use function explode;
29
use function implode;
30
use function strtoupper;
31
use function trim;
32
use function view;
33
34
/**
35
 * EVENTS_RECORDED := {Size=1:90}
36
 * [<EVENT_ATTRIBUTE_TYPE> | <EVENTS_RECORDED>, <EVENT_ATTRIBUTE_TYPE>]
37
 * An enumeration of the different kinds of events that were recorded in a
38
 * particular source. Each enumeration is separated by a comma. Such as a
39
 * parish register of births, deaths, and marriages would be BIRT, DEAT, MARR.
40
 */
41
class EventsRecorded extends AbstractElement
42
{
43
    protected const SUBTAGS = [
44
        'DATE' => '0:1',
45
        'PLAC' => '0:1',
46
    ];
47
48
    protected const EVENTS_RECORDED = [
49
        'INDI:ADOP',
50
        'INDI:BAPM',
51
        'INDI:BARM',
52
        'INDI:BASM',
53
        'INDI:BIRT',
54
        'INDI:BLES',
55
        'INDI:BURI',
56
        'INDI:CAST',
57
        'INDI:CHR',
58
        'INDI:CENS',
59
        'INDI:CHRA',
60
        'INDI:CONF',
61
        'INDI:CREM',
62
        'INDI:DEAT',
63
        'INDI:DSCR',
64
        'INDI:EDUC',
65
        'INDI:EMIG',
66
        'INDI:FCOM',
67
        'INDI:GRAD',
68
        'INDI:IDNO',
69
        'INDI:IMMI',
70
        'INDI:NATI',
71
        'INDI:NATU',
72
        'INDI:NCHI',
73
        'INDI:NMR',
74
        'INDI:OCCU',
75
        'INDI:ORDN',
76
        'INDI:PROB',
77
        'INDI:PROP',
78
        'INDI:RELI',
79
        'INDI:RESI',
80
        'INDI:RETI',
81
        'INDI:SSN',
82
        'INDI:TITL',
83
        'INDI:WILL',
84
        'FAM:ANUL',
85
        'FAM:DIV',
86
        'FAM:DIVF',
87
        'FAM:ENGA',
88
        'FAM:MARB',
89
        'FAM:MARC',
90
        'FAM:MARL',
91
        'FAM:MARS',
92
        'FAM:MARR',
93
    ];
94
95
    /**
96
     * Convert a value to a canonical form.
97
     *
98
     * @param string $value
99
     *
100
     * @return string
101
     */
102
    public function canonical(string $value): string
103
    {
104
        $value = strtoupper(strtr(parent::canonical($value), [' ' => ',']));
105
106
        while (str_contains($value, ',,')) {
107
            $value = strtr($value, [',,' => ',']);
108
        }
109
110
        return trim($value, ',');
111
    }
112
113
    /**
114
     * An edit control for this data.
115
     *
116
     * @param string $id
117
     * @param string $name
118
     * @param string $value
119
     * @param Tree   $tree
120
     *
121
     * @return string
122
     */
123
    public function edit(string $id, string $name, string $value, Tree $tree): string
124
    {
125
        $factory = Registry::elementFactory();
126
127
        $options = Collection::make(self::EVENTS_RECORDED)
0 ignored issues
show
self::EVENTS_RECORDED of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::make(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

127
        $options = Collection::make(/** @scrutinizer ignore-type */ self::EVENTS_RECORDED)
Loading history...
128
            ->mapWithKeys(static fn (string $tag): array => [explode(':', $tag)[1] => $factory->make($tag)->label()])
129
            ->sort()
130
            ->all();
131
132
        $id2 = Registry::idFactory()->id();
133
134
        // Our form element name contains "[]", and multiple selections would create multiple values.
135
        $hidden = '<input type="hidden" id="' . e($id) . '" name="' . e($name) . '" value="' . e($value) . '" />';
136
        // Combine them into a single value.
137
        $js = 'document.getElementById("' . $id2 . '").addEventListener("change", function () { document.getElementById("' . $id . '").value = Array.from(document.getElementById("' . $id2 . '").selectedOptions).map(x => x.value).join(","); });';
138
139
        return view('components/select', [
140
            'class'    => 'tom-select',
141
            'name'     => '',
142
            'id'       => $id2,
143
            'options'  => $options,
144
            'selected' => explode(',', strtr($value, [' ' => ''])),
145
        ]) . $hidden . '<script>' . $js . '</script>';
146
    }
147
148
    /**
149
     * Display the value of this type of element.
150
     *
151
     * @param string $value
152
     * @param Tree   $tree
153
     *
154
     * @return string
155
     */
156
    public function value(string $value, Tree $tree): string
157
    {
158
        $tags = explode(',', $this->canonical($value));
159
160
        $events = array_map(static function (string $tag): string {
161
            foreach (['INDI', 'FAM'] as $record_type) {
162
                $element = Registry::elementFactory()->make($record_type . ':' . $tag);
163
164
                if (!$element instanceof UnknownElement) {
165
                    return $element->label();
166
                }
167
            }
168
169
            return e($tag);
170
        }, $tags);
171
172
        return implode(I18N::$list_separator, $events);
173
    }
174
}
175