| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\HTMLForm\Field; |
| 4 |
|
| 5 |
use MediaWiki\Widget\TitlesMultiselectWidget; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class HTMLTitlesMultiselectField extends HTMLTitleTextField { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public function __construct( $params ) { |
| 31 |
$params += [ |
| 32 |
|
| 33 |
'required' => false, |
| 34 |
]; |
| 35 |
|
| 36 |
parent::__construct( $params ); |
| 37 |
} |
| 38 |
|
| 39 |
public function loadDataFromRequest( $request ) { |
| 40 |
$value = $request->getText( $this->mName, $this->getDefault() ?? '' ); |
| 41 |
|
| 42 |
$titlesArray = explode( "\n", $value ); |
| 43 |
|
| 44 |
$titlesArray = array_values( array_filter( $titlesArray, static function ( $title ) { |
| 45 |
return trim( $title ) !== ''; |
| 46 |
} ) ); |
| 47 |
|
| 48 |
return implode( "\n", $titlesArray ); |
| 49 |
} |
| 50 |
|
| 51 |
public function validate( $value, $alldata ) { |
| 52 |
if ( !$this->mParams['exists'] ) { |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
if ( $value === null ) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
$titlesArray = explode( "\n", $value ); |
| 62 |
|
| 63 |
if ( isset( $this->mParams['max'] ) && ( count( $titlesArray ) > $this->mParams['max'] ) ) { |
| 64 |
return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] ); |
| 65 |
} |
| 66 |
|
| 67 |
foreach ( $titlesArray as $title ) { |
| 68 |
$result = parent::validate( $title, $alldata ); |
| 69 |
if ( $result !== true ) { |
| 70 |
return $result; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
public function getInputHTML( $value ) { |
| 78 |
$this->mParent->getOutput()->enableOOUI(); |
| 79 |
return $this->getInputOOUI( $value ); |
| 80 |
} |
| 81 |
|
| 82 |
public function getInputOOUI( $value ) { |
| 83 |
$this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' ); |
| 84 |
|
| 85 |
$params = [ |
| 86 |
'id' => $this->mID, |
| 87 |
'name' => $this->mName, |
| 88 |
'dir' => $this->mDir, |
| 89 |
]; |
| 90 |
|
| 91 |
if ( isset( $this->mParams['disabled'] ) ) { |
| 92 |
$params['disabled'] = $this->mParams['disabled']; |
| 93 |
} |
| 94 |
|
| 95 |
if ( isset( $this->mParams['default'] ) ) { |
| 96 |
$params['default'] = $this->mParams['default']; |
| 97 |
} |
| 98 |
|
| 99 |
$params['placeholder'] = $this->mParams['placeholder'] ?? |
| 100 |
$this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain(); |
| 101 |
|
| 102 |
if ( isset( $this->mParams['max'] ) ) { |
| 103 |
$params['tagLimit'] = $this->mParams['max']; |
| 104 |
} |
| 105 |
|
| 106 |
if ( isset( $this->mParams['showMissing'] ) ) { |
| 107 |
$params['showMissing'] = $this->mParams['showMissing']; |
| 108 |
} |
| 109 |
if ( isset( $this->mParams['excludeDynamicNamespaces'] ) ) { |
| 110 |
$params['excludeDynamicNamespaces'] = $this->mParams['excludeDynamicNamespaces']; |
| 111 |
} |
| 112 |
if ( isset( $this->mParams['allowEditTags'] ) ) { |
| 113 |
$params['allowEditTags'] = $this->mParams['allowEditTags']; |
| 114 |
} |
| 115 |
|
| 116 |
if ( isset( $this->mParams['input'] ) ) { |
| 117 |
$params['input'] = $this->mParams['input']; |
| 118 |
} |
| 119 |
|
| 120 |
if ( $value !== null ) { |
| 121 |
|
| 122 |
$params['default'] = $value === '' ? [] : explode( "\n", $value ); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
$params['infusable'] = true; |
| 127 |
$params['classes'] = [ 'mw-htmlform-autoinfuse' ]; |
| 128 |
|
| 129 |
return $this->getInputWidget( $params ); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
protected function getInputWidget( $params ) { |
| 136 |
$widget = new TitlesMultiselectWidget( $params ); |
| 137 |
$widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] ); |
| 138 |
return $widget; |
| 139 |
} |
| 140 |
|
| 141 |
protected function shouldInfuseOOUI() { |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
protected function getOOUIModules() { |
| 146 |
return [ 'mediawiki.widgets.TitlesMultiselectWidget' ]; |
| 147 |
} |
| 148 |
|
| 149 |
public function getInputCodex( $value, $hasErrors ) { |
| 150 |
|
| 151 |
|
| 152 |
$textAreaField = new HTMLTextAreaField( $this->mParams + [ 'rows' => 10 ] ); |
| 153 |
return $textAreaField->getInputCodex( $value, $hasErrors ); |
| 154 |
} |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
class_alias( HTMLTitlesMultiselectField::class, 'HTMLTitlesMultiselectField' ); |