widget: Fix changes of copyright year
[lhc/web/wiklou.git] / includes / widget / DateInputWidget.php
1 <?php
2
3 namespace MediaWiki\Widget;
4
5 use DateTime;
6
7 /**
8 * Date input widget.
9 *
10 * @since 1.29
11 * @copyright 2016 MediaWiki Widgets Team and others; see AUTHORS.txt
12 * @license MIT
13 */
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
25 /**
26 * @param array $config Configuration options
27 * - string $config['inputFormat'] Date format string to use for the textual input field.
28 * Displayed while the widget is active, and the user can type in a date in this format.
29 * Should be short and easy to type. (default: 'YYYY-MM-DD' or 'YYYY-MM', depending on
30 * `precision`)
31 * - string $config['displayFormat'] Date format string to use for the clickable label.
32 * while the widget is inactive. Should be as unambiguous as possible (for example, prefer
33 * to spell out the month, rather than rely on the order), even if that makes it longer.
34 * Applicable only if the widget is infused. (default: language-specific)
35 * - string $config['longDisplayFormat'] If a custom displayFormat is not specified, use
36 * unabbreviated day of the week and month names in the default language-specific
37 * displayFormat. (default: false)
38 * - string $config['placeholderLabel'] Placeholder text shown when the widget is not
39 * selected. Applicable only if the widget is infused. (default: taken from message
40 * `mw-widgets-dateinput-no-date`)
41 * - string $config['placeholderDateFormat'] User-visible date format string displayed
42 * in the textual input field when it's empty. Should be the same as `inputFormat`, but
43 * translated to the user's language. (default: 'YYYY-MM-DD' or 'YYYY-MM', depending on
44 * `precision`)
45 * - string $config['precision'] Date precision to use, 'day' or 'month' (default: 'day')
46 * - string $config['mustBeAfter'] Validates the date to be after this.
47 * In the 'YYYY-MM-DD' or 'YYYY-MM' format, depending on `precision`.
48 * - string $config['mustBeBefore'] Validates the date to be before this.
49 * In the 'YYYY-MM-DD' or 'YYYY-MM' format, depending on `precision`.
50 */
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 }