Bug #68651 » DateViewHelper.php

modified TYPO3 7.6 viewhelper - Jörg Velletti, 2016-12-09 14:41

 
1
<?php
2
namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3

    
4
/*                                                                        *
5
 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid".   *
6
 *                                                                        *
7
 * It is free software; you can redistribute it and/or modify it under    *
8
 * the terms of the GNU Lesser General Public License, either version 3   *
9
 *  of the License, or (at your option) any later version.                *
10
 *                                                                        *
11
 * The TYPO3 project - inspiring people to share!                         *
12
 *                                                                        */
13

    
14
use TYPO3\CMS\Core\Utility\MathUtility;
15
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
16
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
17
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
18
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
19

    
20
/**
21
 * Formats an object implementing \DateTimeInterface.
22
 *
23
 * = Examples =
24
 *
25
 * <code title="Defaults">
26
 * <f:format.date>{dateObject}</f:format.date>
27
 * </code>
28
 * <output>
29
 * 1980-12-13
30
 * (depending on the current date)
31
 * </output>
32
 *
33
 * <code title="Custom date format">
34
 * <f:format.date format="H:i">{dateObject}</f:format.date>
35
 * </code>
36
 * <output>
37
 * 01:23
38
 * (depending on the current time)
39
 * </output>
40
 *
41
 * <code title="Custom date format with Time/zone">
42
 * <f:format.date format="H:i" timeZone="Europe/Berlin">{dateObject}</f:format.date>
43
 * </code>
44
 * <output>
45
 * 00:23
46
 * (depending on the current time. UTC or see http://php.net/manual/en/timezones.php)
47
 * </output>
48
 *
49
 * <code title="Relative date with given time">
50
 * <f:format.date format="Y" base="{dateObject}">-1 year</f:format.date>
51
 * </code>
52
 * <output>
53
 * 2016
54
 * (assuming dateObject is in 2017)
55
 * </output>
56
 *
57
 * <code title="strtotime string">
58
 * <f:format.date format="d.m.Y - H:i:s">+1 week 2 days 4 hours 2 seconds</f:format.date>
59
 * </code>
60
 * <output>
61
 * 13.12.1980 - 21:03:42
62
 * (depending on the current time, see http://www.php.net/manual/en/function.strtotime.php)
63
 * </output>
64
 *
65
 * <code title="Localized dates using strftime date format">
66
 * <f:format.date format="%d. %B %Y">{dateObject}</f:format.date>
67
 * </code>
68
 * <output>
69
 * 13. Dezember 1980
70
 * (depending on the current date and defined locale. In the example you see the 1980-12-13 in a german locale)
71
 * </output>
72
 *
73
 * <code title="Inline notation">
74
 * {f:format.date(date: dateObject)}
75
 * </code>
76
 * <output>
77
 * 1980-12-13
78
 * (depending on the value of {dateObject})
79
 * </output>
80
 *
81
 * <code title="Inline notation (2nd variant)">
82
 * {dateObject -> f:format.date()}
83
 * </code>
84
 * <output>
85
 * 1980-12-13
86
 * (depending on the value of {dateObject})
87
 * </output>
88
 *
89
 * @api
90
 */
91
class DateViewHelper extends AbstractViewHelper implements CompilableInterface
92
{
93
    /**
94
     * @var bool
95
     */
96
    protected $escapingInterceptorEnabled = false;
97

    
98
    /**
99
     * Render the supplied DateTime object as a formatted date.
100
     *
101
     * @param mixed $date either an object implementing DateTimeInterface or a string that is accepted by DateTime constructor
102
     * @param string $format Format String which is taken to format the Date/Time
103
     * @param mixed $base A base time (an object implementing DateTimeInterface or a string) used if $date is a relative date specification. Defaults to current time.
104
	 * @param string $timeZone overWrite PHP timeZone Settings for Date/Time Object
105
	 *
106
     * @return string Formatted date
107
     * @throws Exception
108
     * @api
109
     */
110
    public function render($date = null, $format = '', $base = null , $timeZone = null )
111
    {
112

    
113
        return static::renderStatic(
114
            [
115
                'date' => $date,
116
                'format' => $format,
117
                'base' => $base,
118
				'timeZone' => $timeZone
119
            ],
120
            $this->buildRenderChildrenClosure(),
121
            $this->renderingContext
122
        );
123
    }
124

    
125
    /**
126
     * @param array $arguments
127
     * @param \Closure $renderChildrenClosure
128
     * @param RenderingContextInterface $renderingContext
129
     *
130
     * @return string
131
     * @throws Exception
132
     */
133
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
134
    {
135
        $date = $arguments['date'];
136
        $format = $arguments['format'];
137
        $base = $arguments['base'] === null ? time() : $arguments['base'];
138

    
139
		$timeZone = $arguments['timeZone'] === null ? trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['phpTimeZone'])  : $arguments['timeZone'];
140
		if( ! in_array($timeZone, timezone_identifiers_list()) ) {
141
			$timeZone = date_default_timezone_get() ;
142
		}
143
		if( ! in_array($timeZone, timezone_identifiers_list()) ) {
144
			$timeZone = 'UTC' ;
145
		}
146

    
147
		if (is_string($base)) {
148
            $base = trim($base);
149
        }
150

    
151
        if ($format === '') {
152
            $format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
153
        }
154

    
155
        if ($date === null) {
156
            $date = $renderChildrenClosure();
157
            if ($date === null) {
158
                return '';
159
            }
160
        }
161

    
162
        if (is_string($date)) {
163
            $date = trim($date);
164
        }
165

    
166
        if ($date === '') {
167
            $date = 'now';
168
        }
169

    
170
        if (!$date instanceof \DateTimeInterface) {
171
            try {
172
                $base = $base instanceof \DateTimeInterface ? $base->format('U') : strtotime((MathUtility::canBeInterpretedAsInteger($base) ? '@' : '') . $base);
173
                $dateTimestamp = strtotime((MathUtility::canBeInterpretedAsInteger($date) ? '@' : '') . $date, $base);
174
                $date = new \DateTime('@' . $dateTimestamp);
175
                $date->setTimezone(new \DateTimeZone($timeZone));
176
            } catch (\Exception $exception) {
177
                throw new Exception('"' . $date . '" could not be parsed by \DateTime constructor: ' . $exception->getMessage(), 1241722579);
178
            }
179
        }
180

    
181
        if (strpos($format, '%') !== false) {
182
            return strftime($format, $date->format('U'));
183
        } else {
184
            return $date->format($format);
185
        }
186
    }
187
}
(1-1/2)