xref: /webtrees/resources/views/edit/new-individual.phtml (revision 48d3aca6ceace0d3ba3a7269675164a03186bb15)
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;
87c7d1e03SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\AddChildToFamilyAction;
97c7d1e03SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\AddChildToIndividualAction;
107c7d1e03SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\AddParentToIndividualAction;
117c7d1e03SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\AddSpouseToFamilyAction;
127c7d1e03SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\AddSpouseToIndividualAction;
137c7d1e03SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\AddUnlinkedAction;
143b3db8adSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\EditRawFactPage;
1555749c76SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\EditFactAction;
16820b62dfSGreg Roachuse Fisharebest\Webtrees\I18N;
17820b62dfSGreg Roachuse Fisharebest\Webtrees\Individual;
18820b62dfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition;
19820b62dfSGreg Roachuse Fisharebest\Webtrees\View;
20ae71f733SGreg Roachuse Illuminate\Support\Collection;
21820b62dfSGreg Roach
22820b62dfSGreg Roach/**
23820b62dfSGreg Roach * @var Individual|null $individual
24820b62dfSGreg Roach * @var Fact|null       $name_fact
25820b62dfSGreg Roach */
26820b62dfSGreg Roach
27820b62dfSGreg Roach?>
28dd6b2bfcSGreg Roach
29dd6b2bfcSGreg Roach<?php
30820b62dfSGreg Roachif ($individual instanceof Individual) {
31c0935879SGreg Roach    $xref       = $individual->xref();
32dd6b2bfcSGreg Roach    $cancel_url = $individual->url();
33dd6b2bfcSGreg Roach} elseif ($family !== null) {
34c0935879SGreg Roach    $xref       = $family->xref();
35dd6b2bfcSGreg Roach    $cancel_url = $family->url();
36dd6b2bfcSGreg Roach} else {
370c0910bfSGreg Roach    $cancel_url = route('manage-trees', ['tree' => $tree->name()]);
38dd6b2bfcSGreg Roach    $xref       = 'new';
39dd6b2bfcSGreg Roach}
40dd6b2bfcSGreg Roach
41dd6b2bfcSGreg Roach// Different cultures do surnames differently
42dd6b2bfcSGreg Roach$surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION'));
43dd6b2bfcSGreg Roach
44820b62dfSGreg Roachif ($name_fact instanceof Fact) {
45dd6b2bfcSGreg Roach    // Editing an existing name
46905ab80aSGreg Roach    $name_fact_id = $name_fact->id();
47138ca96cSGreg Roach    $namerec      = $name_fact->gedcom();
48dd6b2bfcSGreg Roach    $name_fields  = [
4984586c02SGreg Roach        'NAME' => $name_fact->value(),
503425616eSGreg Roach        'TYPE' => $name_fact->attribute('TYPE'),
513425616eSGreg Roach        'NPFX' => $name_fact->attribute('NPFX'),
523425616eSGreg Roach        'GIVN' => $name_fact->attribute('GIVN'),
533425616eSGreg Roach        'NICK' => $name_fact->attribute('NICK'),
543425616eSGreg Roach        'SPFX' => $name_fact->attribute('SPFX'),
553425616eSGreg Roach        'SURN' => $name_fact->attribute('SURN'),
563425616eSGreg Roach        'NSFX' => $name_fact->attribute('NSFX'),
57dd6b2bfcSGreg Roach    ];
58dd6b2bfcSGreg Roach} else {
59dd6b2bfcSGreg Roach    // Creating a new name
60dd6b2bfcSGreg Roach    $name_fact_id = '';
61dd6b2bfcSGreg Roach    $namerec      = '';
62dd6b2bfcSGreg Roach    $name_fields  = [
63dd6b2bfcSGreg Roach        'NAME' => '',
64dd6b2bfcSGreg Roach        'TYPE' => '',
65dd6b2bfcSGreg Roach        'NPFX' => '',
66dd6b2bfcSGreg Roach        'GIVN' => '',
67dd6b2bfcSGreg Roach        'NICK' => '',
68dd6b2bfcSGreg Roach        'SPFX' => '',
69dd6b2bfcSGreg Roach        'SURN' => '',
70dd6b2bfcSGreg Roach        'NSFX' => '',
71dd6b2bfcSGreg Roach    ];
72dd6b2bfcSGreg Roach
73dd6b2bfcSGreg Roach    // Inherit surname from parents, spouse or child
74dd6b2bfcSGreg Roach    if ($family) {
7539ca88baSGreg Roach        $father = $family->husband();
76820b62dfSGreg Roach        if ($father instanceof Individual && $father->facts(['NAME'])->isNotEmpty()) {
77820b62dfSGreg Roach            $father_name = $father->facts(['NAME'])->first()->value();
78dd6b2bfcSGreg Roach        } else {
79dd6b2bfcSGreg Roach            $father_name = '';
80dd6b2bfcSGreg Roach        }
8139ca88baSGreg Roach        $mother = $family->wife();
82820b62dfSGreg Roach        if ($mother instanceof Individual && $mother->facts(['NAME'])->isNotEmpty()) {
83820b62dfSGreg Roach            $mother_name = $mother->facts(['NAME'])->first()->value();
84dd6b2bfcSGreg Roach        } else {
85dd6b2bfcSGreg Roach            $mother_name = '';
86dd6b2bfcSGreg Roach        }
87dd6b2bfcSGreg Roach    } else {
88dd6b2bfcSGreg Roach        $father      = null;
89dd6b2bfcSGreg Roach        $mother      = null;
90dd6b2bfcSGreg Roach        $father_name = '';
91dd6b2bfcSGreg Roach        $mother_name = '';
92dd6b2bfcSGreg Roach    }
93820b62dfSGreg Roach    if ($individual && $individual->facts(['NAME'])->isNotEmpty()) {
94820b62dfSGreg Roach        $indi_name = $individual->facts(['NAME'])->first()->value();
95dd6b2bfcSGreg Roach    } else {
96dd6b2bfcSGreg Roach        $indi_name = '';
97dd6b2bfcSGreg Roach    }
98dd6b2bfcSGreg Roach
9983615acfSGreg Roach    switch ($next_action) {
1007c7d1e03SGreg Roach        case AddChildToFamilyAction::class:
101dd6b2bfcSGreg Roach            $name_fields = array_merge($name_fields, $surname_tradition->newChildNames($father_name, $mother_name, $gender));
102dd6b2bfcSGreg Roach            break;
1037c7d1e03SGreg Roach        case AddChildToIndividualAction::class:
10439ca88baSGreg Roach            if ($individual->sex() === 'F') {
105dd6b2bfcSGreg Roach                $name_fields = array_merge($name_fields, $surname_tradition->newChildNames('', $indi_name, $gender));
106dd6b2bfcSGreg Roach            } else {
107dd6b2bfcSGreg Roach                $name_fields = array_merge($name_fields, $surname_tradition->newChildNames($indi_name, '', $gender));
108dd6b2bfcSGreg Roach            }
109dd6b2bfcSGreg Roach            break;
1107c7d1e03SGreg Roach        case AddParentToIndividualAction::class:
111dd6b2bfcSGreg Roach            $name_fields = array_merge($name_fields, $surname_tradition->newParentNames($indi_name, $gender));
112dd6b2bfcSGreg Roach            break;
1137c7d1e03SGreg Roach        case AddSpouseToFamilyAction::class:
114dd6b2bfcSGreg Roach            if ($father) {
115dd6b2bfcSGreg Roach                $name_fields = array_merge($name_fields, $surname_tradition->newSpouseNames($father_name, $gender));
116dd6b2bfcSGreg Roach            } else {
117dd6b2bfcSGreg Roach                $name_fields = array_merge($name_fields, $surname_tradition->newSpouseNames($mother_name, $gender));
118dd6b2bfcSGreg Roach            }
119dd6b2bfcSGreg Roach            break;
1207c7d1e03SGreg Roach        case AddSpouseToIndividualAction::class:
121dd6b2bfcSGreg Roach            $name_fields = array_merge($name_fields, $surname_tradition->newSpouseNames($indi_name, $gender));
122dd6b2bfcSGreg Roach            break;
1237c7d1e03SGreg Roach        case AddUnlinkedAction::class:
12455749c76SGreg Roach        case EditFactAction::class:
125dd6b2bfcSGreg Roach            if ($surname_tradition->hasSurnames()) {
126dd6b2bfcSGreg Roach                $name_fields['NAME'] = '//';
127dd6b2bfcSGreg Roach            }
128dd6b2bfcSGreg Roach            break;
129dd6b2bfcSGreg Roach    }
130dd6b2bfcSGreg Roach}
131dd6b2bfcSGreg Roach
132dd6b2bfcSGreg Roach$bdm = ''; // used to copy '1 SOUR' to '2 SOUR' for BIRT DEAT MARR
133dd6b2bfcSGreg Roach
134dd6b2bfcSGreg Roach?>
135dd6b2bfcSGreg Roach<h2 class="wt-page-title"><?= $title ?></h2>
136dd6b2bfcSGreg Roach
137*48d3aca6SGreg Roach<form method="post" action="<?= e(route($next_action, ['tree' => $tree->name(), 'xref' => $xref, 'fact_id' => $name_fact ? $name_fact->id() : null])) ?>" onsubmit="return checkform();">
138dd6b2bfcSGreg Roach    <input type="hidden" name="fact_id" value="<?= e($name_fact_id) ?>">
139dd6b2bfcSGreg Roach    <input type="hidden" name="famtag" value="<?= e($famtag) ?>">
140dd6b2bfcSGreg Roach    <input type="hidden" name="gender" value="<?= $gender ?>">
141dd6b2bfcSGreg Roach    <?= csrf_field() ?>
142dd6b2bfcSGreg Roach
1437c7d1e03SGreg Roach    <?php if ($next_action === AddChildToFamilyAction::class || $next_action === AddChildToIndividualAction::class) : ?>
144dd6b2bfcSGreg Roach        <?= FunctionsEdit::addSimpleTag($tree, '0 PEDI') ?>
145dd6b2bfcSGreg Roach    <?php endif ?>
146dd6b2bfcSGreg Roach
147dd6b2bfcSGreg Roach    <?php
1485eca9df6SGreg Roach    // If we are adding a new individual, choose the sex.
14955749c76SGreg Roach    if ($next_action !== EditFactAction::class) {
1505eca9df6SGreg Roach        if ($famtag === 'HUSB' || $gender === 'M') {
1515eca9df6SGreg Roach            echo FunctionsEdit::addSimpleTag($tree, '0 SEX M');
1525eca9df6SGreg Roach        } elseif ($famtag === 'WIFE' || $gender === 'F') {
1535eca9df6SGreg Roach            echo FunctionsEdit::addSimpleTag($tree, '0 SEX F');
1545eca9df6SGreg Roach        } else {
1557728b506SGreg Roach            echo FunctionsEdit::addSimpleTag($tree, '0 SEX');
1565eca9df6SGreg Roach        }
1575eca9df6SGreg Roach    }
1585eca9df6SGreg Roach    ?>
1595eca9df6SGreg Roach
1605eca9df6SGreg Roach    <?php
161dd6b2bfcSGreg Roach    // First - standard name fields
162dd6b2bfcSGreg Roach    foreach ($name_fields as $tag => $value) {
163dd6b2bfcSGreg Roach        if (substr_compare($tag, '_', 0, 1) !== 0) {
164809fcb31SGreg Roach            echo FunctionsEdit::addSimpleTag($tree, '0 ' . $tag . ' ' . $value, '', '');
165dd6b2bfcSGreg Roach        }
166dd6b2bfcSGreg Roach    }
167dd6b2bfcSGreg Roach
168dd6b2bfcSGreg Roach    // Second - advanced name fields
169dd6b2bfcSGreg Roach    if ($surname_tradition->hasMarriedNames() || preg_match('/\n2 _MARNM /', $namerec)) {
170dd6b2bfcSGreg Roach        $adv_name_fields = ['_MARNM' => ''];
171dd6b2bfcSGreg Roach    } else {
172dd6b2bfcSGreg Roach        $adv_name_fields = [];
173dd6b2bfcSGreg Roach    }
1748d0ebef0SGreg Roach    if (preg_match_all('/(' . Gedcom::REGEX_TAG . ')/', $tree->getPreference('ADVANCED_NAME_FACTS'), $match)) {
175dd6b2bfcSGreg Roach        foreach ($match[1] as $tag) {
176dd6b2bfcSGreg Roach            // Ignore advanced facts that duplicate standard facts
177dd6b2bfcSGreg Roach            if (!in_array($tag, ['TYPE', 'NPFX', 'GIVN', 'NICK', 'SPFX', 'SURN', 'NSFX'])) {
178dd6b2bfcSGreg Roach                $adv_name_fields[$tag] = '';
179dd6b2bfcSGreg Roach            }
180dd6b2bfcSGreg Roach        }
181dd6b2bfcSGreg Roach    }
182dd6b2bfcSGreg Roach
183dd6b2bfcSGreg Roach    foreach (array_keys($adv_name_fields) as $tag) {
184dd6b2bfcSGreg Roach        // Edit existing tags, grouped together
185dd6b2bfcSGreg Roach        if (preg_match_all('/2 ' . $tag . ' (.+)/', $namerec, $match)) {
186dd6b2bfcSGreg Roach            foreach ($match[1] as $value) {
18743e7e97aSGreg Roach                echo FunctionsEdit::addSimpleTag($tree, '2 ' . $tag . ' ' . $value, '', GedcomTag::getLabel('NAME:' . $tag));
188dd6b2bfcSGreg Roach                if ($tag === '_MARNM') {
189dd6b2bfcSGreg Roach                    preg_match_all('/\/([^\/]*)\//', $value, $matches);
190dd6b2bfcSGreg Roach                    echo FunctionsEdit::addSimpleTag($tree, '2 _MARNM_SURN ' . implode(',', $matches[1]));
191dd6b2bfcSGreg Roach                }
192dd6b2bfcSGreg Roach            }
193dd6b2bfcSGreg Roach        }
194dd6b2bfcSGreg Roach        // Allow a new tag to be entered
195dd6b2bfcSGreg Roach        if (!array_key_exists($tag, $name_fields)) {
19643e7e97aSGreg Roach            echo FunctionsEdit::addSimpleTag($tree, '0 ' . $tag, '', GedcomTag::getLabel('NAME:' . $tag));
197dd6b2bfcSGreg Roach            if ($tag === '_MARNM') {
198dd6b2bfcSGreg Roach                echo FunctionsEdit::addSimpleTag($tree, '0 _MARNM_SURN');
199dd6b2bfcSGreg Roach            }
200dd6b2bfcSGreg Roach        }
201dd6b2bfcSGreg Roach    }
202dd6b2bfcSGreg Roach
203dd6b2bfcSGreg Roach    // Third - new/existing custom name fields
204dd6b2bfcSGreg Roach    foreach ($name_fields as $tag => $value) {
205dd6b2bfcSGreg Roach        if (substr_compare($tag, '_', 0, 1) === 0) {
206dd6b2bfcSGreg Roach            echo FunctionsEdit::addSimpleTag($tree, '0 ' . $tag . ' ' . $value);
207dd6b2bfcSGreg Roach            if ($tag === '_MARNM') {
208dd6b2bfcSGreg Roach                preg_match_all('/\/([^\/]*)\//', $value, $matches);
209dd6b2bfcSGreg Roach                echo FunctionsEdit::addSimpleTag($tree, '2 _MARNM_SURN ' . implode(',', $matches[1]));
210dd6b2bfcSGreg Roach            }
211dd6b2bfcSGreg Roach        }
212dd6b2bfcSGreg Roach    }
213dd6b2bfcSGreg Roach
214dd6b2bfcSGreg Roach    // Fourth - SOUR, NOTE, _CUSTOM, etc.
215dd6b2bfcSGreg Roach    if ($namerec !== '') {
216dd6b2bfcSGreg Roach        $gedlines = explode("\n", $namerec); // -- find the number of lines in the record
217dd6b2bfcSGreg Roach        $fields   = explode(' ', $gedlines[0]);
218dd6b2bfcSGreg Roach        $glevel   = $fields[0];
219dd6b2bfcSGreg Roach        $level    = $glevel;
220dd6b2bfcSGreg Roach        $type     = $fields[1];
221dd6b2bfcSGreg Roach        $tags     = [];
222dd6b2bfcSGreg Roach        $i        = 0;
223dd6b2bfcSGreg Roach        do {
224dd6b2bfcSGreg Roach            if ($type !== 'TYPE' && !array_key_exists($type, $name_fields) && !array_key_exists($type, $adv_name_fields)) {
225dd6b2bfcSGreg Roach                $text = '';
226dd6b2bfcSGreg Roach                for ($j = 2; $j < count($fields); $j++) {
227dd6b2bfcSGreg Roach                    if ($j > 2) {
228dd6b2bfcSGreg Roach                        $text .= ' ';
229dd6b2bfcSGreg Roach                    }
230dd6b2bfcSGreg Roach                    $text .= $fields[$j];
231dd6b2bfcSGreg Roach                }
232dd6b2bfcSGreg Roach                while (($i + 1 < count($gedlines)) && (preg_match('/' . ($level + 1) . ' CONT ?(.*)/', $gedlines[$i + 1], $cmatch) > 0)) {
233dd6b2bfcSGreg Roach                    $text .= "\n" . $cmatch[1];
234dd6b2bfcSGreg Roach                    $i++;
235dd6b2bfcSGreg Roach                }
236dd6b2bfcSGreg Roach                echo FunctionsEdit::addSimpleTag($tree, $level . ' ' . $type . ' ' . $text);
237dd6b2bfcSGreg Roach            }
238dd6b2bfcSGreg Roach            $tags[] = $type;
239dd6b2bfcSGreg Roach            $i++;
240dd6b2bfcSGreg Roach            if (isset($gedlines[$i])) {
241dd6b2bfcSGreg Roach                $fields = explode(' ', $gedlines[$i]);
242dd6b2bfcSGreg Roach                $level  = $fields[0];
243dd6b2bfcSGreg Roach                if (isset($fields[1])) {
244dd6b2bfcSGreg Roach                    $type = $fields[1];
245dd6b2bfcSGreg Roach                }
246dd6b2bfcSGreg Roach            }
247dd6b2bfcSGreg Roach        } while (($level > $glevel) && ($i < count($gedlines)));
248dd6b2bfcSGreg Roach    }
249dd6b2bfcSGreg Roach
250dd6b2bfcSGreg Roach    // If we are adding a new individual, add the basic details
25155749c76SGreg Roach    if ($next_action !== EditFactAction::class) {
252dd6b2bfcSGreg Roach        $bdm = 'BD';
253ae71f733SGreg Roach        $tags = new Collection();
254ae71f733SGreg Roach        preg_match_all('/(' . Gedcom::REGEX_TAG . ')/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches);
255ae71f733SGreg Roach        $tags = $tags->merge($matches[1]);
256ae71f733SGreg Roach
257ae71f733SGreg Roach        // If adding a spouse add the option to add a marriage fact to the new family
2587c7d1e03SGreg Roach        if ($next_action === AddSpouseToIndividualAction::class || $next_action === AddSpouseToFamilyAction::class) {
259dd6b2bfcSGreg Roach            $bdm .= 'M';
260ae71f733SGreg Roach            preg_match_all('/(' . Gedcom::REGEX_TAG . ')/', $tree->getPreference('QUICK_REQUIRED_FAMFACTS'), $matches);
261ae71f733SGreg Roach            $tags = $tags->merge($matches[1]);
262dd6b2bfcSGreg Roach        }
263ae71f733SGreg Roach
264ae71f733SGreg Roach        foreach (Fact::sortFactTags($tags) as $tag) {
26596a50d32SGreg Roach            echo view('cards/add-fact', [
26696a50d32SGreg Roach                'tag' => $tag,
26796a50d32SGreg Roach                'tree'  => $tree,
26896a50d32SGreg Roach            ]);
269dd6b2bfcSGreg Roach        }
270dd6b2bfcSGreg Roach    }
271dd6b2bfcSGreg Roach
27255749c76SGreg Roach    if ($next_action === EditFactAction::class ) {
273dd6b2bfcSGreg Roach        // GEDCOM 5.5.1 spec says NAME doesn’t get a OBJE
274dd6b2bfcSGreg Roach        echo view('cards/add-source-citation', [
275dd6b2bfcSGreg Roach            'level'          => 2,
276dd6b2bfcSGreg Roach            'full_citations' => $tree->getPreference('FULL_SOURCES'),
277dd6b2bfcSGreg Roach            'tree'           => $tree,
278dd6b2bfcSGreg Roach        ]);
279dd6b2bfcSGreg Roach        echo view('cards/add-note', [
280dd6b2bfcSGreg Roach            'level' => 2,
281dd6b2bfcSGreg Roach            'tree'  => $tree,
282dd6b2bfcSGreg Roach        ]);
283dd6b2bfcSGreg Roach        echo view('cards/add-shared-note', [
284dd6b2bfcSGreg Roach            'level' => 2,
285dd6b2bfcSGreg Roach            'tree'  => $tree,
286dd6b2bfcSGreg Roach        ]);
287dd6b2bfcSGreg Roach        echo view('cards/add-restriction', [
288dd6b2bfcSGreg Roach            'level' => 2,
289dd6b2bfcSGreg Roach            'tree'  => $tree,
290dd6b2bfcSGreg Roach        ]);
291dd6b2bfcSGreg Roach    } else {
292dd6b2bfcSGreg Roach        echo view('cards/add-source-citation', [
293dd6b2bfcSGreg Roach            'bdm'                     => $bdm,
294dd6b2bfcSGreg Roach            'level'                   => 1,
295dd6b2bfcSGreg Roach            'full_citations'          => $tree->getPreference('FULL_SOURCES'),
296dd6b2bfcSGreg Roach            'prefer_level2_sources'   => $tree->getPreference('PREFER_LEVEL2_SOURCES'),
297dd6b2bfcSGreg Roach            'quick_required_facts'    => $tree->getPreference('QUICK_REQUIRED_FACTS'),
298dd6b2bfcSGreg Roach            'quick_required_famfacts' => $tree->getPreference('QUICK_REQUIRED_FAMFACTS'),
299dd6b2bfcSGreg Roach            'tree'                    => $tree,
300dd6b2bfcSGreg Roach        ]);
301dd6b2bfcSGreg Roach        echo view('cards/add-note', [
302dd6b2bfcSGreg Roach            'level' => 1,
303dd6b2bfcSGreg Roach            'tree'  => $tree,
304dd6b2bfcSGreg Roach        ]);
305dd6b2bfcSGreg Roach        echo view('cards/add-shared-note', [
306dd6b2bfcSGreg Roach            'level' => 1,
307dd6b2bfcSGreg Roach            'tree'  => $tree,
308dd6b2bfcSGreg Roach        ]);
309dd6b2bfcSGreg Roach        echo view('cards/add-restriction', [
310dd6b2bfcSGreg Roach            'level' => 1,
311dd6b2bfcSGreg Roach            'tree'  => $tree,
312dd6b2bfcSGreg Roach        ]);
313dd6b2bfcSGreg Roach    }
314dd6b2bfcSGreg Roach
315dd6b2bfcSGreg Roach    ?>
316dd6b2bfcSGreg Roach    <div class="row form-group">
317dd6b2bfcSGreg Roach        <div class="col-sm-9 offset-sm-3">
318dd6b2bfcSGreg Roach            <button class="btn btn-primary" type="submit">
319d993d560SGreg Roach                <?= view('icons/save') ?>
320dd6b2bfcSGreg Roach                <?= /* I18N: A button label. */
321dd6b2bfcSGreg Roach                I18N::translate('save') ?>
322dd6b2bfcSGreg Roach            </button>
323bee967faSGreg Roach            <?php if ($next_action !== EditFactAction::class) : ?>
324dc21071fSGreg Roach                <button class="btn btn-primary" type="submit" name="goto" value="new">
325d993d560SGreg Roach                    <?= view('icons/save') ?>
326dd6b2bfcSGreg Roach                    <?= /* I18N: A button label. */
327dd6b2bfcSGreg Roach                    I18N::translate('go to new individual') ?>
328dd6b2bfcSGreg Roach                </button>
329dd6b2bfcSGreg Roach            <?php endif ?>
330dd6b2bfcSGreg Roach            <a class="btn btn-secondary" href="<?= e($cancel_url) ?>">
331d993d560SGreg Roach                <?= view('icons/cancel') ?>
332dd6b2bfcSGreg Roach                <?= /* I18N: A button label. */
333dd6b2bfcSGreg Roach                I18N::translate('cancel') ?>
334dd6b2bfcSGreg Roach            </a>
335dd6b2bfcSGreg Roach
336dd6b2bfcSGreg Roach            <?php if ($name_fact instanceof Fact && (Auth::isAdmin() || $tree->getPreference('SHOW_GEDCOM_RECORD'))) : ?>
3373b3db8adSGreg Roach                <a class="btn btn-link" href="<?= e(route(EditRawFactPage::class, ['xref' => $xref, 'fact_id' => $name_fact->id(), 'tree' => $tree->name()])) ?>">
338dd6b2bfcSGreg Roach                    <?= I18N::translate('Edit the raw GEDCOM') ?>
339dd6b2bfcSGreg Roach                </a>
340dd6b2bfcSGreg Roach            <?php endif ?>
341dd6b2bfcSGreg Roach        </div>
342dd6b2bfcSGreg Roach    </div>
343dd6b2bfcSGreg Roach</form>
344dd6b2bfcSGreg Roach
345dd6b2bfcSGreg Roach<?= view('modals/on-screen-keyboard') ?>
346dd6b2bfcSGreg Roach<?= view('modals/ajax') ?>
347dd6b2bfcSGreg Roach<?= view('edit/initialize-calendar-popup') ?>
348dd6b2bfcSGreg Roach
349dd6b2bfcSGreg Roach<?php View::push('javascript') ?>
350dd6b2bfcSGreg Roach<script>
351dd6b2bfcSGreg Roach    var SURNAME_TRADITION = <?= json_encode($tree->getPreference('SURNAME_TRADITION')) ?>;
352dd6b2bfcSGreg Roach
353dd6b2bfcSGreg Roach    var NAME = $("[name=NAME]");
354dd6b2bfcSGreg Roach
355dd6b2bfcSGreg Roach    // Generate a full name from the name components
356dd6b2bfcSGreg Roach    function generate_name() {
35759e18f0cSGreg Roach        var npfx      = document.querySelector("[name=NPFX]").value;
35859e18f0cSGreg Roach        var givn      = document.querySelector("[name=GIVN]").value;
35959e18f0cSGreg Roach        var spfx      = document.querySelector("[name=SPFX]").value;
36059e18f0cSGreg Roach        var surn      = document.querySelector("[name=SURN]").value;
36159e18f0cSGreg Roach        var nsfx      = document.querySelector("[name=NSFX]").value;
36259e18f0cSGreg Roach        var sex_input = document.querySelector("[name=SEX]:checked");
36359e18f0cSGreg Roach        var sex       = sex_input ? sex_input.value : "U";
364dd6b2bfcSGreg Roach
36559e18f0cSGreg Roach        return webtrees.buildNameFromParts(npfx, givn, spfx, surn, nsfx, sex);
366dd6b2bfcSGreg Roach    }
367dd6b2bfcSGreg Roach
368dd6b2bfcSGreg Roach    // Update the NAME and _MARNM fields from the name components
369dd6b2bfcSGreg Roach    // and also display the value in read-only "gedcom" format.
370dd6b2bfcSGreg Roach    function updatewholename() {
371dd6b2bfcSGreg Roach        // Don’t update the name if the user manually changed it
372dd6b2bfcSGreg Roach        if (manualChange) {
373dd6b2bfcSGreg Roach            return;
374dd6b2bfcSGreg Roach        }
375dd6b2bfcSGreg Roach
37659e18f0cSGreg Roach        var npfx = document.querySelector("[name=NPFX]").value;
37759e18f0cSGreg Roach        var givn = document.querySelector("[name=GIVN]").value;
37859e18f0cSGreg Roach        var spfx = document.querySelector("[name=SPFX]").value;
37959e18f0cSGreg Roach        var nsfx = document.querySelector("[name=NSFX]").value;
380dd6b2bfcSGreg Roach        var name = generate_name();
381dd6b2bfcSGreg Roach
382820b62dfSGreg Roach        var display_id = NAME.attr("id") + "_display";
383dd6b2bfcSGreg Roach
384dd6b2bfcSGreg Roach        NAME.val(name);
385dd6b2bfcSGreg Roach        $("#" + display_id).text(name);
38659e18f0cSGreg Roach
387dd6b2bfcSGreg Roach        // Married names inherit some NSFX values, but not these
388dd6b2bfcSGreg Roach        nsfx = nsfx.replace(/^(I|II|III|IV|V|VI|Junior|Jr\.?|Senior|Sr\.?)$/i, "");
38959e18f0cSGreg Roach
390dd6b2bfcSGreg Roach        // Update _MARNM field from _MARNM_SURN field and display it
391dd6b2bfcSGreg Roach        var ip       = document.getElementsByTagName("input");
392dd6b2bfcSGreg Roach        var marnm_id = "";
393dd6b2bfcSGreg Roach        var romn     = "";
394dd6b2bfcSGreg Roach        var heb      = "";
395f6875112SGreg Roach        var i;
39659e18f0cSGreg Roach
397f6875112SGreg Roach        for (i = 0; i < ip.length; i++) {
39859e18f0cSGreg Roach            if (ip[i].id.indexOf("_HEB") === 0) {
39959e18f0cSGreg Roach                // Remember this field - we might need it later
400dd6b2bfcSGreg Roach                heb = val;
40159e18f0cSGreg Roach            }
40259e18f0cSGreg Roach            if (ip[i].id.indexOf("ROMN") === 0) {
40359e18f0cSGreg Roach                // Remember this field - we might need it later
404dd6b2bfcSGreg Roach                romn = val;
40559e18f0cSGreg Roach            }
406f6875112SGreg Roach        }
407f6875112SGreg Roach
408f6875112SGreg Roach        for (i = 0; i < ip.length; i++) {
409f6875112SGreg Roach            var val = ip[i].value;
410f6875112SGreg Roach
411dd6b2bfcSGreg Roach            if (ip[i].id.indexOf("_MARNM") === 0) {
412dd6b2bfcSGreg Roach                if (ip[i].id.indexOf("_MARNM_SURN") === 0) {
413dd6b2bfcSGreg Roach                    var msurn = "";
414dd6b2bfcSGreg Roach                    if (val !== "") {
41559e18f0cSGreg Roach                        if (webtrees.detectScript(val) === webtrees.detectScript(name)) {
41659e18f0cSGreg Roach                            // Same script as NAME field?
41759e18f0cSGreg Roach                            msurn = webtrees.buildNameFromParts(npfx, givn, spfx, val, nsfx);
41859e18f0cSGreg Roach                        } else if (heb !== "" && webtrees.detectScript(val) === webtrees.detectScript(heb)) {
41959e18f0cSGreg Roach                            // Same script as _HEB field?
420dd6b2bfcSGreg Roach                            msurn = heb.replace(/\/.*\//, "/" + val + "/");
42159e18f0cSGreg Roach                        } else if (romn !== "" && webtrees.detectScript(val) === webtrees.detectScript(romn)) {
42259e18f0cSGreg Roach                            //. Same script as ROMN field
423dd6b2bfcSGreg Roach                            msurn = romn.replace(/\/.*\//, "/" + val + "/");
424dd6b2bfcSGreg Roach                        }
425b2091446SRico Sonntag                    }
426dd6b2bfcSGreg Roach                    document.getElementById(marnm_id).value                  = msurn;
427dd6b2bfcSGreg Roach                    document.getElementById(marnm_id + "_display").innerHTML = msurn;
428dd6b2bfcSGreg Roach                } else {
429dd6b2bfcSGreg Roach                    marnm_id = ip[i].id;
430dd6b2bfcSGreg Roach                }
431dd6b2bfcSGreg Roach            }
432dd6b2bfcSGreg Roach        }
433dd6b2bfcSGreg Roach    }
434dd6b2bfcSGreg Roach
435dd6b2bfcSGreg Roach    // Toggle the name editor fields between
436dd6b2bfcSGreg Roach    // <input type="hidden"> <span style="display:inline">
437dd6b2bfcSGreg Roach    // <input type="text">   <span style="display:none">
438dd6b2bfcSGreg Roach
439dd6b2bfcSGreg Roach    var oldName = "";
440dd6b2bfcSGreg Roach
441dd6b2bfcSGreg Roach    // Calls to generate_name() trigger an update - hence need to
442dd6b2bfcSGreg Roach    // set the manual change to true first. We are probably
443dd6b2bfcSGreg Roach    // listening to the wrong events on the input fields...
444dd6b2bfcSGreg Roach    var manualChange = generate_name() !== NAME.val();
445dd6b2bfcSGreg Roach
446dd6b2bfcSGreg Roach    function convertHidden(eid) {
447dd6b2bfcSGreg Roach        var input1 = $("#" + eid);
448dd6b2bfcSGreg Roach        var input2 = $("#" + eid + "_display");
44959e18f0cSGreg Roach
450dd6b2bfcSGreg Roach        if (input1.attr("type") === "hidden") {
45159e18f0cSGreg Roach            input1.attr("type", "text");
452dd6b2bfcSGreg Roach            input2.hide();
453dd6b2bfcSGreg Roach        } else {
45459e18f0cSGreg Roach            input1.attr("type", "hidden");
455dd6b2bfcSGreg Roach            input2.show();
456dd6b2bfcSGreg Roach        }
457dd6b2bfcSGreg Roach    }
458dd6b2bfcSGreg Roach
459dd6b2bfcSGreg Roach    /**
460dd6b2bfcSGreg Roach     * if the user manually changed the NAME field, then update the textual
461dd6b2bfcSGreg Roach     * HTML representation of it
462dd6b2bfcSGreg Roach     * If the value changed set manualChange to true so that changing
463dd6b2bfcSGreg Roach     * the other fields doesn’t change the NAME line
464dd6b2bfcSGreg Roach     */
465dd6b2bfcSGreg Roach    function updateTextName(eid) {
466dd6b2bfcSGreg Roach        var element = document.getElementById(eid);
467dd6b2bfcSGreg Roach        if (element) {
468dd6b2bfcSGreg Roach            if (element.value !== oldName) {
469dd6b2bfcSGreg Roach                manualChange = true;
470dd6b2bfcSGreg Roach            }
471dd6b2bfcSGreg Roach            var delement = document.getElementById(eid + "_display");
472dd6b2bfcSGreg Roach            if (delement) {
473dd6b2bfcSGreg Roach                delement.innerHTML = element.value;
474dd6b2bfcSGreg Roach            }
475dd6b2bfcSGreg Roach        }
476dd6b2bfcSGreg Roach    }
477dd6b2bfcSGreg Roach
478dd6b2bfcSGreg Roach    function checkform() {
479dd6b2bfcSGreg Roach        var ip = document.getElementsByTagName("input");
480dd6b2bfcSGreg Roach        for (var i = 0; i < ip.length; i++) {
481dd6b2bfcSGreg Roach            // ADD slashes to _HEB and _AKA names
482dd6b2bfcSGreg Roach            if (ip[i].id.indexOf("_AKA") === 0 || ip[i].id.indexOf("_HEB") === 0 || ip[i].id.indexOf("ROMN") === 0)
483dd6b2bfcSGreg Roach                if (ip[i].value.indexOf("/") < 0 && ip[i].value !== "")
484dd6b2bfcSGreg Roach                    ip[i].value = ip[i].value.replace(/([^\s]+)\s*$/, "/$1/");
485dd6b2bfcSGreg Roach            // Blank out temporary _MARNM_SURN
486dd6b2bfcSGreg Roach            if (ip[i].id.indexOf("_MARNM_SURN") === 0)
487dd6b2bfcSGreg Roach                ip[i].value = "";
488dd6b2bfcSGreg Roach            // Convert "xxx yyy" and "xxx y yyy" surnames to "xxx,yyy"
489dd6b2bfcSGreg Roach            if ((SURNAME_TRADITION === "spanish" || "SURNAME_TRADITION" === "portuguese") && ip[i].id.indexOf("SURN") === 0) {
490dd6b2bfcSGreg Roach                ip[i].value = document.forms[0].SURN.value.replace(/^\s*([^\s,]{2,})\s+([iIyY] +)?([^\s,]{2,})\s*$/, "$1,$3");
491dd6b2bfcSGreg Roach            }
492dd6b2bfcSGreg Roach        }
493dd6b2bfcSGreg Roach        return true;
494dd6b2bfcSGreg Roach    }
495dd6b2bfcSGreg Roach
496dd6b2bfcSGreg Roach    // If the name isnt initially formed from the components in a standard way,
497dd6b2bfcSGreg Roach    // then dont automatically update it.
498dd6b2bfcSGreg Roach    if (NAME.val() !== generate_name() && NAME.val() !== "//") {
499dd6b2bfcSGreg Roach        convertHidden(NAME.attr("id"));
500dd6b2bfcSGreg Roach    }
501dd6b2bfcSGreg Roach</script>
502dd6b2bfcSGreg Roach<?php View::endpush() ?>
503dd6b2bfcSGreg Roach
504