useradminpanel.php 9.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * User administration panel
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
15
 *
16 17 18 19 20 21 22 23 24 25 26 27
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Settings
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
 * @author    Zach Copley <zach@status.net>
 * @author    Sarven Capadisli <csarven@status.net>
28
 * @copyright 2008-2010 StatusNet, Inc.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

if (!defined('STATUSNET')) {
    exit(1);
}

/**
 * Administer user settings
 *
 * @category Admin
 * @package  StatusNet
 * @author   Evan Prodromou <evan@status.net>
 * @author   Zach Copley <zach@status.net>
 * @author   Sarven Capadisli <csarven@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link     http://status.net/
 */
class UseradminpanelAction extends AdminPanelAction
{
    /**
     * Returns the page title
     *
     * @return string page title
     */
    function title()
    {
57
        // TRANS: User admin panel title.
58
        return _m('TITLE', 'User');
59 60 61 62 63 64 65 66 67
    }

    /**
     * Instructions for using this form.
     *
     * @return string instructions
     */
    function getInstructions()
    {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
68
        // TRANS: Instruction for user admin panel.
69
        return _('User settings for this StatusNet site');
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    }

    /**
     * Show the site admin panel form
     *
     * @return void
     */
    function showForm()
    {
        $form = new UserAdminPanelForm($this);
        $form->show();
        return;
    }

    /**
     * Save settings from the form
     *
     * @return void
     */
    function saveSettings()
    {
Zach Copley's avatar
Zach Copley committed
91 92 93 94 95 96 97 98
        static $settings = array(
                'profile' => array('biolimit'),
                'newuser' => array('welcome', 'default')
        );

        static $booleans = array(
            'invite' => array('enabled')
        );
99 100 101

        $values = array();

Zach Copley's avatar
Zach Copley committed
102 103 104 105 106 107 108 109 110 111
        foreach ($settings as $section => $parts) {
            foreach ($parts as $setting) {
                $values[$section][$setting] = $this->trimmed("$section-$setting");
            }
        }

        foreach ($booleans as $section => $parts) {
            foreach ($parts as $setting) {
                $values[$section][$setting] = ($this->boolean("$section-$setting")) ? 1 : 0;
            }
112 113 114 115 116 117 118 119 120 121 122 123
        }

        // This throws an exception on validation errors

        $this->validate($values);

        // assert(all values are valid);

        $config = new Config();

        $config->query('BEGIN');

Zach Copley's avatar
Zach Copley committed
124 125 126 127 128 129 130 131 132 133
        foreach ($settings as $section => $parts) {
            foreach ($parts as $setting) {
                Config::save($section, $setting, $values[$section][$setting]);
            }
        }

        foreach ($booleans as $section => $parts) {
            foreach ($parts as $setting) {
                Config::save($section, $setting, $values[$section][$setting]);
            }
134 135 136 137 138 139 140 141 142
        }

        $config->query('COMMIT');

        return;
    }

    function validate(&$values)
    {
143 144 145
        // Validate biolimit

        if (!Validate::number($values['profile']['biolimit'])) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
146 147
            // TRANS: Form validation error in user admin panel when a non-numeric character limit was set.
            $this->clientError(_('Invalid bio limit. Must be numeric.'));
148 149 150 151 152
        }

        // Validate welcome text

        if (mb_strlen($values['newuser']['welcome']) > 255) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
153 154
            // TRANS: Form validation error in user admin panel when welcome text is too long.
            $this->clientError(_('Invalid welcome text. Maximum length is 255 characters.'));
155 156 157 158 159 160 161 162 163
        }

        // Validate default subscription

        if (!empty($values['newuser']['default'])) {
            $defuser = User::staticGet('nickname', trim($values['newuser']['default']));
            if (empty($defuser)) {
                $this->clientError(
                    sprintf(
Siebrand Mazeland's avatar
Siebrand Mazeland committed
164 165
                        // TRANS: Client error displayed when trying to set a non-existing user as default subscription for new
                        // TRANS: users in user admin panel. %1$s is the invalid nickname.
166
                        _('Invalid default subscripton: "%1$s" is not a user.'),
167 168 169 170 171
                        $values['newuser']['default']
                    )
                );
            }
        }
172 173 174
    }
}

175
// @todo FIXME: Class documentation missing.
Zach Copley's avatar
Zach Copley committed
176
class UserAdminPanelForm extends AdminForm
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
{
    /**
     * ID of the form
     *
     * @return int ID of the form
     */
    function id()
    {
        return 'useradminpanel';
    }

    /**
     * class of the form
     *
     * @return string class of the form
     */
    function formClass()
    {
Zach Copley's avatar
Zach Copley committed
195
        return 'form_settings';
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    }

    /**
     * Action of the form
     *
     * @return string URL of the action
     */
    function action()
    {
        return common_local_url('useradminpanel');
    }

    /**
     * Data elements of the form
     *
     * @return void
     */
    function formData()
    {
Zach Copley's avatar
Zach Copley committed
215
        $this->out->elementStart('fieldset', array('id' => 'settings_user-profile'));
216 217
        // TRANS: Fieldset legend in user administration panel.
        $this->out->element('legend', null, _m('LEGEND','Profile'));
Zach Copley's avatar
Zach Copley committed
218 219
        $this->out->elementStart('ul', 'form_data');

220
        $this->li();
Siebrand Mazeland's avatar
Siebrand Mazeland committed
221
        // TRANS: Field label in user admin panel for setting the character limit for the bio field.
Zach Copley's avatar
Zach Copley committed
222
        $this->input('biolimit', _('Bio Limit'),
Siebrand Mazeland's avatar
Siebrand Mazeland committed
223
                     // TRANS: Tooltip in user admin panel for setting the character limit for the bio field.
Zach Copley's avatar
Zach Copley committed
224 225 226 227 228 229 230 231
                     _('Maximum length of a profile bio in characters.'),
                     'profile');
        $this->unli();

        $this->out->elementEnd('ul');
        $this->out->elementEnd('fieldset');

        $this->out->elementStart('fieldset', array('id' => 'settings_user-newusers'));
Siebrand Mazeland's avatar
Siebrand Mazeland committed
232
        // TRANS: Form legend in user admin panel.
Zach Copley's avatar
Zach Copley committed
233 234
        $this->out->element('legend', null, _('New users'));
        $this->out->elementStart('ul', 'form_data');
235

Zach Copley's avatar
Zach Copley committed
236
        $this->li();
Siebrand Mazeland's avatar
Siebrand Mazeland committed
237
        // TRANS: Field label in user admin panel for setting new user welcome text.
Zach Copley's avatar
Zach Copley committed
238
        $this->input('welcome', _('New user welcome'),
Siebrand Mazeland's avatar
Siebrand Mazeland committed
239 240
                     // TRANS: Tooltip in user admin panel for setting new user welcome text.
                     _('Welcome text for new users (maximum 255 characters).'),
Zach Copley's avatar
Zach Copley committed
241 242
                     'newuser');
        $this->unli();
243

Zach Copley's avatar
Zach Copley committed
244
        $this->li();
Siebrand Mazeland's avatar
Siebrand Mazeland committed
245
        // TRANS: Field label in user admin panel for setting default subscription for new users.
Zach Copley's avatar
Zach Copley committed
246
        $this->input('default', _('Default subscription'),
Siebrand Mazeland's avatar
Siebrand Mazeland committed
247
                     // TRANS: Tooltip in user admin panel for setting default subscription for new users.
Zach Copley's avatar
Zach Copley committed
248 249
                     _('Automatically subscribe new users to this user.'),
                     'newuser');
250
        $this->unli();
Zach Copley's avatar
Zach Copley committed
251 252 253 254 255 256

        $this->out->elementEnd('ul');

        $this->out->elementEnd('fieldset');

        $this->out->elementStart('fieldset', array('id' => 'settings_user-invitations'));
Siebrand Mazeland's avatar
Siebrand Mazeland committed
257
        // TRANS: Form legend in user admin panel.
Zach Copley's avatar
Zach Copley committed
258 259 260
        $this->out->element('legend', null, _('Invitations'));
        $this->out->elementStart('ul', 'form_data');

261 262
        $this->li();

Siebrand Mazeland's avatar
Siebrand Mazeland committed
263
        // TRANS: Field label for checkbox in user admin panel for allowing users to invite friend using site e-mail.
Zach Copley's avatar
Zach Copley committed
264 265
        $this->out->checkbox('invite-enabled', _('Invitations enabled'),
                              (bool) $this->value('enabled', 'invite'),
Siebrand Mazeland's avatar
Siebrand Mazeland committed
266
                              // TRANS: Tooltip for checkbox in user admin panel for allowing users to invite friend using site e-mail.
Zach Copley's avatar
Zach Copley committed
267 268 269 270 271
                              _('Whether to allow users to invite new users.'));
        $this->unli();

        $this->out->elementEnd('ul');
        $this->out->elementEnd('fieldset');
272 273 274 275
    }

    /**
     * Utility to simplify some of the duplicated code around
Zach Copley's avatar
Zach Copley committed
276 277
     * params and settings.  Overrided from base class to be
     * more specific about input ids.
278 279 280 281
     *
     * @param string $setting      Name of the setting
     * @param string $title        Title to use for the input
     * @param string $instructions Instructions for this field
Zach Copley's avatar
Zach Copley committed
282
     * @param string $section      config section, default = 'site'
283 284 285
     *
     * @return void
     */
Zach Copley's avatar
Zach Copley committed
286
    function input($setting, $title, $instructions, $section='site')
287
    {
Zach Copley's avatar
Zach Copley committed
288
        $this->out->input("$section-$setting", $title, $this->value($setting, $section), $instructions);
289 290 291 292 293 294 295 296 297
    }

    /**
     * Action elements
     *
     * @return void
     */
    function formActions()
    {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
298 299 300 301 302 303
        $this->out->submit('submit',
                           // TRANS: Button text to save user settings in user admin panel.
                           _m('BUTTON','Save'),
                           'submit',
                           null,
                           // TRANS: Title for button to save user settings in user admin panel.
304
                           _('Save user settings.'));
305 306
    }
}