aboutsummaryrefslogtreecommitdiffstats
path: root/includes/htmlform/CodexHTMLForm.php
blob: fd0e9337896f724d3590b4cc631e9c4631530b8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php

/**
 * HTML form generation using Codex components.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

namespace MediaWiki\HTMLForm;

use MediaWiki\Html\Html;
use MediaWiki\HTMLForm\Field\HTMLButtonField;
use MediaWiki\Linker\Linker;
use MediaWiki\Parser\Sanitizer;

/**
 * Codex based HTML form
 *
 * @since 1.41
 */
class CodexHTMLForm extends HTMLForm {

	/** @inheritDoc */
	protected $displayFormat = 'codex';

	public static function loadInputFromParameters( $fieldname, $descriptor,
		?HTMLForm $parent = null
	) {
		$field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
		$field->setShowEmptyLabel( false );
		return $field;
	}

	public function getHTML( $submitResult ) {
		$this->getOutput()->addModuleStyles( [
			'mediawiki.htmlform.codex.styles',
		] );

		return parent::getHTML( $submitResult );
	}

	/**
	 * @inheritDoc
	 */
	protected function formatField( HTMLFormField $field, $value ) {
		return $field->getCodex( $value );
	}

	protected function getFormAttributes() {
		$attribs = parent::getFormAttributes();
		$attribs['class'] = [ 'mw-htmlform', 'mw-htmlform-codex' ];
		return $attribs;
	}

	public function wrapForm( $html ) {
		return Html::rawElement( 'form', $this->getFormAttributes(), $html );
	}

	protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
		$attributes['class'] = 'cdx-field';
		$legendElement = Html::rawElement( 'legend', [ 'class' => [ 'cdx-label' ] ], $legend );
		return Html::rawElement( 'fieldset', $attributes, "$legendElement\n$section" ) . "\n";
	}

	/**
	 * Note that this method returns HTML, while the parent method specifies that it should return
	 * a plain string. This method is only used to get the `$legend` argument of the
	 * wrapFieldSetSection() call, so we can be reasonably sure that returning HTML here is okay.
	 *
	 * @inheritDoc
	 */
	public function getLegend( $key ) {
		$legendText = $this->msg(
			$this->mMessagePrefix ? "{$this->mMessagePrefix}-$key" : $key
		)->text();
		$legendTextMarkup = Html::element(
			'span',
			[ 'class' => [ 'cdx-label__label__text' ] ],
			$legendText
		);

		$isOptional = $this->mSections[$key]['optional'] ?? false;
		$optionalFlagMarkup = '';
		if ( $isOptional ) {
			$optionalFlagMarkup = Html::element(
				'span',
				[ 'class' => [ 'cdx-label__label__optional-flag' ] ],
				$this->msg( 'word-separator' )->text() . $this->msg( 'htmlform-optional-flag' )->text()
			);
		}

		$descriptionMarkup = '';
		if ( isset( $this->mSections[$key]['description-message'] ) ) {
			$needsParse = $this->mSections[ $key ][ 'description-message-parse' ] ?? false;
			$descriptionMessage = $this->msg( $this->mSections[ $key ][ 'description-message' ] );
			$descriptionMarkup = Html::rawElement(
				'span',
				[ 'class' => [ 'cdx-label__description' ] ],
				$needsParse ? $descriptionMessage->parse() : $descriptionMessage->escaped()
			);
		} elseif ( isset( $this->mSections[$key]['description'] ) ) {
			$descriptionMarkup = Html::element(
				'span',
				[ 'class' => [ 'cdx-label__description' ] ],
				$this->mSections[ $key ][ 'description' ]
			);
		}

		return Html::rawElement(
			'span',
			[ 'class' => [ 'cdx-label__label' ] ],
			$legendTextMarkup . $optionalFlagMarkup
		) . $descriptionMarkup;
	}

	protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
		if ( !$fieldsHtml ) {
			// Do not generate any wrappers for empty sections. Sections may be empty if they only
			// have subsections, but no fields. A legend will still be added in
			// wrapFieldSetSection().
			return '';
		}

		$html = implode( '', $fieldsHtml );

		if ( $sectionName ) {
			$attribs = [
				'id' => Sanitizer::escapeIdForAttribute( $sectionName ),
				'class' => [ 'cdx-field__control' ]
			];

			return Html::rawElement( 'div', $attribs, $html );
		}

		return $html;
	}

	/**
	 * Get the submit and cancel buttons.
	 * @stable to override
	 * @return string HTML.
	 */
	public function getButtons() {
		$buttons = [];

		if ( $this->mShowSubmit ) {
			$value = $this->getSubmitText();
			// Define flag classes for the submit button
			$submitFlags = $this->mSubmitFlags;
			$submitClasses = [ 'mw-htmlform-submit', 'cdx-button' ];
			$submitButtonLabel = $this->getSubmitText();
			$submitID = $this->mSubmitID;
			$submitName = $this->mSubmitName;
			$submitTooltip = [];

			if ( $this->mSubmitTooltip !== null ) {
				$submitTooltip += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
			}

			$buttonAttribs = [
				'value' => $value,
				'type' => 'submit',
				'name' => $submitName,
				'id' => $submitID,
				'class' => $submitClasses,
				'formnovalidate' => false,
			] + $submitTooltip;

			$button = HTMLButtonField::buildCodexComponent(
				$submitFlags,
				$submitButtonLabel,
				$buttonAttribs
			);
			$buttons[] = $button;
		}

		// The reset button is unused and will be removed from HTMLForm (T361032).

		if ( $this->mShowCancel ) {
			$target = $this->getCancelTargetURL();
			$buttonClasses = [
				'cdx-button',
				'cdx-button--fake-button',
				'cdx-button--fake-button--enabled',
			];
			$attr = [
				'href' => $target,
				'class' => $buttonClasses,
				'role' => 'button',
			];
			$cancelButton = Html::element(
				'a', $attr, $this->msg( 'cancel' )->text()
			);
			$buttons[] = $cancelButton;
		}

		foreach ( $this->mButtons as $button ) {
			$attrs = [
				'type' => 'submit',
				'name' => $button['name'],
				'value' => $button['value']
			];

			if ( isset( $button['label-message'] ) ) {
				$label = $this->getMessage( $button['label-message'] )->parse();
			} elseif ( isset( $button['label'] ) ) {
				$label = htmlspecialchars( $button['label'] );
			} elseif ( isset( $button['label-raw'] ) ) {
				$label = $button['label-raw'];
			} else {
				$label = htmlspecialchars( $button['value'] );
			}

			// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in self::addButton
			if ( $button['attribs'] ) {
				// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in self::addButton
				$attrs += $button['attribs'];
			}

			if ( isset( $button['id'] ) ) {
				$attrs['id'] = $button['id'];
			}

			if ( isset( $attrs['class'] ) ) {
				// Ensure $attrs['class'] is always treated as an array whether it's initially set
				// as an array or a string.
				$attrs['class'] = (array)( $attrs['class'] ?? [] );
			}

			$attrs['class'][] = 'cdx-button';

			$buttons[] = Html::rawElement( 'button', $attrs, $label ) . "\n";
		}

		if ( !$buttons ) {
			return '';
		}

		return Html::rawElement(
			'div',
			[ 'class' => 'mw-htmlform-submit-buttons' ],
			implode( "\n", $buttons )
		) . "\n";
	}
}

/** @deprecated class alias since 1.42 */
class_alias( CodexHTMLForm::class, 'CodexHTMLForm' );