| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\HTMLForm; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
use DomainException; |
| 27 |
use MediaWiki\Html\Html; |
| 28 |
use MediaWiki\Linker\Linker; |
| 29 |
use MediaWiki\Parser\Sanitizer; |
| 30 |
use MediaWiki\Status\Status; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class OOUIHTMLForm extends HTMLForm { |
| 38 |
|
| 39 |
private $oouiErrors; |
| 40 |
|
| 41 |
private $oouiWarnings; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function __construct( $descriptor, $context = null, $messagePrefix = '' ) { |
| 48 |
parent::__construct( $descriptor, $context, $messagePrefix ); |
| 49 |
$this->getOutput()->enableOOUI(); |
| 50 |
$this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' ); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
protected $displayFormat = 'ooui'; |
| 55 |
|
| 56 |
public static function loadInputFromParameters( $fieldname, $descriptor, |
| 57 |
?HTMLForm $parent = null |
| 58 |
) { |
| 59 |
$field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent ); |
| 60 |
$field->setShowEmptyLabel( false ); |
| 61 |
return $field; |
| 62 |
} |
| 63 |
|
| 64 |
public function getButtons() { |
| 65 |
$buttons = ''; |
| 66 |
|
| 67 |
if ( $this->mShowSubmit ) { |
| 68 |
$attribs = [ |
| 69 |
'infusable' => true, |
| 70 |
'classes' => [ 'mw-htmlform-submit' ], |
| 71 |
'type' => 'submit', |
| 72 |
'label' => $this->getSubmitText(), |
| 73 |
'value' => $this->getSubmitText(), |
| 74 |
'flags' => $this->mSubmitFlags, |
| 75 |
]; |
| 76 |
|
| 77 |
if ( isset( $this->mSubmitID ) ) { |
| 78 |
$attribs['id'] = $this->mSubmitID; |
| 79 |
} |
| 80 |
|
| 81 |
if ( isset( $this->mSubmitName ) ) { |
| 82 |
$attribs['name'] = $this->mSubmitName; |
| 83 |
} |
| 84 |
|
| 85 |
if ( isset( $this->mSubmitTooltip ) ) { |
| 86 |
$attribs += [ |
| 87 |
'title' => Linker::titleAttrib( $this->mSubmitTooltip ), |
| 88 |
'accessKey' => Linker::accesskey( $this->mSubmitTooltip ), |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
$buttons .= new \OOUI\ButtonInputWidget( $attribs ); |
| 93 |
} |
| 94 |
|
| 95 |
if ( $this->mShowCancel ) { |
| 96 |
$buttons .= new \OOUI\ButtonWidget( [ |
| 97 |
'label' => $this->msg( 'cancel' )->text(), |
| 98 |
'href' => $this->getCancelTargetURL(), |
| 99 |
] ); |
| 100 |
} |
| 101 |
|
| 102 |
foreach ( $this->mButtons as $button ) { |
| 103 |
$attrs = []; |
| 104 |
|
| 105 |
|
| 106 |
if ( $button['attribs'] ) { |
| 107 |
|
| 108 |
$attrs += $button['attribs']; |
| 109 |
} |
| 110 |
|
| 111 |
if ( isset( $button['id'] ) ) { |
| 112 |
$attrs['id'] = $button['id']; |
| 113 |
} |
| 114 |
|
| 115 |
if ( isset( $button['label-message'] ) ) { |
| 116 |
$label = new \OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() ); |
| 117 |
} elseif ( isset( $button['label'] ) ) { |
| 118 |
$label = $button['label']; |
| 119 |
} elseif ( isset( $button['label-raw'] ) ) { |
| 120 |
$label = new \OOUI\HtmlSnippet( $button['label-raw'] ); |
| 121 |
} else { |
| 122 |
$label = $button['value']; |
| 123 |
} |
| 124 |
|
| 125 |
$attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : []; |
| 126 |
|
| 127 |
$buttons .= new \OOUI\ButtonInputWidget( [ |
| 128 |
'type' => 'submit', |
| 129 |
'name' => $button['name'], |
| 130 |
'value' => $button['value'], |
| 131 |
'label' => $label, |
| 132 |
|
| 133 |
'flags' => $button['flags'], |
| 134 |
|
| 135 |
'framed' => $button['framed'], |
| 136 |
] + $attrs ); |
| 137 |
} |
| 138 |
|
| 139 |
if ( !$buttons ) { |
| 140 |
return ''; |
| 141 |
} |
| 142 |
|
| 143 |
return Html::rawElement( 'div', |
| 144 |
[ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n"; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) { |
| 152 |
|
| 153 |
$layout = new \OOUI\PanelLayout( [ |
| 154 |
'expanded' => false, |
| 155 |
'padded' => true, |
| 156 |
'framed' => true, |
| 157 |
] ); |
| 158 |
|
| 159 |
$layout->appendContent( |
| 160 |
new \OOUI\FieldsetLayout( [ |
| 161 |
'label' => $legend, |
| 162 |
'items' => [ |
| 163 |
new \OOUI\Widget( [ |
| 164 |
'content' => new \OOUI\HtmlSnippet( $section ) |
| 165 |
] ), |
| 166 |
], |
| 167 |
] + $attributes ) |
| 168 |
); |
| 169 |
return $layout; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
protected function formatField( HTMLFormField $field, $value ) { |
| 177 |
return $field->getOOUI( $value ); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) { |
| 188 |
if ( !$fieldsHtml ) { |
| 189 |
|
| 190 |
|
| 191 |
return ''; |
| 192 |
} |
| 193 |
|
| 194 |
$html = implode( '', $fieldsHtml ); |
| 195 |
|
| 196 |
if ( $sectionName ) { |
| 197 |
return Html::rawElement( |
| 198 |
'div', |
| 199 |
[ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ], |
| 200 |
$html |
| 201 |
); |
| 202 |
} |
| 203 |
return $html; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public function getErrorsOrWarnings( $elements, $elementsType ) { |
| 212 |
if ( $elements === '' ) { |
| 213 |
return ''; |
| 214 |
} |
| 215 |
|
| 216 |
if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) { |
| 217 |
throw new DomainException( $elementsType . ' is not a valid type.' ); |
| 218 |
} |
| 219 |
$errors = []; |
| 220 |
if ( $elements instanceof Status ) { |
| 221 |
if ( !$elements->isGood() ) { |
| 222 |
foreach ( $elements->getMessages( $elementsType ) as $msg ) { |
| 223 |
$errors[] = $this->getMessage( $msg )->parse(); |
| 224 |
} |
| 225 |
} |
| 226 |
} elseif ( $elementsType === 'error' ) { |
| 227 |
if ( is_array( $elements ) ) { |
| 228 |
foreach ( $elements as $error ) { |
| 229 |
$errors[] = $this->getMessage( $error )->parse(); |
| 230 |
} |
| 231 |
} elseif ( $elements && $elements !== true ) { |
| 232 |
$errors[] = (string)$elements; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
foreach ( $errors as &$error ) { |
| 237 |
$error = new \OOUI\HtmlSnippet( $error ); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
if ( $elementsType === 'error' ) { |
| 242 |
$this->oouiErrors = $errors; |
| 243 |
} else { |
| 244 |
$this->oouiWarnings = $errors; |
| 245 |
} |
| 246 |
return ''; |
| 247 |
} |
| 248 |
|
| 249 |
public function getHeaderHtml( $section = null ) { |
| 250 |
if ( $section === null ) { |
| 251 |
|
| 252 |
return ''; |
| 253 |
} else { |
| 254 |
return parent::getHeaderHtml( $section ); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
protected function formatFormHeader() { |
| 259 |
if ( !( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) ) { |
| 260 |
return ''; |
| 261 |
} |
| 262 |
$classes = [ |
| 263 |
'mw-htmlform-ooui-header', |
| 264 |
...$this->oouiErrors ? [ 'mw-htmlform-ooui-header-errors' ] : [], |
| 265 |
...$this->oouiWarnings ? [ 'mw-htmlform-ooui-header-warnings' ] : [], |
| 266 |
]; |
| 267 |
|
| 268 |
if ( $this->mHeader ) { |
| 269 |
$element = new \OOUI\LabelWidget( [ 'label' => new \OOUI\HtmlSnippet( $this->mHeader ) ] ); |
| 270 |
} else { |
| 271 |
$element = new \OOUI\Widget( [] ); |
| 272 |
} |
| 273 |
return new \OOUI\FieldLayout( |
| 274 |
$element, |
| 275 |
[ |
| 276 |
'align' => 'top', |
| 277 |
'errors' => $this->oouiErrors, |
| 278 |
'notices' => $this->oouiWarnings, |
| 279 |
'classes' => $classes, |
| 280 |
] |
| 281 |
); |
| 282 |
} |
| 283 |
|
| 284 |
public function getBody() { |
| 285 |
return $this->formatFormHeader() . parent::getBody(); |
| 286 |
} |
| 287 |
|
| 288 |
public function wrapForm( $html ) { |
| 289 |
if ( is_string( $this->mWrapperLegend ) ) { |
| 290 |
$phpClass = $this->mCollapsible ? CollapsibleFieldsetLayout::class : \OOUI\FieldsetLayout::class; |
| 291 |
$content = new $phpClass( [ |
| 292 |
'label' => $this->mWrapperLegend, |
| 293 |
'collapsed' => $this->mCollapsed, |
| 294 |
'items' => [ |
| 295 |
new \OOUI\Widget( [ |
| 296 |
'content' => new \OOUI\HtmlSnippet( $html ) |
| 297 |
] ), |
| 298 |
], |
| 299 |
] + \OOUI\Element::configFromHtmlAttributes( $this->mWrapperAttributes ) ); |
| 300 |
} else { |
| 301 |
$content = new \OOUI\HtmlSnippet( $html ); |
| 302 |
} |
| 303 |
|
| 304 |
$form = new \OOUI\FormLayout( $this->getFormAttributes() + [ |
| 305 |
'classes' => [ 'mw-htmlform', 'mw-htmlform-ooui' ], |
| 306 |
'content' => $content, |
| 307 |
] ); |
| 308 |
|
| 309 |
|
| 310 |
$form = new \OOUI\PanelLayout( [ |
| 311 |
'classes' => [ 'mw-htmlform-ooui-wrapper' ], |
| 312 |
'expanded' => false, |
| 313 |
'padded' => $this->mWrapperLegend !== false, |
| 314 |
'framed' => $this->mWrapperLegend !== false, |
| 315 |
'content' => $form, |
| 316 |
] ); |
| 317 |
|
| 318 |
return $form; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
class_alias( OOUIHTMLForm::class, 'OOUIHTMLForm' ); |