| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Widget; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use OOUI\TextInputWidget; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class DateInputWidget extends TextInputWidget { |
| 16 |
|
| 17 |
|
| 18 |
protected $inputFormat = null; |
| 19 |
|
| 20 |
protected $displayFormat = null; |
| 21 |
|
| 22 |
protected $longDisplayFormat = null; |
| 23 |
|
| 24 |
protected $placeholderLabel = null; |
| 25 |
|
| 26 |
protected $placeholderDateFormat = null; |
| 27 |
|
| 28 |
protected $precision = null; |
| 29 |
|
| 30 |
protected $mustBeAfter = null; |
| 31 |
|
| 32 |
protected $mustBeBefore = null; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function __construct( array $config = [] ) { |
| 65 |
$config = array_merge( [ |
| 66 |
|
| 67 |
'precision' => 'day', |
| 68 |
'longDisplayFormat' => false, |
| 69 |
], $config ); |
| 70 |
|
| 71 |
|
| 72 |
if ( isset( $config['inputFormat'] ) ) { |
| 73 |
$this->inputFormat = $config['inputFormat']; |
| 74 |
} |
| 75 |
if ( isset( $config['placeholderDateFormat'] ) ) { |
| 76 |
$this->placeholderDateFormat = $config['placeholderDateFormat']; |
| 77 |
} |
| 78 |
$this->precision = $config['precision']; |
| 79 |
if ( isset( $config['mustBeAfter'] ) ) { |
| 80 |
$this->mustBeAfter = $config['mustBeAfter']; |
| 81 |
} elseif ( isset( $config['min'] ) ) { |
| 82 |
$this->mustBeAfter = $this->modifyDate( $config['min'], '-1 day' ); |
| 83 |
} |
| 84 |
if ( isset( $config['mustBeBefore'] ) ) { |
| 85 |
$this->mustBeBefore = $config['mustBeBefore']; |
| 86 |
} elseif ( isset( $config['max'] ) ) { |
| 87 |
$this->mustBeBefore = $this->modifyDate( $config['max'], '+1 day' ); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
if ( isset( $config['displayFormat'] ) ) { |
| 92 |
$this->displayFormat = $config['displayFormat']; |
| 93 |
} |
| 94 |
if ( isset( $config['longDisplayFormat'] ) ) { |
| 95 |
$this->longDisplayFormat = $config['longDisplayFormat']; |
| 96 |
} |
| 97 |
if ( isset( $config['placeholderLabel'] ) ) { |
| 98 |
$this->placeholderLabel = $config['placeholderLabel']; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
if ( $this->placeholderDateFormat !== null ) { |
| 103 |
$placeholder = $this->placeholderDateFormat; |
| 104 |
} elseif ( $this->inputFormat !== null ) { |
| 105 |
|
| 106 |
$placeholder = ''; |
| 107 |
} else { |
| 108 |
$placeholder = wfMessage( "mw-widgets-dateinput-placeholder-$this->precision" )->text(); |
| 109 |
} |
| 110 |
|
| 111 |
$config = array_merge( [ |
| 112 |
|
| 113 |
'placeholder' => $placeholder, |
| 114 |
], $config ); |
| 115 |
|
| 116 |
parent::__construct( $config ); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
if ( $this->mustBeAfter !== null ) { |
| 121 |
$this->input->setAttributes( [ 'min' => $this->modifyDate( $this->mustBeAfter, '+1 day' ) ] ); |
| 122 |
} |
| 123 |
if ( $this->mustBeBefore !== null ) { |
| 124 |
$this->input->setAttributes( [ 'max' => $this->modifyDate( $this->mustBeBefore, '-1 day' ) ] ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
$this->addClasses( [ 'mw-widget-dateInputWidget' ] ); |
| 129 |
} |
| 130 |
|
| 131 |
protected function getJavaScriptClassName() { |
| 132 |
return 'mw.widgets.DateInputWidget'; |
| 133 |
} |
| 134 |
|
| 135 |
public function getConfig( &$config ) { |
| 136 |
if ( $this->inputFormat !== null ) { |
| 137 |
$config['inputFormat'] = $this->inputFormat; |
| 138 |
} |
| 139 |
if ( $this->displayFormat !== null ) { |
| 140 |
$config['displayFormat'] = $this->displayFormat; |
| 141 |
} |
| 142 |
if ( $this->longDisplayFormat !== null ) { |
| 143 |
$config['longDisplayFormat'] = $this->longDisplayFormat; |
| 144 |
} |
| 145 |
if ( $this->placeholderLabel !== null ) { |
| 146 |
$config['placeholderLabel'] = $this->placeholderLabel; |
| 147 |
} |
| 148 |
if ( $this->placeholderDateFormat !== null ) { |
| 149 |
$config['placeholderDateFormat'] = $this->placeholderDateFormat; |
| 150 |
} |
| 151 |
if ( $this->precision !== null ) { |
| 152 |
$config['precision'] = $this->precision; |
| 153 |
} |
| 154 |
if ( $this->mustBeAfter !== null ) { |
| 155 |
$config['mustBeAfter'] = $this->mustBeAfter; |
| 156 |
} |
| 157 |
if ( $this->mustBeBefore !== null ) { |
| 158 |
$config['mustBeBefore'] = $this->mustBeBefore; |
| 159 |
} |
| 160 |
$config['$overlay'] = true; |
| 161 |
return parent::getConfig( $config ); |
| 162 |
} |
| 163 |
|
| 164 |
public function getInputElement( $config ) { |
| 165 |
|
| 166 |
return parent::getInputElement( $config ) |
| 167 |
->setAttributes( [ |
| 168 |
'type' => ( $config['precision'] === 'month' ) ? 'month' : 'date' |
| 169 |
] ); |
| 170 |
} |
| 171 |
|
| 172 |
private function modifyDate( string $date, string $modifier ): string { |
| 173 |
$datetime = new DateTime( $date ); |
| 174 |
$datetime->modify( $modifier ); |
| 175 |
return $datetime->format( 'Y-m-d' ); |
| 176 |
} |
| 177 |
} |