xref: /webtrees/app/Elements/EventsRecorded.php (revision d97c97e899282d8da9c8b511a7cfb2e17d5e8568)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 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        return strtoupper(strtr(parent::canonical($value), [' ' => '']));
105    }
106
107    /**
108     * An edit control for this data.
109     *
110     * @param string $id
111     * @param string $name
112     * @param string $value
113     * @param Tree   $tree
114     *
115     * @return string
116     */
117    public function edit(string $id, string $name, string $value, Tree $tree): string
118    {
119        $factory = Registry::elementFactory();
120
121        $options = Collection::make(self::EVENTS_RECORDED)
122            ->mapWithKeys(static function (string $tag) use ($factory): array {
123                return [explode(':', $tag)[1] => $factory->make($tag)->label()];
124            })
125            ->sort()
126            ->all();
127
128        $id2 = Uuid::uuid4()->toString();
129
130        // Our form element name contains "[]", and multiple selections would create multiple values.
131        $hidden = '<input type="hidden" id="' . e($id) . '" name="' . e($name) . '" value="' . e($value) . '">';
132        // Combine them into a single value.
133        // The change event doesn't seem to fire for select2 controls, so use form.submit instead.
134        $js = 'document.getElementById("' . $id2 . '").form.addEventListener("submit", function () { document.getElementById("' . $id . '").value = Array.from(document.getElementById("' . $id2 . '").selectedOptions).map(x => x.value).join(","); });';
135
136        return view('components/select', [
137            'class'    => 'select2',
138            'name'     => '',
139            'id'       => $id2,
140            'options'  => $options,
141            'selected' => explode(',', strtr($value, [' ' => ''])),
142        ]) . $hidden . '<script>' . $js . '</script>';
143    }
144
145    /**
146     * Display the value of this type of element.
147     *
148     * @param string $value
149     * @param Tree   $tree
150     *
151     * @return string
152     */
153    public function value(string $value, Tree $tree): string
154    {
155        $tags = explode(',', $this->canonical($value));
156
157        $events = array_map(static function (string $tag): string {
158            foreach (['INDI', 'FAM'] as $record_type) {
159                $element = Registry::elementFactory()->make($record_type . ':' . $tag);
160
161                if (!$element instanceof UnknownElement) {
162                    return $element->label();
163                }
164            }
165
166            return $tag;
167        }, $tags);
168
169        return implode(I18N::$list_separator, $events);
170    }
171}
172