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