Merge "selenium: invoke jobs to enforce eventual consistency"
[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 /**
22 * Form to edit user preferences.
23 *
24 * @since 1.32
25 */
26 class PreferencesFormOOUI extends OOUIHTMLForm {
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 if ( !$this->getModifiedUser()->isAllowedAny( 'editmyprivateinfo', 'editmyoptions' ) ) {
75 return '';
76 }
77
78 $html = parent::getButtons();
79
80 if ( $this->getModifiedUser()->isAllowed( 'editmyoptions' ) ) {
81 $t = $this->getTitle()->getSubpage( 'reset' );
82
83 $html .= new OOUI\ButtonWidget( [
84 'infusable' => true,
85 'id' => 'mw-prefs-restoreprefs',
86 'label' => $this->msg( 'restoreprefs' )->text(),
87 'href' => $t->getLinkURL(),
88 'flags' => [ 'destructive' ],
89 'framed' => false,
90 ] );
91
92 $html = Xml::tags( 'div', [ 'class' => 'mw-prefs-buttons' ], $html );
93 }
94
95 return $html;
96 }
97
98 /**
99 * Separate multi-option preferences into multiple preferences, since we
100 * have to store them separately
101 * @param array $data
102 * @return array
103 */
104 function filterDataForSubmit( $data ) {
105 foreach ( $this->mFlatFields as $fieldname => $field ) {
106 if ( $field instanceof HTMLNestedFilterable ) {
107 $info = $field->mParams;
108 $prefix = $info['prefix'] ?? $fieldname;
109 foreach ( $field->filterDataForSubmit( $data[$fieldname] ) as $key => $value ) {
110 $data["$prefix$key"] = $value;
111 }
112 unset( $data[$fieldname] );
113 }
114 }
115
116 return $data;
117 }
118
119 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
120 // to get a user visible effect, wrap the fieldset into a framed panel layout
121 if ( $isRoot ) {
122 // Mimic TabPanelLayout
123 $wrapper = new OOUI\PanelLayout( [
124 'expanded' => false,
125 'scrollable' => true,
126 // Framed and padded for no-JS, frame hidden with CSS
127 'framed' => true,
128 'infusable' => false,
129 'classes' => [ 'oo-ui-stackLayout oo-ui-indexLayout-stackLayout' ]
130 ] );
131 $layout = new OOUI\PanelLayout( [
132 'expanded' => false,
133 'scrollable' => true,
134 'infusable' => false,
135 'classes' => [ 'oo-ui-tabPanelLayout' ]
136 ] );
137 $wrapper->appendContent( $layout );
138 } else {
139 $wrapper = $layout = new OOUI\PanelLayout( [
140 'expanded' => false,
141 'padded' => true,
142 'framed' => true,
143 'infusable' => false,
144 ] );
145 }
146
147 $layout->appendContent(
148 new OOUI\FieldsetLayout( [
149 'label' => $legend,
150 'infusable' => false,
151 'items' => [
152 new OOUI\Widget( [
153 'content' => new OOUI\HtmlSnippet( $section )
154 ] ),
155 ],
156 ] + $attributes )
157 );
158 return $wrapper;
159 }
160
161 /**
162 * Get the whole body of the form.
163 * @return string
164 */
165 function getBody() {
166 // Construct fake tabs to avoid FOUC. The structure mimics OOUI's tabPanelLayout.
167 // TODO: Consider creating an infusable TabPanelLayout in OOUI-PHP.
168 $fakeTabs = [];
169 foreach ( $this->getPreferenceSections() as $i => $key ) {
170 $fakeTabs[] =
171 Html::rawElement(
172 'div',
173 [
174 'class' =>
175 'oo-ui-widget oo-ui-widget-enabled oo-ui-optionWidget ' .
176 'oo-ui-tabOptionWidget oo-ui-labelElement' .
177 ( $i === 0 ? ' oo-ui-optionWidget-selected' : '' )
178 ],
179 Html::element(
180 'a',
181 [
182 'class' => 'oo-ui-labelElement-label',
183 // Make this a usable link instead of a span so the tabs
184 // can be used before JS runs
185 'href' => '#mw-prefsection-' . $key
186 ],
187 $this->getLegend( $key )
188 )
189 );
190 }
191 $fakeTabsHtml = Html::rawElement(
192 'div',
193 [ 'class' => 'oo-ui-layout oo-ui-panelLayout oo-ui-indexLayout-tabPanel' ],
194 Html::rawElement(
195 'div',
196 [ 'class' => 'oo-ui-widget oo-ui-widget-enabled oo-ui-selectWidget ' .
197 'oo-ui-selectWidget-depressed oo-ui-tabSelectWidget' ],
198 implode( $fakeTabs )
199 )
200 );
201
202 return Html::rawElement(
203 'div',
204 [ 'class' => 'oo-ui-layout oo-ui-panelLayout oo-ui-panelLayout-framed mw-prefs-faketabs' ],
205 Html::rawElement(
206 'div',
207 [ 'class' => 'oo-ui-layout oo-ui-menuLayout oo-ui-menuLayout-static ' .
208 'oo-ui-menuLayout-top oo-ui-menuLayout-showMenu oo-ui-indexLayout' ],
209 Html::rawElement(
210 'div',
211 [ 'class' => 'oo-ui-menuLayout-menu' ],
212 $fakeTabsHtml
213 ) .
214 Html::rawElement(
215 'div',
216 [ 'class' => 'oo-ui-menuLayout-content mw-htmlform-autoinfuse-lazy' ],
217 $this->displaySection( $this->mFieldTree, '', 'mw-prefsection-' )
218 )
219 )
220 );
221 }
222
223 /**
224 * Get the "<legend>" for a given section key. Normally this is the
225 * prefs-$key message but we'll allow extensions to override it.
226 * @param string $key
227 * @return string
228 */
229 function getLegend( $key ) {
230 $aliasKey = ( $key === 'optoutwatchlist' || $key === 'optoutrc' ) ? 'opt-out' : $key;
231 $legend = parent::getLegend( $aliasKey );
232 Hooks::run( 'PreferencesGetLegend', [ $this, $key, &$legend ] );
233 return $legend;
234 }
235
236 /**
237 * Get the keys of each top level preference section.
238 * @return array of section keys
239 */
240 function getPreferenceSections() {
241 return array_keys( array_filter( $this->mFieldTree, 'is_array' ) );
242 }
243 }