HTMLDateTimeField.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * A field that will contain a date and/or time
  4. *
  5. * Currently recognizes only {YYYY}-{MM}-{DD}T{HH}:{MM}:{SS.S*}Z formatted dates.
  6. *
  7. * Besides the parameters recognized by HTMLTextField, additional recognized
  8. * parameters in the field descriptor array include:
  9. * type - 'date', 'time', or 'datetime'
  10. * min - The minimum date to allow, in any recognized format.
  11. * max - The maximum date to allow, in any recognized format.
  12. * placeholder - The default comes from the htmlform-(date|time|datetime)-placeholder message.
  13. *
  14. * The result is a formatted date.
  15. *
  16. * @note This widget is not likely to work well in non-OOUI forms.
  17. */
  18. class HTMLDateTimeField extends HTMLTextField {
  19. protected static $patterns = [
  20. 'date' => '[0-9]{4}-[01][0-9]-[0-3][0-9]',
  21. 'time' => '[0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?',
  22. 'datetime' => '[0-9]{4}-[01][0-9]-[0-3][0-9][T ][0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?Z?',
  23. ];
  24. protected $mType = 'datetime';
  25. public function __construct( $params ) {
  26. parent::__construct( $params );
  27. $this->mType = array_key_exists( 'type', $params )
  28. ? $params['type']
  29. : 'datetime';
  30. if ( !in_array( $this->mType, [ 'date', 'time', 'datetime' ] ) ) {
  31. throw new InvalidArgumentException( "Invalid type '$this->mType'" );
  32. }
  33. if ( $this->mPlaceholder === '' ) {
  34. // Messages: htmlform-date-placeholder htmlform-time-placeholder htmlform-datetime-placeholder
  35. $this->mPlaceholder = $this->msg( "htmlform-{$this->mType}-placeholder" )->text();
  36. }
  37. $this->mClass .= ' mw-htmlform-datetime-field';
  38. }
  39. public function getAttributes( array $list ) {
  40. $parentList = array_diff( $list, [ 'min', 'max' ] );
  41. $ret = parent::getAttributes( $parentList );
  42. if ( in_array( 'min', $list ) && isset( $this->mParams['min'] ) ) {
  43. $min = $this->parseDate( $this->mParams['min'] );
  44. if ( $min ) {
  45. $ret['min'] = $this->formatDate( $min );
  46. }
  47. }
  48. if ( in_array( 'max', $list ) && isset( $this->mParams['max'] ) ) {
  49. $max = $this->parseDate( $this->mParams['max'] );
  50. if ( $max ) {
  51. $ret['max'] = $this->formatDate( $max );
  52. }
  53. }
  54. $ret['step'] = 1;
  55. $ret['type'] = $this->mType;
  56. $ret['pattern'] = static::$patterns[$this->mType];
  57. return $ret;
  58. }
  59. public function loadDataFromRequest( $request ) {
  60. if ( !$request->getCheck( $this->mName ) ) {
  61. return $this->getDefault();
  62. }
  63. $value = $request->getText( $this->mName );
  64. $date = $this->parseDate( $value );
  65. return $date ? $this->formatDate( $date ) : $value;
  66. }
  67. public function validate( $value, $alldata ) {
  68. $p = parent::validate( $value, $alldata );
  69. if ( $p !== true ) {
  70. return $p;
  71. }
  72. if ( $value === '' ) {
  73. // required was already checked by parent::validate
  74. return true;
  75. }
  76. $date = $this->parseDate( $value );
  77. if ( !$date ) {
  78. // Messages: htmlform-date-invalid htmlform-time-invalid htmlform-datetime-invalid
  79. return $this->msg( "htmlform-{$this->mType}-invalid" );
  80. }
  81. if ( isset( $this->mParams['min'] ) ) {
  82. $min = $this->parseDate( $this->mParams['min'] );
  83. if ( $min && $date < $min ) {
  84. // Messages: htmlform-date-toolow htmlform-time-toolow htmlform-datetime-toolow
  85. return $this->msg( "htmlform-{$this->mType}-toolow", $this->formatDate( $min ) );
  86. }
  87. }
  88. if ( isset( $this->mParams['max'] ) ) {
  89. $max = $this->parseDate( $this->mParams['max'] );
  90. if ( $max && $date > $max ) {
  91. // Messages: htmlform-date-toohigh htmlform-time-toohigh htmlform-datetime-toohigh
  92. return $this->msg( "htmlform-{$this->mType}-toohigh", $this->formatDate( $max ) );
  93. }
  94. }
  95. return true;
  96. }
  97. protected function parseDate( $value ) {
  98. $value = trim( $value );
  99. if ( $value === '' ) {
  100. return false;
  101. }
  102. if ( $this->mType === 'date' ) {
  103. $value .= ' T00:00:00+0000';
  104. }
  105. if ( $this->mType === 'time' ) {
  106. $value = '1970-01-01 ' . $value . '+0000';
  107. }
  108. try {
  109. $date = new DateTime( $value, new DateTimeZone( 'GMT' ) );
  110. return $date->getTimestamp();
  111. } catch ( Exception $ex ) {
  112. return false;
  113. }
  114. }
  115. protected function formatDate( $value ) {
  116. switch ( $this->mType ) {
  117. case 'date':
  118. return gmdate( 'Y-m-d', $value );
  119. case 'time':
  120. return gmdate( 'H:i:s', $value );
  121. case 'datetime':
  122. return gmdate( 'Y-m-d\\TH:i:s\\Z', $value );
  123. }
  124. }
  125. public function getInputOOUI( $value ) {
  126. $params = [
  127. 'type' => $this->mType,
  128. 'value' => $value,
  129. 'name' => $this->mName,
  130. 'id' => $this->mID,
  131. ];
  132. if ( isset( $this->mParams['min'] ) ) {
  133. $min = $this->parseDate( $this->mParams['min'] );
  134. if ( $min ) {
  135. $params['min'] = $this->formatDate( $min );
  136. }
  137. }
  138. if ( isset( $this->mParams['max'] ) ) {
  139. $max = $this->parseDate( $this->mParams['max'] );
  140. if ( $max ) {
  141. $params['max'] = $this->formatDate( $max );
  142. }
  143. }
  144. if ( $this->mType === 'date' ) {
  145. $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
  146. return new MediaWiki\Widget\DateInputWidget( $params );
  147. } else {
  148. return new MediaWiki\Widget\DateTimeInputWidget( $params );
  149. }
  150. }
  151. protected function getOOUIModules() {
  152. if ( $this->mType === 'date' ) {
  153. return [ 'mediawiki.widgets.DateInputWidget' ];
  154. } else {
  155. return [ 'mediawiki.widgets.datetime' ];
  156. }
  157. }
  158. protected function shouldInfuseOOUI() {
  159. return true;
  160. }
  161. }