xref: /webtrees/resources/views/edit/new-individual.phtml (revision c7aa856b917ed7f821ff91e715981bae3c43d016)
1<?php
2
3use Fisharebest\Webtrees\Auth;
4use Fisharebest\Webtrees\Fact;
5use Fisharebest\Webtrees\Functions\FunctionsEdit;
6use Fisharebest\Webtrees\Gedcom;
7use Fisharebest\Webtrees\GedcomTag;
8use Fisharebest\Webtrees\Http\RequestHandlers\EditRawFactPage;
9use Fisharebest\Webtrees\I18N;
10use Fisharebest\Webtrees\Individual;
11use Fisharebest\Webtrees\SurnameTradition;
12use Fisharebest\Webtrees\View;
13use Illuminate\Support\Collection;
14
15/**
16 * @var Individual|null $individual
17 * @var Fact|null       $name_fact
18 */
19
20?>
21
22<?php
23if ($individual instanceof Individual) {
24    $xref       = $individual->xref();
25    $cancel_url = $individual->url();
26} elseif ($family !== null) {
27    $xref       = $family->xref();
28    $cancel_url = $family->url();
29} else {
30    $cancel_url = route('manage-trees', ['tree' => $tree->name()]);
31    $xref       = 'new';
32}
33
34// Different cultures do surnames differently
35$surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION'));
36
37if ($name_fact instanceof Fact) {
38    // Editing an existing name
39    $name_fact_id = $name_fact->id();
40    $namerec      = $name_fact->gedcom();
41    $name_fields  = [
42        'NAME' => $name_fact->value(),
43        'TYPE' => $name_fact->attribute('TYPE'),
44        'NPFX' => $name_fact->attribute('NPFX'),
45        'GIVN' => $name_fact->attribute('GIVN'),
46        'NICK' => $name_fact->attribute('NICK'),
47        'SPFX' => $name_fact->attribute('SPFX'),
48        'SURN' => $name_fact->attribute('SURN'),
49        'NSFX' => $name_fact->attribute('NSFX'),
50    ];
51} else {
52    // Creating a new name
53    $name_fact_id = '';
54    $namerec      = '';
55    $name_fields  = [
56        'NAME' => '',
57        'TYPE' => '',
58        'NPFX' => '',
59        'GIVN' => '',
60        'NICK' => '',
61        'SPFX' => '',
62        'SURN' => '',
63        'NSFX' => '',
64    ];
65
66    // Inherit surname from parents, spouse or child
67    if ($family) {
68        $father = $family->husband();
69        if ($father instanceof Individual && $father->facts(['NAME'])->isNotEmpty()) {
70            $father_name = $father->facts(['NAME'])->first()->value();
71        } else {
72            $father_name = '';
73        }
74        $mother = $family->wife();
75        if ($mother instanceof Individual && $mother->facts(['NAME'])->isNotEmpty()) {
76            $mother_name = $mother->facts(['NAME'])->first()->value();
77        } else {
78            $mother_name = '';
79        }
80    } else {
81        $father      = null;
82        $mother      = null;
83        $father_name = '';
84        $mother_name = '';
85    }
86    if ($individual && $individual->facts(['NAME'])->isNotEmpty()) {
87        $indi_name = $individual->facts(['NAME'])->first()->value();
88    } else {
89        $indi_name = '';
90    }
91
92    switch ($next_action) {
93        case 'add-child-to-family-action':
94            $name_fields = array_merge($name_fields, $surname_tradition->newChildNames($father_name, $mother_name, $gender));
95            break;
96        case 'add-child-to-individual-action':
97            if ($individual->sex() === 'F') {
98                $name_fields = array_merge($name_fields, $surname_tradition->newChildNames('', $indi_name, $gender));
99            } else {
100                $name_fields = array_merge($name_fields, $surname_tradition->newChildNames($indi_name, '', $gender));
101            }
102            break;
103        case 'add-parent-to-individual-action':
104            $name_fields = array_merge($name_fields, $surname_tradition->newParentNames($indi_name, $gender));
105            break;
106        case 'add-spouse-to-family-action':
107            if ($father) {
108                $name_fields = array_merge($name_fields, $surname_tradition->newSpouseNames($father_name, $gender));
109            } else {
110                $name_fields = array_merge($name_fields, $surname_tradition->newSpouseNames($mother_name, $gender));
111            }
112            break;
113        case 'add-spouse-to-individual-action':
114            $name_fields = array_merge($name_fields, $surname_tradition->newSpouseNames($indi_name, $gender));
115            break;
116        case 'add-unlinked-individual-action':
117        case 'add-name-action':
118        case 'edit-name-action':
119            if ($surname_tradition->hasSurnames()) {
120                $name_fields['NAME'] = '//';
121            }
122            break;
123    }
124}
125
126$bdm = ''; // used to copy '1 SOUR' to '2 SOUR' for BIRT DEAT MARR
127
128?>
129<h2 class="wt-page-title"><?= $title ?></h2>
130
131<form method="post" action="<?= e(route($next_action, ['tree' => $tree->name(), 'xref' => $xref])) ?>" onsubmit="return checkform();">
132    <input type="hidden" name="fact_id" value="<?= e($name_fact_id) ?>">
133    <input type="hidden" name="famtag" value="<?= e($famtag) ?>">
134    <input type="hidden" name="gender" value="<?= $gender ?>">
135    <?= csrf_field() ?>
136
137    <?php if ($next_action === 'add-child-to-family-action' || $next_action === 'add-child-to-individual-action') : ?>
138        <?= FunctionsEdit::addSimpleTag($tree, '0 PEDI') ?>
139    <?php endif ?>
140
141    <?php
142    // First - standard name fields
143    foreach ($name_fields as $tag => $value) {
144        if (substr_compare($tag, '_', 0, 1) !== 0) {
145            echo FunctionsEdit::addSimpleTag($tree, '0 ' . $tag . ' ' . $value, '', '');
146        }
147    }
148
149    // Second - advanced name fields
150    if ($surname_tradition->hasMarriedNames() || preg_match('/\n2 _MARNM /', $namerec)) {
151        $adv_name_fields = ['_MARNM' => ''];
152    } else {
153        $adv_name_fields = [];
154    }
155    if (preg_match_all('/(' . Gedcom::REGEX_TAG . ')/', $tree->getPreference('ADVANCED_NAME_FACTS'), $match)) {
156        foreach ($match[1] as $tag) {
157            // Ignore advanced facts that duplicate standard facts
158            if (!in_array($tag, ['TYPE', 'NPFX', 'GIVN', 'NICK', 'SPFX', 'SURN', 'NSFX'])) {
159                $adv_name_fields[$tag] = '';
160            }
161        }
162    }
163
164    foreach (array_keys($adv_name_fields) as $tag) {
165        // Edit existing tags, grouped together
166        if (preg_match_all('/2 ' . $tag . ' (.+)/', $namerec, $match)) {
167            foreach ($match[1] as $value) {
168                echo FunctionsEdit::addSimpleTag($tree, '2 ' . $tag . ' ' . $value, '', GedcomTag::getLabel('NAME:' . $tag, $individual));
169                if ($tag === '_MARNM') {
170                    preg_match_all('/\/([^\/]*)\//', $value, $matches);
171                    echo FunctionsEdit::addSimpleTag($tree, '2 _MARNM_SURN ' . implode(',', $matches[1]));
172                }
173            }
174        }
175        // Allow a new tag to be entered
176        if (!array_key_exists($tag, $name_fields)) {
177            echo FunctionsEdit::addSimpleTag($tree, '0 ' . $tag, '', GedcomTag::getLabel('NAME:' . $tag, $individual));
178            if ($tag === '_MARNM') {
179                echo FunctionsEdit::addSimpleTag($tree, '0 _MARNM_SURN');
180            }
181        }
182    }
183
184    // Third - new/existing custom name fields
185    foreach ($name_fields as $tag => $value) {
186        if (substr_compare($tag, '_', 0, 1) === 0) {
187            echo FunctionsEdit::addSimpleTag($tree, '0 ' . $tag . ' ' . $value);
188            if ($tag === '_MARNM') {
189                preg_match_all('/\/([^\/]*)\//', $value, $matches);
190                echo FunctionsEdit::addSimpleTag($tree, '2 _MARNM_SURN ' . implode(',', $matches[1]));
191            }
192        }
193    }
194
195    // Fourth - SOUR, NOTE, _CUSTOM, etc.
196    if ($namerec !== '') {
197        $gedlines = explode("\n", $namerec); // -- find the number of lines in the record
198        $fields   = explode(' ', $gedlines[0]);
199        $glevel   = $fields[0];
200        $level    = $glevel;
201        $type     = $fields[1];
202        $tags     = [];
203        $i        = 0;
204        do {
205            if ($type !== 'TYPE' && !array_key_exists($type, $name_fields) && !array_key_exists($type, $adv_name_fields)) {
206                $text = '';
207                for ($j = 2; $j < count($fields); $j++) {
208                    if ($j > 2) {
209                        $text .= ' ';
210                    }
211                    $text .= $fields[$j];
212                }
213                while (($i + 1 < count($gedlines)) && (preg_match('/' . ($level + 1) . ' CONT ?(.*)/', $gedlines[$i + 1], $cmatch) > 0)) {
214                    $text .= "\n" . $cmatch[1];
215                    $i++;
216                }
217                echo FunctionsEdit::addSimpleTag($tree, $level . ' ' . $type . ' ' . $text);
218            }
219            $tags[] = $type;
220            $i++;
221            if (isset($gedlines[$i])) {
222                $fields = explode(' ', $gedlines[$i]);
223                $level  = $fields[0];
224                if (isset($fields[1])) {
225                    $type = $fields[1];
226                }
227            }
228        } while (($level > $glevel) && ($i < count($gedlines)));
229    }
230
231    // If we are adding a new individual, add the basic details
232    if ($next_action !== 'add-name-action' && $next_action !== 'edit-name-action') {
233        // 1 SEX
234        if ($famtag === 'HUSB' || $gender === 'M') {
235            echo FunctionsEdit::addSimpleTag($tree, '0 SEX M');
236        } elseif ($famtag === 'WIFE' || $gender === 'F') {
237            echo FunctionsEdit::addSimpleTag($tree, '0 SEX F');
238        } else {
239            echo FunctionsEdit::addSimpleTag($tree, '0 SEX U');
240        }
241        $bdm = 'BD';
242        $tags = new Collection();
243        preg_match_all('/(' . Gedcom::REGEX_TAG . ')/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches);
244        $tags = $tags->merge($matches[1]);
245
246        // If adding a spouse add the option to add a marriage fact to the new family
247        if ($next_action === 'add-spouse-to-individual-action' || $next_action === 'add-spouse-to-family-action') {
248            $bdm .= 'M';
249            preg_match_all('/(' . Gedcom::REGEX_TAG . ')/', $tree->getPreference('QUICK_REQUIRED_FAMFACTS'), $matches);
250            $tags = $tags->merge($matches[1]);
251        }
252
253        foreach (Fact::sortFactTags($tags) as $tag) {
254            echo view('cards/add-fact', [
255                'tag' => $tag,
256                'tree'  => $tree,
257            ]);
258        }
259    }
260
261    if ($next_action === 'edit-name-action' || $next_action === 'add-name-action') {
262        // GEDCOM 5.5.1 spec says NAME doesn’t get a OBJE
263        echo view('cards/add-source-citation', [
264            'level'          => 2,
265            'full_citations' => $tree->getPreference('FULL_SOURCES'),
266            'tree'           => $tree,
267        ]);
268        echo view('cards/add-note', [
269            'level' => 2,
270            'tree'  => $tree,
271        ]);
272        echo view('cards/add-shared-note', [
273            'level' => 2,
274            'tree'  => $tree,
275        ]);
276        echo view('cards/add-restriction', [
277            'level' => 2,
278            'tree'  => $tree,
279        ]);
280    } else {
281        echo view('cards/add-source-citation', [
282            'bdm'                     => $bdm,
283            'level'                   => 1,
284            'full_citations'          => $tree->getPreference('FULL_SOURCES'),
285            'prefer_level2_sources'   => $tree->getPreference('PREFER_LEVEL2_SOURCES'),
286            'quick_required_facts'    => $tree->getPreference('QUICK_REQUIRED_FACTS'),
287            'quick_required_famfacts' => $tree->getPreference('QUICK_REQUIRED_FAMFACTS'),
288            'tree'                    => $tree,
289        ]);
290        echo view('cards/add-note', [
291            'level' => 1,
292            'tree'  => $tree,
293        ]);
294        echo view('cards/add-shared-note', [
295            'level' => 1,
296            'tree'  => $tree,
297        ]);
298        echo view('cards/add-restriction', [
299            'level' => 1,
300            'tree'  => $tree,
301        ]);
302    }
303
304    ?>
305    <div class="row form-group">
306        <div class="col-sm-9 offset-sm-3">
307            <button class="btn btn-primary" type="submit">
308                <?= view('icons/save') ?>
309                <?= /* I18N: A button label. */
310                I18N::translate('save') ?>
311            </button>
312            <?php if (preg_match('/^add-(child|spouse|parent|unlinked-individual)/', $next_action)) : ?>
313                <button class="btn btn-primary" type="submit" name="goto" value="new">
314                    <?= view('icons/save') ?>
315                    <?= /* I18N: A button label. */
316                    I18N::translate('go to new individual') ?>
317                </button>
318            <?php endif ?>
319            <a class="btn btn-secondary" href="<?= e($cancel_url) ?>">
320                <?= view('icons/cancel') ?>
321                <?= /* I18N: A button label. */
322                I18N::translate('cancel') ?>
323            </a>
324
325            <?php if ($name_fact instanceof Fact && (Auth::isAdmin() || $tree->getPreference('SHOW_GEDCOM_RECORD'))) : ?>
326                <a class="btn btn-link" href="<?= e(route(EditRawFactPage::class, ['xref' => $xref, 'fact_id' => $name_fact->id(), 'tree' => $tree->name()])) ?>">
327                    <?= I18N::translate('Edit the raw GEDCOM') ?>
328                </a>
329            <?php endif ?>
330        </div>
331    </div>
332</form>
333
334<?= view('modals/on-screen-keyboard') ?>
335<?= view('modals/ajax') ?>
336<?= view('edit/initialize-calendar-popup') ?>
337
338<?php View::push('javascript') ?>
339<script>
340    var SURNAME_TRADITION = <?= json_encode($tree->getPreference('SURNAME_TRADITION')) ?>;
341
342    var NAME = $("[name=NAME]");
343
344    // Generate a full name from the name components
345    function generate_name() {
346        var npfx      = document.querySelector("[name=NPFX]").value;
347        var givn      = document.querySelector("[name=GIVN]").value;
348        var spfx      = document.querySelector("[name=SPFX]").value;
349        var surn      = document.querySelector("[name=SURN]").value;
350        var nsfx      = document.querySelector("[name=NSFX]").value;
351        var sex_input = document.querySelector("[name=SEX]:checked");
352        var sex       = sex_input ? sex_input.value : "U";
353
354        return webtrees.buildNameFromParts(npfx, givn, spfx, surn, nsfx, sex);
355    }
356
357    // Update the NAME and _MARNM fields from the name components
358    // and also display the value in read-only "gedcom" format.
359    function updatewholename() {
360        // Don’t update the name if the user manually changed it
361        if (manualChange) {
362            return;
363        }
364
365        var npfx = document.querySelector("[name=NPFX]").value;
366        var givn = document.querySelector("[name=GIVN]").value;
367        var spfx = document.querySelector("[name=SPFX]").value;
368        var nsfx = document.querySelector("[name=NSFX]").value;
369        var name = generate_name();
370
371        var display_id = NAME.attr("id") + "_display";
372
373        NAME.val(name);
374        $("#" + display_id).text(name);
375
376        // Married names inherit some NSFX values, but not these
377        nsfx = nsfx.replace(/^(I|II|III|IV|V|VI|Junior|Jr\.?|Senior|Sr\.?)$/i, "");
378
379        // Update _MARNM field from _MARNM_SURN field and display it
380        var ip       = document.getElementsByTagName("input");
381        var marnm_id = "";
382        var romn     = "";
383        var heb      = "";
384        var i;
385
386        for (i = 0; i < ip.length; i++) {
387            if (ip[i].id.indexOf("_HEB") === 0) {
388                // Remember this field - we might need it later
389                heb = val;
390            }
391            if (ip[i].id.indexOf("ROMN") === 0) {
392                // Remember this field - we might need it later
393                romn = val;
394            }
395        }
396
397        for (i = 0; i < ip.length; i++) {
398            var val = ip[i].value;
399
400            if (ip[i].id.indexOf("_MARNM") === 0) {
401                if (ip[i].id.indexOf("_MARNM_SURN") === 0) {
402                    var msurn = "";
403                    if (val !== "") {
404                        if (webtrees.detectScript(val) === webtrees.detectScript(name)) {
405                            // Same script as NAME field?
406                            msurn = webtrees.buildNameFromParts(npfx, givn, spfx, val, nsfx);
407                        } else if (heb !== "" && webtrees.detectScript(val) === webtrees.detectScript(heb)) {
408                            // Same script as _HEB field?
409                            msurn = heb.replace(/\/.*\//, "/" + val + "/");
410                        } else if (romn !== "" && webtrees.detectScript(val) === webtrees.detectScript(romn)) {
411                            //. Same script as ROMN field
412                            msurn = romn.replace(/\/.*\//, "/" + val + "/");
413                        }
414                    }
415                    document.getElementById(marnm_id).value                  = msurn;
416                    document.getElementById(marnm_id + "_display").innerHTML = msurn;
417                } else {
418                    marnm_id = ip[i].id;
419                }
420            }
421        }
422    }
423
424    // Toggle the name editor fields between
425    // <input type="hidden"> <span style="display:inline">
426    // <input type="text">   <span style="display:none">
427
428    var oldName = "";
429
430    // Calls to generate_name() trigger an update - hence need to
431    // set the manual change to true first. We are probably
432    // listening to the wrong events on the input fields...
433    var manualChange = generate_name() !== NAME.val();
434
435    function convertHidden(eid) {
436        var input1 = $("#" + eid);
437        var input2 = $("#" + eid + "_display");
438
439        if (input1.attr("type") === "hidden") {
440            input1.attr("type", "text");
441            input2.hide();
442        } else {
443            input1.attr("type", "hidden");
444            input2.show();
445        }
446    }
447
448    /**
449     * if the user manually changed the NAME field, then update the textual
450     * HTML representation of it
451     * If the value changed set manualChange to true so that changing
452     * the other fields doesn’t change the NAME line
453     */
454    function updateTextName(eid) {
455        var element = document.getElementById(eid);
456        if (element) {
457            if (element.value !== oldName) {
458                manualChange = true;
459            }
460            var delement = document.getElementById(eid + "_display");
461            if (delement) {
462                delement.innerHTML = element.value;
463            }
464        }
465    }
466
467    function checkform() {
468        var ip = document.getElementsByTagName("input");
469        for (var i = 0; i < ip.length; i++) {
470            // ADD slashes to _HEB and _AKA names
471            if (ip[i].id.indexOf("_AKA") === 0 || ip[i].id.indexOf("_HEB") === 0 || ip[i].id.indexOf("ROMN") === 0)
472                if (ip[i].value.indexOf("/") < 0 && ip[i].value !== "")
473                    ip[i].value = ip[i].value.replace(/([^\s]+)\s*$/, "/$1/");
474            // Blank out temporary _MARNM_SURN
475            if (ip[i].id.indexOf("_MARNM_SURN") === 0)
476                ip[i].value = "";
477            // Convert "xxx yyy" and "xxx y yyy" surnames to "xxx,yyy"
478            if ((SURNAME_TRADITION === "spanish" || "SURNAME_TRADITION" === "portuguese") && ip[i].id.indexOf("SURN") === 0) {
479                ip[i].value = document.forms[0].SURN.value.replace(/^\s*([^\s,]{2,})\s+([iIyY] +)?([^\s,]{2,})\s*$/, "$1,$3");
480            }
481        }
482        return true;
483    }
484
485    // If the name isnt initially formed from the components in a standard way,
486    // then dont automatically update it.
487    if (NAME.val() !== generate_name() && NAME.val() !== "//") {
488        convertHidden(NAME.attr("id"));
489    }
490</script>
491<?php View::endpush() ?>
492
493