1<?php
2
3/**
4 * HTML form generation and submission handling, vertical-form 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.
26 *
27 * @stable to extend
28 */
29class VFormHTMLForm extends HTMLForm {
30	/**
31	 * Wrapper and its legend are never generated in VForm mode.
32	 * @var bool
33	 */
34	protected $mWrapperLegend = false;
35
36	/**
37	 * Symbolic display format name.
38	 * @var string
39	 */
40	protected $displayFormat = 'vform';
41
42	public static function loadInputFromParameters( $fieldname, $descriptor,
43		HTMLForm $parent = null
44	) {
45		$field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
46		$field->setShowEmptyLabel( false );
47		return $field;
48	}
49
50	public function getHTML( $submitResult ) {
51		// This is required for VForm HTMLForms that use that style regardless
52		// of wgUseMediaWikiUIEverywhere (since they pre-date it).
53		// When wgUseMediaWikiUIEverywhere is removed, this should be consolidated
54		// with the addModuleStyles in SpecialPage->setHeaders.
55		$this->getOutput()->addModuleStyles( [
56			'mediawiki.ui',
57			'mediawiki.ui.button',
58			'mediawiki.ui.input',
59			'mediawiki.ui.checkbox',
60		] );
61
62		return parent::getHTML( $submitResult );
63	}
64
65	protected function getFormAttributes() {
66		$attribs = parent::getFormAttributes();
67		$attribs['class'] = [ 'mw-htmlform', 'mw-ui-vform', 'mw-ui-container' ];
68		return $attribs;
69	}
70
71	public function wrapForm( $html ) {
72		// Always discard $this->mWrapperLegend
73		return Html::rawElement( 'form', $this->getFormAttributes(), $html );
74	}
75
76	public function getButtons() {
77		$buttons = '';
78
79		if ( $this->mShowSubmit ) {
80			$attribs = [];
81
82			if ( isset( $this->mSubmitID ) ) {
83				$attribs['id'] = $this->mSubmitID;
84			}
85
86			if ( isset( $this->mSubmitName ) ) {
87				$attribs['name'] = $this->mSubmitName;
88			}
89
90			if ( isset( $this->mSubmitTooltip ) ) {
91				$attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
92			}
93
94			$attribs['class'] = [
95				'mw-htmlform-submit',
96				'mw-ui-button mw-ui-big mw-ui-block',
97			];
98			foreach ( $this->mSubmitFlags as $flag ) {
99				$attribs['class'][] = 'mw-ui-' . $flag;
100			}
101
102			$buttons .= Xml::submitButton( $this->getSubmitText(), $attribs ) . "\n";
103		}
104
105		if ( $this->mShowReset ) {
106			$buttons .= Html::element(
107				'input',
108				[
109					'type' => 'reset',
110					'value' => $this->msg( 'htmlform-reset' )->text(),
111					'class' => 'mw-ui-button mw-ui-big mw-ui-block',
112				]
113			) . "\n";
114		}
115
116		if ( $this->mShowCancel ) {
117			$target = $this->getCancelTargetURL();
118			$buttons .= Html::element(
119					'a',
120					[
121						'class' => 'mw-ui-button mw-ui-big mw-ui-block',
122						'href' => $target,
123					],
124					$this->msg( 'cancel' )->text()
125				) . "\n";
126		}
127
128		foreach ( $this->mButtons as $button ) {
129			$attrs = [
130				'type' => 'submit',
131				'name' => $button['name'],
132				'value' => $button['value']
133			];
134
135			if ( $button['attribs'] ) {
136				$attrs += $button['attribs'];
137			}
138
139			if ( isset( $button['id'] ) ) {
140				$attrs['id'] = $button['id'];
141			}
142
143			$attrs['class'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
144			$attrs['class'][] = 'mw-ui-button mw-ui-big mw-ui-block';
145
146			$buttons .= Html::element( 'input', $attrs ) . "\n";
147		}
148
149		if ( !$buttons ) {
150			return '';
151		}
152
153		return Html::rawElement( 'div',
154			[ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
155	}
156}
157