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