1c2ed51d1SGreg Roach<?php 2c2ed51d1SGreg Roach 3c2ed51d1SGreg Roach/** 4c2ed51d1SGreg Roach * webtrees: online genealogy 5*5bfc6897SGreg Roach * Copyright (C) 2022 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. 60b55cbc6bSGreg Roach $localization_service = app(LocalizationService::class); 61b55cbc6bSGreg Roach assert($localization_service instanceof LocalizationService); 62b55cbc6bSGreg Roach 63b55cbc6bSGreg Roach $dmy = $localization_service->dateFormatToOrder(I18N::dateFormat()); 64c2ed51d1SGreg Roach 65c2ed51d1SGreg Roach return 66c2ed51d1SGreg Roach '<div class="input-group">' . 674a213054SGreg Roach '<input class="form-control" type="text" id="' . $id . '" name="' . $name . '" value="' . e($value) . '" onchange="webtrees.reformatDate(this, \'' . e($dmy) . '\')" dir="ltr" />' . 68c2ed51d1SGreg Roach view('edit/input-addon-calendar', ['id' => $id]) . 69c2ed51d1SGreg Roach view('edit/input-addon-help', ['fact' => 'DATE']) . 70c2ed51d1SGreg Roach '</div>' . 71c2ed51d1SGreg Roach '<div id="caldiv' . $id . '" style="position:absolute;visibility:hidden;background-color:white;z-index:1000"></div>' . 72315eb316SGreg Roach '<div class="form-text">' . (new Date($value))->display() . '</div>'; 73c2ed51d1SGreg Roach } 74c2ed51d1SGreg Roach 75c2ed51d1SGreg Roach /** 76c2ed51d1SGreg Roach * Escape @ signs in a GEDCOM export. 77c2ed51d1SGreg Roach * 78c2ed51d1SGreg Roach * @param string $value 79c2ed51d1SGreg Roach * 80c2ed51d1SGreg Roach * @return string 81c2ed51d1SGreg Roach */ 82c2ed51d1SGreg Roach public function escape(string $value): string 83c2ed51d1SGreg Roach { 84c2ed51d1SGreg Roach // Only escape @ signs in an INT phrase 85c2ed51d1SGreg Roach return preg_replace_callback('/\(.*@.*\)/', static function (array $matches): string { 86c2ed51d1SGreg Roach return strtr($matches[0], ['@' => '@@']); 87c2ed51d1SGreg Roach }, $value); 88c2ed51d1SGreg Roach } 89c2ed51d1SGreg Roach 90c2ed51d1SGreg Roach /** 91c2ed51d1SGreg Roach * Display the value of this type of element. 92c2ed51d1SGreg Roach * 93c2ed51d1SGreg Roach * @param string $value 94c2ed51d1SGreg Roach * @param Tree $tree 95c2ed51d1SGreg Roach * 96c2ed51d1SGreg Roach * @return string 97c2ed51d1SGreg Roach */ 98c2ed51d1SGreg Roach public function value(string $value, Tree $tree): string 99c2ed51d1SGreg Roach { 100c2ed51d1SGreg Roach $canonical = $this->canonical($value); 101c2ed51d1SGreg Roach 102c2ed51d1SGreg Roach $date = new Date($canonical); 103c2ed51d1SGreg Roach 1043d2c98d1SGreg Roach return $date->display(); 105c2ed51d1SGreg Roach } 106c2ed51d1SGreg Roach} 107