xref: /webtrees/app/Elements/EventsRecorded.php (revision 3a3c35160db2b0ae62787c386f75885682bfd3bd)
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\I18N;
23use Fisharebest\Webtrees\Registry;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Support\Collection;
26use Ramsey\Uuid\Uuid;
27
28use function array_map;
29use function explode;
30use function implode;
31use function strtoupper;
32use function trim;
33use function view;
34
35/**
36 * EVENTS_RECORDED := {Size=1:90}
37 * [<EVENT_ATTRIBUTE_TYPE> | <EVENTS_RECORDED>, <EVENT_ATTRIBUTE_TYPE>]
38 * An enumeration of the different kinds of events that were recorded in a
39 * particular source. Each enumeration is separated by a comma. Such as a
40 * parish register of births, deaths, and marriages would be BIRT, DEAT, MARR.
41 */
42class EventsRecorded extends AbstractElement
43{
44    protected const SUBTAGS = [
45        'DATE' => '0:1',
46        'PLAC' => '0:1',
47    ];
48
49    protected const EVENTS_RECORDED = [
50        'INDI:ADOP',
51        'INDI:BAPM',
52        'INDI:BARM',
53        'INDI:BASM',
54        'INDI:BIRT',
55        'INDI:BLES',
56        'INDI:BURI',
57        'INDI:CAST',
58        'INDI:CHR',
59        'INDI:CENS',
60        'INDI:CHRA',
61        'INDI:CONF',
62        'INDI:CREM',
63        'INDI:DEAT',
64        'INDI:DSCR',
65        'INDI:EDUC',
66        'INDI:EMIG',
67        'INDI:FCOM',
68        'INDI:GRAD',
69        'INDI:IDNO',
70        'INDI:IMMI',
71        'INDI:NATI',
72        'INDI:NATU',
73        'INDI:NCHI',
74        'INDI:NMR',
75        'INDI:OCCU',
76        'INDI:ORDN',
77        'INDI:PROB',
78        'INDI:PROP',
79        'INDI:RELI',
80        'INDI:RESI',
81        'INDI:RETI',
82        'INDI:SSN',
83        'INDI:TITL',
84        'INDI:WILL',
85        'FAM:ANUL',
86        'FAM:DIV',
87        'FAM:DIVF',
88        'FAM:ENGA',
89        'FAM:MARB',
90        'FAM:MARC',
91        'FAM:MARL',
92        'FAM:MARS',
93        'FAM:MARR',
94    ];
95
96    /**
97     * Convert a value to a canonical form.
98     *
99     * @param string $value
100     *
101     * @return string
102     */
103    public function canonical(string $value): string
104    {
105        $value = strtoupper(strtr(parent::canonical($value), [' ' => ',']));
106
107        while (str_contains($value, ',,')) {
108            $value = strtr($value, [',,' => ',']);
109        }
110
111        return trim($value, ',');
112    }
113
114    /**
115     * An edit control for this data.
116     *
117     * @param string $id
118     * @param string $name
119     * @param string $value
120     * @param Tree   $tree
121     *
122     * @return string
123     */
124    public function edit(string $id, string $name, string $value, Tree $tree): string
125    {
126        $factory = Registry::elementFactory();
127
128        $options = Collection::make(self::EVENTS_RECORDED)
129            ->mapWithKeys(static function (string $tag) use ($factory): array {
130                return [explode(':', $tag)[1] => $factory->make($tag)->label()];
131            })
132            ->sort()
133            ->all();
134
135        $id2 = Uuid::uuid4()->toString();
136
137        // Our form element name contains "[]", and multiple selections would create multiple values.
138        $hidden = '<input type="hidden" id="' . e($id) . '" name="' . e($name) . '" value="' . e($value) . '" />';
139        // Combine them into a single value.
140        $js = 'document.getElementById("' . $id2 . '").addEventListener("change", function () { document.getElementById("' . $id . '").value = Array.from(document.getElementById("' . $id2 . '").selectedOptions).map(x => x.value).join(","); });';
141
142        return view('components/select', [
143            'class'    => 'tom-select',
144            'name'     => '',
145            'id'       => $id2,
146            'options'  => $options,
147            'selected' => explode(',', strtr($value, [' ' => ''])),
148        ]) . $hidden . '<script>' . $js . '</script>';
149    }
150
151    /**
152     * Display the value of this type of element.
153     *
154     * @param string $value
155     * @param Tree   $tree
156     *
157     * @return string
158     */
159    public function value(string $value, Tree $tree): string
160    {
161        $tags = explode(',', $this->canonical($value));
162
163        $events = array_map(static function (string $tag): string {
164            foreach (['INDI', 'FAM'] as $record_type) {
165                $element = Registry::elementFactory()->make($record_type . ':' . $tag);
166
167                if (!$element instanceof UnknownElement) {
168                    return $element->label();
169                }
170            }
171
172            return e($tag);
173        }, $tags);
174
175        return implode(I18N::$list_separator, $events);
176    }
177}
178