]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/useradminpanel.php
Merge remote branch 'gitorious/0.9.x' into 0.9.x
[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
49 class UseradminpanelAction extends AdminPanelAction
50 {
51     /**
52      * Returns the page title
53      *
54      * @return string page title
55      */
56
57     function title()
58     {
59         // TRANS: User admin panel title
60         return _m('TITLE', 'User');
61     }
62
63     /**
64      * Instructions for using this form.
65      *
66      * @return string instructions
67      */
68
69     function getInstructions()
70     {
71         return _('User settings for this StatusNet site');
72     }
73
74     /**
75      * Show the site admin panel form
76      *
77      * @return void
78      */
79
80     function showForm()
81     {
82         $form = new UserAdminPanelForm($this);
83         $form->show();
84         return;
85     }
86
87     /**
88      * Save settings from the form
89      *
90      * @return void
91      */
92
93     function saveSettings()
94     {
95         static $settings = array(
96                 'profile' => array('biolimit'),
97                 'newuser' => array('welcome', 'default')
98         );
99
100         static $booleans = array(
101             'invite' => array('enabled')
102         );
103
104         $values = array();
105
106         foreach ($settings as $section => $parts) {
107             foreach ($parts as $setting) {
108                 $values[$section][$setting] = $this->trimmed("$section-$setting");
109             }
110         }
111
112         foreach ($booleans as $section => $parts) {
113             foreach ($parts as $setting) {
114                 $values[$section][$setting] = ($this->boolean("$section-$setting")) ? 1 : 0;
115             }
116         }
117
118         // This throws an exception on validation errors
119
120         $this->validate($values);
121
122         // assert(all values are valid);
123
124         $config = new Config();
125
126         $config->query('BEGIN');
127
128         foreach ($settings as $section => $parts) {
129             foreach ($parts as $setting) {
130                 Config::save($section, $setting, $values[$section][$setting]);
131             }
132         }
133
134         foreach ($booleans as $section => $parts) {
135             foreach ($parts as $setting) {
136                 Config::save($section, $setting, $values[$section][$setting]);
137             }
138         }
139
140         $config->query('COMMIT');
141
142         return;
143     }
144
145     function validate(&$values)
146     {
147         // Validate biolimit
148
149         if (!Validate::number($values['profile']['biolimit'])) {
150             $this->clientError(_("Invalid bio limit. Must be numeric."));
151         }
152
153         // Validate welcome text
154
155         if (mb_strlen($values['newuser']['welcome']) > 255) {
156             $this->clientError(_("Invalid welcome text. Max length is 255 characters."));
157         }
158
159         // Validate default subscription
160
161         if (!empty($values['newuser']['default'])) {
162             $defuser = User::staticGet('nickname', trim($values['newuser']['default']));
163             if (empty($defuser)) {
164                 $this->clientError(
165                     sprintf(
166                         _('Invalid default subscripton: \'%1$s\' is not user.'),
167                         $values['newuser']['default']
168                     )
169                 );
170             }
171         }
172     }
173 }
174
175 class UserAdminPanelForm extends AdminForm
176 {
177     /**
178      * ID of the form
179      *
180      * @return int ID of the form
181      */
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
194     function formClass()
195     {
196         return 'form_settings';
197     }
198
199     /**
200      * Action of the form
201      *
202      * @return string URL of the action
203      */
204
205     function action()
206     {
207         return common_local_url('useradminpanel');
208     }
209
210     /**
211      * Data elements of the form
212      *
213      * @return void
214      */
215
216     function formData()
217     {
218         $this->out->elementStart('fieldset', array('id' => 'settings_user-profile'));
219         $this->out->element('legend', null, _('Profile'));
220         $this->out->elementStart('ul', 'form_data');
221
222         $this->li();
223         $this->input('biolimit', _('Bio Limit'),
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         $this->out->element('legend', null, _('New users'));
233         $this->out->elementStart('ul', 'form_data');
234
235         $this->li();
236         $this->input('welcome', _('New user welcome'),
237                      _('Welcome text for new users (Max 255 chars).'),
238                      'newuser');
239         $this->unli();
240
241         $this->li();
242         $this->input('default', _('Default subscription'),
243                      _('Automatically subscribe new users to this user.'),
244                      'newuser');
245         $this->unli();
246
247         $this->out->elementEnd('ul');
248
249         $this->out->elementEnd('fieldset');
250
251         $this->out->elementStart('fieldset', array('id' => 'settings_user-invitations'));
252         $this->out->element('legend', null, _('Invitations'));
253         $this->out->elementStart('ul', 'form_data');
254
255         $this->li();
256
257         $this->out->checkbox('invite-enabled', _('Invitations enabled'),
258                               (bool) $this->value('enabled', 'invite'),
259                               _('Whether to allow users to invite new users.'));
260         $this->unli();
261
262         $this->out->elementEnd('ul');
263         $this->out->elementEnd('fieldset');
264
265
266
267     }
268
269     /**
270      * Utility to simplify some of the duplicated code around
271      * params and settings.  Overrided from base class to be
272      * more specific about input ids.
273      *
274      * @param string $setting      Name of the setting
275      * @param string $title        Title to use for the input
276      * @param string $instructions Instructions for this field
277      * @param string $section      config section, default = 'site'
278      *
279      * @return void
280      */
281
282     function input($setting, $title, $instructions, $section='site')
283     {
284         $this->out->input("$section-$setting", $title, $this->value($setting, $section), $instructions);
285     }
286
287     /**
288      * Action elements
289      *
290      * @return void
291      */
292
293     function formActions()
294     {
295         $this->out->submit('submit', _('Save'), 'submit', null, _('Save user settings'));
296     }
297 }