a781254352683b426a9a972767ed04d21787ec45
[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 class PreferencesFormOOUI extends OOUIHTMLForm implements PreferencesForm {
25 // Override default value from HTMLForm
26 protected $mSubSectionBeforeFields = false;
27
28 private $modifiedUser;
29
30 /**
31 * @param User $user
32 */
33 public function setModifiedUser( $user ) {
34 $this->modifiedUser = $user;
35 }
36
37 /**
38 * @return User
39 */
40 public function getModifiedUser() {
41 if ( $this->modifiedUser === null ) {
42 return $this->getUser();
43 } else {
44 return $this->modifiedUser;
45 }
46 }
47
48 /**
49 * Get extra parameters for the query string when redirecting after
50 * successful save.
51 *
52 * @return array
53 */
54 public function getExtraSuccessRedirectParameters() {
55 return [];
56 }
57
58 /**
59 * @param string $html
60 * @return string
61 */
62 function wrapForm( $html ) {
63 $html = Xml::tags( 'div', [ 'id' => 'preferences' ], $html );
64
65 return parent::wrapForm( $html );
66 }
67
68 /**
69 * @return string
70 */
71 function getButtons() {
72 if ( !$this->getModifiedUser()->isAllowedAny( 'editmyprivateinfo', 'editmyoptions' ) ) {
73 return '';
74 }
75
76 $html = parent::getButtons();
77
78 if ( $this->getModifiedUser()->isAllowed( 'editmyoptions' ) ) {
79 $t = $this->getTitle()->getSubpage( 'reset' );
80
81 $html .= new OOUI\ButtonWidget( [
82 'infusable' => true,
83 'id' => 'mw-prefs-restoreprefs',
84 'label' => $this->msg( 'restoreprefs' )->text(),
85 'href' => $t->getLinkURL(),
86 'flags' => [ 'destructive' ],
87 'framed' => false,
88 ] );
89
90 $html = Xml::tags( 'div', [ 'class' => 'mw-prefs-buttons' ], $html );
91 }
92
93 return $html;
94 }
95
96 /**
97 * Separate multi-option preferences into multiple preferences, since we
98 * have to store them separately
99 * @param array $data
100 * @return array
101 */
102 function filterDataForSubmit( $data ) {
103 foreach ( $this->mFlatFields as $fieldname => $field ) {
104 if ( $field instanceof HTMLNestedFilterable ) {
105 $info = $field->mParams;
106 $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $fieldname;
107 foreach ( $field->filterDataForSubmit( $data[$fieldname] ) as $key => $value ) {
108 $data["$prefix$key"] = $value;
109 }
110 unset( $data[$fieldname] );
111 }
112 }
113
114 return $data;
115 }
116
117 /**
118 * Get the whole body of the form.
119 * @return string
120 */
121 function getBody() {
122 return $this->displaySection( $this->mFieldTree, '', 'mw-prefsection-' );
123 }
124
125 /**
126 * Get the "<legend>" for a given section key. Normally this is the
127 * prefs-$key message but we'll allow extensions to override it.
128 * @param string $key
129 * @return string
130 */
131 function getLegend( $key ) {
132 $legend = parent::getLegend( $key );
133 Hooks::run( 'PreferencesGetLegend', [ $this, $key, &$legend ] );
134 return $legend;
135 }
136
137 /**
138 * Get the keys of each top level preference section.
139 * @return array of section keys
140 */
141 function getPreferenceSections() {
142 return array_keys( array_filter( $this->mFieldTree, 'is_array' ) );
143 }
144 }