1<?php 2 3/** 4 * HTML form generation and submission handling, OOUI style. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License along 17 * with this program; if not, write to the Free Software Foundation, Inc., 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 * http://www.gnu.org/copyleft/gpl.html 20 * 21 * @file 22 */ 23 24/** 25 * Compact stacked vertical format for forms, implemented using OOUI widgets. 26 * 27 * @stable to extend 28 */ 29class OOUIHTMLForm extends HTMLForm { 30 private $oouiErrors; 31 private $oouiWarnings; 32 33 /** 34 * @stable to call 35 * @inheritDoc 36 */ 37 public function __construct( $descriptor, $context = null, $messagePrefix = '' ) { 38 parent::__construct( $descriptor, $context, $messagePrefix ); 39 $this->getOutput()->enableOOUI(); 40 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' ); 41 } 42 43 /** 44 * Symbolic display format name. 45 * @var string 46 */ 47 protected $displayFormat = 'ooui'; 48 49 public static function loadInputFromParameters( $fieldname, $descriptor, 50 HTMLForm $parent = null 51 ) { 52 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent ); 53 $field->setShowEmptyLabel( false ); 54 return $field; 55 } 56 57 public function getButtons() { 58 $buttons = ''; 59 60 if ( $this->mShowSubmit ) { 61 $attribs = [ 'infusable' => true ]; 62 63 if ( isset( $this->mSubmitID ) ) { 64 $attribs['id'] = $this->mSubmitID; 65 } 66 67 if ( isset( $this->mSubmitName ) ) { 68 $attribs['name'] = $this->mSubmitName; 69 } 70 71 if ( isset( $this->mSubmitTooltip ) ) { 72 $attribs += [ 73 'title' => Linker::titleAttrib( $this->mSubmitTooltip ), 74 'accessKey' => Linker::accesskey( $this->mSubmitTooltip ), 75 ]; 76 } 77 78 $attribs['classes'] = [ 'mw-htmlform-submit' ]; 79 $attribs['type'] = 'submit'; 80 $attribs['label'] = $this->getSubmitText(); 81 $attribs['value'] = $this->getSubmitText(); 82 $attribs['flags'] = $this->mSubmitFlags; 83 84 $buttons .= new OOUI\ButtonInputWidget( $attribs ); 85 } 86 87 if ( $this->mShowReset ) { 88 $buttons .= new OOUI\ButtonInputWidget( [ 89 'type' => 'reset', 90 'label' => $this->msg( 'htmlform-reset' )->text(), 91 ] ); 92 } 93 94 if ( $this->mShowCancel ) { 95 $target = $this->mCancelTarget ?: Title::newMainPage(); 96 if ( $target instanceof Title ) { 97 $target = $target->getLocalURL(); 98 } 99 $buttons .= new OOUI\ButtonWidget( [ 100 'label' => $this->msg( 'cancel' )->text(), 101 'href' => $target, 102 ] ); 103 } 104 105 foreach ( $this->mButtons as $button ) { 106 $attrs = []; 107 108 if ( $button['attribs'] ) { 109 $attrs += $button['attribs']; 110 } 111 112 if ( isset( $button['id'] ) ) { 113 $attrs['id'] = $button['id']; 114 } 115 116 if ( isset( $button['label-message'] ) ) { 117 $label = new OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() ); 118 } elseif ( isset( $button['label'] ) ) { 119 $label = $button['label']; 120 } elseif ( isset( $button['label-raw'] ) ) { 121 $label = new OOUI\HtmlSnippet( $button['label-raw'] ); 122 } else { 123 $label = $button['value']; 124 } 125 126 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : []; 127 128 $buttons .= new OOUI\ButtonInputWidget( [ 129 'type' => 'submit', 130 'name' => $button['name'], 131 'value' => $button['value'], 132 'label' => $label, 133 'flags' => $button['flags'], 134 'framed' => $button['framed'], 135 ] + $attrs ); 136 } 137 138 if ( !$buttons ) { 139 return ''; 140 } 141 142 return Html::rawElement( 'div', 143 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n"; 144 } 145 146 /** 147 * @inheritDoc 148 * @return OOUI\PanelLayout 149 */ 150 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) { 151 // to get a user visible effect, wrap the fieldset into a framed panel layout 152 $layout = new OOUI\PanelLayout( [ 153 'expanded' => false, 154 'padded' => true, 155 'framed' => true, 156 ] ); 157 158 $layout->appendContent( 159 new OOUI\FieldsetLayout( [ 160 'label' => $legend, 161 'items' => [ 162 new OOUI\Widget( [ 163 'content' => new OOUI\HtmlSnippet( $section ) 164 ] ), 165 ], 166 ] + $attributes ) 167 ); 168 return $layout; 169 } 170 171 /** 172 * Put a form section together from the individual fields' HTML, merging it and wrapping. 173 * @param OOUI\FieldLayout[] $fieldsHtml 174 * @param string $sectionName 175 * @param bool $anyFieldHasLabel Unused 176 * @return string HTML 177 */ 178 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) { 179 if ( !$fieldsHtml ) { 180 // Do not generate any wrappers for empty sections. Sections may be empty if they only have 181 // subsections, but no fields. A legend will still be added in wrapFieldSetSection(). 182 return ''; 183 } 184 185 $html = implode( '', $fieldsHtml ); 186 187 if ( $sectionName ) { 188 $html = Html::rawElement( 189 'div', 190 [ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ], 191 $html 192 ); 193 } 194 return $html; 195 } 196 197 /** 198 * @param string|array|Status $elements 199 * @param string $elementsType 200 * @return string 201 */ 202 public function getErrorsOrWarnings( $elements, $elementsType ) { 203 if ( $elements === '' ) { 204 return ''; 205 } 206 207 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) { 208 throw new DomainException( $elementsType . ' is not a valid type.' ); 209 } 210 $errors = []; 211 if ( $elements instanceof Status ) { 212 if ( !$elements->isGood() ) { 213 $errors = $elements->getErrorsByType( $elementsType ); 214 foreach ( $errors as &$error ) { 215 // Input: [ 'message' => 'foo', 'params' => [ 'a', 'b', 'c' ] ] 216 // Output: [ 'foo', 'a', 'b', 'c' ] 217 $error = $this->getMessage( 218 array_merge( [ $error['message'] ], $error['params'] ) )->parse(); 219 } 220 } 221 } elseif ( $elementsType === 'error' ) { 222 if ( is_array( $elements ) ) { 223 foreach ( $elements as $error ) { 224 $errors[] = $this->getMessage( $error )->parse(); 225 } 226 } elseif ( $elements && $elements !== true ) { 227 $errors[] = (string)$elements; 228 } 229 } 230 231 foreach ( $errors as &$error ) { 232 $error = new OOUI\HtmlSnippet( $error ); 233 } 234 235 // Used in formatFormHeader() 236 if ( $elementsType === 'error' ) { 237 $this->oouiErrors = $errors; 238 } else { 239 $this->oouiWarnings = $errors; 240 } 241 return ''; 242 } 243 244 public function getHeaderText( $section = null ) { 245 if ( $section === null ) { 246 // We handle $this->mHeader elsewhere, in getBody() 247 return ''; 248 } else { 249 return parent::getHeaderText( $section ); 250 } 251 } 252 253 protected function formatFormHeader() { 254 if ( !( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) ) { 255 return ''; 256 } 257 $classes = [ 'mw-htmlform-ooui-header' ]; 258 if ( $this->oouiErrors ) { 259 $classes[] = 'mw-htmlform-ooui-header-errors'; 260 } 261 if ( $this->oouiWarnings ) { 262 $classes[] = 'mw-htmlform-ooui-header-warnings'; 263 } 264 // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder 265 if ( $this->mHeader ) { 266 $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] ); 267 } else { 268 $element = new OOUI\Widget( [] ); 269 } 270 return new OOUI\FieldLayout( 271 $element, 272 [ 273 'align' => 'top', 274 'errors' => $this->oouiErrors, 275 'notices' => $this->oouiWarnings, 276 'classes' => $classes, 277 ] 278 ); 279 } 280 281 public function getBody() { 282 $html = parent::getBody(); 283 $html = $this->formatFormHeader() . $html; 284 return $html; 285 } 286 287 public function wrapForm( $html ) { 288 if ( is_string( $this->mWrapperLegend ) ) { 289 $phpClass = $this->mCollapsible ? CollapsibleFieldsetLayout::class : OOUI\FieldsetLayout::class; 290 $content = new $phpClass( [ 291 'label' => $this->mWrapperLegend, 292 'collapsed' => $this->mCollapsed, 293 'items' => [ 294 new OOUI\Widget( [ 295 'content' => new OOUI\HtmlSnippet( $html ) 296 ] ), 297 ], 298 ] + OOUI\Element::configFromHtmlAttributes( $this->mWrapperAttributes ) ); 299 } else { 300 $content = new OOUI\HtmlSnippet( $html ); 301 } 302 303 $classes = [ 'mw-htmlform', 'mw-htmlform-ooui' ]; 304 $form = new OOUI\FormLayout( $this->getFormAttributes() + [ 305 'classes' => $classes, 306 'content' => $content, 307 ] ); 308 309 // Include a wrapper for style, if requested. 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