-
Notifications
You must be signed in to change notification settings - Fork 638
/
DateViewHelper.php
235 lines (218 loc) · 7.97 KB
/
DateViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Localization\DateFormatter;
use TYPO3\CMS\Core\Localization\Locale;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
/**
* Formats an object implementing :php:`\DateTimeInterface`.
*
* Possible date/time formats can be found in the PHP documentation:
* https://www.php.net/manual/datetime.format.php
*
* Examples
* ========
*
* Defaults
* --------
*
* ::
*
* <f:format.date>{dateObject}</f:format.date>
*
* ``1980-12-13``
* Depending on the current date.
*
* Custom date format
* ------------------
*
* ::
*
* <f:format.date format="H:i">{dateObject}</f:format.date>
*
* ``01:23``
* Depending on the current time.
*
* Relative date with given time
* -----------------------------
*
* ::
*
* <f:format.date format="Y" base="{dateObject}">-1 year</f:format.date>
*
* ``2016``
* Assuming dateObject is in 2017.
*
* strtotime string
* ----------------
*
* ::
*
* <f:format.date format="d.m.Y - H:i:s">+1 week 2 days 4 hours 2 seconds</f:format.date>
*
* ``13.12.1980 - 21:03:42``
* Depending on the current time, see https://www.php.net/manual/function.strtotime.php.
*
* Localized dates using strftime date format
* ------------------------------------------
*
* ::
*
* <f:format.date format="%d. %B %Y">{dateObject}</f:format.date>
*
* ``13. Dezember 1980``
* Depending on the current date and defined locale. In the example you see the 1980-12-13 in a german locale.
*
* Localized dates using ICU-based date and time formatting
* --------------------------------------------------------
*
* ::
*
* <f:format.date pattern="dd. MMMM yyyy" locale="de-DE">{dateObject}</f:format.date>
*
* ``13. Dezember 1980``
* Depending on the current date. In the example you see the 1980-12-13 in a german locale.
*
* Localized dates using default formatting patterns
* -------------------------------------------------
*
* ::
*
* <f:format.date pattern="FULL" locale="fr-FR">{dateObject}</f:format.date>
*
* ``jeudi 9 mars 2023 à 21:40:49 temps universel coordonné``
* Depending on the current date and operating system setting. In the example you see the 2023-03-09 in a french locale.
*
* Inline notation
* ---------------
*
* ::
*
* {f:format.date(date: dateObject)}
*
* ``1980-12-13``
* Depending on the value of ``{dateObject}``.
*
* Inline notation (2nd variant)
* -----------------------------
*
* ::
*
* {dateObject -> f:format.date()}
*
* ``1980-12-13``
* Depending on the value of ``{dateObject}``.
*/
final class DateViewHelper extends AbstractViewHelper
{
use CompileWithContentArgumentAndRenderStatic;
/**
* Needed as child node's output can return a DateTime object which can't be escaped
*
* @var bool
*/
protected $escapeChildren = false;
public function initializeArguments(): void
{
$this->registerArgument('date', 'mixed', 'Either an object implementing DateTimeInterface or a string that is accepted by DateTime constructor');
$this->registerArgument('format', 'string', 'Format String which is taken to format the Date/Time', false, '');
$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.');
$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');
$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.');
}
/**
* @throws Exception
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
{
$format = $arguments['format'] ?? '';
$pattern = $arguments['pattern'] ?? null;
$base = $arguments['base'] ?? GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp');
if (is_string($base)) {
$base = trim($base);
}
if ($format === '') {
$format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
}
$date = $renderChildrenClosure();
if ($date === null) {
return '';
}
if (is_string($date)) {
$date = trim($date);
}
if ($date === '') {
$date = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp', 'now');
}
if (!$date instanceof \DateTimeInterface) {
$base = $base instanceof \DateTimeInterface
? (int)$base->format('U')
: (int)strtotime((MathUtility::canBeInterpretedAsInteger($base) ? '@' : '') . $base);
$dateTimestamp = strtotime((MathUtility::canBeInterpretedAsInteger($date) ? '@' : '') . $date, $base);
if ($dateTimestamp === false) {
throw new Exception('"' . $date . '" could not be converted to a timestamp. Probably due to a parsing error.', 1241722579);
}
$date = (new \DateTime())->setTimestamp($dateTimestamp);
}
if ($pattern !== null) {
$locale = $arguments['locale'] ?? self::resolveLocale($renderingContext);
return (new DateFormatter())->format($date, $pattern, $locale);
}
if (str_contains($format, '%')) {
// @todo: deprecate this syntax in TYPO3 v13.
$locale = $arguments['locale'] ?? self::resolveLocale($renderingContext);
return (new DateFormatter())->strftime($format, $date, $locale);
}
return $date->format($format);
}
/**
* Explicitly set argument name to be used as content.
*/
public function resolveContentArgumentName(): string
{
return 'date';
}
private static function resolveLocale(RenderingContextInterface $renderingContext): Locale
{
$request = null;
if ($renderingContext instanceof RenderingContext) {
$request = $renderingContext->getRequest();
} elseif (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface) {
$request = $GLOBALS['TYPO3_REQUEST'];
}
if ($request && ApplicationType::fromRequest($request)->isFrontend()) {
// Frontend application
$siteLanguage = $request->getAttribute('language');
// Get values from site language
if ($siteLanguage !== null) {
return $siteLanguage->getLocale();
}
} elseif (($GLOBALS['BE_USER'] ?? null) instanceof BackendUserAuthentication
&& !empty($GLOBALS['BE_USER']->user['lang'])) {
return new Locale($GLOBALS['BE_USER']->user['lang']);
}
return new Locale();
}
}