From 5c69a32835b3fd58057d095ef0239289fa3cc664 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Wed, 26 Apr 2023 18:21:11 +0200 Subject: [PATCH] Replace |time modifier by {time} function Closes #5277 --- .../TimeFunctionTemplatePlugin.class.php | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 wcfsetup/install/files/lib/system/template/plugin/TimeFunctionTemplatePlugin.class.php diff --git a/wcfsetup/install/files/lib/system/template/plugin/TimeFunctionTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/TimeFunctionTemplatePlugin.class.php new file mode 100644 index 0000000000..157065aa03 --- /dev/null +++ b/wcfsetup/install/files/lib/system/template/plugin/TimeFunctionTemplatePlugin.class.php @@ -0,0 +1,73 @@ + + * @since 6.0 + */ +final class TimeFunctionTemplatePlugin implements IFunctionTemplatePlugin +{ + public function execute($tagArgs, TemplateEngine $tplObj): string + { + $time = $tagArgs['time'] ?? ''; + $type = $tagArgs['type'] ?? ''; + + if ($time instanceof \DateTimeInterface) { + $dateTime = $time; + } else { + $timestamp = \intval($time); + $dateTime = new \DateTimeImmutable('@' . $timestamp); + } + + if ($type === '') { + $isFutureDate = $dateTime->getTimestamp() > TIME_NOW; + + $dateAndTime = \IntlDateFormatter::create( + WCF::getLanguage()->getLocale(), + \IntlDateFormatter::LONG, + \IntlDateFormatter::SHORT, + WCF::getUser()->getTimeZone() + )->format($dateTime); + + return \sprintf( + '%s', + $dateTime->format('c'), + $isFutureDate ? ' static' : '', + $dateAndTime + ); + } else if ($type === 'plain') { + return \str_replace( + '%time%', + DateUtil::format($dateTime, DateUtil::TIME_FORMAT), + \str_replace( + '%date%', + DateUtil::format($dateTime, DateUtil::DATE_FORMAT), + WCF::getLanguage()->get('wcf.date.dateTimeFormat') + ) + ); + } else if ($type === 'date') { + $format = $tagArgs['format'] ?? ''; + return DateUtil::format( + $dateTime, + $format ?: DateUtil::DATE_FORMAT + ); + } else { + throw new \InvalidArgumentException("Invalid type '{$type}' given."); + } + } +} -- 2.20.1