summaryrefslogtreecommitdiff
path: root/app/Elements/DateValue.php
blob: 477572fa96287f4b3067a1345b7908cfc4d023a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

/**
 * webtrees: online genealogy
 * Copyright (C) 2026 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

declare(strict_types=1);

namespace Fisharebest\Webtrees\Elements;

use Fisharebest\Webtrees\Date;
use Fisharebest\Webtrees\Html;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Tree;

use function e;
use function preg_replace_callback;
use function view;

/**
 * DATE_VALUE := {Size=1:35}
 * [ <DATE> | <DATE_PERIOD> | <DATE_RANGE>| <DATE_APPROXIMATED> | INT <DATE> (<DATE_PHRASE>) | (<DATE_PHRASE>) ]
 * The DATE_VALUE represents the date of an activity, attribute, or event where:
 * INT = Interpreted from knowledge about the associated date phrase included in parentheses.
 * An acceptable alternative to the date phrase choice is to use one of the other choices such as
 * <DATE_APPROXIMATED> choice as the DATE line value and then include the date phrase value as a
 * NOTE value subordinate to the DATE line tag.
 * The date value can take on the date form of just a date, an approximated date, between a date
 * and another date, and from one date to another date. The preferred form of showing date
 * imprecision, is to show, for example, MAY 1890 rather than ABT 12 MAY 1890. This is because
 * limits have not been assigned to the precision of the prefixes such as ABT or EST.
 */
class DateValue extends AbstractElement
{
    /**
     * An edit control for this data.
     *
     * @param string $id
     * @param string $name
     * @param string $value
     * @param Tree   $tree
     *
     * @return string
     */
    public function edit(string $id, string $name, string $value, Tree $tree): string
    {
        // Need to know if the user prefers DMY/MDY/YMD so we can validate dates properly.
        $dmy = I18N::language()->dateOrder();

        $attributes = [
            'class'     => 'form-control',
            'dir'       => 'ltr',
            'type'      => 'text',
            'id'        => $id,
            'name'      => $name,
            'value'     => $value,
            'onchange'  => 'webtrees.reformatDate(this, \'' . e($dmy) . '\')',
            'maxlength' => static::MAXIMUM_LENGTH,
            'pattern'   => static::PATTERN,
        ];

        return
            '<div class="input-group">' .
            '<input ' . Html::attributes($attributes) . ' />' .
            view('edit/input-addon-calendar', ['id' => $id]) .
            view('edit/input-addon-help', ['topic' => 'DATE']) .
            '</div>' .
            '<div id="caldiv' . $id . '" style="position:absolute;visibility:hidden;background-color:white;z-index:1000"></div>' .
            '<div class="form-text">' . (new Date($value))->display() . '</div>';
    }

    /**
     * Escape @ signs in a GEDCOM export.
     *
     * @param string $value
     *
     * @return string
     */
    public function escape(string $value): string
    {
        // Only escape @ signs in an INT phrase
        return preg_replace_callback('/\(.*@.*\)/', static fn (array $matches): string => strtr($matches[0], ['@' => '@@']), $value);
    }

    /**
     * Display the value of this type of element.
     *
     * @param string $value
     * @param Tree   $tree
     *
     * @return string
     */
    public function value(string $value, Tree $tree): string
    {
        $canonical = $this->canonical($value);

        $date = new Date($canonical);

        return $date->display();
    }
}