MediaWiki  master
OOUIHTMLForm.php
Go to the documentation of this file.
1 <?php
2 
29 class OOUIHTMLForm extends HTMLForm {
30  private $oouiErrors;
31  private $oouiWarnings;
32 
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 
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  // IE<8 has bugs with <button>, so we'll need to avoid them.
61  $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
62 
63  if ( $this->mShowSubmit ) {
64  $attribs = [ 'infusable' => true ];
65 
66  if ( isset( $this->mSubmitID ) ) {
67  $attribs['id'] = $this->mSubmitID;
68  }
69 
70  if ( isset( $this->mSubmitName ) ) {
71  $attribs['name'] = $this->mSubmitName;
72  }
73 
74  if ( isset( $this->mSubmitTooltip ) ) {
75  $attribs += [
76  'title' => Linker::titleAttrib( $this->mSubmitTooltip ),
77  'accessKey' => Linker::accesskey( $this->mSubmitTooltip ),
78  ];
79  }
80 
81  $attribs['classes'] = [ 'mw-htmlform-submit' ];
82  $attribs['type'] = 'submit';
83  $attribs['label'] = $this->getSubmitText();
84  $attribs['value'] = $this->getSubmitText();
85  $attribs['flags'] = $this->mSubmitFlags;
86  $attribs['useInputTag'] = $isBadIE;
87 
88  $buttons .= new OOUI\ButtonInputWidget( $attribs );
89  }
90 
91  if ( $this->mShowReset ) {
92  $buttons .= new OOUI\ButtonInputWidget( [
93  'type' => 'reset',
94  'label' => $this->msg( 'htmlform-reset' )->text(),
95  'useInputTag' => $isBadIE,
96  ] );
97  }
98 
99  if ( $this->mShowCancel ) {
100  $target = $this->mCancelTarget ?: Title::newMainPage();
101  if ( $target instanceof Title ) {
102  $target = $target->getLocalURL();
103  }
104  $buttons .= new OOUI\ButtonWidget( [
105  'label' => $this->msg( 'cancel' )->text(),
106  'href' => $target,
107  ] );
108  }
109 
110  foreach ( $this->mButtons as $button ) {
111  $attrs = [];
112 
113  if ( $button['attribs'] ) {
114  $attrs += $button['attribs'];
115  }
116 
117  if ( isset( $button['id'] ) ) {
118  $attrs['id'] = $button['id'];
119  }
120 
121  if ( $isBadIE ) {
122  $label = $button['value'];
123  } elseif ( isset( $button['label-message'] ) ) {
124  $label = new OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
125  } elseif ( isset( $button['label'] ) ) {
126  $label = $button['label'];
127  } elseif ( isset( $button['label-raw'] ) ) {
128  $label = new OOUI\HtmlSnippet( $button['label-raw'] );
129  } else {
130  $label = $button['value'];
131  }
132 
133  $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
134 
135  $buttons .= new OOUI\ButtonInputWidget( [
136  'type' => 'submit',
137  'name' => $button['name'],
138  'value' => $button['value'],
139  'label' => $label,
140  'flags' => $button['flags'],
141  'framed' => $button['framed'],
142  'useInputTag' => $isBadIE,
143  ] + $attrs );
144  }
145 
146  if ( !$buttons ) {
147  return '';
148  }
149 
150  return Html::rawElement( 'div',
151  [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
152  }
153 
158  protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
159  // to get a user visible effect, wrap the fieldset into a framed panel layout
160  $layout = new OOUI\PanelLayout( [
161  'expanded' => false,
162  'padded' => true,
163  'framed' => true,
164  ] );
165 
166  $layout->appendContent(
167  new OOUI\FieldsetLayout( [
168  'label' => $legend,
169  'items' => [
170  new OOUI\Widget( [
171  'content' => new OOUI\HtmlSnippet( $section )
172  ] ),
173  ],
174  ] + $attributes )
175  );
176  return $layout;
177  }
178 
186  protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
187  if ( !$fieldsHtml ) {
188  // Do not generate any wrappers for empty sections. Sections may be empty if they only have
189  // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
190  return '';
191  }
192 
193  $html = implode( '', $fieldsHtml );
194 
195  if ( $sectionName ) {
196  $html = Html::rawElement(
197  'div',
198  [ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ],
199  $html
200  );
201  }
202  return $html;
203  }
204 
210  public function getErrorsOrWarnings( $elements, $elementsType ) {
211  if ( $elements === '' ) {
212  return '';
213  }
214 
215  if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
216  throw new DomainException( $elementsType . ' is not a valid type.' );
217  }
218  $errors = [];
219  if ( $elements instanceof Status ) {
220  if ( !$elements->isGood() ) {
221  $errors = $elements->getErrorsByType( $elementsType );
222  foreach ( $errors as &$error ) {
223  // Input: [ 'message' => 'foo', 'errors' => [ 'a', 'b', 'c' ] ]
224  // Output: [ 'foo', 'a', 'b', 'c' ]
225  $error = array_merge( [ $error['message'] ], $error['params'] );
226  }
227  }
228  } elseif ( $elementsType === 'error' ) {
229  if ( is_array( $elements ) ) {
230  $errors = $elements;
231  } elseif ( is_string( $elements ) ) {
232  $errors = [ $elements ];
233  }
234  }
235 
236  foreach ( $errors as &$error ) {
237  $error = $this->getMessage( $error )->parse();
238  $error = new OOUI\HtmlSnippet( $error );
239  }
240 
241  // Used in formatFormHeader()
242  if ( $elementsType === 'error' ) {
243  $this->oouiErrors = $errors;
244  } else {
245  $this->oouiWarnings = $errors;
246  }
247  return '';
248  }
249 
250  public function getHeaderText( $section = null ) {
251  if ( $section === null ) {
252  // We handle $this->mHeader elsewhere, in getBody()
253  return '';
254  } else {
255  return parent::getHeaderText( $section );
256  }
257  }
258 
259  protected function formatFormHeader() {
260  if ( !( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) ) {
261  return '';
262  }
263  $classes = [ 'mw-htmlform-ooui-header' ];
264  if ( $this->oouiErrors ) {
265  $classes[] = 'mw-htmlform-ooui-header-errors';
266  }
267  if ( $this->oouiWarnings ) {
268  $classes[] = 'mw-htmlform-ooui-header-warnings';
269  }
270  // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
271  if ( $this->mHeader ) {
272  $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] );
273  } else {
274  $element = new OOUI\Widget( [] );
275  }
276  return new OOUI\FieldLayout(
277  $element,
278  [
279  'align' => 'top',
280  'errors' => $this->oouiErrors,
281  'notices' => $this->oouiWarnings,
282  'classes' => $classes,
283  ]
284  );
285  }
286 
287  public function getBody() {
288  $html = parent::getBody();
289  $html = $this->formatFormHeader() . $html;
290  return $html;
291  }
292 
293  public function wrapForm( $html ) {
294  if ( is_string( $this->mWrapperLegend ) ) {
295  $phpClass = $this->mCollapsible ? CollapsibleFieldsetLayout::class : OOUI\FieldsetLayout::class;
296  $content = new $phpClass( [
297  'label' => $this->mWrapperLegend,
298  'collapsed' => $this->mCollapsed,
299  'items' => [
300  new OOUI\Widget( [
301  'content' => new OOUI\HtmlSnippet( $html )
302  ] ),
303  ],
304  ] + OOUI\Element::configFromHtmlAttributes( $this->mWrapperAttributes ) );
305  } else {
306  $content = new OOUI\HtmlSnippet( $html );
307  }
308 
309  $classes = [ 'mw-htmlform', 'mw-htmlform-ooui' ];
310  $form = new OOUI\FormLayout( $this->getFormAttributes() + [
311  'classes' => $classes,
312  'content' => $content,
313  ] );
314 
315  // Include a wrapper for style, if requested.
316  $form = new OOUI\PanelLayout( [
317  'classes' => [ 'mw-htmlform-ooui-wrapper' ],
318  'expanded' => false,
319  'padded' => $this->mWrapperLegend !== false,
320  'framed' => $this->mWrapperLegend !== false,
321  'content' => $form,
322  ] );
323 
324  return $form;
325  }
326 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:34
Sanitizer\escapeIdForAttribute
static escapeIdForAttribute( $id, $mode=self::ID_PRIMARY)
Given a section name or other user-generated or otherwise unsafe string, escapes it to be a valid HTM...
Definition: Sanitizer.php:815
true
return true
Definition: router.php:90
HTMLForm\$mSubmitID
$mSubmitID
Definition: HTMLForm.php:211
OOUIHTMLForm\formatSection
formatSection(array $fieldsHtml, $sectionName, $anyFieldHasLabel)
Put a form section together from the individual fields' HTML, merging it and wrapping.
Definition: OOUIHTMLForm.php:186
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:76
Title\newMainPage
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:653
OOUIHTMLForm\getButtons
getButtons()
Get the submit and (potentially) reset buttons.
Definition: OOUIHTMLForm.php:57
OOUIHTMLForm\__construct
__construct( $descriptor, $context=null, $messagePrefix='')
Stable to call Build a new HTMLForm from an array of field attributes.Stable to callArray of Field co...
Definition: OOUIHTMLForm.php:37
HTMLForm\$mSubmitName
$mSubmitName
Definition: HTMLForm.php:212
OOUIHTMLForm\getErrorsOrWarnings
getErrorsOrWarnings( $elements, $elementsType)
Definition: OOUIHTMLForm.php:210
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:44
OOUIHTMLForm
Compact stacked vertical format for forms, implemented using OOUI widgets.
Definition: OOUIHTMLForm.php:29
HTMLForm\$mSubmitFlags
string[] $mSubmitFlags
Definition: HTMLForm.php:194
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:121
OOUIHTMLForm\wrapForm
wrapForm( $html)
Wrap the form innards in an actual "<form>" element Stable to override.
Definition: OOUIHTMLForm.php:293
OOUIHTMLForm\formatFormHeader
formatFormHeader()
Definition: OOUIHTMLForm.php:259
OOUIHTMLForm\loadInputFromParameters
static loadInputFromParameters( $fieldname, $descriptor, HTMLForm $parent=null)
Initialise a new Object for the field Stable to override.
Definition: OOUIHTMLForm.php:49
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:184
HTMLForm\getFormAttributes
getFormAttributes()
Get HTML attributes for the <form> tag.
Definition: HTMLForm.php:1123
$content
$content
Definition: router.php:76
OOUIHTMLForm\$displayFormat
string $displayFormat
Symbolic display format name.
Definition: OOUIHTMLForm.php:47
Linker\titleAttrib
static titleAttrib( $name, $options=null, array $msgParams=[])
Given the id of an interface element, constructs the appropriate title attribute from the system mess...
Definition: Linker.php:2110
OOUIHTMLForm\$oouiErrors
$oouiErrors
Definition: OOUIHTMLForm.php:30
Title
Represents a title within MediaWiki.
Definition: Title.php:41
HTMLForm\getSubmitText
getSubmitText()
Get the text for the submit button, either customised or a default.
Definition: HTMLForm.php:1424
HTMLForm\getMessage
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
Definition: HTMLForm.php:1957
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
OOUIHTMLForm\$oouiWarnings
$oouiWarnings
Definition: OOUIHTMLForm.php:31
OOUIHTMLForm\getBody
getBody()
Get the whole body of the form.
Definition: OOUIHTMLForm.php:287
Linker\accesskey
static accesskey( $name)
Given the id of an interface element, constructs the appropriate accesskey attribute from the system ...
Definition: Linker.php:2158
OOUIHTMLForm\getHeaderText
getHeaderText( $section=null)
Get header text.
Definition: OOUIHTMLForm.php:250
OOUIHTMLForm\wrapFieldSetSection
wrapFieldSetSection( $legend, $section, $attributes, $isRoot)
Wraps the given $section into an user-visible fieldset.Stable to overrideLegend text for the fieldset...
Definition: OOUIHTMLForm.php:158
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:135