]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/useradminpanel.php
Don't accept non-objects before testing with "instanceof".
[quix0rs-gnu-social.git] / actions / useradminpanel.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * User administration panel
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Settings
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @author    Zach Copley <zach@status.net>
27  * @author    Sarven Capadisli <csarven@status.net>
28  * @copyright 2008-2010 StatusNet, Inc.
29  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30  * @link      http://status.net/
31  */
32
33 if (!defined('STATUSNET')) {
34     exit(1);
35 }
36
37 /**
38  * Administer user settings
39  *
40  * @category Admin
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @author   Zach Copley <zach@status.net>
44  * @author   Sarven Capadisli <csarven@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  */
48 class UseradminpanelAction extends AdminPanelAction
49 {
50     /**
51      * Returns the page title
52      *
53      * @return string page title
54      */
55     function title()
56     {
57         // TRANS: User admin panel title.
58         return _m('TITLE', 'User');
59     }
60
61     /**
62      * Instructions for using this form.
63      *
64      * @return string instructions
65      */
66     function getInstructions()
67     {
68         // TRANS: Instruction for user admin panel.
69         return _('User settings for this StatusNet site');
70     }
71
72     /**
73      * Show the site admin panel form
74      *
75      * @return void
76      */
77     function showForm()
78     {
79         $form = new UserAdminPanelForm($this);
80         $form->show();
81         return;
82     }
83
84     /**
85      * Save settings from the form
86      *
87      * @return void
88      */
89     function saveSettings()
90     {
91         static $settings = array(
92                 'profile' => array('biolimit'),
93                 'newuser' => array('welcome', 'default')
94         );
95
96         static $booleans = array(
97             'invite' => array('enabled')
98         );
99
100         $values = array();
101
102         foreach ($settings as $section => $parts) {
103             foreach ($parts as $setting) {
104                 $values[$section][$setting] = $this->trimmed("$section-$setting");
105             }
106         }
107
108         foreach ($booleans as $section => $parts) {
109             foreach ($parts as $setting) {
110                 $values[$section][$setting] = ($this->boolean("$section-$setting")) ? 1 : 0;
111             }
112         }
113
114         // This throws an exception on validation errors
115
116         $this->validate($values);
117
118         // assert(all values are valid);
119
120         $config = new Config();
121
122         $config->query('BEGIN');
123
124         foreach ($settings as $section => $parts) {
125             foreach ($parts as $setting) {
126                 Config::save($section, $setting, $values[$section][$setting]);
127             }
128         }
129
130         foreach ($booleans as $section => $parts) {
131             foreach ($parts as $setting) {
132                 Config::save($section, $setting, $values[$section][$setting]);
133             }
134         }
135
136         $config->query('COMMIT');
137
138         return;
139     }
140
141     function validate(&$values)
142     {
143         // Validate biolimit
144
145         if (!Validate::number($values['profile']['biolimit'])) {
146             // TRANS: Form validation error in user admin panel when a non-numeric character limit was set.
147             $this->clientError(_('Invalid bio limit. Must be numeric.'));
148         }
149
150         // Validate welcome text
151
152         if (mb_strlen($values['newuser']['welcome']) > 255) {
153             // TRANS: Form validation error in user admin panel when welcome text is too long.
154             $this->clientError(_('Invalid welcome text. Maximum length is 255 characters.'));
155         }
156
157         // Validate default subscription
158
159         if (!empty($values['newuser']['default'])) {
160             $defuser = User::getKV('nickname', trim($values['newuser']['default']));
161             if (empty($defuser)) {
162                 $this->clientError(
163                     sprintf(
164                         // TRANS: Client error displayed when trying to set a non-existing user as default subscription for new
165                         // TRANS: users in user admin panel. %1$s is the invalid nickname.
166                         _('Invalid default subscripton: "%1$s" is not a user.'),
167                         $values['newuser']['default']
168                     )
169                 );
170             }
171         }
172     }
173 }
174
175 // @todo FIXME: Class documentation missing.
176 class UserAdminPanelForm extends AdminForm
177 {
178     /**
179      * ID of the form
180      *
181      * @return int ID of the form
182      */
183     function id()
184     {
185         return 'useradminpanel';
186     }
187
188     /**
189      * class of the form
190      *
191      * @return string class of the form
192      */
193     function formClass()
194     {
195         return 'form_settings';
196     }
197
198     /**
199      * Action of the form
200      *
201      * @return string URL of the action
202      */
203     function action()
204     {
205         return common_local_url('useradminpanel');
206     }
207
208     /**
209      * Data elements of the form
210      *
211      * @return void
212      */
213     function formData()
214     {
215         $this->out->elementStart('fieldset', array('id' => 'settings_user-profile'));
216         // TRANS: Fieldset legend in user administration panel.
217         $this->out->element('legend', null, _m('LEGEND','Profile'));
218         $this->out->elementStart('ul', 'form_data');
219
220         $this->li();
221         // TRANS: Field label in user admin panel for setting the character limit for the bio field.
222         $this->input('biolimit', _('Bio Limit'),
223                      // TRANS: Tooltip in user admin panel for setting the character limit for the bio field.
224                      _('Maximum length of a profile bio in characters.'),
225                      'profile');
226         $this->unli();
227
228         $this->out->elementEnd('ul');
229         $this->out->elementEnd('fieldset');
230
231         $this->out->elementStart('fieldset', array('id' => 'settings_user-newusers'));
232         // TRANS: Form legend in user admin panel.
233         $this->out->element('legend', null, _('New users'));
234         $this->out->elementStart('ul', 'form_data');
235
236         $this->li();
237         // TRANS: Field label in user admin panel for setting new user welcome text.
238         $this->input('welcome', _('New user welcome'),
239                      // TRANS: Tooltip in user admin panel for setting new user welcome text.
240                      _('Welcome text for new users (maximum 255 characters).'),
241                      'newuser');
242         $this->unli();
243
244         $this->li();
245         // TRANS: Field label in user admin panel for setting default subscription for new users.
246         $this->input('default', _('Default subscription'),
247                      // TRANS: Tooltip in user admin panel for setting default subscription for new users.
248                      _('Automatically subscribe new users to this user.'),
249                      'newuser');
250         $this->unli();
251
252         $this->out->elementEnd('ul');
253
254         $this->out->elementEnd('fieldset');
255
256         $this->out->elementStart('fieldset', array('id' => 'settings_user-invitations'));
257         // TRANS: Form legend in user admin panel.
258         $this->out->element('legend', null, _('Invitations'));
259         $this->out->elementStart('ul', 'form_data');
260
261         $this->li();
262
263         // TRANS: Field label for checkbox in user admin panel for allowing users to invite friend using site e-mail.
264         $this->out->checkbox('invite-enabled', _('Invitations enabled'),
265                               (bool) $this->value('enabled', 'invite'),
266                               // TRANS: Tooltip for checkbox in user admin panel for allowing users to invite friend using site e-mail.
267                               _('Whether to allow users to invite new users.'));
268         $this->unli();
269
270         $this->out->elementEnd('ul');
271         $this->out->elementEnd('fieldset');
272     }
273
274     /**
275      * Utility to simplify some of the duplicated code around
276      * params and settings.  Overrided from base class to be
277      * more specific about input ids.
278      *
279      * @param string $setting      Name of the setting
280      * @param string $title        Title to use for the input
281      * @param string $instructions Instructions for this field
282      * @param string $section      config section, default = 'site'
283      *
284      * @return void
285      */
286     function input($setting, $title, $instructions, $section='site')
287     {
288         $this->out->input("$section-$setting", $title, $this->value($setting, $section), $instructions);
289     }
290
291     /**
292      * Action elements
293      *
294      * @return void
295      */
296     function formActions()
297     {
298         $this->out->submit('submit',
299                            // TRANS: Button text to save user settings in user admin panel.
300                            _m('BUTTON','Save'),
301                            'submit',
302                            null,
303                            // TRANS: Button title to save user settings in user admin panel.
304                            _('Save user settings.'));
305     }
306 }