‪TYPO3CMS  ‪main
DateViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
29 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
30 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
31 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
32 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
33 
133 final class ‪DateViewHelper extends AbstractViewHelper
134 {
135  use CompileWithContentArgumentAndRenderStatic;
136 
142  protected ‪$escapeChildren = false;
143 
144  public function ‪initializeArguments(): void
145  {
146  $this->registerArgument('date', 'mixed', 'Either an object implementing DateTimeInterface or a string that is accepted by DateTime constructor');
147  $this->registerArgument('format', 'string', 'Format String which is taken to format the Date/Time', false, '');
148  $this->registerArgument('pattern', 'string', 'Format date based on unicode ICO format pattern given see https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax. If both "pattern" and "format" arguments are given, pattern will be used.');
149  $this->registerArgument('locale', 'string', 'A locale format such as "nl-NL" to format the date in a specific locale, if none given, uses the current locale of the current request. Only works when pattern argument is given');
150  $this->registerArgument('base', 'mixed', 'A base time (an object implementing DateTimeInterface or a string) used if $date is a relative date specification. Defaults to current time.');
151  }
152 
156  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
157  {
158  $format = $arguments['format'] ?? '';
159  $pattern = $arguments['pattern'] ?? null;
160  $base = $arguments['base'] ?? GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp');
161  if (is_string($base)) {
162  $base = trim($base);
163  }
164 
165  if ($format === '') {
166  $format = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
167  }
168 
169  $date = $renderChildrenClosure();
170  if ($date === null) {
171  return '';
172  }
173 
174  if (is_string($date)) {
175  $date = trim($date);
176  }
177 
178  if ($date === '') {
179  $date = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp', 'now');
180  }
181 
182  if (!$date instanceof \DateTimeInterface) {
183  $base = $base instanceof \DateTimeInterface
184  ? (int)$base->format('U')
185  : (int)strtotime((‪MathUtility::canBeInterpretedAsInteger($base) ? '@' : '') . $base);
186  $dateTimestamp = strtotime((‪MathUtility::canBeInterpretedAsInteger($date) ? '@' : '') . $date, $base);
187  if ($dateTimestamp === false) {
188  throw new Exception('"' . $date . '" could not be converted to a timestamp. Probably due to a parsing error.', 1241722579);
189  }
190  $date = (new \DateTime())->setTimestamp($dateTimestamp);
191  }
192 
193  if ($pattern !== null) {
194  $locale = $arguments['locale'] ?? ‪self::resolveLocale($renderingContext);
195  return (new DateFormatter())->format($date, $pattern, $locale);
196  }
197  if (str_contains($format, '%')) {
198  // @todo: deprecate this syntax in TYPO3 v13.
199  $locale = $arguments['locale'] ?? ‪self::resolveLocale($renderingContext);
200  return (new DateFormatter())->strftime($format, $date, $locale);
201  }
202  return $date->format($format);
203  }
204 
208  public function ‪resolveContentArgumentName(): string
209  {
210  return 'date';
211  }
212 
213  private static function ‪resolveLocale(RenderingContextInterface $renderingContext): ‪Locale
214  {
215  $request = null;
216  if ($renderingContext instanceof ‪RenderingContext) {
217  $request = $renderingContext->getRequest();
218  } elseif ((‪$GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface) {
219  $request = ‪$GLOBALS['TYPO3_REQUEST'];
220  }
221  if ($request && ‪ApplicationType::fromRequest($request)->isFrontend()) {
222  // Frontend application
223  $siteLanguage = $request->getAttribute('language');
224 
225  // Get values from site language
226  if ($siteLanguage !== null) {
227  return $siteLanguage->getLocale();
228  }
229  } elseif ((‪$GLOBALS['BE_USER'] ?? null) instanceof BackendUserAuthentication
230  && !empty(‪$GLOBALS['BE_USER']->user['lang'])) {
231  return new Locale(‪$GLOBALS['BE_USER']->user['lang']);
232  }
233  return new Locale();
234  }
235 }
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper\resolveLocale
‪static resolveLocale(RenderingContextInterface $renderingContext)
Definition: DateViewHelper.php:211
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper\initializeArguments
‪initializeArguments()
Definition: DateViewHelper.php:142
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: DateViewHelper.php:154
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper
Definition: DateViewHelper.php:134
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: DateViewHelper.php:140
‪TYPO3\CMS\Core\Localization\DateFormatter
Definition: DateFormatter.php:27
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Fluid\ViewHelpers\Format
Definition: AbstractEncodingViewHelper.php:18
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Localization\Locale
Definition: Locale.php:30
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Http\fromRequest
‪@ fromRequest
Definition: ApplicationType.php:66
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContext
Definition: RenderingContext.php:35
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper\resolveContentArgumentName
‪resolveContentArgumentName()
Definition: DateViewHelper.php:206
‪TYPO3\CMS\Core\Http\ApplicationType
‪ApplicationType
Definition: ApplicationType.php:55