xref: /webtrees/app/Elements/DateValue.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Elements;
21
22use Fisharebest\Webtrees\Date;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\LocalizationService;
25use Fisharebest\Webtrees\Tree;
26
27use function app;
28use function e;
29use function preg_replace_callback;
30use function view;
31
32/**
33 * DATE_VALUE := {Size=1:35}
34 * [ <DATE> | <DATE_PERIOD> | <DATE_RANGE>| <DATE_APPROXIMATED> | INT <DATE> (<DATE_PHRASE>) | (<DATE_PHRASE>) ]
35 * The DATE_VALUE represents the date of an activity, attribute, or event where:
36 * INT = Interpreted from knowledge about the associated date phrase included in parentheses.
37 * An acceptable alternative to the date phrase choice is to use one of the other choices such as
38 * <DATE_APPROXIMATED> choice as the DATE line value and then include the date phrase value as a
39 * NOTE value subordinate to the DATE line tag.
40 * The date value can take on the date form of just a date, an approximated date, between a date
41 * and another date, and from one date to another date. The preferred form of showing date
42 * imprecision, is to show, for example, MAY 1890 rather than ABT 12 MAY 1890. This is because
43 * limits have not been assigned to the precision of the prefixes such as ABT or EST.
44 */
45class DateValue extends AbstractElement
46{
47    /**
48     * An edit control for this data.
49     *
50     * @param string $id
51     * @param string $name
52     * @param string $value
53     * @param Tree   $tree
54     *
55     * @return string
56     */
57    public function edit(string $id, string $name, string $value, Tree $tree): string
58    {
59        // Need to know if the user prefers DMY/MDY/YMD so we can validate dates properly.
60        $localization_service = app(LocalizationService::class);
61        assert($localization_service instanceof LocalizationService);
62
63        $dmy = $localization_service->dateFormatToOrder(I18N::dateFormat());
64
65        return
66            '<div class="input-group">' .
67            '<input class="form-control" type="text" id="' . $id . '" name="' . $name . '" value="' . e($value) . '" onchange="webtrees.reformatDate(this, \'' . e($dmy) . '\')" dir="ltr" />' .
68            view('edit/input-addon-calendar', ['id' => $id]) .
69            view('edit/input-addon-help', ['fact' => 'DATE']) .
70            '</div>' .
71            '<div id="caldiv' . $id . '" style="position:absolute;visibility:hidden;background-color:white;z-index:1000"></div>' .
72            '<div class="form-text">' . (new Date($value))->display() . '</div>';
73    }
74
75    /**
76     * Escape @ signs in a GEDCOM export.
77     *
78     * @param string $value
79     *
80     * @return string
81     */
82    public function escape(string $value): string
83    {
84        // Only escape @ signs in an INT phrase
85        return preg_replace_callback('/\(.*@.*\)/', static function (array $matches): string {
86            return strtr($matches[0], ['@' => '@@']);
87        }, $value);
88    }
89
90    /**
91     * Display the value of this type of element.
92     *
93     * @param string $value
94     * @param Tree   $tree
95     *
96     * @return string
97     */
98    public function value(string $value, Tree $tree): string
99    {
100        $canonical = $this->canonical($value);
101
102        $date = new Date($canonical);
103
104        return $date->display();
105    }
106}
107