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