HTMLExpiryField.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. use MediaWiki\Widget\ExpiryInputWidget;
  3. /**
  4. * Expiry Field that allows the user to specify a precise date or a
  5. * relative date string.
  6. */
  7. class HTMLExpiryField extends HTMLFormField {
  8. /**
  9. * @var HTMLFormField
  10. */
  11. protected $relativeField;
  12. /**
  13. * Relative Date Time Field.
  14. * @param array $params
  15. */
  16. public function __construct( array $params = [] ) {
  17. parent::__construct( $params );
  18. $type = !empty( $params['options'] ) ? 'selectorother' : 'text';
  19. $this->relativeField = $this->getFieldByType( $type );
  20. }
  21. /**
  22. * @inheritDoc
  23. *
  24. * Use whatever the relative field is as the standard HTML input.
  25. */
  26. public function getInputHTML( $value ) {
  27. return $this->relativeField->getInputHTML( $value );
  28. }
  29. protected function shouldInfuseOOUI() {
  30. return true;
  31. }
  32. /**
  33. * @inheritDoc
  34. */
  35. protected function getOOUIModules() {
  36. return array_merge(
  37. [
  38. 'mediawiki.widgets.expiry',
  39. ],
  40. $this->relativeField->getOOUIModules()
  41. );
  42. }
  43. /**
  44. * @inheritDoc
  45. */
  46. public function getInputOOUI( $value ) {
  47. return new ExpiryInputWidget(
  48. $this->relativeField->getInputOOUI( $value ),
  49. [
  50. 'id' => $this->mID,
  51. 'required' => $this->mParams['required'] ?? false,
  52. ]
  53. );
  54. }
  55. /**
  56. * @inheritDoc
  57. */
  58. public function loadDataFromRequest( $request ) {
  59. return $this->relativeField->loadDataFromRequest( $request );
  60. }
  61. /**
  62. * Get the HTMLForm field by the type string.
  63. *
  64. * @param string $type
  65. * @return \HTMLFormField
  66. */
  67. protected function getFieldByType( $type ) {
  68. $class = HTMLForm::$typeMappings[$type];
  69. $params = $this->mParams;
  70. $params['type'] = $type;
  71. $params['class'] = $class;
  72. // Remove Parameters that are being used on the parent.
  73. unset( $params['label-message'] );
  74. return new $class( $params );
  75. }
  76. }