OOUIHTMLForm.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * HTML form generation and submission handling, OOUI style.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Compact stacked vertical format for forms, implemented using OOUI widgets.
  24. */
  25. class OOUIHTMLForm extends HTMLForm {
  26. private $oouiErrors;
  27. private $oouiWarnings;
  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. * Symbolic display format name.
  35. * @var string
  36. */
  37. protected $displayFormat = 'ooui';
  38. public static function loadInputFromParameters( $fieldname, $descriptor,
  39. HTMLForm $parent = null
  40. ) {
  41. $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
  42. $field->setShowEmptyLabel( false );
  43. return $field;
  44. }
  45. public function getButtons() {
  46. $buttons = '';
  47. // IE<8 has bugs with <button>, so we'll need to avoid them.
  48. $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
  49. if ( $this->mShowSubmit ) {
  50. $attribs = [ 'infusable' => true ];
  51. if ( isset( $this->mSubmitID ) ) {
  52. $attribs['id'] = $this->mSubmitID;
  53. }
  54. if ( isset( $this->mSubmitName ) ) {
  55. $attribs['name'] = $this->mSubmitName;
  56. }
  57. if ( isset( $this->mSubmitTooltip ) ) {
  58. $attribs += [
  59. 'title' => Linker::titleAttrib( $this->mSubmitTooltip ),
  60. 'accessKey' => Linker::accesskey( $this->mSubmitTooltip ),
  61. ];
  62. }
  63. $attribs['classes'] = [ 'mw-htmlform-submit' ];
  64. $attribs['type'] = 'submit';
  65. $attribs['label'] = $this->getSubmitText();
  66. $attribs['value'] = $this->getSubmitText();
  67. $attribs['flags'] = $this->mSubmitFlags;
  68. $attribs['useInputTag'] = $isBadIE;
  69. $buttons .= new OOUI\ButtonInputWidget( $attribs );
  70. }
  71. if ( $this->mShowReset ) {
  72. $buttons .= new OOUI\ButtonInputWidget( [
  73. 'type' => 'reset',
  74. 'label' => $this->msg( 'htmlform-reset' )->text(),
  75. 'useInputTag' => $isBadIE,
  76. ] );
  77. }
  78. if ( $this->mShowCancel ) {
  79. $target = $this->mCancelTarget ?: Title::newMainPage();
  80. if ( $target instanceof Title ) {
  81. $target = $target->getLocalURL();
  82. }
  83. $buttons .= new OOUI\ButtonWidget( [
  84. 'label' => $this->msg( 'cancel' )->text(),
  85. 'href' => $target,
  86. ] );
  87. }
  88. foreach ( $this->mButtons as $button ) {
  89. $attrs = [];
  90. if ( $button['attribs'] ) {
  91. $attrs += $button['attribs'];
  92. }
  93. if ( isset( $button['id'] ) ) {
  94. $attrs['id'] = $button['id'];
  95. }
  96. if ( $isBadIE ) {
  97. $label = $button['value'];
  98. } elseif ( isset( $button['label-message'] ) ) {
  99. $label = new OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
  100. } elseif ( isset( $button['label'] ) ) {
  101. $label = $button['label'];
  102. } elseif ( isset( $button['label-raw'] ) ) {
  103. $label = new OOUI\HtmlSnippet( $button['label-raw'] );
  104. } else {
  105. $label = $button['value'];
  106. }
  107. $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
  108. $buttons .= new OOUI\ButtonInputWidget( [
  109. 'type' => 'submit',
  110. 'name' => $button['name'],
  111. 'value' => $button['value'],
  112. 'label' => $label,
  113. 'flags' => $button['flags'],
  114. 'framed' => $button['framed'],
  115. 'useInputTag' => $isBadIE,
  116. ] + $attrs );
  117. }
  118. if ( !$buttons ) {
  119. return '';
  120. }
  121. return Html::rawElement( 'div',
  122. [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
  123. }
  124. /**
  125. * @inheritDoc
  126. * @return OOUI\PanelLayout
  127. */
  128. protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
  129. // to get a user visible effect, wrap the fieldset into a framed panel layout
  130. $layout = new OOUI\PanelLayout( [
  131. 'expanded' => false,
  132. 'padded' => true,
  133. 'framed' => true,
  134. ] );
  135. $layout->appendContent(
  136. new OOUI\FieldsetLayout( [
  137. 'label' => $legend,
  138. 'items' => [
  139. new OOUI\Widget( [
  140. 'content' => new OOUI\HtmlSnippet( $section )
  141. ] ),
  142. ],
  143. ] + $attributes )
  144. );
  145. return $layout;
  146. }
  147. /**
  148. * Put a form section together from the individual fields' HTML, merging it and wrapping.
  149. * @param OOUI\FieldLayout[] $fieldsHtml
  150. * @param string $sectionName
  151. * @param bool $anyFieldHasLabel Unused
  152. * @return string HTML
  153. */
  154. protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
  155. if ( !$fieldsHtml ) {
  156. // Do not generate any wrappers for empty sections. Sections may be empty if they only have
  157. // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
  158. return '';
  159. }
  160. $html = implode( '', $fieldsHtml );
  161. if ( $sectionName ) {
  162. $html = Html::rawElement(
  163. 'div',
  164. [ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ],
  165. $html
  166. );
  167. }
  168. return $html;
  169. }
  170. /**
  171. * @param string|array|Status $elements
  172. * @param string $elementsType
  173. * @return string
  174. */
  175. public function getErrorsOrWarnings( $elements, $elementsType ) {
  176. if ( $elements === '' ) {
  177. return '';
  178. }
  179. if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
  180. throw new DomainException( $elementsType . ' is not a valid type.' );
  181. }
  182. $errors = [];
  183. if ( $elements instanceof Status ) {
  184. if ( !$elements->isGood() ) {
  185. $errors = $elements->getErrorsByType( $elementsType );
  186. foreach ( $errors as &$error ) {
  187. // Input: [ 'message' => 'foo', 'errors' => [ 'a', 'b', 'c' ] ]
  188. // Output: [ 'foo', 'a', 'b', 'c' ]
  189. $error = array_merge( [ $error['message'] ], $error['params'] );
  190. }
  191. }
  192. } elseif ( $elementsType === 'error' ) {
  193. if ( is_array( $elements ) ) {
  194. $errors = $elements;
  195. } elseif ( is_string( $elements ) ) {
  196. $errors = [ $elements ];
  197. }
  198. }
  199. foreach ( $errors as &$error ) {
  200. $error = $this->getMessage( $error )->parse();
  201. $error = new OOUI\HtmlSnippet( $error );
  202. }
  203. // Used in getBody()
  204. if ( $elementsType === 'error' ) {
  205. $this->oouiErrors = $errors;
  206. } else {
  207. $this->oouiWarnings = $errors;
  208. }
  209. return '';
  210. }
  211. public function getHeaderText( $section = null ) {
  212. if ( is_null( $section ) ) {
  213. // We handle $this->mHeader elsewhere, in getBody()
  214. return '';
  215. } else {
  216. return parent::getHeaderText( $section );
  217. }
  218. }
  219. public function getBody() {
  220. $html = parent::getBody();
  221. if ( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) {
  222. $classes = [ 'mw-htmlform-ooui-header' ];
  223. if ( $this->oouiErrors ) {
  224. $classes[] = 'mw-htmlform-ooui-header-errors';
  225. }
  226. if ( $this->oouiWarnings ) {
  227. $classes[] = 'mw-htmlform-ooui-header-warnings';
  228. }
  229. // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
  230. if ( $this->mHeader ) {
  231. $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] );
  232. } else {
  233. $element = new OOUI\Widget( [] );
  234. }
  235. $html = new OOUI\FieldLayout(
  236. $element,
  237. [
  238. 'align' => 'top',
  239. 'errors' => $this->oouiErrors,
  240. 'notices' => $this->oouiWarnings,
  241. 'classes' => $classes,
  242. ]
  243. ) . $html;
  244. }
  245. return $html;
  246. }
  247. public function wrapForm( $html ) {
  248. if ( is_string( $this->mWrapperLegend ) ) {
  249. $phpClass = $this->mCollapsible ? CollapsibleFieldsetLayout::class : OOUI\FieldsetLayout::class;
  250. $content = new $phpClass( [
  251. 'label' => $this->mWrapperLegend,
  252. 'collapsed' => $this->mCollapsed,
  253. 'items' => [
  254. new OOUI\Widget( [
  255. 'content' => new OOUI\HtmlSnippet( $html )
  256. ] ),
  257. ],
  258. ] + OOUI\Element::configFromHtmlAttributes( $this->mWrapperAttributes ) );
  259. } else {
  260. $content = new OOUI\HtmlSnippet( $html );
  261. }
  262. $classes = [ 'mw-htmlform', 'mw-htmlform-ooui' ];
  263. $form = new OOUI\FormLayout( $this->getFormAttributes() + [
  264. 'classes' => $classes,
  265. 'content' => $content,
  266. ] );
  267. // Include a wrapper for style, if requested.
  268. $form = new OOUI\PanelLayout( [
  269. 'classes' => [ 'mw-htmlform-ooui-wrapper' ],
  270. 'expanded' => false,
  271. 'padded' => $this->mWrapperLegend !== false,
  272. 'framed' => $this->mWrapperLegend !== false,
  273. 'content' => $form,
  274. ] );
  275. return $form;
  276. }
  277. }