. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Elements; use Fisharebest\Webtrees\Date; use Fisharebest\Webtrees\Html; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Tree; use function e; use function preg_replace_callback; use function view; /** * DATE_VALUE := {Size=1:35} * [ | | | | INT () | () ] * The DATE_VALUE represents the date of an activity, attribute, or event where: * INT = Interpreted from knowledge about the associated date phrase included in parentheses. * An acceptable alternative to the date phrase choice is to use one of the other choices such as * choice as the DATE line value and then include the date phrase value as a * NOTE value subordinate to the DATE line tag. * The date value can take on the date form of just a date, an approximated date, between a date * and another date, and from one date to another date. The preferred form of showing date * imprecision, is to show, for example, MAY 1890 rather than ABT 12 MAY 1890. This is because * limits have not been assigned to the precision of the prefixes such as ABT or EST. */ class DateValue extends AbstractElement { /** * An edit control for this data. * * @param string $id * @param string $name * @param string $value * @param Tree $tree * * @return string */ public function edit(string $id, string $name, string $value, Tree $tree): string { // Need to know if the user prefers DMY/MDY/YMD so we can validate dates properly. $dmy = I18N::language()->dateOrder(); $attributes = [ 'class' => 'form-control', 'dir' => 'ltr', 'type' => 'text', 'id' => $id, 'name' => $name, 'value' => $value, 'onchange' => 'webtrees.reformatDate(this, \'' . e($dmy) . '\')', 'maxlength' => static::MAXIMUM_LENGTH, 'pattern' => static::PATTERN, ]; return '
' . '' . view('edit/input-addon-calendar', ['id' => $id]) . view('edit/input-addon-help', ['topic' => 'DATE']) . '
' . '' . '
' . (new Date($value))->display() . '
'; } /** * Escape @ signs in a GEDCOM export. * * @param string $value * * @return string */ public function escape(string $value): string { // Only escape @ signs in an INT phrase return preg_replace_callback('/\(.*@.*\)/', static fn (array $matches): string => strtr($matches[0], ['@' => '@@']), $value); } /** * Display the value of this type of element. * * @param string $value * @param Tree $tree * * @return string */ public function value(string $value, Tree $tree): string { $canonical = $this->canonical($value); $date = new Date($canonical); return $date->display(); } }