MediaWiki  master
PreferencesFormOOUI.php
Go to the documentation of this file.
1 <?php
28  protected $mSubSectionBeforeFields = false;
29 
31  private $modifiedUser;
32 
34  private $privateInfoEditable = true;
35 
37  private $optionsEditable = true;
38 
42  public function setModifiedUser( $user ) {
43  $this->modifiedUser = $user;
44  }
45 
49  public function getModifiedUser() {
50  if ( $this->modifiedUser === null ) {
51  return $this->getUser();
52  } else {
53  return $this->modifiedUser;
54  }
55  }
56 
60  public function isPrivateInfoEditable() {
61  return $this->privateInfoEditable;
62  }
63 
68  public function setPrivateInfoEditable( $editable ) {
69  $this->privateInfoEditable = $editable;
70  }
71 
75  public function areOptionsEditable() {
76  return $this->optionsEditable;
77  }
78 
82  public function setOptionsEditable( $optionsEditable ) {
83  $this->optionsEditable = $optionsEditable;
84  }
85 
93  return [];
94  }
95 
100  public function wrapForm( $html ) {
101  $html = Xml::tags( 'div', [ 'id' => 'preferences' ], $html );
102 
103  return parent::wrapForm( $html );
104  }
105 
109  public function getButtons() {
110  if ( !$this->areOptionsEditable() && !$this->isPrivateInfoEditable() ) {
111  return '';
112  }
113 
114  $html = parent::getButtons();
115 
116  if ( $this->areOptionsEditable() ) {
117  $html = Xml::tags( 'div', [ 'class' => 'mw-prefs-buttons' ], $html );
118  }
119 
120  return $html;
121  }
122 
129  public function filterDataForSubmit( $data ) {
130  foreach ( $this->mFlatFields as $fieldname => $field ) {
131  if ( $field instanceof HTMLNestedFilterable ) {
132  $info = $field->mParams;
133  $prefix = $info['prefix'] ?? $fieldname;
134  foreach ( $field->filterDataForSubmit( $data[$fieldname] ) as $key => $value ) {
135  $data["$prefix$key"] = $value;
136  }
137  unset( $data[$fieldname] );
138  }
139  }
140 
141  return $data;
142  }
143 
144  protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
145  $layout = parent::wrapFieldSetSection( $legend, $section, $attributes, $isRoot );
146 
147  $layout->addClasses( [ 'mw-prefs-fieldset-wrapper' ] );
148  $layout->removeClasses( [ 'oo-ui-panelLayout-framed' ] );
149 
150  return $layout;
151  }
152 
157  public function getBody() {
158  $out = $this->getOutput();
159  $skin = $out->getSkin();
160  $this->getHookRunner()->onPreferencesGetLayout( $useMobileLayout,
161  $skin->getSkinName(), [ 'isResponsive' => $skin->isResponsive() ] );
162 
163  if ( $useMobileLayout ) {
164  // Import the icons used in the mobile view
165  $out->addModuleStyles(
166  [
167  'oojs-ui.styles.icons-user',
168  'oojs-ui.styles.icons-editing-core',
169  'oojs-ui.styles.icons-editing-advanced',
170  'oojs-ui.styles.icons-wikimediaui',
171  'oojs-ui.styles.icons-content',
172  'oojs-ui.styles.icons-moderation',
173  'oojs-ui.styles.icons-interactions',
174  'oojs-ui.styles.icons-movement',
175  'oojs-ui.styles.icons-wikimedia',
176  'oojs-ui.styles.icons-media',
177  'oojs-ui.styles.icons-accessibility',
178  'oojs-ui.styles.icons-layout',
179  ]
180  );
181  $form = $this->createMobilePreferencesForm();
182  } else {
183  $form = $this->createDesktopPreferencesForm();
184  }
185 
186  $header = $this->formatFormHeader();
187 
188  return $header . $form;
189  }
190 
197  public function getLegend( $key ) {
198  $legend = parent::getLegend( $key );
199  $this->getHookRunner()->onPreferencesGetLegend( $this, $key, $legend );
200  return $legend;
201  }
202 
207  public function getPreferenceSections() {
208  return array_keys( array_filter( $this->mFieldTree, 'is_array' ) );
209  }
210 
215  private function createMobilePreferencesForm() {
216  $prefPanels = [];
217  $iconNames = [
218  'personal' => 'userAvatar',
219  'rendering' => 'palette',
220  'editing' => 'edit',
221  'rc' => 'recentChanges',
222  'watchlist' => 'watchlist',
223  'searchoptions' => 'search',
224  'misc' => '',
225  ];
226  $hookIcons = [];
227  // Get icons from extensions that have their own sections
228  $this->getHookRunner()->onPreferencesGetIcon( $hookIcons );
229  $iconNames += $hookIcons;
230 
231  foreach ( $this->mFieldTree as $key => $val ) {
232  if ( !is_array( $val ) ) {
233  wfDebug( __METHOD__ . " encountered a field not attached to a section: '$key'" );
234  continue;
235  }
236  $label = $this->getLegend( $key );
237  $content =
238  $this->getHeaderHtml( $key ) .
239  $this->displaySection(
240  $val,
241  "",
242  "mw-prefsection-$key-"
243  ) .
244  $this->getFooterHtml( $key );
245 
246  $prefPanel = new OOUI\PanelLayout( [
247  'expanded' => false,
248  'content' => [],
249  'framed' => false,
250  'classes' => [ 'mw-mobile-preferences-option' ]
251  ] );
252 
253  $iconHeaderDiv = ( new OOUI\Tag( 'div' ) )
254  ->addClasses( [ 'mw-prefs-header-container' ] );
255  $iconExists = array_key_exists( $key, $iconNames );
256  if ( $iconExists ) {
257  $iconName = $iconNames[ $key ];
258  } else {
259  $iconName = "settings";
260  }
261  $spanIcon = new OOUI\IconWidget( [
262  'icon' => $iconName,
263  'label' => $label,
264  'title' => $label,
265  'classes' => [ 'mw-prefs-icon' ],
266  ] );
267  $prefTitle = ( new OOUI\Tag( 'h5' ) )->appendContent( $label )->addClasses( [ 'prefs-title' ] );
268  $iconHeaderDiv->appendContent( $spanIcon );
269  $iconHeaderDiv->appendContent( $prefTitle );
270  $prefPanel->appendContent( $iconHeaderDiv );
271  $prefDescriptionMsg = $this->msg( "prefs-description-" . $key );
272  $prefDescription = $prefDescriptionMsg->exists() ? $prefDescriptionMsg->text() : "";
273  $prefPanel->appendContent( ( new OOUI\Tag( 'p' ) )
274  ->appendContent( $prefDescription )
275  ->addClasses( [ 'mw-prefs-description' ] )
276  );
277  $contentDiv = ( new OOUI\Tag( 'div' ) )->addClasses( [ 'mw-prefs-hidden' ] );
278  $contentDiv->addClasses( [ 'mw-prefs-content-page' ] );
279  $contentDiv->setAttributes( [
280  'id' => 'mw-prefs-option-' . $key . '-content'
281  ] );
282  $contentHeader = ( new OOUI\Tag( 'div' ) )->addClasses( [ 'mw-prefs-content-header' ] );
283  $contentHeaderBackButton = new OOUI\IconWidget( [
284  'icon' => 'previous',
285  'label' => $this->msg( "prefs-back-label" ),
286  'title' => $this->msg( "prefs-back-title" ),
287  'classes' => [ 'mw-prefs-header-icon' ],
288  ] );
289  $contentHeaderBackButton->setAttributes( [
290  'id' => 'mw-prefs-option-' . $key . '-back-button',
291  ] );
292  $contentHeaderTitle = ( new OOUI\Tag( 'h5' ) )
293  ->appendContent( $label )->addClasses( [ 'mw-prefs-header-title' ] );
294  $formContent = new OOUI\Widget( [
295  'content' => new OOUI\HtmlSnippet( $content )
296  ] );
297  $hiddenForm = ( new OOUI\Tag( 'div' ) )->appendContent( $formContent );
298  $contentHeader->appendContent( $contentHeaderBackButton );
299  $contentHeader->appendContent( $contentHeaderTitle );
300  $contentDiv->appendContent( $contentHeader );
301  $contentDiv->appendContent( $hiddenForm );
302  $prefPanel->appendContent( $contentDiv );
303  $prefPanel->setAttributes( [
304  'id' => 'mw-prefs-option-' . $key,
305  ] );
306  $prefPanel->setInfusable( true );
307  $prefPanels[] = $prefPanel;
308  }
309 
310  $form = new OOUI\StackLayout( [
311  'items' => $prefPanels,
312  'continuous' => true,
313  'expanded' => false,
314  'classes' => [ 'mw-mobile-preferences-container' ]
315  ] );
316  $form->setAttributes( [
317  'id' => 'mw-prefs-container',
318  ] );
319  $form->setInfusable( true );
320 
321  return $form;
322  }
323 
328  private function createDesktopPreferencesForm() {
329  $tabPanels = [];
330  foreach ( $this->mFieldTree as $key => $val ) {
331  if ( !is_array( $val ) ) {
332  wfDebug( __METHOD__ . " encountered a field not attached to a section: '$key'" );
333  continue;
334  }
335  $label = $this->getLegend( $key );
336  $content =
337  $this->getHeaderHtml( $key ) .
338  $this->displaySection(
339  $val,
340  "",
341  "mw-prefsection-$key-"
342  ) .
343  $this->getFooterHtml( $key );
344 
345  $tabPanels[] = new OOUI\TabPanelLayout( 'mw-prefsection-' . $key, [
346  'classes' => [ 'mw-htmlform-autoinfuse-lazy' ],
347  'label' => $label,
348  'content' => new OOUI\FieldsetLayout( [
349  'classes' => [ 'mw-prefs-section-fieldset' ],
350  'id' => "mw-prefsection-$key",
351  'label' => $label,
352  'items' => [
353  new OOUI\Widget( [
354  'content' => new OOUI\HtmlSnippet( $content )
355  ] ),
356  ],
357  ] ),
358  'expanded' => false,
359  'framed' => true,
360  ] );
361  }
362 
363  $indexLayout = new OOUI\IndexLayout( [
364  'infusable' => true,
365  'expanded' => false,
366  'autoFocus' => false,
367  'classes' => [ 'mw-prefs-tabs' ],
368  ] );
369  $indexLayout->addTabPanels( $tabPanels );
370 
371  $form = new OOUI\PanelLayout( [
372  'framed' => true,
373  'expanded' => false,
374  'classes' => [ 'mw-prefs-tabs-wrapper' ],
375  'content' => $indexLayout
376  ] );
377 
378  return $form;
379  }
380 }
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
displaySection( $fields, $sectionName='', $fieldsetIDPrefix='', &$hasUserVisibleFields=false)
Definition: HTMLForm.php:1914
getFooterHtml( $section=null)
Get footer HTML.
Definition: HTMLForm.php:1017
Compact stacked vertical format for forms, implemented using OOUI widgets.
getHeaderHtml( $section=null)
Get header HTML.
Form to edit user preferences.
filterDataForSubmit( $data)
Separate multi-option preferences into multiple preferences, since we have to store them separately.
getPreferenceSections()
Get the keys of each top level preference section.
getBody()
Get the whole body of the form.
bool $mSubSectionBeforeFields
Override default value from HTMLForm.
getLegend( $key)
Get the "<legend>" for a given section key.
wrapFieldSetSection( $legend, $section, $attributes, $isRoot)
Wraps the given $section into an user-visible fieldset.Stability: stableto overrideLegend text for th...
getExtraSuccessRedirectParameters()
Get extra parameters for the query string when redirecting after successful save.
setPrivateInfoEditable( $editable)
Whether the.
setOptionsEditable( $optionsEditable)
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:134
$content
Definition: router.php:76
$header