summaryrefslogtreecommitdiff
path: root/www/wiki/includes/htmlform/fields/HTMLDateTimeField.php
blob: 7b59a1d6df3c94ed5172997551b93b57f4697c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php

/**
 * A field that will contain a date and/or time
 *
 * Currently recognizes only {YYYY}-{MM}-{DD}T{HH}:{MM}:{SS.S*}Z formatted dates.
 *
 * Besides the parameters recognized by HTMLTextField, additional recognized
 * parameters in the field descriptor array include:
 *  type - 'date', 'time', or 'datetime'
 *  min - The minimum date to allow, in any recognized format.
 *  max - The maximum date to allow, in any recognized format.
 *  placeholder - The default comes from the htmlform-(date|time|datetime)-placeholder message.
 *
 * The result is a formatted date.
 *
 * @note This widget is not likely to work well in non-OOUI forms.
 */
class HTMLDateTimeField extends HTMLTextField {
	protected static $patterns = [
		'date' => '[0-9]{4}-[01][0-9]-[0-3][0-9]',
		'time' => '[0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?',
		'datetime' => '[0-9]{4}-[01][0-9]-[0-3][0-9][T ][0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?Z?',
	];

	protected $mType = 'datetime';

	public function __construct( $params ) {
		parent::__construct( $params );

		$this->mType = array_key_exists( 'type', $params )
			? $params['type']
			: 'datetime';

		if ( !in_array( $this->mType, [ 'date', 'time', 'datetime' ] ) ) {
			throw new InvalidArgumentException( "Invalid type '$this->mType'" );
		}

		if ( $this->mPlaceholder === '' ) {
			// Messages: htmlform-date-placeholder htmlform-time-placeholder htmlform-datetime-placeholder
			$this->mPlaceholder = $this->msg( "htmlform-{$this->mType}-placeholder" )->text();
		}

		$this->mClass .= ' mw-htmlform-datetime-field';
	}

	public function getAttributes( array $list ) {
		$parentList = array_diff( $list, [ 'min', 'max' ] );
		$ret = parent::getAttributes( $parentList );

		if ( in_array( 'min', $list ) && isset( $this->mParams['min'] ) ) {
			$min = $this->parseDate( $this->mParams['min'] );
			if ( $min ) {
				$ret['min'] = $this->formatDate( $min );
			}
		}
		if ( in_array( 'max', $list ) && isset( $this->mParams['max'] ) ) {
			$max = $this->parseDate( $this->mParams['max'] );
			if ( $max ) {
				$ret['max'] = $this->formatDate( $max );
			}
		}

		$ret['step'] = 1;

		$ret['type'] = $this->mType;
		$ret['pattern'] = static::$patterns[$this->mType];

		return $ret;
	}

	public function loadDataFromRequest( $request ) {
		if ( !$request->getCheck( $this->mName ) ) {
			return $this->getDefault();
		}

		$value = $request->getText( $this->mName );
		$date = $this->parseDate( $value );
		return $date ? $this->formatDate( $date ) : $value;
	}

	public function validate( $value, $alldata ) {
		$p = parent::validate( $value, $alldata );

		if ( $p !== true ) {
			return $p;
		}

		if ( $value === '' ) {
			// required was already checked by parent::validate
			return true;
		}

		$date = $this->parseDate( $value );
		if ( !$date ) {
			// Messages: htmlform-date-invalid htmlform-time-invalid htmlform-datetime-invalid
			return $this->msg( "htmlform-{$this->mType}-invalid" );
		}

		if ( isset( $this->mParams['min'] ) ) {
			$min = $this->parseDate( $this->mParams['min'] );
			if ( $min && $date < $min ) {
				// Messages: htmlform-date-toolow htmlform-time-toolow htmlform-datetime-toolow
				return $this->msg( "htmlform-{$this->mType}-toolow", $this->formatDate( $min ) );
			}
		}

		if ( isset( $this->mParams['max'] ) ) {
			$max = $this->parseDate( $this->mParams['max'] );
			if ( $max && $date > $max ) {
				// Messages: htmlform-date-toohigh htmlform-time-toohigh htmlform-datetime-toohigh
				return $this->msg( "htmlform-{$this->mType}-toohigh", $this->formatDate( $max ) );
			}
		}

		return true;
	}

	protected function parseDate( $value ) {
		$value = trim( $value );
		if ( $value === '' ) {
			return false;
		}

		if ( $this->mType === 'date' ) {
			$value .= ' T00:00:00+0000';
		}
		if ( $this->mType === 'time' ) {
			$value = '1970-01-01 ' . $value . '+0000';
		}

		try {
			$date = new DateTime( $value, new DateTimeZone( 'GMT' ) );
			return $date->getTimestamp();
		} catch ( Exception $ex ) {
			return false;
		}
	}

	protected function formatDate( $value ) {
		switch ( $this->mType ) {
			case 'date':
				return gmdate( 'Y-m-d', $value );

			case 'time':
				return gmdate( 'H:i:s', $value );

			case 'datetime':
				return gmdate( 'Y-m-d\\TH:i:s\\Z', $value );
		}
	}

	public function getInputOOUI( $value ) {
		$params = [
			'type' => $this->mType,
			'value' => $value,
			'name' => $this->mName,
			'id' => $this->mID,
		];

		if ( isset( $this->mParams['min'] ) ) {
			$min = $this->parseDate( $this->mParams['min'] );
			if ( $min ) {
				$params['min'] = $this->formatDate( $min );
			}
		}
		if ( isset( $this->mParams['max'] ) ) {
			$max = $this->parseDate( $this->mParams['max'] );
			if ( $max ) {
				$params['max'] = $this->formatDate( $max );
			}
		}

		return new MediaWiki\Widget\DateTimeInputWidget( $params );
	}

	protected function getOOUIModules() {
		return [ 'mediawiki.widgets.datetime' ];
	}

	protected function shouldInfuseOOUI() {
		return true;
	}

}