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