MediaWiki 1.41.2
DateInputWidget.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Widget;
4
5use DateTime;
6
14class DateInputWidget extends \OOUI\TextInputWidget {
15
16 protected $inputFormat = null;
17 protected $displayFormat = null;
18 protected $longDisplayFormat = null;
19 protected $placeholderLabel = null;
20 protected $placeholderDateFormat = null;
21 protected $precision = null;
22 protected $mustBeAfter = null;
23 protected $mustBeBefore = null;
24
55 public function __construct( array $config = [] ) {
56 $config = array_merge( [
57 // Default config values
58 'precision' => 'day',
59 'longDisplayFormat' => false,
60 ], $config );
61
62 // Properties
63 if ( isset( $config['inputFormat'] ) ) {
64 $this->inputFormat = $config['inputFormat'];
65 }
66 if ( isset( $config['placeholderDateFormat'] ) ) {
67 $this->placeholderDateFormat = $config['placeholderDateFormat'];
68 }
69 $this->precision = $config['precision'];
70 if ( isset( $config['mustBeAfter'] ) ) {
71 $this->mustBeAfter = $config['mustBeAfter'];
72 } elseif ( isset( $config['min'] ) ) {
73 $this->mustBeAfter = $this->modifyDate( $config['min'], '-1 day' );
74 }
75 if ( isset( $config['mustBeBefore'] ) ) {
76 $this->mustBeBefore = $config['mustBeBefore'];
77 } elseif ( isset( $config['max'] ) ) {
78 $this->mustBeBefore = $this->modifyDate( $config['max'], '+1 day' );
79 }
80
81 // Properties stored for the infused JS widget
82 if ( isset( $config['displayFormat'] ) ) {
83 $this->displayFormat = $config['displayFormat'];
84 }
85 if ( isset( $config['longDisplayFormat'] ) ) {
86 $this->longDisplayFormat = $config['longDisplayFormat'];
87 }
88 if ( isset( $config['placeholderLabel'] ) ) {
89 $this->placeholderLabel = $config['placeholderLabel'];
90 }
91
92 // Set up placeholder text visible if the browser doesn't override it (logic taken from JS)
93 if ( $this->placeholderDateFormat !== null ) {
94 $placeholder = $this->placeholderDateFormat;
95 } elseif ( $this->inputFormat !== null ) {
96 // We have no way to display a translated placeholder for custom formats
97 $placeholder = '';
98 } else {
99 $placeholder = wfMessage( "mw-widgets-dateinput-placeholder-$this->precision" )->text();
100 }
101
102 $config = array_merge( [
103 // Processed config values
104 'placeholder' => $placeholder,
105 ], $config );
106
107 parent::__construct( $config );
108
109 // Calculate min/max attributes (which are skipped by TextInputWidget) and add to <input>
110 // min/max attributes are inclusive, but mustBeAfter/Before are exclusive
111 if ( $this->mustBeAfter !== null ) {
112 $this->input->setAttributes( [ 'min' => $this->modifyDate( $this->mustBeAfter, '+1 day' ) ] );
113 }
114 if ( $this->mustBeBefore !== null ) {
115 $this->input->setAttributes( [ 'max' => $this->modifyDate( $this->mustBeBefore, '-1 day' ) ] );
116 }
117
118 // Initialization
119 $this->addClasses( [ 'mw-widget-dateInputWidget' ] );
120 }
121
122 protected function getJavaScriptClassName() {
123 return 'mw.widgets.DateInputWidget';
124 }
125
126 public function getConfig( &$config ) {
127 if ( $this->inputFormat !== null ) {
128 $config['inputFormat'] = $this->inputFormat;
129 }
130 if ( $this->displayFormat !== null ) {
131 $config['displayFormat'] = $this->displayFormat;
132 }
133 if ( $this->longDisplayFormat !== null ) {
134 $config['longDisplayFormat'] = $this->longDisplayFormat;
135 }
136 if ( $this->placeholderLabel !== null ) {
137 $config['placeholderLabel'] = $this->placeholderLabel;
138 }
139 if ( $this->placeholderDateFormat !== null ) {
140 $config['placeholderDateFormat'] = $this->placeholderDateFormat;
141 }
142 if ( $this->precision !== null ) {
143 $config['precision'] = $this->precision;
144 }
145 if ( $this->mustBeAfter !== null ) {
146 $config['mustBeAfter'] = $this->mustBeAfter;
147 }
148 if ( $this->mustBeBefore !== null ) {
149 $config['mustBeBefore'] = $this->mustBeBefore;
150 }
151 $config['$overlay'] = true;
152 return parent::getConfig( $config );
153 }
154
155 public function getInputElement( $config ) {
156 // Inserts date/month type attribute
157 return parent::getInputElement( $config )
158 ->setAttributes( [
159 'type' => ( $config['precision'] === 'month' ) ? 'month' : 'date'
160 ] );
161 }
162
163 private function modifyDate( string $date, string $modifier ): string {
164 $datetime = new DateTime( $date );
165 $datetime->modify( $modifier );
166 return $datetime->format( 'Y-m-d' );
167 }
168}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.