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