xref: /webtrees/resources/views/edit/new-individual.phtml (revision ae71f733426ba390fe96aebda1f69dd50ac56bc0)
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        echo '</table><br><table class="table wt-facts-table">';
234        // 1 SEX
235        if ($famtag === 'HUSB' || $gender === 'M') {
236            echo FunctionsEdit::addSimpleTag($tree, '0 SEX M');
237        } elseif ($famtag === 'WIFE' || $gender === 'F') {
238            echo FunctionsEdit::addSimpleTag($tree, '0 SEX F');
239        } else {
240            echo FunctionsEdit::addSimpleTag($tree, '0 SEX U');
241        }
242        $bdm = 'BD';
243        $tags = new Collection();
244        preg_match_all('/(' . Gedcom::REGEX_TAG . ')/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches);
245        $tags = $tags->merge($matches[1]);
246
247        // If adding a spouse add the option to add a marriage fact to the new family
248        if ($next_action === 'add-spouse-to-individual-action' || $next_action === 'add-spouse-to-family-action') {
249            $bdm .= 'M';
250            preg_match_all('/(' . Gedcom::REGEX_TAG . ')/', $tree->getPreference('QUICK_REQUIRED_FAMFACTS'), $matches);
251            $tags = $tags->merge($matches[1]);
252        }
253
254        foreach (Fact::sortFactTags($tags) as $tag) {
255            FunctionsEdit::addSimpleTags($tree, $tag);
256        }
257    }
258
259    echo '</table>';
260    if ($next_action === 'edit-name-action' || $next_action === 'add-name-action') {
261        // GEDCOM 5.5.1 spec says NAME doesn’t get a OBJE
262        echo view('cards/add-source-citation', [
263            'level'          => 2,
264            'full_citations' => $tree->getPreference('FULL_SOURCES'),
265            'tree'           => $tree,
266        ]);
267        echo view('cards/add-note', [
268            'level' => 2,
269            'tree'  => $tree,
270        ]);
271        echo view('cards/add-shared-note', [
272            'level' => 2,
273            'tree'  => $tree,
274        ]);
275        echo view('cards/add-restriction', [
276            'level' => 2,
277            'tree'  => $tree,
278        ]);
279    } else {
280        echo view('cards/add-source-citation', [
281            'bdm'                     => $bdm,
282            'level'                   => 1,
283            'full_citations'          => $tree->getPreference('FULL_SOURCES'),
284            'prefer_level2_sources'   => $tree->getPreference('PREFER_LEVEL2_SOURCES'),
285            'quick_required_facts'    => $tree->getPreference('QUICK_REQUIRED_FACTS'),
286            'quick_required_famfacts' => $tree->getPreference('QUICK_REQUIRED_FAMFACTS'),
287            'tree'                    => $tree,
288        ]);
289        echo view('cards/add-note', [
290            'level' => 1,
291            'tree'  => $tree,
292        ]);
293        echo view('cards/add-shared-note', [
294            'level' => 1,
295            'tree'  => $tree,
296        ]);
297        echo view('cards/add-restriction', [
298            'level' => 1,
299            'tree'  => $tree,
300        ]);
301    }
302
303    ?>
304    <div class="row form-group">
305        <div class="col-sm-9 offset-sm-3">
306            <button class="btn btn-primary" type="submit">
307                <?= view('icons/save') ?>
308                <?= /* I18N: A button label. */
309                I18N::translate('save') ?>
310            </button>
311            <?php if (preg_match('/^add-(child|spouse|parent|unlinked-individual)/', $next_action)) : ?>
312                <button class="btn btn-primary" type="submit" name="goto" value="<?= $xref ?>">
313                    <?= view('icons/save') ?>
314                    <?= /* I18N: A button label. */
315                    I18N::translate('go to new individual') ?>
316                </button>
317            <?php endif ?>
318            <a class="btn btn-secondary" href="<?= e($cancel_url) ?>">
319                <?= view('icons/cancel') ?>
320                <?= /* I18N: A button label. */
321                I18N::translate('cancel') ?>
322            </a>
323
324            <?php if ($name_fact instanceof Fact && (Auth::isAdmin() || $tree->getPreference('SHOW_GEDCOM_RECORD'))) : ?>
325                <a class="btn btn-link" href="<?= e(route(EditRawFactPage::class, ['xref' => $xref, 'fact_id' => $name_fact->id(), 'tree' => $tree->name()])) ?>">
326                    <?= I18N::translate('Edit the raw GEDCOM') ?>
327                </a>
328            <?php endif ?>
329        </div>
330    </div>
331</form>
332
333<?= view('modals/on-screen-keyboard') ?>
334<?= view('modals/ajax') ?>
335<?= view('edit/initialize-calendar-popup') ?>
336
337<?php View::push('javascript') ?>
338<script>
339    var SURNAME_TRADITION = <?= json_encode($tree->getPreference('SURNAME_TRADITION')) ?>;
340
341    var NAME = $("[name=NAME]");
342
343    // Generate a full name from the name components
344    function generate_name() {
345        var npfx      = document.querySelector("[name=NPFX]").value;
346        var givn      = document.querySelector("[name=GIVN]").value;
347        var spfx      = document.querySelector("[name=SPFX]").value;
348        var surn      = document.querySelector("[name=SURN]").value;
349        var nsfx      = document.querySelector("[name=NSFX]").value;
350        var sex_input = document.querySelector("[name=SEX]:checked");
351        var sex       = sex_input ? sex_input.value : "U";
352
353        return webtrees.buildNameFromParts(npfx, givn, spfx, surn, nsfx, sex);
354    }
355
356    // Update the NAME and _MARNM fields from the name components
357    // and also display the value in read-only "gedcom" format.
358    function updatewholename() {
359        // Don’t update the name if the user manually changed it
360        if (manualChange) {
361            return;
362        }
363
364        var npfx = document.querySelector("[name=NPFX]").value;
365        var givn = document.querySelector("[name=GIVN]").value;
366        var spfx = document.querySelector("[name=SPFX]").value;
367        var nsfx = document.querySelector("[name=NSFX]").value;
368        var name = generate_name();
369
370        var display_id = NAME.attr("id") + "_display";
371
372        NAME.val(name);
373        $("#" + display_id).text(name);
374
375        // Married names inherit some NSFX values, but not these
376        nsfx = nsfx.replace(/^(I|II|III|IV|V|VI|Junior|Jr\.?|Senior|Sr\.?)$/i, "");
377
378        // Update _MARNM field from _MARNM_SURN field and display it
379        var ip       = document.getElementsByTagName("input");
380        var marnm_id = "";
381        var romn     = "";
382        var heb      = "";
383        var i;
384
385        for (i = 0; i < ip.length; i++) {
386            if (ip[i].id.indexOf("_HEB") === 0) {
387                // Remember this field - we might need it later
388                heb = val;
389            }
390            if (ip[i].id.indexOf("ROMN") === 0) {
391                // Remember this field - we might need it later
392                romn = val;
393            }
394        }
395
396        for (i = 0; i < ip.length; i++) {
397            var val = ip[i].value;
398
399            if (ip[i].id.indexOf("_MARNM") === 0) {
400                if (ip[i].id.indexOf("_MARNM_SURN") === 0) {
401                    var msurn = "";
402                    if (val !== "") {
403                        if (webtrees.detectScript(val) === webtrees.detectScript(name)) {
404                            // Same script as NAME field?
405                            msurn = webtrees.buildNameFromParts(npfx, givn, spfx, val, nsfx);
406                        } else if (heb !== "" && webtrees.detectScript(val) === webtrees.detectScript(heb)) {
407                            // Same script as _HEB field?
408                            msurn = heb.replace(/\/.*\//, "/" + val + "/");
409                        } else if (romn !== "" && webtrees.detectScript(val) === webtrees.detectScript(romn)) {
410                            //. Same script as ROMN field
411                            msurn = romn.replace(/\/.*\//, "/" + val + "/");
412                        }
413                    }
414                    document.getElementById(marnm_id).value                  = msurn;
415                    document.getElementById(marnm_id + "_display").innerHTML = msurn;
416                } else {
417                    marnm_id = ip[i].id;
418                }
419            }
420        }
421    }
422
423    // Toggle the name editor fields between
424    // <input type="hidden"> <span style="display:inline">
425    // <input type="text">   <span style="display:none">
426
427    var oldName = "";
428
429    // Calls to generate_name() trigger an update - hence need to
430    // set the manual change to true first. We are probably
431    // listening to the wrong events on the input fields...
432    var manualChange = generate_name() !== NAME.val();
433
434    function convertHidden(eid) {
435        var input1 = $("#" + eid);
436        var input2 = $("#" + eid + "_display");
437
438        if (input1.attr("type") === "hidden") {
439            input1.attr("type", "text");
440            input2.hide();
441        } else {
442            input1.attr("type", "hidden");
443            input2.show();
444        }
445    }
446
447    /**
448     * if the user manually changed the NAME field, then update the textual
449     * HTML representation of it
450     * If the value changed set manualChange to true so that changing
451     * the other fields doesn’t change the NAME line
452     */
453    function updateTextName(eid) {
454        var element = document.getElementById(eid);
455        if (element) {
456            if (element.value !== oldName) {
457                manualChange = true;
458            }
459            var delement = document.getElementById(eid + "_display");
460            if (delement) {
461                delement.innerHTML = element.value;
462            }
463        }
464    }
465
466    function checkform() {
467        var ip = document.getElementsByTagName("input");
468        for (var i = 0; i < ip.length; i++) {
469            // ADD slashes to _HEB and _AKA names
470            if (ip[i].id.indexOf("_AKA") === 0 || ip[i].id.indexOf("_HEB") === 0 || ip[i].id.indexOf("ROMN") === 0)
471                if (ip[i].value.indexOf("/") < 0 && ip[i].value !== "")
472                    ip[i].value = ip[i].value.replace(/([^\s]+)\s*$/, "/$1/");
473            // Blank out temporary _MARNM_SURN
474            if (ip[i].id.indexOf("_MARNM_SURN") === 0)
475                ip[i].value = "";
476            // Convert "xxx yyy" and "xxx y yyy" surnames to "xxx,yyy"
477            if ((SURNAME_TRADITION === "spanish" || "SURNAME_TRADITION" === "portuguese") && ip[i].id.indexOf("SURN") === 0) {
478                ip[i].value = document.forms[0].SURN.value.replace(/^\s*([^\s,]{2,})\s+([iIyY] +)?([^\s,]{2,})\s*$/, "$1,$3");
479            }
480        }
481        return true;
482    }
483
484    // If the name isnt initially formed from the components in a standard way,
485    // then dont automatically update it.
486    if (NAME.val() !== generate_name() && NAME.val() !== "//") {
487        convertHidden(NAME.attr("id"));
488    }
489</script>
490<?php View::endpush() ?>
491
492