xref: /webtrees/app/Elements/EventsRecorded.php (revision c8d78f19d0bdf4c0ec4728253bd37250d2e6cec4)
1c2ed51d1SGreg Roach<?php
2c2ed51d1SGreg Roach
3c2ed51d1SGreg Roach/**
4c2ed51d1SGreg Roach * webtrees: online genealogy
5c2ed51d1SGreg Roach * Copyright (C) 2021 webtrees development team
6c2ed51d1SGreg Roach * This program is free software: you can redistribute it and/or modify
7c2ed51d1SGreg Roach * it under the terms of the GNU General Public License as published by
8c2ed51d1SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9c2ed51d1SGreg Roach * (at your option) any later version.
10c2ed51d1SGreg Roach * This program is distributed in the hope that it will be useful,
11c2ed51d1SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12c2ed51d1SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13c2ed51d1SGreg Roach * GNU General Public License for more details.
14c2ed51d1SGreg Roach * You should have received a copy of the GNU General Public License
15c2ed51d1SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16c2ed51d1SGreg Roach */
17c2ed51d1SGreg Roach
18c2ed51d1SGreg Roachdeclare(strict_types=1);
19c2ed51d1SGreg Roach
20c2ed51d1SGreg Roachnamespace Fisharebest\Webtrees\Elements;
21c2ed51d1SGreg Roach
22c2ed51d1SGreg Roachuse Fisharebest\Webtrees\I18N;
23c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Registry;
24c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Tree;
25c2ed51d1SGreg Roachuse Illuminate\Support\Collection;
26c2ed51d1SGreg Roachuse Ramsey\Uuid\Uuid;
27c2ed51d1SGreg Roach
28c2ed51d1SGreg Roachuse function array_map;
29c2ed51d1SGreg Roachuse function explode;
30c2ed51d1SGreg Roachuse function implode;
313d2c98d1SGreg Roachuse function strpos;
32c2ed51d1SGreg Roachuse function strtoupper;
333d2c98d1SGreg Roachuse function trim;
34c2ed51d1SGreg Roachuse function view;
35c2ed51d1SGreg Roach
36c2ed51d1SGreg Roach/**
37c2ed51d1SGreg Roach * EVENTS_RECORDED := {Size=1:90}
38c2ed51d1SGreg Roach * [<EVENT_ATTRIBUTE_TYPE> | <EVENTS_RECORDED>, <EVENT_ATTRIBUTE_TYPE>]
39c2ed51d1SGreg Roach * An enumeration of the different kinds of events that were recorded in a
40c2ed51d1SGreg Roach * particular source. Each enumeration is separated by a comma. Such as a
41c2ed51d1SGreg Roach * parish register of births, deaths, and marriages would be BIRT, DEAT, MARR.
42c2ed51d1SGreg Roach */
43c2ed51d1SGreg Roachclass EventsRecorded extends AbstractElement
44c2ed51d1SGreg Roach{
45c2ed51d1SGreg Roach    protected const SUBTAGS = [
46c2ed51d1SGreg Roach        'DATE' => '0:1',
47c2ed51d1SGreg Roach        'PLAC' => '0:1',
48c2ed51d1SGreg Roach    ];
49c2ed51d1SGreg Roach
50c2ed51d1SGreg Roach    protected const EVENTS_RECORDED = [
51c2ed51d1SGreg Roach        'INDI:ADOP',
52c2ed51d1SGreg Roach        'INDI:BAPM',
53c2ed51d1SGreg Roach        'INDI:BARM',
54c2ed51d1SGreg Roach        'INDI:BASM',
55c2ed51d1SGreg Roach        'INDI:BIRT',
56c2ed51d1SGreg Roach        'INDI:BLES',
57c2ed51d1SGreg Roach        'INDI:BURI',
58c2ed51d1SGreg Roach        'INDI:CAST',
59c2ed51d1SGreg Roach        'INDI:CHR',
60c2ed51d1SGreg Roach        'INDI:CENS',
61c2ed51d1SGreg Roach        'INDI:CHRA',
62c2ed51d1SGreg Roach        'INDI:CONF',
63c2ed51d1SGreg Roach        'INDI:CREM',
64c2ed51d1SGreg Roach        'INDI:DEAT',
65c2ed51d1SGreg Roach        'INDI:DSCR',
66c2ed51d1SGreg Roach        'INDI:EDUC',
67c2ed51d1SGreg Roach        'INDI:EMIG',
68c2ed51d1SGreg Roach        'INDI:FCOM',
69c2ed51d1SGreg Roach        'INDI:GRAD',
70c2ed51d1SGreg Roach        'INDI:IDNO',
71c2ed51d1SGreg Roach        'INDI:IMMI',
72c2ed51d1SGreg Roach        'INDI:NATI',
73c2ed51d1SGreg Roach        'INDI:NATU',
74c2ed51d1SGreg Roach        'INDI:NCHI',
75c2ed51d1SGreg Roach        'INDI:NMR',
76c2ed51d1SGreg Roach        'INDI:OCCU',
77c2ed51d1SGreg Roach        'INDI:ORDN',
78c2ed51d1SGreg Roach        'INDI:PROB',
79c2ed51d1SGreg Roach        'INDI:PROP',
80c2ed51d1SGreg Roach        'INDI:RELI',
81c2ed51d1SGreg Roach        'INDI:RESI',
82c2ed51d1SGreg Roach        'INDI:RETI',
83c2ed51d1SGreg Roach        'INDI:SSN',
84c2ed51d1SGreg Roach        'INDI:TITL',
85c2ed51d1SGreg Roach        'INDI:WILL',
86c2ed51d1SGreg Roach        'FAM:ANUL',
87c2ed51d1SGreg Roach        'FAM:DIV',
88c2ed51d1SGreg Roach        'FAM:DIVF',
89c2ed51d1SGreg Roach        'FAM:ENGA',
90c2ed51d1SGreg Roach        'FAM:MARB',
91c2ed51d1SGreg Roach        'FAM:MARC',
92c2ed51d1SGreg Roach        'FAM:MARL',
93c2ed51d1SGreg Roach        'FAM:MARS',
94c2ed51d1SGreg Roach        'FAM:MARR',
95c2ed51d1SGreg Roach    ];
96c2ed51d1SGreg Roach
97c2ed51d1SGreg Roach    /**
98c2ed51d1SGreg Roach     * Convert a value to a canonical form.
99c2ed51d1SGreg Roach     *
100c2ed51d1SGreg Roach     * @param string $value
101c2ed51d1SGreg Roach     *
102c2ed51d1SGreg Roach     * @return string
103c2ed51d1SGreg Roach     */
104c2ed51d1SGreg Roach    public function canonical(string $value): string
105c2ed51d1SGreg Roach    {
1063d2c98d1SGreg Roach        $value = strtoupper(strtr(parent::canonical($value), [' ' => ',']));
1073d2c98d1SGreg Roach
1083d2c98d1SGreg Roach        while (strpos($value, ',,') !== false) {
1093d2c98d1SGreg Roach            $value = strtr($value, [',,' => ',']);
1103d2c98d1SGreg Roach        }
1113d2c98d1SGreg Roach
1123d2c98d1SGreg Roach        return trim($value, ',');
113c2ed51d1SGreg Roach    }
114c2ed51d1SGreg Roach
115c2ed51d1SGreg Roach    /**
116c2ed51d1SGreg Roach     * An edit control for this data.
117c2ed51d1SGreg Roach     *
118c2ed51d1SGreg Roach     * @param string $id
119c2ed51d1SGreg Roach     * @param string $name
120c2ed51d1SGreg Roach     * @param string $value
121c2ed51d1SGreg Roach     * @param Tree   $tree
122c2ed51d1SGreg Roach     *
123c2ed51d1SGreg Roach     * @return string
124c2ed51d1SGreg Roach     */
125c2ed51d1SGreg Roach    public function edit(string $id, string $name, string $value, Tree $tree): string
126c2ed51d1SGreg Roach    {
127c2ed51d1SGreg Roach        $factory = Registry::elementFactory();
128c2ed51d1SGreg Roach
129c2ed51d1SGreg Roach        $options = Collection::make(self::EVENTS_RECORDED)
130c2ed51d1SGreg Roach            ->mapWithKeys(static function (string $tag) use ($factory): array {
131c2ed51d1SGreg Roach                return [explode(':', $tag)[1] => $factory->make($tag)->label()];
132c2ed51d1SGreg Roach            })
133c2ed51d1SGreg Roach            ->sort()
134c2ed51d1SGreg Roach            ->all();
135c2ed51d1SGreg Roach
136c2ed51d1SGreg Roach        $id2 = Uuid::uuid4()->toString();
137c2ed51d1SGreg Roach
138c2ed51d1SGreg Roach        // Our form element name contains "[]", and multiple selections would create multiple values.
1394a213054SGreg Roach        $hidden = '<input type="hidden" id="' . e($id) . '" name="' . e($name) . '" value="' . e($value) . '" />';
140c2ed51d1SGreg Roach        // Combine them into a single value.
141*c8d78f19SGreg Roach        $js = 'document.getElementById("' . $id2 . '").addEventListener("change", function () { document.getElementById("' . $id . '").value = Array.from(document.getElementById("' . $id2 . '").selectedOptions).map(x => x.value).join(","); });';
142c2ed51d1SGreg Roach
143c2ed51d1SGreg Roach        return view('components/select', [
144*c8d78f19SGreg Roach            'class'    => 'tom-select',
145c2ed51d1SGreg Roach            'name'     => '',
146c2ed51d1SGreg Roach            'id'       => $id2,
147c2ed51d1SGreg Roach            'options'  => $options,
148c2ed51d1SGreg Roach            'selected' => explode(',', strtr($value, [' ' => ''])),
149c2ed51d1SGreg Roach        ]) . $hidden . '<script>' . $js . '</script>';
150c2ed51d1SGreg Roach    }
151c2ed51d1SGreg Roach
152c2ed51d1SGreg Roach    /**
153c2ed51d1SGreg Roach     * Display the value of this type of element.
154c2ed51d1SGreg Roach     *
155c2ed51d1SGreg Roach     * @param string $value
156c2ed51d1SGreg Roach     * @param Tree   $tree
157c2ed51d1SGreg Roach     *
158c2ed51d1SGreg Roach     * @return string
159c2ed51d1SGreg Roach     */
160c2ed51d1SGreg Roach    public function value(string $value, Tree $tree): string
161c2ed51d1SGreg Roach    {
162c2ed51d1SGreg Roach        $tags = explode(',', $this->canonical($value));
163c2ed51d1SGreg Roach
164c2ed51d1SGreg Roach        $events = array_map(static function (string $tag): string {
165c2ed51d1SGreg Roach            foreach (['INDI', 'FAM'] as $record_type) {
166c2ed51d1SGreg Roach                $element = Registry::elementFactory()->make($record_type . ':' . $tag);
167c2ed51d1SGreg Roach
168c2ed51d1SGreg Roach                if (!$element instanceof UnknownElement) {
169c2ed51d1SGreg Roach                    return $element->label();
170c2ed51d1SGreg Roach                }
171c2ed51d1SGreg Roach            }
172c2ed51d1SGreg Roach
1733d2c98d1SGreg Roach            return e($tag);
174c2ed51d1SGreg Roach        }, $tags);
175c2ed51d1SGreg Roach
176c2ed51d1SGreg Roach        return implode(I18N::$list_separator, $events);
177c2ed51d1SGreg Roach    }
178c2ed51d1SGreg Roach}
179