Merge "Add attributes parameter to ShowSearchHitTitle"
[lhc/web/wiklou.git] / includes / specials / forms / PreferencesForm.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use MediaWiki\MediaWikiServices;
22
23 /**
24 * Form to edit user preferences.
25 */
26 class PreferencesForm extends HTMLForm {
27 // Override default value from HTMLForm
28 protected $mSubSectionBeforeFields = false;
29
30 private $modifiedUser;
31
32 /**
33 * @param User $user
34 */
35 public function setModifiedUser( $user ) {
36 $this->modifiedUser = $user;
37 }
38
39 /**
40 * @return User
41 */
42 public function getModifiedUser() {
43 if ( $this->modifiedUser === null ) {
44 return $this->getUser();
45 } else {
46 return $this->modifiedUser;
47 }
48 }
49
50 /**
51 * Get extra parameters for the query string when redirecting after
52 * successful save.
53 *
54 * @return array
55 */
56 public function getExtraSuccessRedirectParameters() {
57 return [];
58 }
59
60 /**
61 * @param string $html
62 * @return string
63 */
64 function wrapForm( $html ) {
65 $html = Xml::tags( 'div', [ 'id' => 'preferences' ], $html );
66
67 return parent::wrapForm( $html );
68 }
69
70 /**
71 * @return string
72 */
73 function getButtons() {
74 $attrs = [ 'id' => 'mw-prefs-restoreprefs' ];
75
76 if ( !$this->getModifiedUser()->isAllowedAny( 'editmyprivateinfo', 'editmyoptions' ) ) {
77 return '';
78 }
79
80 $html = parent::getButtons();
81
82 if ( $this->getModifiedUser()->isAllowed( 'editmyoptions' ) ) {
83 $t = $this->getTitle()->getSubpage( 'reset' );
84
85 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
86 $html .= "\n" . $linkRenderer->makeLink( $t, $this->msg( 'restoreprefs' )->text(),
87 Html::buttonAttributes( $attrs, [ 'mw-ui-quiet' ] ) );
88
89 $html = Xml::tags( 'div', [ 'class' => 'mw-prefs-buttons' ], $html );
90 }
91
92 return $html;
93 }
94
95 /**
96 * Separate multi-option preferences into multiple preferences, since we
97 * have to store them separately
98 * @param array $data
99 * @return array
100 */
101 function filterDataForSubmit( $data ) {
102 foreach ( $this->mFlatFields as $fieldname => $field ) {
103 if ( $field instanceof HTMLNestedFilterable ) {
104 $info = $field->mParams;
105 $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $fieldname;
106 foreach ( $field->filterDataForSubmit( $data[$fieldname] ) as $key => $value ) {
107 $data["$prefix$key"] = $value;
108 }
109 unset( $data[$fieldname] );
110 }
111 }
112
113 return $data;
114 }
115
116 /**
117 * Get the whole body of the form.
118 * @return string
119 */
120 function getBody() {
121 return $this->displaySection( $this->mFieldTree, '', 'mw-prefsection-' );
122 }
123
124 /**
125 * Get the "<legend>" for a given section key. Normally this is the
126 * prefs-$key message but we'll allow extensions to override it.
127 * @param string $key
128 * @return string
129 */
130 function getLegend( $key ) {
131 $legend = parent::getLegend( $key );
132 Hooks::run( 'PreferencesGetLegend', [ $this, $key, &$legend ] );
133 return $legend;
134 }
135
136 /**
137 * Get the keys of each top level preference section.
138 * @return array of section keys
139 */
140 function getPreferenceSections() {
141 return array_keys( array_filter( $this->mFieldTree, 'is_array' ) );
142 }
143 }