Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TDateFromat formatting component.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TDateFormat.php 2510 2008-10-13 10:28:33Z carl $
10
 * @package System.I18N
11
 */
12
 
13
/**
14
 * Get the DateFormat class.
15
 */
16
Prado::using('System.I18N.core.DateFormat');
17
 
18
/**
19
 * Get the parent control class.
20
 */
21
Prado::using('System.I18N.TI18NControl');
22
 
23
/**
24
 * To format dates and/or time according to the current locale use
25
 * <code>
26
 * <com:TDateFormat Pattern="dd:MMM:yyyy" Value="01/01/2001" />
27
 *</code>
28
 * The date will be formatted according to the current locale (or culture)
29
 * using the format specified by 'Pattern' attribute.
30
 *
31
 * To format date and/or time for a locale (e.g. de_DE) include a Culture
32
 * attribute, for example:
33
 * <code>
34
 * <com:TDateFormat Culture="de_DE" Value="01/01/2001 12:00" />
35
 * </code>
36
 * The date will be formatted according to this format.
37
 *
38
 * If no Pattern was specified then the date will be formatted with the
39
 * default format (both date and time). If no value for the date is specified
40
 * then the current date will be used. E.g.: <code><com:TDateFormat /></code>
41
 * will result in the current date, formatted with default localized pattern.
42
 *
43
 * Namespace: System.I18N
44
 *
45
 * Properties
46
 * - <b>Value</b>, date,
47
 *   <br>Gets or sets the date to format. The tag content is used as Value
48
 *   if the Value property is not specified.
49
 * - <b>Pattern</b>, string,
50
 *   <br>Gets or sets the formatting pattern. The predefined patterns are
51
 *   'fulldate',           'longdate', 'mediumdate', 'shortdate', 'fulltime',
52
 * 'longtime', 'mediumtime', and 'shorttime'. Custom patterns can   specified
53
 * when the Pattern property does not match the predefined   patterns.
54
 * - <b>DefaultText</b>, string,
55
 * <br>Gets or sets the default text. If Value is not set, DefaultText will be
56
 * shown instead of todays date and time.
57
 *
58
 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
59
 * @version v1.0, last update on Sat Dec 11 15:25:11 EST 2004
60
 * @package System.I18N
61
 */
62
class TDateFormat extends TI18NControl implements IDataRenderer
63
{
64
	/**
65
	 * Default DateFormat, set to the application culture.
66
	 * @var DateFormat
67
	 */
68
	protected static $formatter;
69
 
70
	/**
71
	 * A set of pattern presets and their respective formatting shorthand.
72
	 * @var array
73
	 */
74
	static private $_patternPresets = array(
75
			'fulldate'=>'P','full'=>'P',
76
			'longdate'=>'D','long'=>'d',
77
			'mediumdate'=>'p','medium'=>'p',
78
			'shortdate'=>'d','short'=>'d',
79
			'fulltime'=>'Q', 'longtime'=>'T',
80
			'mediumtime'=>'q', 'shorttime'=>'t');
81
 
82
	/**
83
	 * Sets the date time formatting pattern.
84
	 * @param string format pattern.
85
	 */
86
	public function setPattern($value)
87
	{
88
		$this->setViewState('Pattern',$value,'');
89
	}
90
 
91
	/**
92
	 * Gets the date time format pattern.
93
	 * @return string format pattern.
94
	 */
95
	public function getPattern()
96
	{
97
		$string = $this->getViewState('Pattern','');
98
 
99
		$pattern = null;
100
 
101
		//try the subpattern of "date time" presets
102
		$subpatterns = explode(' ',$string,2);
103
		$datetime = array();
104
		if(count($subpatterns)==2)
105
		{
106
			$datetime[] = $this->getPreset($subpatterns[0]);
107
			$datetime[] = $this->getPreset($subpatterns[1]);
108
		}
109
 
110
		//we have a good subpattern
111
		if(count($datetime) == 2
112
			&& strlen($datetime[0]) == 1
113
			&& strlen($datetime[1]) == 1)
114
		{
115
			$pattern = $datetime;
116
		}
117
		else //no subpattern, try the presets
118
			$pattern = $this->getPreset($string);
119
 
120
		//no presets found, use the string as the pattern
121
		//and let the DateFormat handle it.
122
		if(is_null($pattern))
123
			$pattern = $string;
124
		if (!is_array($pattern) && strlen($pattern) == 0)
125
			$pattern = null;
126
		return $pattern;
127
	}
128
 
129
	/**
130
	 * For a given string, try and find a preset pattern.
131
	 * @param string the preset pattern name
132
	 * @return string a preset pattern if found, null otherwise.
133
	 */
134
	protected function getPreset($string)
135
	{
136
		$string = strtolower($string);
137
		foreach(self::$_patternPresets as $pattern => $preset)
138
		{
139
			if($string == $pattern)
140
				return $preset;
141
		}
142
	}
143
 
144
	/**
145
	 * Get the date-time value for this control.
146
	 * @return string date time value.
147
	 */
148
	public function getValue()
149
	{
150
		$value = $this->getViewState('Value','');
151
		if(empty($value))
152
		{
153
			$defaultText = $this->getDefaultText();
154
			if(empty($defaultText))
155
				return time();
156
		}
157
		return $value;
158
	}
159
 
160
	/**
161
	 * Set the date-time value for this control.
162
	 * @param string the date-time value.
163
	 */
164
	public function setValue($value)
165
	{
166
		$this->setViewState('Value',$value,'');
167
	}
168
 
169
	/**
170
	 * Get the default text value for this control.
171
	 * @return string default text value
172
	 */
173
	public function getDefaultText()
174
	{
175
		return $this->getViewState('DefaultText','');
176
	}
177
 
178
	/**
179
	 * Set the default text value for this control.
180
	 * @param string default text value
181
	 */
182
	public function setDefaultText($value)
183
	{
184
		$this->setViewState('DefaultText',$value,'');
185
	}
186
 
187
	/**
188
	 * Get the date-time value for this control.
189
	 * This method is required by {@link IDataRenderer}.
190
	 * It is the same as {@link getValue()}.
191
	 * @return string date time value.
192
	 * @see getValue
193
	 * @since 3.1.2
194
	 */
195
	public function getData()
196
	{
197
		return $this->getValue();
198
	}
199
 
200
	/**
201
	 * Set the date-time value for this control.
202
	 * This method is required by {@link IDataRenderer}.
203
	 * It is the same as {@link setValue()}.
204
	 * @param string the date-time value.
205
	 * @see setValue
206
	 * @since 3.1.2
207
	 */
208
	public function setData($value)
209
	{
210
		$this->setValue($value);
211
	}
212
 
213
	/**
214
	 * Renders the localized version of the date-time value.
215
	 * If the culture is not specified, the default application
216
	 * culture will be used.
217
	 * This method overrides parent's implementation.
218
	 */
219
	protected function getFormattedDate()
220
	{
221
		$value = $this->getValue();
222
		$defaultText = $this->getDefaultText();
223
		if(empty($value) && !empty($defaultText))
224
			return $this->getDefaultText();
225
 
226
		$app = $this->getApplication()->getGlobalization();
227
 
228
		//initialized the default class wide formatter
229
		if(is_null(self::$formatter))
230
			self::$formatter = new DateFormat($app->getCulture());
231
 
232
		$culture = $this->getCulture();
233
 
234
		//return the specific cultural formatted date time
235
		if(strlen($culture) && $app->getCulture() !== $culture)
236
		{
237
			$formatter = new DateFormat($culture);
238
			return $formatter->format($value,
239
									  $this->getPattern(),
240
									  $this->getCharset());
241
		}
242
		//return the application wide culture formatted date time.
243
		$result = self::$formatter->format($value,
244
										$this->getPattern(),
245
										$this->getCharset());
246
		return $result;
247
	}
248
 
249
	public function render($writer)
250
	{
251
		$writer->write($this->getFormattedDate());
252
	}
253
 
254
}