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