Merge "Add semantic tags to license info text"
[lhc/web/wiklou.git] / includes / widget / DateInputWidget.php
1 <?php
2 /**
3 * MediaWiki Widgets – DateInputWidget class.
4 *
5 * @copyright 2016 MediaWiki Widgets Team and others; see AUTHORS.txt
6 * @license The MIT License (MIT); see LICENSE.txt
7 */
8
9 namespace MediaWiki\Widget;
10
11 use DateTime;
12
13 /**
14 * Date input widget.
15 *
16 * @since 1.29
17 */
18 class DateInputWidget extends \OOUI\TextInputWidget {
19
20 protected $inputFormat = null;
21 protected $displayFormat = null;
22 protected $longDisplayFormat = null;
23 protected $placeholderLabel = null;
24 protected $placeholderDateFormat = null;
25 protected $precision = null;
26 protected $mustBeAfter = null;
27 protected $mustBeBefore = null;
28
29 /**
30 * @param array $config Configuration options
31 * @param string $config['inputFormat'] Date format string to use for the textual input field.
32 * Displayed while the widget is active, and the user can type in a date in this format.
33 * Should be short and easy to type. (default: 'YYYY-MM-DD' or 'YYYY-MM', depending on
34 * `precision`)
35 * @param string $config['displayFormat'] Date format string to use for the clickable label.
36 * while the widget is inactive. Should be as unambiguous as possible (for example, prefer
37 * to spell out the month, rather than rely on the order), even if that makes it longer.
38 * Applicable only if the widget is infused. (default: language-specific)
39 * @param string $config['longDisplayFormat'] If a custom displayFormat is not specified, use
40 * unabbreviated day of the week and month names in the default language-specific
41 * displayFormat. (default: false)
42 * @param string $config['placeholderLabel'] Placeholder text shown when the widget is not
43 * selected. Applicable only if the widget is infused. (default: taken from message
44 * `mw-widgets-dateinput-no-date`)
45 * @param string $config['placeholderDateFormat'] User-visible date format string displayed
46 * in the textual input field when it's empty. Should be the same as `inputFormat`, but
47 * translated to the user's language. (default: 'YYYY-MM-DD' or 'YYYY-MM', depending on
48 * `precision`)
49 * @param string $config['precision'] Date precision to use, 'day' or 'month' (default: 'day')
50 * @param string $config['mustBeAfter'] Validates the date to be after this.
51 * In the 'YYYY-MM-DD' or 'YYYY-MM' format, depending on `precision`.
52 * @param string $config['mustBeBefore'] Validates the date to be before this.
53 * In the 'YYYY-MM-DD' or 'YYYY-MM' format, depending on `precision`.
54 */
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 }
73 if ( isset( $config['mustBeBefore'] ) ) {
74 $this->mustBeBefore = $config['mustBeBefore'];
75 }
76
77 // Properties stored for the infused JS widget
78 if ( isset( $config['displayFormat'] ) ) {
79 $this->displayFormat = $config['displayFormat'];
80 }
81 if ( isset( $config['longDisplayFormat'] ) ) {
82 $this->longDisplayFormat = $config['longDisplayFormat'];
83 }
84 if ( isset( $config['placeholderLabel'] ) ) {
85 $this->placeholderLabel = $config['placeholderLabel'];
86 }
87
88 // Set up placeholder text visible if the browser doesn't override it (logic taken from JS)
89 if ( $this->placeholderDateFormat !== null ) {
90 $placeholder = $this->placeholderDateFormat;
91 } elseif ( $this->inputFormat !== null ) {
92 // We have no way to display a translated placeholder for custom formats
93 $placeholder = '';
94 } else {
95 $placeholder = wfMessage( "mw-widgets-dateinput-placeholder-$this->precision" )->text();
96 }
97
98 $config = array_merge( [
99 // Processed config values
100 'placeholder' => $placeholder,
101 ], $config );
102
103 parent::__construct( $config );
104
105 // Calculate min/max attributes (which are skipped by TextInputWidget) and add to <input>
106 // min/max attributes are inclusive, but mustBeAfter/Before are exclusive
107 if ( $this->mustBeAfter !== null ) {
108 $min = new DateTime( $this->mustBeAfter );
109 $min = $min->modify( '+1 day' );
110 $min = $min->format( 'Y-m-d' );
111 $this->input->setAttributes( [ 'min' => $min ] );
112 }
113 if ( $this->mustBeBefore !== null ) {
114 $max = new DateTime( $this->mustBeBefore );
115 $max = $max->modify( '-1 day' );
116 $max = $max->format( 'Y-m-d' );
117 $this->input->setAttributes( [ 'max' => $max ] );
118 }
119
120 // Initialization
121 $this->addClasses( [ 'mw-widget-dateInputWidget' ] );
122 }
123
124 protected function getJavaScriptClassName() {
125 return 'mw.widgets.DateInputWidget';
126 }
127
128 public function getConfig( &$config ) {
129 if ( $this->inputFormat !== null ) {
130 $config['inputFormat'] = $this->inputFormat;
131 }
132 if ( $this->displayFormat !== null ) {
133 $config['displayFormat'] = $this->displayFormat;
134 }
135 if ( $this->longDisplayFormat !== null ) {
136 $config['longDisplayFormat'] = $this->longDisplayFormat;
137 }
138 if ( $this->placeholderLabel !== null ) {
139 $config['placeholderLabel'] = $this->placeholderLabel;
140 }
141 if ( $this->placeholderDateFormat !== null ) {
142 $config['placeholderDateFormat'] = $this->placeholderDateFormat;
143 }
144 if ( $this->precision !== null ) {
145 $config['precision'] = $this->precision;
146 }
147 if ( $this->mustBeAfter !== null ) {
148 $config['mustBeAfter'] = $this->mustBeAfter;
149 }
150 if ( $this->mustBeBefore !== null ) {
151 $config['mustBeBefore'] = $this->mustBeBefore;
152 }
153 $config['$overlay'] = true;
154 return parent::getConfig( $config );
155 }
156
157 public function getInputElement( $config ) {
158 // Inserts date/month type attribute
159 return parent::getInputElement( $config )
160 ->setAttributes( [
161 'type' => ( $config['precision'] === 'month' ) ? 'month' : 'date'
162 ] );
163 }
164 }