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