summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/utils/HTMLJsSelectToInputField.php
blob: 57f4443ce42b71c6f730cc381b8feac8d90903fe (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
<?php
/**
 * Implementation of JsSelectToInput class which is compatible with MediaWiki's preferences system.
 * @file
 * @author Niklas Laxström
 * @copyright Copyright © 2010 Niklas Laxström
 * @license GPL-2.0-or-later
 */

/**
 * Implementation of JsSelectToInput class which is extends HTMLTextField.
 */
class HTMLJsSelectToInputField extends HTMLTextField {
	/**
	 * @param string $value
	 * @return string
	 */
	public function getInputHTML( $value ) {
		$input = parent::getInputHTML( $value );

		if ( isset( $this->mParams['select'] ) ) {
			/**
			 * @var JsSelectToInput $select
			 */
			$select = $this->mParams['select'];
			$input = $select->getHtmlAndPrepareJS() . '<br />' . $input;
		}

		return $input;
	}

	/**
	 * @param string $value
	 * @return array
	 */
	protected function tidy( $value ) {
		$value = array_map( 'trim', explode( ',', $value ) );
		$value = array_unique( array_filter( $value ) );

		return $value;
	}

	/**
	 * @param string $value
	 * @param array $alldata
	 * @return bool|string
	 */
	public function validate( $value, $alldata ) {
		$p = parent::validate( $value, $alldata );

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

		if ( !isset( $this->mParams['valid-values'] ) ) {
			return true;
		}

		if ( $value === 'default' ) {
			return true;
		}

		$codes = $this->tidy( $value );
		$valid = array_flip( $this->mParams['valid-values'] );

		foreach ( $codes as $code ) {
			if ( !isset( $valid[$code] ) ) {
				return wfMessage( 'translate-pref-editassistlang-bad', $code )->parseAsBlock();
			}
		}

		return true;
	}

	/**
	 * @param string $value
	 * @param array $alldata
	 * @return string
	 */
	public function filter( $value, $alldata ) {
		$value = parent::filter( $value, $alldata );

		return implode( ', ', $this->tidy( $value ) );
	}
}