MediaWiki master
DateInputWidget.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Widget;
4
5use DateTime;
6use OOUI\TextInputWidget;
7
15class DateInputWidget extends TextInputWidget {
16
18 protected $inputFormat = null;
20 protected $displayFormat = null;
22 protected $longDisplayFormat = null;
24 protected $placeholderLabel = null;
26 protected $placeholderDateFormat = null;
28 protected $precision = null;
30 protected $mustBeAfter = null;
32 protected $mustBeBefore = null;
33
64 public function __construct( array $config = [] ) {
65 $config = array_merge( [
66 // Default config values
67 'precision' => 'day',
68 'longDisplayFormat' => false,
69 ], $config );
70
71 // Properties
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 // Properties stored for the infused JS widget
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 // Set up placeholder text visible if the browser doesn't override it (logic taken from JS)
102 if ( $this->placeholderDateFormat !== null ) {
103 $placeholder = $this->placeholderDateFormat;
104 } elseif ( $this->inputFormat !== null ) {
105 // We have no way to display a translated placeholder for custom formats
106 $placeholder = '';
107 } else {
108 $placeholder = wfMessage( "mw-widgets-dateinput-placeholder-$this->precision" )->text();
109 }
110
111 $config = array_merge( [
112 // Processed config values
113 'placeholder' => $placeholder,
114 ], $config );
115
116 parent::__construct( $config );
117
118 // Calculate min/max attributes (which are skipped by TextInputWidget) and add to <input>
119 // min/max attributes are inclusive, but mustBeAfter/Before are exclusive
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 // Initialization
128 $this->addClasses( [ 'mw-widget-dateInputWidget' ] );
129 $this->appendContent( new PendingTextInputWidget() );
130 }
131
132 protected function getJavaScriptClassName() {
133 return 'mw.widgets.DateInputWidget';
134 }
135
136 public function getConfig( &$config ) {
137 if ( $this->inputFormat !== null ) {
138 $config['inputFormat'] = $this->inputFormat;
139 }
140 if ( $this->displayFormat !== null ) {
141 $config['displayFormat'] = $this->displayFormat;
142 }
143 if ( $this->longDisplayFormat !== null ) {
144 $config['longDisplayFormat'] = $this->longDisplayFormat;
145 }
146 if ( $this->placeholderLabel !== null ) {
147 $config['placeholderLabel'] = $this->placeholderLabel;
148 }
149 if ( $this->placeholderDateFormat !== null ) {
150 $config['placeholderDateFormat'] = $this->placeholderDateFormat;
151 }
152 if ( $this->precision !== null ) {
153 $config['precision'] = $this->precision;
154 }
155 if ( $this->mustBeAfter !== null ) {
156 $config['mustBeAfter'] = $this->mustBeAfter;
157 }
158 if ( $this->mustBeBefore !== null ) {
159 $config['mustBeBefore'] = $this->mustBeBefore;
160 }
161 $config['$overlay'] = true;
162 return parent::getConfig( $config );
163 }
164
165 public function getInputElement( $config ) {
166 // Inserts date/month type attribute
167 return parent::getInputElement( $config )
168 ->setAttributes( [
169 'type' => ( $config['precision'] === 'month' ) ? 'month' : 'date'
170 ] );
171 }
172
173 private function modifyDate( string $date, string $modifier ): string {
174 $datetime = new DateTime( $date );
175 $datetime->modify( $modifier );
176 return $datetime->format( 'Y-m-d' );
177 }
178}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Text input widget that displays pending animation.