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 */ 36 public function __construct( $descriptor, $context = null, $messagePrefix = '' ) { 37 parent::__construct( $descriptor, $context, $messagePrefix ); 38 $this->getOutput()->enableOOUI(); 39 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' ); 40 } 41 42 /** 43 * Symbolic display format name. 44 * @var string 45 */ 46 protected $displayFormat = 'ooui'; 47 48 public static function loadInputFromParameters( $fieldname, $descriptor, 49 HTMLForm $parent = null 50 ) { 51 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent ); 52 $field->setShowEmptyLabel( false ); 53 return $field; 54 } 55 56 public function getButtons() { 57 $buttons = ''; 58 59 // IE<8 has bugs with <button>, so we'll need to avoid them. 60 $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) ); 61 62 if ( $this->mShowSubmit ) { 63 $attribs = [ 'infusable' => true ]; 64 65 if ( isset( $this->mSubmitID ) ) { 66 $attribs['id'] = $this->mSubmitID; 67 } 68 69 if ( isset( $this->mSubmitName ) ) { 70 $attribs['name'] = $this->mSubmitName; 71 } 72 73 if ( isset( $this->mSubmitTooltip ) ) { 74 $attribs += [ 75 'title' => Linker::titleAttrib( $this->mSubmitTooltip ), 76 'accessKey' => Linker::accesskey( $this->mSubmitTooltip ), 77 ]; 78 } 79 80 $attribs['classes'] = [ 'mw-htmlform-submit' ]; 81 $attribs['type'] = 'submit'; 82 $attribs['label'] = $this->getSubmitText(); 83 $attribs['value'] = $this->getSubmitText(); 84 $attribs['flags'] = $this->mSubmitFlags; 85 $attribs['useInputTag'] = $isBadIE; 86 87 $buttons .= new OOUI\ButtonInputWidget( $attribs ); 88 } 89 90 if ( $this->mShowReset ) { 91 $buttons .= new OOUI\ButtonInputWidget( [ 92 'type' => 'reset', 93 'label' => $this->msg( 'htmlform-reset' )->text(), 94 'useInputTag' => $isBadIE, 95 ] ); 96 } 97 98 if ( $this->mShowCancel ) { 99 $target = $this->mCancelTarget ?: Title::newMainPage(); 100 if ( $target instanceof Title ) { 101 $target = $target->getLocalURL(); 102 } 103 $buttons .= new OOUI\ButtonWidget( [ 104 'label' => $this->msg( 'cancel' )->text(), 105 'href' => $target, 106 ] ); 107 } 108 109 foreach ( $this->mButtons as $button ) { 110 $attrs = []; 111 112 if ( $button['attribs'] ) { 113 $attrs += $button['attribs']; 114 } 115 116 if ( isset( $button['id'] ) ) { 117 $attrs['id'] = $button['id']; 118 } 119 120 if ( $isBadIE ) { 121 $label = $button['value']; 122 } elseif ( isset( $button['label-message'] ) ) { 123 $label = new OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() ); 124 } elseif ( isset( $button['label'] ) ) { 125 $label = $button['label']; 126 } elseif ( isset( $button['label-raw'] ) ) { 127 $label = new OOUI\HtmlSnippet( $button['label-raw'] ); 128 } else { 129 $label = $button['value']; 130 } 131 132 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : []; 133 134 $buttons .= new OOUI\ButtonInputWidget( [ 135 'type' => 'submit', 136 'name' => $button['name'], 137 'value' => $button['value'], 138 'label' => $label, 139 'flags' => $button['flags'], 140 'framed' => $button['framed'], 141 'useInputTag' => $isBadIE, 142 ] + $attrs ); 143 } 144 145 if ( !$buttons ) { 146 return ''; 147 } 148 149 return Html::rawElement( 'div', 150 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n"; 151 } 152 153 /** 154 * @inheritDoc 155 * @return OOUI\PanelLayout 156 */ 157 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) { 158 // to get a user visible effect, wrap the fieldset into a framed panel layout 159 $layout = new OOUI\PanelLayout( [ 160 'expanded' => false, 161 'padded' => true, 162 'framed' => true, 163 ] ); 164 165 $layout->appendContent( 166 new OOUI\FieldsetLayout( [ 167 'label' => $legend, 168 'items' => [ 169 new OOUI\Widget( [ 170 'content' => new OOUI\HtmlSnippet( $section ) 171 ] ), 172 ], 173 ] + $attributes ) 174 ); 175 return $layout; 176 } 177 178 /** 179 * Put a form section together from the individual fields' HTML, merging it and wrapping. 180 * @param OOUI\FieldLayout[] $fieldsHtml 181 * @param string $sectionName 182 * @param bool $anyFieldHasLabel Unused 183 * @return string HTML 184 */ 185 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) { 186 if ( !$fieldsHtml ) { 187 // Do not generate any wrappers for empty sections. Sections may be empty if they only have 188 // subsections, but no fields. A legend will still be added in wrapFieldSetSection(). 189 return ''; 190 } 191 192 $html = implode( '', $fieldsHtml ); 193 194 if ( $sectionName ) { 195 $html = Html::rawElement( 196 'div', 197 [ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ], 198 $html 199 ); 200 } 201 return $html; 202 } 203 204 /** 205 * @param string|array|Status $elements 206 * @param string $elementsType 207 * @return string 208 */ 209 public function getErrorsOrWarnings( $elements, $elementsType ) { 210 if ( $elements === '' ) { 211 return ''; 212 } 213 214 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) { 215 throw new DomainException( $elementsType . ' is not a valid type.' ); 216 } 217 $errors = []; 218 if ( $elements instanceof Status ) { 219 if ( !$elements->isGood() ) { 220 $errors = $elements->getErrorsByType( $elementsType ); 221 foreach ( $errors as &$error ) { 222 // Input: [ 'message' => 'foo', 'errors' => [ 'a', 'b', 'c' ] ] 223 // Output: [ 'foo', 'a', 'b', 'c' ] 224 $error = array_merge( [ $error['message'] ], $error['params'] ); 225 } 226 } 227 } elseif ( $elementsType === 'error' ) { 228 if ( is_array( $elements ) ) { 229 $errors = $elements; 230 } elseif ( is_string( $elements ) ) { 231 $errors = [ $elements ]; 232 } 233 } 234 235 foreach ( $errors as &$error ) { 236 $error = $this->getMessage( $error )->parse(); 237 $error = new OOUI\HtmlSnippet( $error ); 238 } 239 240 // Used in formatFormHeader() 241 if ( $elementsType === 'error' ) { 242 $this->oouiErrors = $errors; 243 } else { 244 $this->oouiWarnings = $errors; 245 } 246 return ''; 247 } 248 249 public function getHeaderText( $section = null ) { 250 if ( $section === null ) { 251 // We handle $this->mHeader elsewhere, in getBody() 252 return ''; 253 } else { 254 return parent::getHeaderText( $section ); 255 } 256 } 257 258 protected function formatFormHeader() { 259 if ( !( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) ) { 260 return ''; 261 } 262 $classes = [ 'mw-htmlform-ooui-header' ]; 263 if ( $this->oouiErrors ) { 264 $classes[] = 'mw-htmlform-ooui-header-errors'; 265 } 266 if ( $this->oouiWarnings ) { 267 $classes[] = 'mw-htmlform-ooui-header-warnings'; 268 } 269 // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder 270 if ( $this->mHeader ) { 271 $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] ); 272 } else { 273 $element = new OOUI\Widget( [] ); 274 } 275 return new OOUI\FieldLayout( 276 $element, 277 [ 278 'align' => 'top', 279 'errors' => $this->oouiErrors, 280 'notices' => $this->oouiWarnings, 281 'classes' => $classes, 282 ] 283 ); 284 } 285 286 public function getBody() { 287 $html = parent::getBody(); 288 $html = $this->formatFormHeader() . $html; 289 return $html; 290 } 291 292 public function wrapForm( $html ) { 293 if ( is_string( $this->mWrapperLegend ) ) { 294 $phpClass = $this->mCollapsible ? CollapsibleFieldsetLayout::class : OOUI\FieldsetLayout::class; 295 $content = new $phpClass( [ 296 'label' => $this->mWrapperLegend, 297 'collapsed' => $this->mCollapsed, 298 'items' => [ 299 new OOUI\Widget( [ 300 'content' => new OOUI\HtmlSnippet( $html ) 301 ] ), 302 ], 303 ] + OOUI\Element::configFromHtmlAttributes( $this->mWrapperAttributes ) ); 304 } else { 305 $content = new OOUI\HtmlSnippet( $html ); 306 } 307 308 $classes = [ 'mw-htmlform', 'mw-htmlform-ooui' ]; 309 $form = new OOUI\FormLayout( $this->getFormAttributes() + [ 310 'classes' => $classes, 311 'content' => $content, 312 ] ); 313 314 // Include a wrapper for style, if requested. 315 $form = new OOUI\PanelLayout( [ 316 'classes' => [ 'mw-htmlform-ooui-wrapper' ], 317 'expanded' => false, 318 'padded' => $this->mWrapperLegend !== false, 319 'framed' => $this->mWrapperLegend !== false, 320 'content' => $form, 321 ] ); 322 323 return $form; 324 } 325} 326