/framework/I18N/TDateFormat.php

https://bitbucket.org/volatileeight/prado · PHP · 253 lines · 109 code · 24 blank · 120 comment · 21 complexity · a91794163e5021466f8c387aa62679b3 MD5 · raw file

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