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