ea23973de494fc9d2c881794ebf3b1d8820e5e6f
[lhc/web/wiklou.git] / includes / specials / forms / PreferencesFormOOUI.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 * @since 1.32
27 */
28 class PreferencesFormOOUI extends OOUIHTMLForm {
29 // Override default value from HTMLForm
30 protected $mSubSectionBeforeFields = false;
31
32 private $modifiedUser;
33
34 /**
35 * @param User $user
36 */
37 public function setModifiedUser( $user ) {
38 $this->modifiedUser = $user;
39 }
40
41 /**
42 * @return User
43 */
44 public function getModifiedUser() {
45 if ( $this->modifiedUser === null ) {
46 return $this->getUser();
47 } else {
48 return $this->modifiedUser;
49 }
50 }
51
52 /**
53 * Get extra parameters for the query string when redirecting after
54 * successful save.
55 *
56 * @return array
57 */
58 public function getExtraSuccessRedirectParameters() {
59 return [];
60 }
61
62 /**
63 * @param string $html
64 * @return string
65 */
66 function wrapForm( $html ) {
67 $html = Xml::tags( 'div', [ 'id' => 'preferences' ], $html );
68
69 return parent::wrapForm( $html );
70 }
71
72 /**
73 * @return string
74 */
75 function getButtons() {
76 if ( !MediaWikiServices::getInstance()
77 ->getPermissionManager()
78 ->userHasAnyRight( $this->getModifiedUser(), 'editmyprivateinfo', 'editmyoptions' )
79 ) {
80 return '';
81 }
82
83 $html = parent::getButtons();
84
85 if ( $this->getModifiedUser()->isAllowed( 'editmyoptions' ) ) {
86 $t = $this->getTitle()->getSubpage( 'reset' );
87
88 $html .= new OOUI\ButtonWidget( [
89 'infusable' => true,
90 'id' => 'mw-prefs-restoreprefs',
91 'label' => $this->msg( 'restoreprefs' )->text(),
92 'href' => $t->getLinkURL(),
93 'flags' => [ 'destructive' ],
94 'framed' => false,
95 ] );
96
97 $html = Xml::tags( 'div', [ 'class' => 'mw-prefs-buttons' ], $html );
98 }
99
100 return $html;
101 }
102
103 /**
104 * Separate multi-option preferences into multiple preferences, since we
105 * have to store them separately
106 * @param array $data
107 * @return array
108 */
109 function filterDataForSubmit( $data ) {
110 foreach ( $this->mFlatFields as $fieldname => $field ) {
111 if ( $field instanceof HTMLNestedFilterable ) {
112 $info = $field->mParams;
113 $prefix = $info['prefix'] ?? $fieldname;
114 foreach ( $field->filterDataForSubmit( $data[$fieldname] ) as $key => $value ) {
115 $data["$prefix$key"] = $value;
116 }
117 unset( $data[$fieldname] );
118 }
119 }
120
121 return $data;
122 }
123
124 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
125 $layout = parent::wrapFieldSetSection( $legend, $section, $attributes, $isRoot );
126
127 $layout->addClasses( [ 'mw-prefs-fieldset-wrapper' ] );
128 $layout->removeClasses( [ 'oo-ui-panelLayout-framed' ] );
129
130 return $layout;
131 }
132
133 /**
134 * Get the whole body of the form.
135 * @return string
136 */
137 function getBody() {
138 $tabPanels = [];
139 foreach ( $this->mFieldTree as $key => $val ) {
140 if ( !is_array( $val ) ) {
141 wfDebug( __METHOD__ . " encountered a field not attached to a section: '$key'" );
142 continue;
143 }
144 $label = $this->getLegend( $key );
145 $content =
146 $this->getHeaderText( $key ) .
147 $this->displaySection(
148 $this->mFieldTree[$key],
149 "",
150 "mw-prefsection-$key-"
151 ) .
152 $this->getFooterText( $key );
153
154 $tabPanels[] = new OOUI\TabPanelLayout( 'mw-prefsection-' . $key, [
155 'classes' => [ 'mw-htmlform-autoinfuse-lazy' ],
156 'label' => $label,
157 'content' => new OOUI\FieldsetLayout( [
158 'classes' => [ 'mw-prefs-section-fieldset' ],
159 'id' => "mw-prefsection-$key",
160 'label' => $label,
161 'items' => [
162 new OOUI\Widget( [
163 'content' => new OOUI\HtmlSnippet( $content )
164 ] ),
165 ],
166 ] ),
167 'expanded' => false,
168 'framed' => true,
169 ] );
170 }
171
172 $indexLayout = new OOUI\IndexLayout( [
173 'infusable' => true,
174 'expanded' => false,
175 'autoFocus' => false,
176 'classes' => [ 'mw-prefs-tabs' ],
177 ] );
178 $indexLayout->addTabPanels( $tabPanels );
179
180 return new OOUI\PanelLayout( [
181 'framed' => true,
182 'expanded' => false,
183 'classes' => [ 'mw-prefs-tabs-wrapper' ],
184 'content' => $indexLayout
185 ] );
186 }
187
188 /**
189 * Get the "<legend>" for a given section key. Normally this is the
190 * prefs-$key message but we'll allow extensions to override it.
191 * @param string $key
192 * @return string
193 */
194 function getLegend( $key ) {
195 $legend = parent::getLegend( $key );
196 Hooks::run( 'PreferencesGetLegend', [ $this, $key, &$legend ] );
197 return $legend;
198 }
199
200 /**
201 * Get the keys of each top level preference section.
202 * @return string[] List of section keys
203 */
204 function getPreferenceSections() {
205 return array_keys( array_filter( $this->mFieldTree, 'is_array' ) );
206 }
207 }