Merge "Don't fallback from uk to ru"
[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 $placeholderLabel = null;
23 protected $placeholderDateFormat = null;
24 protected $precision = null;
25 protected $mustBeAfter = null;
26 protected $mustBeBefore = null;
27 protected $overlay = 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['placeholderLabel'] Placeholder text shown when the widget is not
40 * selected. Applicable only if the widget is infused. (default: taken from message
41 * `mw-widgets-dateinput-no-date`)
42 * @param string $config['placeholderDateFormat'] User-visible date format string displayed
43 * in the textual input field when it's empty. Should be the same as `inputFormat`, but
44 * translated to the user's language. (default: 'YYYY-MM-DD' or 'YYYY-MM', depending on
45 * `precision`)
46 * @param string $config['precision'] Date precision to use, 'day' or 'month' (default: 'day')
47 * @param string $config['mustBeAfter'] Validates the date to be after this.
48 * In the 'YYYY-MM-DD' or 'YYYY-MM' format, depending on `precision`.
49 * @param string $config['mustBeBefore'] Validates the date to be before this.
50 * In the 'YYYY-MM-DD' or 'YYYY-MM' format, depending on `precision`.
51 * @param string $config['overlay'] The jQuery selector for the overlay layer on which to render
52 * the calendar. This configuration is useful in cases where the expanded calendar is larger
53 * than its container. The specified overlay layer is usually on top of the container and has
54 * a larger area. Applicable only if the widget is infused. By default, the calendar uses
55 * relative positioning.
56 */
57 public function __construct( array $config = [] ) {
58 $config = array_merge( [
59 // Default config values
60 'precision' => 'day',
61 ], $config );
62
63 // Properties
64 if ( isset( $config['inputFormat'] ) ) {
65 $this->inputFormat = $config['inputFormat'];
66 }
67 if ( isset( $config['placeholderDateFormat'] ) ) {
68 $this->placeholderDateFormat = $config['placeholderDateFormat'];
69 }
70 $this->precision = $config['precision'];
71 if ( isset( $config['mustBeAfter'] ) ) {
72 $this->mustBeAfter = $config['mustBeAfter'];
73 }
74 if ( isset( $config['mustBeBefore'] ) ) {
75 $this->mustBeBefore = $config['mustBeBefore'];
76 }
77
78 // Properties stored for the infused JS widget
79 if ( isset( $config['displayFormat'] ) ) {
80 $this->displayFormat = $config['displayFormat'];
81 }
82 if ( isset( $config['placeholderLabel'] ) ) {
83 $this->placeholderLabel = $config['placeholderLabel'];
84 }
85 if ( isset( $config['overlay'] ) ) {
86 $this->overlay = $config['overlay'];
87 }
88
89 // Set up placeholder text visible if the browser doesn't override it (logic taken from JS)
90 if ( $this->placeholderDateFormat !== null ) {
91 $placeholder = $this->placeholderDateFormat;
92 } elseif ( $this->inputFormat !== null ) {
93 // We have no way to display a translated placeholder for custom formats
94 $placeholder = '';
95 } else {
96 $placeholder = wfMessage( "mw-widgets-dateinput-placeholder-$this->precision" )->text();
97 }
98
99 $config = array_merge( [
100 // Processed config values
101 'placeholder' => $placeholder,
102 ], $config );
103
104 // Parent constructor
105 parent::__construct( $config );
106
107 // Calculate min/max attributes (which are skipped by TextInputWidget) and add to <input>
108 // min/max attributes are inclusive, but mustBeAfter/Before are exclusive
109 if ( $this->mustBeAfter !== null ) {
110 $min = new DateTime( $this->mustBeAfter );
111 $min = $min->modify( '+1 day' );
112 $min = $min->format( 'Y-m-d' );
113 $this->input->setAttributes( [ 'min' => $min ] );
114 }
115 if ( $this->mustBeBefore !== null ) {
116 $max = new DateTime( $this->mustBeBefore );
117 $max = $max->modify( '-1 day' );
118 $max = $max->format( 'Y-m-d' );
119 $this->input->setAttributes( [ 'max' => $max ] );
120 }
121
122 // Initialization
123 $this->addClasses( [ 'mw-widget-dateInputWidget' ] );
124 }
125
126 protected function getJavaScriptClassName() {
127 return 'mw.widgets.DateInputWidget';
128 }
129
130 public function getConfig( &$config ) {
131 if ( $this->inputFormat !== null ) {
132 $config['inputFormat'] = $this->inputFormat;
133 }
134 if ( $this->displayFormat !== null ) {
135 $config['displayFormat'] = $this->displayFormat;
136 }
137 if ( $this->placeholderLabel !== null ) {
138 $config['placeholderLabel'] = $this->placeholderLabel;
139 }
140 if ( $this->placeholderDateFormat !== null ) {
141 $config['placeholderDateFormat'] = $this->placeholderDateFormat;
142 }
143 if ( $this->precision !== null ) {
144 $config['precision'] = $this->precision;
145 }
146 if ( $this->mustBeAfter !== null ) {
147 $config['mustBeAfter'] = $this->mustBeAfter;
148 }
149 if ( $this->mustBeBefore !== null ) {
150 $config['mustBeBefore'] = $this->mustBeBefore;
151 }
152 if ( $this->overlay !== null ) {
153 $config['overlay'] = $this->overlay;
154 }
155 return parent::getConfig( $config );
156 }
157
158 public function getInputElement( $config ) {
159 // Inserts date/month type attribute
160 return parent::getInputElement( $config )
161 ->setAttributes( [
162 'type' => ( $config['precision'] === 'month' ) ? 'month' : 'date'
163 ] );
164 }
165 }