HTMLForm: Move header formatting OOUI-specific code to OOUIHTMLForm
[lhc/web/wiklou.git] / includes / htmlform / OOUIHTMLForm.php
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 class OOUIHTMLForm extends HTMLForm {
28 public function __construct( $descriptor, $context = null, $messagePrefix = '' ) {
29 parent::__construct( $descriptor, $context, $messagePrefix );
30 $this->getOutput()->enableOOUI();
31 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' );
32 }
33
34 /**
35 * Symbolic display format name.
36 * @var string
37 */
38 protected $displayFormat = 'ooui';
39
40 public static function loadInputFromParameters( $fieldname, $descriptor, HTMLForm $parent = null ) {
41 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
42 $field->setShowEmptyLabel( false );
43 return $field;
44 }
45
46 function getButtons() {
47 $buttons = '';
48
49 if ( $this->mShowSubmit ) {
50 $attribs = array( 'infusable' => true );
51
52 if ( isset( $this->mSubmitID ) ) {
53 $attribs['id'] = $this->mSubmitID;
54 }
55
56 if ( isset( $this->mSubmitName ) ) {
57 $attribs['name'] = $this->mSubmitName;
58 }
59
60 if ( isset( $this->mSubmitTooltip ) ) {
61 $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
62 }
63
64 $attribs['classes'] = array( 'mw-htmlform-submit' );
65 $attribs['type'] = 'submit';
66 $attribs['label'] = $this->getSubmitText();
67 $attribs['value'] = $this->getSubmitText();
68 $attribs['flags'] = $this->mSubmitFlags;
69
70 $buttons .= new OOUI\ButtonInputWidget( $attribs );
71 }
72
73 if ( $this->mShowReset ) {
74 $buttons .= new OOUI\ButtonInputWidget( array(
75 'type' => 'reset',
76 'label' => $this->msg( 'htmlform-reset' )->text(),
77 ) );
78 }
79
80 foreach ( $this->mButtons as $button ) {
81 $attrs = array();
82
83 if ( $button['attribs'] ) {
84 $attrs += $button['attribs'];
85 }
86
87 if ( isset( $button['id'] ) ) {
88 $attrs['id'] = $button['id'];
89 }
90
91 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : array();
92
93 $buttons .= new OOUI\ButtonInputWidget( array(
94 'type' => 'submit',
95 'name' => $button['name'],
96 'value' => $button['value'],
97 'label' => $button['value'],
98 ) + $attrs );
99 }
100
101 $html = Html::rawElement( 'div',
102 array( 'class' => 'mw-htmlform-submit-buttons' ), "\n$buttons" ) . "\n";
103
104 return $html;
105 }
106
107 /**
108 * Put a form section together from the individual fields' HTML, merging it and wrapping.
109 * @param OOUI\\FieldLayout[] $fieldsHtml
110 * @param string $sectionName
111 * @param bool $anyFieldHasLabel Unused
112 * @return string HTML
113 */
114 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
115 $config = array(
116 'items' => $fieldsHtml,
117 );
118 if ( $sectionName ) {
119 $config['id'] = Sanitizer::escapeId( $sectionName );
120 }
121 if ( is_string( $this->mWrapperLegend ) ) {
122 $config['label'] = $this->mWrapperLegend;
123 }
124 return new OOUI\FieldsetLayout( $config );
125 }
126
127 /**
128 * @param string|array|Status $errors
129 * @return string
130 */
131 function getErrors( $errors ) {
132 // TODO Write me!
133 return '';
134 }
135
136 function getHeaderText( $section = null ) {
137 if ( is_null( $section ) ) {
138 // We handle $this->mHeader elsewhere, in getBody()
139 return '';
140 } else {
141 return parent::getHeaderText( $section );
142 }
143 }
144
145 function getBody() {
146 $fieldset = parent::getBody();
147 // FIXME This only works for forms with no subsections
148 if ( $fieldset instanceof OOUI\FieldsetLayout ) {
149 $fieldset->group->prependContent( new OOUI\HtmlSnippet( $this->mHeader ) );
150 }
151 return $fieldset;
152 }
153
154 function wrapForm( $html ) {
155 $form = new OOUI\FormLayout( $this->getFormAttributes() + array(
156 'classes' => array( 'mw-htmlform-ooui' ),
157 'content' => new OOUI\HtmlSnippet( $html ),
158 ) );
159
160 // Include a wrapper for style, if requested.
161 $form = new OOUI\PanelLayout( array(
162 'classes' => array( 'mw-htmlform-ooui-wrapper' ),
163 'expanded' => false,
164 'padded' => $this->mWrapperLegend !== false,
165 'framed' => $this->mWrapperLegend !== false,
166 'content' => $form,
167 ) );
168
169 return $form;
170 }
171 }