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