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  $this->suppressDefaultSubmit( !$this->privateInfoEditable && !$this->optionsEditable );
71  }
72 
76  public function areOptionsEditable() {
77  return $this->optionsEditable;
78  }
79 
83  public function setOptionsEditable( $optionsEditable ) {
84  $this->optionsEditable = $optionsEditable;
85  $this->suppressDefaultSubmit( !$this->privateInfoEditable && !$this->optionsEditable );
86  }
87 
95  return [];
96  }
97 
98  public function wrapForm( $html ) {
99  $html = Xml::tags( 'div', [ 'id' => 'preferences' ], $html );
100 
101  return parent::wrapForm( $html );
102  }
103 
110  public function filterDataForSubmit( $data ) {
111  foreach ( $this->mFlatFields as $fieldname => $field ) {
112  if ( $field instanceof HTMLNestedFilterable ) {
113  $info = $field->mParams;
114  $prefix = $info['prefix'] ?? $fieldname;
115  foreach ( $field->filterDataForSubmit( $data[$fieldname] ) as $key => $value ) {
116  $data["$prefix$key"] = $value;
117  }
118  unset( $data[$fieldname] );
119  }
120  }
121 
122  return $data;
123  }
124 
125  protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
126  $layout = parent::wrapFieldSetSection( $legend, $section, $attributes, $isRoot );
127 
128  $layout->addClasses( [ 'mw-prefs-fieldset-wrapper' ] );
129  $layout->removeClasses( [ 'oo-ui-panelLayout-framed' ] );
130 
131  return $layout;
132  }
133 
138  public function getBody() {
139  $out = $this->getOutput();
140  $skin = $out->getSkin();
141  $this->getHookRunner()->onPreferencesGetLayout( $useMobileLayout,
142  $skin->getSkinName(), [ 'isResponsive' => $skin->isResponsive() ] );
143 
144  if ( $useMobileLayout ) {
145  // Import the icons used in the mobile view
146  $out->addModuleStyles(
147  [
148  'oojs-ui.styles.icons-user',
149  'oojs-ui.styles.icons-editing-core',
150  'oojs-ui.styles.icons-editing-advanced',
151  'oojs-ui.styles.icons-wikimediaui',
152  'oojs-ui.styles.icons-content',
153  'oojs-ui.styles.icons-moderation',
154  'oojs-ui.styles.icons-interactions',
155  'oojs-ui.styles.icons-movement',
156  'oojs-ui.styles.icons-wikimedia',
157  'oojs-ui.styles.icons-media',
158  'oojs-ui.styles.icons-accessibility',
159  'oojs-ui.styles.icons-layout',
160  ]
161  );
162  $form = $this->createMobilePreferencesForm();
163  } else {
164  $form = $this->createDesktopPreferencesForm();
165  }
166 
167  $header = $this->formatFormHeader();
168 
169  return $header . $form;
170  }
171 
178  public function getLegend( $key ) {
179  $legend = parent::getLegend( $key );
180  $this->getHookRunner()->onPreferencesGetLegend( $this, $key, $legend );
181  return $legend;
182  }
183 
188  public function getPreferenceSections() {
189  return array_keys( array_filter( $this->mFieldTree, 'is_array' ) );
190  }
191 
196  private function createMobilePreferencesForm() {
197  $prefPanels = [];
198  $iconNames = [
199  'personal' => 'userAvatar',
200  'rendering' => 'palette',
201  'editing' => 'edit',
202  'rc' => 'recentChanges',
203  'watchlist' => 'watchlist',
204  'searchoptions' => 'search',
205  'misc' => '',
206  ];
207  $hookIcons = [];
208  // Get icons from extensions that have their own sections
209  $this->getHookRunner()->onPreferencesGetIcon( $hookIcons );
210  $iconNames += $hookIcons;
211 
212  foreach ( $this->mFieldTree as $key => $val ) {
213  if ( !is_array( $val ) ) {
214  wfDebug( __METHOD__ . " encountered a field not attached to a section: '$key'" );
215  continue;
216  }
217  $label = $this->getLegend( $key );
218  $content =
219  $this->getHeaderHtml( $key ) .
220  $this->displaySection(
221  $val,
222  "",
223  "mw-prefsection-$key-"
224  ) .
225  $this->getFooterHtml( $key );
226 
227  $prefPanel = new OOUI\PanelLayout( [
228  'expanded' => false,
229  'content' => [],
230  'framed' => false,
231  'classes' => [
232  'mw-mobile-prefsection',
233  'mw-prefs-section-fieldset',
234  ],
235  'tagName' => 'fieldset',
236  ] );
237 
238  $iconHeaderDiv = ( new OOUI\Tag( 'div' ) )
239  ->addClasses( [ 'mw-prefs-header-container' ] );
240  $iconExists = array_key_exists( $key, $iconNames );
241  if ( $iconExists ) {
242  $iconName = $iconNames[ $key ];
243  } else {
244  $iconName = "settings";
245  }
246  $spanIcon = new OOUI\IconWidget( [
247  'icon' => $iconName,
248  'label' => $label,
249  'title' => $label,
250  'classes' => [ 'mw-prefs-icon' ],
251  ] );
252  $prefTitle = ( new OOUI\Tag( 'h5' ) )->appendContent( $label )->addClasses( [ 'prefs-title' ] );
253  $iconHeaderDiv->appendContent( $spanIcon );
254  $iconHeaderDiv->appendContent( $prefTitle );
255  $prefPanel->appendContent( $iconHeaderDiv );
256  $prefDescriptionMsg = $this->msg( "prefs-description-" . $key );
257  $prefDescription = $prefDescriptionMsg->exists() ? $prefDescriptionMsg->text() : "";
258  $prefPanel->appendContent( ( new OOUI\Tag( 'p' ) )
259  ->appendContent( $prefDescription )
260  ->addClasses( [ 'mw-prefs-description' ] )
261  );
262  $contentDiv = ( new OOUI\Tag( 'div' ) );
263  $contentDiv->addClasses( [ 'mw-prefs-content-page' ] );
264  $contentDiv->setAttributes( [
265  'id' => 'mw-mobile-prefs-' . $key . '-content'
266  ] );
267  $contentHeader = ( new OOUI\Tag( 'div' ) )->setAttributes( [
268  'id' => 'mw-mobile-prefs-' . $key . '-head'
269  ] );
270  $contentHeader->addClasses( [ 'mw-prefs-content-head' ] );
271  $contentHeaderTitle = ( new OOUI\Tag( 'h5' ) )->setAttributes( [
272  'id' => 'mw-mobile-prefs-' . $key . '-title',
273  ] );
274  $contentHeaderTitle->appendContent( $label )->addClasses( [ 'mw-prefs-header-title' ] );
275  $formContent = new OOUI\Widget( [
276  'content' => new OOUI\HtmlSnippet( $content )
277  ] );
278  $hiddenForm = ( new OOUI\Tag( 'div' ) )->appendContent( $formContent );
279  $contentHeader->appendContent( $contentHeaderTitle );
280  $contentDiv->appendContent( $contentHeader );
281  $contentDiv->appendContent( $hiddenForm );
282  $prefPanel->appendContent( $contentDiv );
283  $prefPanel->setAttributes( [
284  'id' => 'mw-mobile-prefs-' . $key,
285  ] );
286  $prefPanel->setInfusable( true );
287  $prefPanels[] = $prefPanel;
288  }
289 
290  $form = new OOUI\StackLayout( [
291  'items' => $prefPanels,
292  'continuous' => true,
293  'expanded' => true,
294  'classes' => [ 'mw-mobile-preferences-container' ]
295  ] );
296  $form->setAttributes( [
297  'id' => 'mw-prefs-container',
298  ] );
299  $form->setInfusable( true );
300 
301  return $form;
302  }
303 
308  private function createDesktopPreferencesForm() {
309  $tabPanels = [];
310  foreach ( $this->mFieldTree as $key => $val ) {
311  if ( !is_array( $val ) ) {
312  wfDebug( __METHOD__ . " encountered a field not attached to a section: '$key'" );
313  continue;
314  }
315  $label = $this->getLegend( $key );
316  $content =
317  $this->getHeaderHtml( $key ) .
318  $this->displaySection(
319  $val,
320  "",
321  "mw-prefsection-$key-"
322  ) .
323  $this->getFooterHtml( $key );
324 
325  $tabPanels[] = new OOUI\TabPanelLayout( 'mw-prefsection-' . $key, [
326  'classes' => [ 'mw-htmlform-autoinfuse-lazy' ],
327  'label' => $label,
328  'content' => new OOUI\FieldsetLayout( [
329  'classes' => [ 'mw-prefs-section-fieldset' ],
330  'id' => "mw-prefsection-$key",
331  'label' => $label,
332  'items' => [
333  new OOUI\Widget( [
334  'content' => new OOUI\HtmlSnippet( $content )
335  ] ),
336  ],
337  ] ),
338  'expanded' => false,
339  'framed' => true,
340  ] );
341  }
342 
343  $indexLayout = new OOUI\IndexLayout( [
344  'infusable' => true,
345  'expanded' => false,
346  'autoFocus' => false,
347  'classes' => [ 'mw-prefs-tabs' ],
348  ] );
349  $indexLayout->addTabPanels( $tabPanels );
350 
351  $form = new OOUI\PanelLayout( [
352  'framed' => true,
353  'expanded' => false,
354  'classes' => [ 'mw-prefs-tabs-wrapper' ],
355  'content' => $indexLayout
356  ] );
357 
358  return $form;
359  }
360 }
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()
suppressDefaultSubmit( $suppressSubmit=true)
Stop a default submit button being shown for this form.
Definition: HTMLForm.php:1719
displaySection( $fields, $sectionName='', $fieldsetIDPrefix='', &$hasUserVisibleFields=false)
Definition: HTMLForm.php:1936
getFooterHtml( $section=null)
Get footer HTML.
Definition: HTMLForm.php:1031
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.
wrapForm( $html)
Wrap the form innards in an actual "<form>" element.
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 a user-visible fieldset.Stability: stableto overrideLegend text for the...
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:135
$content
Definition: router.php:76
$header