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