Merge "Declare dynamic properties"
[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 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
77 if ( !$permissionManager->userHasAnyRight(
78 $this->getModifiedUser(),
79 'editmyprivateinfo',
80 'editmyoptions'
81 ) ) {
82 return '';
83 }
84
85 $html = parent::getButtons();
86
87 if ( $permissionManager->userHasRight( $this->getModifiedUser(), 'editmyoptions' ) ) {
88 $t = $this->getTitle()->getSubpage( 'reset' );
89
90 $html .= new OOUI\ButtonWidget( [
91 'infusable' => true,
92 'id' => 'mw-prefs-restoreprefs',
93 'label' => $this->msg( 'restoreprefs' )->text(),
94 'href' => $t->getLinkURL(),
95 'flags' => [ 'destructive' ],
96 'framed' => false,
97 ] );
98
99 $html = Xml::tags( 'div', [ 'class' => 'mw-prefs-buttons' ], $html );
100 }
101
102 return $html;
103 }
104
105 /**
106 * Separate multi-option preferences into multiple preferences, since we
107 * have to store them separately
108 * @param array $data
109 * @return array
110 */
111 function filterDataForSubmit( $data ) {
112 foreach ( $this->mFlatFields as $fieldname => $field ) {
113 if ( $field instanceof HTMLNestedFilterable ) {
114 $info = $field->mParams;
115 $prefix = $info['prefix'] ?? $fieldname;
116 foreach ( $field->filterDataForSubmit( $data[$fieldname] ) as $key => $value ) {
117 $data["$prefix$key"] = $value;
118 }
119 unset( $data[$fieldname] );
120 }
121 }
122
123 return $data;
124 }
125
126 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
127 $layout = parent::wrapFieldSetSection( $legend, $section, $attributes, $isRoot );
128
129 $layout->addClasses( [ 'mw-prefs-fieldset-wrapper' ] );
130 $layout->removeClasses( [ 'oo-ui-panelLayout-framed' ] );
131
132 return $layout;
133 }
134
135 /**
136 * Get the whole body of the form.
137 * @return string
138 */
139 function getBody() {
140 $tabPanels = [];
141 foreach ( $this->mFieldTree as $key => $val ) {
142 if ( !is_array( $val ) ) {
143 wfDebug( __METHOD__ . " encountered a field not attached to a section: '$key'" );
144 continue;
145 }
146 $label = $this->getLegend( $key );
147 $content =
148 $this->getHeaderText( $key ) .
149 $this->displaySection(
150 $this->mFieldTree[$key],
151 "",
152 "mw-prefsection-$key-"
153 ) .
154 $this->getFooterText( $key );
155
156 $tabPanels[] = new OOUI\TabPanelLayout( 'mw-prefsection-' . $key, [
157 'classes' => [ 'mw-htmlform-autoinfuse-lazy' ],
158 'label' => $label,
159 'content' => new OOUI\FieldsetLayout( [
160 'classes' => [ 'mw-prefs-section-fieldset' ],
161 'id' => "mw-prefsection-$key",
162 'label' => $label,
163 'items' => [
164 new OOUI\Widget( [
165 'content' => new OOUI\HtmlSnippet( $content )
166 ] ),
167 ],
168 ] ),
169 'expanded' => false,
170 'framed' => true,
171 ] );
172 }
173
174 $indexLayout = new OOUI\IndexLayout( [
175 'infusable' => true,
176 'expanded' => false,
177 'autoFocus' => false,
178 'classes' => [ 'mw-prefs-tabs' ],
179 ] );
180 $indexLayout->addTabPanels( $tabPanels );
181
182 return new OOUI\PanelLayout( [
183 'framed' => true,
184 'expanded' => false,
185 'classes' => [ 'mw-prefs-tabs-wrapper' ],
186 'content' => $indexLayout
187 ] );
188 }
189
190 /**
191 * Get the "<legend>" for a given section key. Normally this is the
192 * prefs-$key message but we'll allow extensions to override it.
193 * @param string $key
194 * @return string
195 */
196 function getLegend( $key ) {
197 $legend = parent::getLegend( $key );
198 Hooks::run( 'PreferencesGetLegend', [ $this, $key, &$legend ] );
199 return $legend;
200 }
201
202 /**
203 * Get the keys of each top level preference section.
204 * @return string[] List of section keys
205 */
206 function getPreferenceSections() {
207 return array_keys( array_filter( $this->mFieldTree, 'is_array' ) );
208 }
209 }