xref: /webtrees/app/Elements/NamePersonal.php (revision b344f1f7922af1b65f23df39e4b97959ef06b6b4)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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;
25
26use function e;
27use function in_array;
28use function view;
29
30/**
31 * NAME_PERSONAL := {Size=1:120}
32 * [
33 * <NAME_TEXT> | /<NAME_TEXT>/ |
34 * <NAME_TEXT> /<NAME_TEXT>/ | /<NAME_TEXT>/ <NAME_TEXT> |
35 * <NAME_TEXT> /<NAME_TEXT>/ <NAME_TEXT> ]
36 * The surname of an individual, if known, is enclosed between two slash (/)
37 * characters. The order of the name parts should be the order that the person
38 * would, by custom of their culture, have used when giving it to a recorder.
39 * Early versions of Personal Ancestral File ® and other products did not use
40 * the trailing slash when the surname was the last element of the name. If
41 * part of name is illegible, that part is indicated by an ellipsis (...).
42 * Capitalize the name of a person or place in the conventional manner—
43 * capitalize the first letter of each part and lowercase the other letters,
44 * unless conventional usage is otherwise. For example: McMurray.
45 * Examples:
46 * William Lee (given name only or surname not known)
47 * /Parry/ (surname only)
48 * William Lee /Parry/
49 * William Lee /Mac Parry/ (both parts (Mac and Parry) are surname parts
50 * William /Lee/ Parry (surname imbedded in the name string)
51 * William Lee /Pa.../
52 */
53class NamePersonal extends AbstractElement
54{
55    protected const MAXIMUM_LENGTH = 120;
56
57    protected const SUBTAGS = [
58        'TYPE' => '0:1',
59        'NPFX' => '0:1',
60        'GIVN' => '0:1',
61        'SPFX' => '0:1',
62        'SURN' => '0:1',
63        'NSFX' => '0:1',
64        'NICK' => '0:1',
65        'NOTE' => '0:M',
66        'SOUR' => '0:M',
67        'FONE' => '0:M',
68        'ROMN' => '0:M',
69    ];
70
71    // For some languages, we want to show the surname field first.
72    protected const SURNAME_FIRST_LANGUAGES = ['hu', 'jp', 'ko', 'zh-Hans', 'zh-Hant'];
73
74    protected const SUBTAGS_SURNAME_FIRST = [
75        'TYPE' => '0:1',
76        'NPFX' => '0:1',
77        'SPFX' => '0:1',
78        'SURN' => '0:1',
79        'GIVN' => '0:1',
80        'NSFX' => '0:1',
81        'NICK' => '0:1',
82        'NOTE' => '0:M',
83        'SOUR' => '0:M',
84        'FONE' => '0:M',
85        'ROMN' => '0:M',
86    ];
87
88    /**
89     * AbstractGedcomElement constructor.
90     *
91     * @param string             $label
92     * @param array<string>|null $subtags
93     */
94    public function __construct(string $label, array $subtags = null)
95    {
96        if ($subtags === null && in_array(I18N::languageTag(), static::SURNAME_FIRST_LANGUAGES, true)) {
97            $subtags = static::SUBTAGS_SURNAME_FIRST;
98        }
99        parent::__construct($label, $subtags);
100    }
101
102    /**
103     * Convert a value to a canonical form.
104     *
105     * @param string $value
106     *
107     * @return string
108     */
109    public function canonical(string $value): string
110    {
111        $value = parent::canonical($value);
112
113        if ($value === '//') {
114            return '';
115        }
116
117        return $value;
118    }
119
120
121
122    /**
123     * Create a default value for this element.
124     *
125     * @param Tree $tree
126     *
127     * @return string
128     */
129    public function default(Tree $tree): string
130    {
131        return Registry::surnameTraditionFactory()
132            ->make($tree->getPreference('SURNAME_TRADITION'))
133            ->defaultName();
134    }
135
136    /**
137     * An edit control for this data.
138     *
139     * @param string $id
140     * @param string $name
141     * @param string $value
142     * @param Tree   $tree
143     *
144     * @return string
145     */
146    public function edit(string $id, string $name, string $value, Tree $tree): string
147    {
148        return
149            '<div class="input-group">' .
150            view('edit/input-addon-edit-name', ['id' => $id]) .
151            '<input class="form-control" type="text" id="' . e($id) . '" name="' . e($name) . '" value="' . e($value) . '" readonly="readonly" />' .
152            view('edit/input-addon-keyboard', ['id' => $id]) .
153            view('edit/input-addon-help', ['topic' => 'NAME']) .
154            '</div>';
155    }
156}
157