Suppress PhanUndeclaredProperty for custom properties and phan bugs
[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 // @phan-suppress-next-next-line PhanUndeclaredProperty All HTMLForm fields have mParams,
115 // but the instanceof confuses phan, which doesn't support intersections
116 $info = $field->mParams;
117 $prefix = $info['prefix'] ?? $fieldname;
118 foreach ( $field->filterDataForSubmit( $data[$fieldname] ) as $key => $value ) {
119 $data["$prefix$key"] = $value;
120 }
121 unset( $data[$fieldname] );
122 }
123 }
124
125 return $data;
126 }
127
128 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
129 $layout = parent::wrapFieldSetSection( $legend, $section, $attributes, $isRoot );
130
131 $layout->addClasses( [ 'mw-prefs-fieldset-wrapper' ] );
132 $layout->removeClasses( [ 'oo-ui-panelLayout-framed' ] );
133
134 return $layout;
135 }
136
137 /**
138 * Get the whole body of the form.
139 * @return string
140 */
141 function getBody() {
142 $tabPanels = [];
143 foreach ( $this->mFieldTree as $key => $val ) {
144 if ( !is_array( $val ) ) {
145 wfDebug( __METHOD__ . " encountered a field not attached to a section: '$key'" );
146 continue;
147 }
148 $label = $this->getLegend( $key );
149 $content =
150 $this->getHeaderText( $key ) .
151 $this->displaySection(
152 $this->mFieldTree[$key],
153 "",
154 "mw-prefsection-$key-"
155 ) .
156 $this->getFooterText( $key );
157
158 $tabPanels[] = new OOUI\TabPanelLayout( 'mw-prefsection-' . $key, [
159 'classes' => [ 'mw-htmlform-autoinfuse-lazy' ],
160 'label' => $label,
161 'content' => new OOUI\FieldsetLayout( [
162 'classes' => [ 'mw-prefs-section-fieldset' ],
163 'id' => "mw-prefsection-$key",
164 'label' => $label,
165 'items' => [
166 new OOUI\Widget( [
167 'content' => new OOUI\HtmlSnippet( $content )
168 ] ),
169 ],
170 ] ),
171 'expanded' => false,
172 'framed' => true,
173 ] );
174 }
175
176 $indexLayout = new OOUI\IndexLayout( [
177 'infusable' => true,
178 'expanded' => false,
179 'autoFocus' => false,
180 'classes' => [ 'mw-prefs-tabs' ],
181 ] );
182 $indexLayout->addTabPanels( $tabPanels );
183
184 return new OOUI\PanelLayout( [
185 'framed' => true,
186 'expanded' => false,
187 'classes' => [ 'mw-prefs-tabs-wrapper' ],
188 'content' => $indexLayout
189 ] );
190 }
191
192 /**
193 * Get the "<legend>" for a given section key. Normally this is the
194 * prefs-$key message but we'll allow extensions to override it.
195 * @param string $key
196 * @return string
197 */
198 function getLegend( $key ) {
199 $legend = parent::getLegend( $key );
200 Hooks::run( 'PreferencesGetLegend', [ $this, $key, &$legend ] );
201 return $legend;
202 }
203
204 /**
205 * Get the keys of each top level preference section.
206 * @return string[] List of section keys
207 */
208 function getPreferenceSections() {
209 return array_keys( array_filter( $this->mFieldTree, 'is_array' ) );
210 }
211 }