xref: /webtrees/app/Elements/DateValue.php (revision 1821c9e596679be8717c51394b448df204b1f577)
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\Html;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Services\LocalizationService;
26use Fisharebest\Webtrees\Tree;
27
28use function app;
29use function assert;
30use function e;
31use function preg_replace_callback;
32use function view;
33
34/**
35 * DATE_VALUE := {Size=1:35}
36 * [ <DATE> | <DATE_PERIOD> | <DATE_RANGE>| <DATE_APPROXIMATED> | INT <DATE> (<DATE_PHRASE>) | (<DATE_PHRASE>) ]
37 * The DATE_VALUE represents the date of an activity, attribute, or event where:
38 * INT = Interpreted from knowledge about the associated date phrase included in parentheses.
39 * An acceptable alternative to the date phrase choice is to use one of the other choices such as
40 * <DATE_APPROXIMATED> choice as the DATE line value and then include the date phrase value as a
41 * NOTE value subordinate to the DATE line tag.
42 * The date value can take on the date form of just a date, an approximated date, between a date
43 * and another date, and from one date to another date. The preferred form of showing date
44 * imprecision, is to show, for example, MAY 1890 rather than ABT 12 MAY 1890. This is because
45 * limits have not been assigned to the precision of the prefixes such as ABT or EST.
46 */
47class DateValue extends AbstractElement
48{
49    /**
50     * An edit control for this data.
51     *
52     * @param string $id
53     * @param string $name
54     * @param string $value
55     * @param Tree   $tree
56     *
57     * @return string
58     */
59    public function edit(string $id, string $name, string $value, Tree $tree): string
60    {
61        // Need to know if the user prefers DMY/MDY/YMD so we can validate dates properly.
62        $localization_service = app(LocalizationService::class);
63        assert($localization_service instanceof LocalizationService);
64
65        $dmy = $localization_service->dateFormatToOrder(I18N::dateFormat());
66
67        $attributes = [
68            'class'     => 'form-control',
69            'dir'       => 'ltr',
70            'type'      => 'text',
71            'id'        => $id,
72            'name'      => $name,
73            'value'     => $value,
74            'onchange'  => 'webtrees.reformatDate(this, \'' . e($dmy) . '\')',
75            'maxlength' => static::MAXIMUM_LENGTH,
76            'pattern'   => static::PATTERN,
77        ];
78
79        return
80            '<div class="input-group">' .
81            '<input ' . Html::attributes($attributes) . ' />' .
82            view('edit/input-addon-calendar', ['id' => $id]) .
83            view('edit/input-addon-help', ['topic' => 'DATE']) .
84            '</div>' .
85            '<div id="caldiv' . $id . '" style="position:absolute;visibility:hidden;background-color:white;z-index:1000"></div>' .
86            '<div class="form-text">' . (new Date($value))->display() . '</div>';
87    }
88
89    /**
90     * Escape @ signs in a GEDCOM export.
91     *
92     * @param string $value
93     *
94     * @return string
95     */
96    public function escape(string $value): string
97    {
98        // Only escape @ signs in an INT phrase
99        return preg_replace_callback('/\(.*@.*\)/', static function (array $matches): string {
100            return strtr($matches[0], ['@' => '@@']);
101        }, $value);
102    }
103
104    /**
105     * Display the value of this type of element.
106     *
107     * @param string $value
108     * @param Tree   $tree
109     *
110     * @return string
111     */
112    public function value(string $value, Tree $tree): string
113    {
114        $canonical = $this->canonical($value);
115
116        $date = new Date($canonical);
117
118        return $date->display();
119    }
120}
121