xref: /webtrees/resources/views/edit/new-individual.phtml (revision 2917771c25c65897723561cdcefe19f90d29a925)
1820b62dfSGreg Roach<?php
2820b62dfSGreg Roach
3820b62dfSGreg Roachuse Fisharebest\Webtrees\Auth;
4820b62dfSGreg Roachuse Fisharebest\Webtrees\Fact;
5820b62dfSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
6820b62dfSGreg Roachuse Fisharebest\Webtrees\Gedcom;
7820b62dfSGreg Roachuse Fisharebest\Webtrees\GedcomTag;
83b3db8adSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\EditRawFactPage;
9820b62dfSGreg Roachuse Fisharebest\Webtrees\I18N;
10820b62dfSGreg Roachuse Fisharebest\Webtrees\Individual;
11820b62dfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition;
12820b62dfSGreg Roachuse Fisharebest\Webtrees\View;
13820b62dfSGreg Roach
14820b62dfSGreg Roach/**
15820b62dfSGreg Roach * @var Individual|null $individual
16820b62dfSGreg Roach * @var Fact|null       $name_fact
17820b62dfSGreg Roach */
18820b62dfSGreg Roach
19820b62dfSGreg Roach?>
20dd6b2bfcSGreg Roach
21dd6b2bfcSGreg Roach<?php
22820b62dfSGreg Roachif ($individual instanceof Individual) {
23c0935879SGreg Roach    $xref       = $individual->xref();
24dd6b2bfcSGreg Roach    $cancel_url = $individual->url();
25dd6b2bfcSGreg Roach} elseif ($family !== null) {
26c0935879SGreg Roach    $xref       = $family->xref();
27dd6b2bfcSGreg Roach    $cancel_url = $family->url();
28dd6b2bfcSGreg Roach} else {
290c0910bfSGreg Roach    $cancel_url = route('manage-trees', ['tree' => $tree->name()]);
30dd6b2bfcSGreg Roach    $xref       = 'new';
31dd6b2bfcSGreg Roach}
32dd6b2bfcSGreg Roach
33dd6b2bfcSGreg Roach// Different cultures do surnames differently
34dd6b2bfcSGreg Roach$surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION'));
35dd6b2bfcSGreg Roach
36820b62dfSGreg Roachif ($name_fact instanceof Fact) {
37dd6b2bfcSGreg Roach    // Editing an existing name
38905ab80aSGreg Roach    $name_fact_id = $name_fact->id();
39138ca96cSGreg Roach    $namerec      = $name_fact->gedcom();
40dd6b2bfcSGreg Roach    $name_fields  = [
4184586c02SGreg Roach        'NAME' => $name_fact->value(),
423425616eSGreg Roach        'TYPE' => $name_fact->attribute('TYPE'),
433425616eSGreg Roach        'NPFX' => $name_fact->attribute('NPFX'),
443425616eSGreg Roach        'GIVN' => $name_fact->attribute('GIVN'),
453425616eSGreg Roach        'NICK' => $name_fact->attribute('NICK'),
463425616eSGreg Roach        'SPFX' => $name_fact->attribute('SPFX'),
473425616eSGreg Roach        'SURN' => $name_fact->attribute('SURN'),
483425616eSGreg Roach        'NSFX' => $name_fact->attribute('NSFX'),
49dd6b2bfcSGreg Roach    ];
50dd6b2bfcSGreg Roach} else {
51dd6b2bfcSGreg Roach    // Creating a new name
52dd6b2bfcSGreg Roach    $name_fact_id = '';
53dd6b2bfcSGreg Roach    $namerec      = '';
54dd6b2bfcSGreg Roach    $name_fields  = [
55dd6b2bfcSGreg Roach        'NAME' => '',
56dd6b2bfcSGreg Roach        'TYPE' => '',
57dd6b2bfcSGreg Roach        'NPFX' => '',
58dd6b2bfcSGreg Roach        'GIVN' => '',
59dd6b2bfcSGreg Roach        'NICK' => '',
60dd6b2bfcSGreg Roach        'SPFX' => '',
61dd6b2bfcSGreg Roach        'SURN' => '',
62dd6b2bfcSGreg Roach        'NSFX' => '',
63dd6b2bfcSGreg Roach    ];
64dd6b2bfcSGreg Roach
65dd6b2bfcSGreg Roach    // Inherit surname from parents, spouse or child
66dd6b2bfcSGreg Roach    if ($family) {
6739ca88baSGreg Roach        $father = $family->husband();
68820b62dfSGreg Roach        if ($father instanceof Individual && $father->facts(['NAME'])->isNotEmpty()) {
69820b62dfSGreg Roach            $father_name = $father->facts(['NAME'])->first()->value();
70dd6b2bfcSGreg Roach        } else {
71dd6b2bfcSGreg Roach            $father_name = '';
72dd6b2bfcSGreg Roach        }
7339ca88baSGreg Roach        $mother = $family->wife();
74820b62dfSGreg Roach        if ($mother instanceof Individual && $mother->facts(['NAME'])->isNotEmpty()) {
75820b62dfSGreg Roach            $mother_name = $mother->facts(['NAME'])->first()->value();
76dd6b2bfcSGreg Roach        } else {
77dd6b2bfcSGreg Roach            $mother_name = '';
78dd6b2bfcSGreg Roach        }
79dd6b2bfcSGreg Roach    } else {
80dd6b2bfcSGreg Roach        $father      = null;
81dd6b2bfcSGreg Roach        $mother      = null;
82dd6b2bfcSGreg Roach        $father_name = '';
83dd6b2bfcSGreg Roach        $mother_name = '';
84dd6b2bfcSGreg Roach    }
85820b62dfSGreg Roach    if ($individual && $individual->facts(['NAME'])->isNotEmpty()) {
86820b62dfSGreg Roach        $indi_name = $individual->facts(['NAME'])->first()->value();
87dd6b2bfcSGreg Roach    } else {
88dd6b2bfcSGreg Roach        $indi_name = '';
89dd6b2bfcSGreg Roach    }
90dd6b2bfcSGreg Roach
9183615acfSGreg Roach    switch ($next_action) {
9283615acfSGreg Roach        case 'add-child-to-family-action':
93dd6b2bfcSGreg Roach            $name_fields = array_merge($name_fields, $surname_tradition->newChildNames($father_name, $mother_name, $gender));
94dd6b2bfcSGreg Roach            break;
9583615acfSGreg Roach        case 'add-child-to-individual-action':
9639ca88baSGreg Roach            if ($individual->sex() === 'F') {
97dd6b2bfcSGreg Roach                $name_fields = array_merge($name_fields, $surname_tradition->newChildNames('', $indi_name, $gender));
98dd6b2bfcSGreg Roach            } else {
99dd6b2bfcSGreg Roach                $name_fields = array_merge($name_fields, $surname_tradition->newChildNames($indi_name, '', $gender));
100dd6b2bfcSGreg Roach            }
101dd6b2bfcSGreg Roach            break;
10283615acfSGreg Roach        case 'add-parent-to-individual-action':
103dd6b2bfcSGreg Roach            $name_fields = array_merge($name_fields, $surname_tradition->newParentNames($indi_name, $gender));
104dd6b2bfcSGreg Roach            break;
10583615acfSGreg Roach        case 'add-spouse-to-family-action':
106dd6b2bfcSGreg Roach            if ($father) {
107dd6b2bfcSGreg Roach                $name_fields = array_merge($name_fields, $surname_tradition->newSpouseNames($father_name, $gender));
108dd6b2bfcSGreg Roach            } else {
109dd6b2bfcSGreg Roach                $name_fields = array_merge($name_fields, $surname_tradition->newSpouseNames($mother_name, $gender));
110dd6b2bfcSGreg Roach            }
111dd6b2bfcSGreg Roach            break;
11283615acfSGreg Roach        case 'add-spouse-to-individual-action':
113dd6b2bfcSGreg Roach            $name_fields = array_merge($name_fields, $surname_tradition->newSpouseNames($indi_name, $gender));
114dd6b2bfcSGreg Roach            break;
11583615acfSGreg Roach        case 'add-unlinked-individual-action':
11683615acfSGreg Roach        case 'add-name-action':
11783615acfSGreg Roach        case 'edit-name-action':
118dd6b2bfcSGreg Roach            if ($surname_tradition->hasSurnames()) {
119dd6b2bfcSGreg Roach                $name_fields['NAME'] = '//';
120dd6b2bfcSGreg Roach            }
121dd6b2bfcSGreg Roach            break;
122dd6b2bfcSGreg Roach    }
123dd6b2bfcSGreg Roach}
124dd6b2bfcSGreg Roach
125dd6b2bfcSGreg Roach$bdm = ''; // used to copy '1 SOUR' to '2 SOUR' for BIRT DEAT MARR
126dd6b2bfcSGreg Roach
127dd6b2bfcSGreg Roach?>
128dd6b2bfcSGreg Roach<h2 class="wt-page-title"><?= $title ?></h2>
129dd6b2bfcSGreg Roach
130*2917771cSGreg Roach<form method="post" action="<?= e(route($next_action, ['tree' => $tree->name(), 'xref' => $xref])) ?>" onsubmit="return checkform();">
131dd6b2bfcSGreg Roach    <input type="hidden" name="fact_id" value="<?= e($name_fact_id) ?>">
132dd6b2bfcSGreg Roach    <input type="hidden" name="famtag" value="<?= e($famtag) ?>">
133dd6b2bfcSGreg Roach    <input type="hidden" name="gender" value="<?= $gender ?>">
134dd6b2bfcSGreg Roach    <?= csrf_field() ?>
135dd6b2bfcSGreg Roach
13683615acfSGreg Roach    <?php if ($next_action === 'add-child-to-family-action' || $next_action === 'add-child-to-individual-action') : ?>
137dd6b2bfcSGreg Roach        <?= FunctionsEdit::addSimpleTag($tree, '0 PEDI') ?>
138dd6b2bfcSGreg Roach    <?php endif ?>
139dd6b2bfcSGreg Roach
140dd6b2bfcSGreg Roach    <?php
141dd6b2bfcSGreg Roach    // First - standard name fields
142dd6b2bfcSGreg Roach    foreach ($name_fields as $tag => $value) {
143dd6b2bfcSGreg Roach        if (substr_compare($tag, '_', 0, 1) !== 0) {
144809fcb31SGreg Roach            echo FunctionsEdit::addSimpleTag($tree, '0 ' . $tag . ' ' . $value, '', '');
145dd6b2bfcSGreg Roach        }
146dd6b2bfcSGreg Roach    }
147dd6b2bfcSGreg Roach
148dd6b2bfcSGreg Roach    // Second - advanced name fields
149dd6b2bfcSGreg Roach    if ($surname_tradition->hasMarriedNames() || preg_match('/\n2 _MARNM /', $namerec)) {
150dd6b2bfcSGreg Roach        $adv_name_fields = ['_MARNM' => ''];
151dd6b2bfcSGreg Roach    } else {
152dd6b2bfcSGreg Roach        $adv_name_fields = [];
153dd6b2bfcSGreg Roach    }
1548d0ebef0SGreg Roach    if (preg_match_all('/(' . Gedcom::REGEX_TAG . ')/', $tree->getPreference('ADVANCED_NAME_FACTS'), $match)) {
155dd6b2bfcSGreg Roach        foreach ($match[1] as $tag) {
156dd6b2bfcSGreg Roach            // Ignore advanced facts that duplicate standard facts
157dd6b2bfcSGreg Roach            if (!in_array($tag, ['TYPE', 'NPFX', 'GIVN', 'NICK', 'SPFX', 'SURN', 'NSFX'])) {
158dd6b2bfcSGreg Roach                $adv_name_fields[$tag] = '';
159dd6b2bfcSGreg Roach            }
160dd6b2bfcSGreg Roach        }
161dd6b2bfcSGreg Roach    }
162dd6b2bfcSGreg Roach
163dd6b2bfcSGreg Roach    foreach (array_keys($adv_name_fields) as $tag) {
164dd6b2bfcSGreg Roach        // Edit existing tags, grouped together
165dd6b2bfcSGreg Roach        if (preg_match_all('/2 ' . $tag . ' (.+)/', $namerec, $match)) {
166dd6b2bfcSGreg Roach            foreach ($match[1] as $value) {
167dd6b2bfcSGreg Roach                echo FunctionsEdit::addSimpleTag($tree, '2 ' . $tag . ' ' . $value, '', GedcomTag::getLabel('NAME:' . $tag, $individual));
168dd6b2bfcSGreg Roach                if ($tag === '_MARNM') {
169dd6b2bfcSGreg Roach                    preg_match_all('/\/([^\/]*)\//', $value, $matches);
170dd6b2bfcSGreg Roach                    echo FunctionsEdit::addSimpleTag($tree, '2 _MARNM_SURN ' . implode(',', $matches[1]));
171dd6b2bfcSGreg Roach                }
172dd6b2bfcSGreg Roach            }
173dd6b2bfcSGreg Roach        }
174dd6b2bfcSGreg Roach        // Allow a new tag to be entered
175dd6b2bfcSGreg Roach        if (!array_key_exists($tag, $name_fields)) {
176dd6b2bfcSGreg Roach            echo FunctionsEdit::addSimpleTag($tree, '0 ' . $tag, '', GedcomTag::getLabel('NAME:' . $tag, $individual));
177dd6b2bfcSGreg Roach            if ($tag === '_MARNM') {
178dd6b2bfcSGreg Roach                echo FunctionsEdit::addSimpleTag($tree, '0 _MARNM_SURN');
179dd6b2bfcSGreg Roach            }
180dd6b2bfcSGreg Roach        }
181dd6b2bfcSGreg Roach    }
182dd6b2bfcSGreg Roach
183dd6b2bfcSGreg Roach    // Third - new/existing custom name fields
184dd6b2bfcSGreg Roach    foreach ($name_fields as $tag => $value) {
185dd6b2bfcSGreg Roach        if (substr_compare($tag, '_', 0, 1) === 0) {
186dd6b2bfcSGreg Roach            echo FunctionsEdit::addSimpleTag($tree, '0 ' . $tag . ' ' . $value);
187dd6b2bfcSGreg Roach            if ($tag === '_MARNM') {
188dd6b2bfcSGreg Roach                preg_match_all('/\/([^\/]*)\//', $value, $matches);
189dd6b2bfcSGreg Roach                echo FunctionsEdit::addSimpleTag($tree, '2 _MARNM_SURN ' . implode(',', $matches[1]));
190dd6b2bfcSGreg Roach            }
191dd6b2bfcSGreg Roach        }
192dd6b2bfcSGreg Roach    }
193dd6b2bfcSGreg Roach
194dd6b2bfcSGreg Roach    // Fourth - SOUR, NOTE, _CUSTOM, etc.
195dd6b2bfcSGreg Roach    if ($namerec !== '') {
196dd6b2bfcSGreg Roach        $gedlines = explode("\n", $namerec); // -- find the number of lines in the record
197dd6b2bfcSGreg Roach        $fields   = explode(' ', $gedlines[0]);
198dd6b2bfcSGreg Roach        $glevel   = $fields[0];
199dd6b2bfcSGreg Roach        $level    = $glevel;
200dd6b2bfcSGreg Roach        $type     = $fields[1];
201dd6b2bfcSGreg Roach        $tags     = [];
202dd6b2bfcSGreg Roach        $i        = 0;
203dd6b2bfcSGreg Roach        do {
204dd6b2bfcSGreg Roach            if ($type !== 'TYPE' && !array_key_exists($type, $name_fields) && !array_key_exists($type, $adv_name_fields)) {
205dd6b2bfcSGreg Roach                $text = '';
206dd6b2bfcSGreg Roach                for ($j = 2; $j < count($fields); $j++) {
207dd6b2bfcSGreg Roach                    if ($j > 2) {
208dd6b2bfcSGreg Roach                        $text .= ' ';
209dd6b2bfcSGreg Roach                    }
210dd6b2bfcSGreg Roach                    $text .= $fields[$j];
211dd6b2bfcSGreg Roach                }
212dd6b2bfcSGreg Roach                while (($i + 1 < count($gedlines)) && (preg_match('/' . ($level + 1) . ' CONT ?(.*)/', $gedlines[$i + 1], $cmatch) > 0)) {
213dd6b2bfcSGreg Roach                    $text .= "\n" . $cmatch[1];
214dd6b2bfcSGreg Roach                    $i++;
215dd6b2bfcSGreg Roach                }
216dd6b2bfcSGreg Roach                echo FunctionsEdit::addSimpleTag($tree, $level . ' ' . $type . ' ' . $text);
217dd6b2bfcSGreg Roach            }
218dd6b2bfcSGreg Roach            $tags[] = $type;
219dd6b2bfcSGreg Roach            $i++;
220dd6b2bfcSGreg Roach            if (isset($gedlines[$i])) {
221dd6b2bfcSGreg Roach                $fields = explode(' ', $gedlines[$i]);
222dd6b2bfcSGreg Roach                $level  = $fields[0];
223dd6b2bfcSGreg Roach                if (isset($fields[1])) {
224dd6b2bfcSGreg Roach                    $type = $fields[1];
225dd6b2bfcSGreg Roach                }
226dd6b2bfcSGreg Roach            }
227dd6b2bfcSGreg Roach        } while (($level > $glevel) && ($i < count($gedlines)));
228dd6b2bfcSGreg Roach    }
229dd6b2bfcSGreg Roach
230dd6b2bfcSGreg Roach    // If we are adding a new individual, add the basic details
23183615acfSGreg Roach    if ($next_action !== 'add-name-action' && $next_action !== 'edit-name-action') {
232dd6b2bfcSGreg Roach        echo '</table><br><table class="table wt-facts-table">';
233dd6b2bfcSGreg Roach        // 1 SEX
234dd6b2bfcSGreg Roach        if ($famtag === 'HUSB' || $gender === 'M') {
235dd6b2bfcSGreg Roach            echo FunctionsEdit::addSimpleTag($tree, '0 SEX M');
236dd6b2bfcSGreg Roach        } elseif ($famtag === 'WIFE' || $gender === 'F') {
237dd6b2bfcSGreg Roach            echo FunctionsEdit::addSimpleTag($tree, '0 SEX F');
238dd6b2bfcSGreg Roach        } else {
239dd6b2bfcSGreg Roach            echo FunctionsEdit::addSimpleTag($tree, '0 SEX U');
240dd6b2bfcSGreg Roach        }
241dd6b2bfcSGreg Roach        $bdm = 'BD';
2428d0ebef0SGreg Roach        if (preg_match_all('/(' . Gedcom::REGEX_TAG . ')/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
243dd6b2bfcSGreg Roach            foreach ($matches[1] as $match) {
24422d65e5aSGreg Roach                if (!in_array($match, Gedcom::DEATH_EVENTS, true)) {
245dd6b2bfcSGreg Roach                    FunctionsEdit::addSimpleTags($tree, $match);
246dd6b2bfcSGreg Roach                }
247dd6b2bfcSGreg Roach            }
248dd6b2bfcSGreg Roach        }
249dd6b2bfcSGreg Roach        //-- if adding a spouse add the option to add a marriage fact to the new family
25083615acfSGreg Roach        if ($next_action === 'add-spouse-to-individual-action' || $next_action === 'add-spouse-to-family-action') {
251dd6b2bfcSGreg Roach            $bdm .= 'M';
2528d0ebef0SGreg Roach            if (preg_match_all('/(' . Gedcom::REGEX_TAG . ')/', $tree->getPreference('QUICK_REQUIRED_FAMFACTS'), $matches)) {
253dd6b2bfcSGreg Roach                foreach ($matches[1] as $match) {
254dd6b2bfcSGreg Roach                    FunctionsEdit::addSimpleTags($tree, $match);
255dd6b2bfcSGreg Roach                }
256dd6b2bfcSGreg Roach            }
257dd6b2bfcSGreg Roach        }
2588d0ebef0SGreg Roach        if (preg_match_all('/(' . Gedcom::REGEX_TAG . ')/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
259dd6b2bfcSGreg Roach            foreach ($matches[1] as $match) {
26022d65e5aSGreg Roach                if (in_array($match, Gedcom::DEATH_EVENTS, true)) {
261dd6b2bfcSGreg Roach                    FunctionsEdit::addSimpleTags($tree, $match);
262dd6b2bfcSGreg Roach                }
263dd6b2bfcSGreg Roach            }
264dd6b2bfcSGreg Roach        }
265dd6b2bfcSGreg Roach    }
266dd6b2bfcSGreg Roach
267dd6b2bfcSGreg Roach    echo '</table>';
26883615acfSGreg Roach    if ($next_action === 'edit-name-action' || $next_action === 'add-name-action') {
269dd6b2bfcSGreg Roach        // GEDCOM 5.5.1 spec says NAME doesn’t get a OBJE
270dd6b2bfcSGreg Roach        echo view('cards/add-source-citation', [
271dd6b2bfcSGreg Roach            'level'          => 2,
272dd6b2bfcSGreg Roach            'full_citations' => $tree->getPreference('FULL_SOURCES'),
273dd6b2bfcSGreg Roach            'tree'           => $tree,
274dd6b2bfcSGreg Roach        ]);
275dd6b2bfcSGreg Roach        echo view('cards/add-note', [
276dd6b2bfcSGreg Roach            'level' => 2,
277dd6b2bfcSGreg Roach            'tree'  => $tree,
278dd6b2bfcSGreg Roach        ]);
279dd6b2bfcSGreg Roach        echo view('cards/add-shared-note', [
280dd6b2bfcSGreg Roach            'level' => 2,
281dd6b2bfcSGreg Roach            'tree'  => $tree,
282dd6b2bfcSGreg Roach        ]);
283dd6b2bfcSGreg Roach        echo view('cards/add-restriction', [
284dd6b2bfcSGreg Roach            'level' => 2,
285dd6b2bfcSGreg Roach            'tree'  => $tree,
286dd6b2bfcSGreg Roach        ]);
287dd6b2bfcSGreg Roach    } else {
288dd6b2bfcSGreg Roach        echo view('cards/add-source-citation', [
289dd6b2bfcSGreg Roach            'bdm'                     => $bdm,
290dd6b2bfcSGreg Roach            'level'                   => 1,
291dd6b2bfcSGreg Roach            'full_citations'          => $tree->getPreference('FULL_SOURCES'),
292dd6b2bfcSGreg Roach            'prefer_level2_sources'   => $tree->getPreference('PREFER_LEVEL2_SOURCES'),
293dd6b2bfcSGreg Roach            'quick_required_facts'    => $tree->getPreference('QUICK_REQUIRED_FACTS'),
294dd6b2bfcSGreg Roach            'quick_required_famfacts' => $tree->getPreference('QUICK_REQUIRED_FAMFACTS'),
295dd6b2bfcSGreg Roach            'tree'                    => $tree,
296dd6b2bfcSGreg Roach        ]);
297dd6b2bfcSGreg Roach        echo view('cards/add-note', [
298dd6b2bfcSGreg Roach            'level' => 1,
299dd6b2bfcSGreg Roach            'tree'  => $tree,
300dd6b2bfcSGreg Roach        ]);
301dd6b2bfcSGreg Roach        echo view('cards/add-shared-note', [
302dd6b2bfcSGreg Roach            'level' => 1,
303dd6b2bfcSGreg Roach            'tree'  => $tree,
304dd6b2bfcSGreg Roach        ]);
305dd6b2bfcSGreg Roach        echo view('cards/add-restriction', [
306dd6b2bfcSGreg Roach            'level' => 1,
307dd6b2bfcSGreg Roach            'tree'  => $tree,
308dd6b2bfcSGreg Roach        ]);
309dd6b2bfcSGreg Roach    }
310dd6b2bfcSGreg Roach
311dd6b2bfcSGreg Roach    ?>
312dd6b2bfcSGreg Roach    <div class="row form-group">
313dd6b2bfcSGreg Roach        <div class="col-sm-9 offset-sm-3">
314dd6b2bfcSGreg Roach            <button class="btn btn-primary" type="submit">
315d993d560SGreg Roach                <?= view('icons/save') ?>
316dd6b2bfcSGreg Roach                <?= /* I18N: A button label. */
317dd6b2bfcSGreg Roach                I18N::translate('save') ?>
318dd6b2bfcSGreg Roach            </button>
31983615acfSGreg Roach            <?php if (preg_match('/^add-(child|spouse|parent|unlinked-individual)/', $next_action)) : ?>
320dd6b2bfcSGreg Roach                <button class="btn btn-primary" type="submit" name="goto" value="<?= $xref ?>">
321d993d560SGreg Roach                    <?= view('icons/save') ?>
322dd6b2bfcSGreg Roach                    <?= /* I18N: A button label. */
323dd6b2bfcSGreg Roach                    I18N::translate('go to new individual') ?>
324dd6b2bfcSGreg Roach                </button>
325dd6b2bfcSGreg Roach            <?php endif ?>
326dd6b2bfcSGreg Roach            <a class="btn btn-secondary" href="<?= e($cancel_url) ?>">
327d993d560SGreg Roach                <?= view('icons/cancel') ?>
328dd6b2bfcSGreg Roach                <?= /* I18N: A button label. */
329dd6b2bfcSGreg Roach                I18N::translate('cancel') ?>
330dd6b2bfcSGreg Roach            </a>
331dd6b2bfcSGreg Roach
332dd6b2bfcSGreg Roach            <?php if ($name_fact instanceof Fact && (Auth::isAdmin() || $tree->getPreference('SHOW_GEDCOM_RECORD'))) : ?>
3333b3db8adSGreg Roach                <a class="btn btn-link" href="<?= e(route(EditRawFactPage::class, ['xref' => $xref, 'fact_id' => $name_fact->id(), 'tree' => $tree->name()])) ?>">
334dd6b2bfcSGreg Roach                    <?= I18N::translate('Edit the raw GEDCOM') ?>
335dd6b2bfcSGreg Roach                </a>
336dd6b2bfcSGreg Roach            <?php endif ?>
337dd6b2bfcSGreg Roach        </div>
338dd6b2bfcSGreg Roach    </div>
339dd6b2bfcSGreg Roach</form>
340dd6b2bfcSGreg Roach
341dd6b2bfcSGreg Roach<?= view('modals/on-screen-keyboard') ?>
342dd6b2bfcSGreg Roach<?= view('modals/ajax') ?>
343dd6b2bfcSGreg Roach<?= view('edit/initialize-calendar-popup') ?>
344dd6b2bfcSGreg Roach
345dd6b2bfcSGreg Roach<?php View::push('javascript') ?>
346dd6b2bfcSGreg Roach<script>
347dd6b2bfcSGreg Roach    var SURNAME_TRADITION = <?= json_encode($tree->getPreference('SURNAME_TRADITION')) ?>;
348dd6b2bfcSGreg Roach
349dd6b2bfcSGreg Roach    var NAME = $("[name=NAME]");
350dd6b2bfcSGreg Roach
351dd6b2bfcSGreg Roach    // Generate a full name from the name components
352dd6b2bfcSGreg Roach    function generate_name() {
35359e18f0cSGreg Roach        var npfx      = document.querySelector("[name=NPFX]").value;
35459e18f0cSGreg Roach        var givn      = document.querySelector("[name=GIVN]").value;
35559e18f0cSGreg Roach        var spfx      = document.querySelector("[name=SPFX]").value;
35659e18f0cSGreg Roach        var surn      = document.querySelector("[name=SURN]").value;
35759e18f0cSGreg Roach        var nsfx      = document.querySelector("[name=NSFX]").value;
35859e18f0cSGreg Roach        var sex_input = document.querySelector("[name=SEX]:checked");
35959e18f0cSGreg Roach        var sex       = sex_input ? sex_input.value : "U";
360dd6b2bfcSGreg Roach
36159e18f0cSGreg Roach        return webtrees.buildNameFromParts(npfx, givn, spfx, surn, nsfx, sex);
362dd6b2bfcSGreg Roach    }
363dd6b2bfcSGreg Roach
364dd6b2bfcSGreg Roach    // Update the NAME and _MARNM fields from the name components
365dd6b2bfcSGreg Roach    // and also display the value in read-only "gedcom" format.
366dd6b2bfcSGreg Roach    function updatewholename() {
367dd6b2bfcSGreg Roach        // Don’t update the name if the user manually changed it
368dd6b2bfcSGreg Roach        if (manualChange) {
369dd6b2bfcSGreg Roach            return;
370dd6b2bfcSGreg Roach        }
371dd6b2bfcSGreg Roach
37259e18f0cSGreg Roach        var npfx = document.querySelector("[name=NPFX]").value;
37359e18f0cSGreg Roach        var givn = document.querySelector("[name=GIVN]").value;
37459e18f0cSGreg Roach        var spfx = document.querySelector("[name=SPFX]").value;
37559e18f0cSGreg Roach        var nsfx = document.querySelector("[name=NSFX]").value;
376dd6b2bfcSGreg Roach        var name = generate_name();
377dd6b2bfcSGreg Roach
378820b62dfSGreg Roach        var display_id = NAME.attr("id") + "_display";
379dd6b2bfcSGreg Roach
380dd6b2bfcSGreg Roach        NAME.val(name);
381dd6b2bfcSGreg Roach        $("#" + display_id).text(name);
38259e18f0cSGreg Roach
383dd6b2bfcSGreg Roach        // Married names inherit some NSFX values, but not these
384dd6b2bfcSGreg Roach        nsfx = nsfx.replace(/^(I|II|III|IV|V|VI|Junior|Jr\.?|Senior|Sr\.?)$/i, "");
38559e18f0cSGreg Roach
386dd6b2bfcSGreg Roach        // Update _MARNM field from _MARNM_SURN field and display it
387dd6b2bfcSGreg Roach        var ip       = document.getElementsByTagName("input");
388dd6b2bfcSGreg Roach        var marnm_id = "";
389dd6b2bfcSGreg Roach        var romn     = "";
390dd6b2bfcSGreg Roach        var heb      = "";
391f6875112SGreg Roach        var i;
39259e18f0cSGreg Roach
393f6875112SGreg Roach        for (i = 0; i < ip.length; i++) {
39459e18f0cSGreg Roach            if (ip[i].id.indexOf("_HEB") === 0) {
39559e18f0cSGreg Roach                // Remember this field - we might need it later
396dd6b2bfcSGreg Roach                heb = val;
39759e18f0cSGreg Roach            }
39859e18f0cSGreg Roach            if (ip[i].id.indexOf("ROMN") === 0) {
39959e18f0cSGreg Roach                // Remember this field - we might need it later
400dd6b2bfcSGreg Roach                romn = val;
40159e18f0cSGreg Roach            }
402f6875112SGreg Roach        }
403f6875112SGreg Roach
404f6875112SGreg Roach        for (i = 0; i < ip.length; i++) {
405f6875112SGreg Roach            var val = ip[i].value;
406f6875112SGreg Roach
407dd6b2bfcSGreg Roach            if (ip[i].id.indexOf("_MARNM") === 0) {
408dd6b2bfcSGreg Roach                if (ip[i].id.indexOf("_MARNM_SURN") === 0) {
409dd6b2bfcSGreg Roach                    var msurn = "";
410dd6b2bfcSGreg Roach                    if (val !== "") {
41159e18f0cSGreg Roach                        if (webtrees.detectScript(val) === webtrees.detectScript(name)) {
41259e18f0cSGreg Roach                            // Same script as NAME field?
41359e18f0cSGreg Roach                            msurn = webtrees.buildNameFromParts(npfx, givn, spfx, val, nsfx);
41459e18f0cSGreg Roach                        } else if (heb !== "" && webtrees.detectScript(val) === webtrees.detectScript(heb)) {
41559e18f0cSGreg Roach                            // Same script as _HEB field?
416dd6b2bfcSGreg Roach                            msurn = heb.replace(/\/.*\//, "/" + val + "/");
41759e18f0cSGreg Roach                        } else if (romn !== "" && webtrees.detectScript(val) === webtrees.detectScript(romn)) {
41859e18f0cSGreg Roach                            //. Same script as ROMN field
419dd6b2bfcSGreg Roach                            msurn = romn.replace(/\/.*\//, "/" + val + "/");
420dd6b2bfcSGreg Roach                        }
421b2091446SRico Sonntag                    }
422dd6b2bfcSGreg Roach                    document.getElementById(marnm_id).value                  = msurn;
423dd6b2bfcSGreg Roach                    document.getElementById(marnm_id + "_display").innerHTML = msurn;
424dd6b2bfcSGreg Roach                } else {
425dd6b2bfcSGreg Roach                    marnm_id = ip[i].id;
426dd6b2bfcSGreg Roach                }
427dd6b2bfcSGreg Roach            }
428dd6b2bfcSGreg Roach        }
429dd6b2bfcSGreg Roach    }
430dd6b2bfcSGreg Roach
431dd6b2bfcSGreg Roach    // Toggle the name editor fields between
432dd6b2bfcSGreg Roach    // <input type="hidden"> <span style="display:inline">
433dd6b2bfcSGreg Roach    // <input type="text">   <span style="display:none">
434dd6b2bfcSGreg Roach
435dd6b2bfcSGreg Roach    var oldName = "";
436dd6b2bfcSGreg Roach
437dd6b2bfcSGreg Roach    // Calls to generate_name() trigger an update - hence need to
438dd6b2bfcSGreg Roach    // set the manual change to true first. We are probably
439dd6b2bfcSGreg Roach    // listening to the wrong events on the input fields...
440dd6b2bfcSGreg Roach    var manualChange = generate_name() !== NAME.val();
441dd6b2bfcSGreg Roach
442dd6b2bfcSGreg Roach    function convertHidden(eid) {
443dd6b2bfcSGreg Roach        var input1 = $("#" + eid);
444dd6b2bfcSGreg Roach        var input2 = $("#" + eid + "_display");
44559e18f0cSGreg Roach
446dd6b2bfcSGreg Roach        if (input1.attr("type") === "hidden") {
44759e18f0cSGreg Roach            input1.attr("type", "text");
448dd6b2bfcSGreg Roach            input2.hide();
449dd6b2bfcSGreg Roach        } else {
45059e18f0cSGreg Roach            input1.attr("type", "hidden");
451dd6b2bfcSGreg Roach            input2.show();
452dd6b2bfcSGreg Roach        }
453dd6b2bfcSGreg Roach    }
454dd6b2bfcSGreg Roach
455dd6b2bfcSGreg Roach    /**
456dd6b2bfcSGreg Roach     * if the user manually changed the NAME field, then update the textual
457dd6b2bfcSGreg Roach     * HTML representation of it
458dd6b2bfcSGreg Roach     * If the value changed set manualChange to true so that changing
459dd6b2bfcSGreg Roach     * the other fields doesn’t change the NAME line
460dd6b2bfcSGreg Roach     */
461dd6b2bfcSGreg Roach    function updateTextName(eid) {
462dd6b2bfcSGreg Roach        var element = document.getElementById(eid);
463dd6b2bfcSGreg Roach        if (element) {
464dd6b2bfcSGreg Roach            if (element.value !== oldName) {
465dd6b2bfcSGreg Roach                manualChange = true;
466dd6b2bfcSGreg Roach            }
467dd6b2bfcSGreg Roach            var delement = document.getElementById(eid + "_display");
468dd6b2bfcSGreg Roach            if (delement) {
469dd6b2bfcSGreg Roach                delement.innerHTML = element.value;
470dd6b2bfcSGreg Roach            }
471dd6b2bfcSGreg Roach        }
472dd6b2bfcSGreg Roach    }
473dd6b2bfcSGreg Roach
474dd6b2bfcSGreg Roach    function checkform() {
475dd6b2bfcSGreg Roach        var ip = document.getElementsByTagName("input");
476dd6b2bfcSGreg Roach        for (var i = 0; i < ip.length; i++) {
477dd6b2bfcSGreg Roach            // ADD slashes to _HEB and _AKA names
478dd6b2bfcSGreg Roach            if (ip[i].id.indexOf("_AKA") === 0 || ip[i].id.indexOf("_HEB") === 0 || ip[i].id.indexOf("ROMN") === 0)
479dd6b2bfcSGreg Roach                if (ip[i].value.indexOf("/") < 0 && ip[i].value !== "")
480dd6b2bfcSGreg Roach                    ip[i].value = ip[i].value.replace(/([^\s]+)\s*$/, "/$1/");
481dd6b2bfcSGreg Roach            // Blank out temporary _MARNM_SURN
482dd6b2bfcSGreg Roach            if (ip[i].id.indexOf("_MARNM_SURN") === 0)
483dd6b2bfcSGreg Roach                ip[i].value = "";
484dd6b2bfcSGreg Roach            // Convert "xxx yyy" and "xxx y yyy" surnames to "xxx,yyy"
485dd6b2bfcSGreg Roach            if ((SURNAME_TRADITION === "spanish" || "SURNAME_TRADITION" === "portuguese") && ip[i].id.indexOf("SURN") === 0) {
486dd6b2bfcSGreg Roach                ip[i].value = document.forms[0].SURN.value.replace(/^\s*([^\s,]{2,})\s+([iIyY] +)?([^\s,]{2,})\s*$/, "$1,$3");
487dd6b2bfcSGreg Roach            }
488dd6b2bfcSGreg Roach        }
489dd6b2bfcSGreg Roach        return true;
490dd6b2bfcSGreg Roach    }
491dd6b2bfcSGreg Roach
492dd6b2bfcSGreg Roach    // If the name isnt initially formed from the components in a standard way,
493dd6b2bfcSGreg Roach    // then dont automatically update it.
494dd6b2bfcSGreg Roach    if (NAME.val() !== generate_name() && NAME.val() !== "//") {
495dd6b2bfcSGreg Roach        convertHidden(NAME.attr("id"));
496dd6b2bfcSGreg Roach    }
497dd6b2bfcSGreg Roach</script>
498dd6b2bfcSGreg Roach<?php View::endpush() ?>
499dd6b2bfcSGreg Roach
500