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