newgroup.php 6.12 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1 2
<?php
/**
3
 * StatusNet, the distributed open-source microblogging tool
Evan Prodromou's avatar
Evan Prodromou committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Add a new group
 *
 * 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,
 * 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  Group
23
 * @package   StatusNet
24 25
 * @author    Evan Prodromou <evan@status.net>
 * @author    Sarven Capadisli <csarven@status.net>
26
 * @copyright 2008-2009 StatusNet, Inc.
27
 * @copyright 2013 Free Software Foundation, Inc.
Evan Prodromou's avatar
Evan Prodromou committed
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/
Evan Prodromou's avatar
Evan Prodromou committed
30 31
 */

32
if (!defined('GNUSOCIAL')) { exit(1); }
Evan Prodromou's avatar
Evan Prodromou committed
33 34 35 36 37 38 39

/**
 * Add a new group
 *
 * This is the form for adding a new group
 *
 * @category Group
40
 * @package  StatusNet
41
 * @author   Evan Prodromou <evan@status.net>
Evan Prodromou's avatar
Evan Prodromou committed
42
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43
 * @link     http://status.net/
Evan Prodromou's avatar
Evan Prodromou committed
44
 */
45
class NewgroupAction extends FormAction
Evan Prodromou's avatar
Evan Prodromou committed
46
{
47 48
    protected $group;

49 50
    protected $form = 'GroupEdit';

51 52 53 54
    function getGroup() {
        return $this->group;
    }

Evan Prodromou's avatar
Evan Prodromou committed
55 56
    function title()
    {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
57
        // TRANS: Title for form to create a group.
Evan Prodromou's avatar
Evan Prodromou committed
58 59 60
        return _('New group');
    }

61
    protected function doPreparation()
Evan Prodromou's avatar
Evan Prodromou committed
62
    {
63 64
        // $this->scoped is the current user profile
        if (!$this->scoped->hasRight(Right::CREATEGROUP)) {
65
            // TRANS: Client exception thrown when a user tries to create a group while banned.
66
            $this->clientError(_('You are not allowed to create groups on this site.'), 403);
67
        }
Evan Prodromou's avatar
Evan Prodromou committed
68 69
    }

70
    protected function getInstructions()
Evan Prodromou's avatar
Evan Prodromou committed
71
    {
72 73
        // TRANS: Form instructions for group create form.
        return _('Use this form to create a new group.');
Evan Prodromou's avatar
Evan Prodromou committed
74 75
    }

76
    protected function doPost()
Evan Prodromou's avatar
Evan Prodromou committed
77
    {
78
        if (Event::handle('StartGroupSaveForm', array($this))) {
79
            $nickname = Nickname::normalize($this->trimmed('newnickname'), true);
80

81 82 83 84
            $fullname    = $this->trimmed('fullname');
            $homepage    = $this->trimmed('homepage');
            $description = $this->trimmed('description');
            $location    = $this->trimmed('location');
85
            $private     = $this->boolean('private');
86
            $aliasstring = $this->trimmed('aliases');
87

88
            if (!is_null($homepage) && (strlen($homepage) > 0) &&
89
                       !common_valid_http_url($homepage)) {
90
                // TRANS: Group create form validation error.
91
                throw new ClientException(_('Homepage is not a valid URL.'));
92 93
            } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
                // TRANS: Group create form validation error.
94
                throw new ClientException(_('Full name is too long (maximum 255 characters).'));
95 96 97
            } else if (User_group::descriptionTooLong($description)) {
                // TRANS: Group create form validation error.
                // TRANS: %d is the maximum number of allowed characters.
98
                throw new ClientException(sprintf(_m('Description is too long (maximum %d character).',
99 100 101 102 103
                                           'Description is too long (maximum %d characters).',
                                           User_group::maxDescription()),
                                        User_group::maxDescription()));
            } else if (!is_null($location) && mb_strlen($location) > 255) {
                // TRANS: Group create form validation error.
104
                throw new ClientException(_('Location is too long (maximum 255 characters).'));
105
            }
106 107

            if (!empty($aliasstring)) {
108
                $aliases = array_map(array('Nickname', 'normalize'), array_unique(preg_split('/[\s,]+/', $aliasstring)));
109 110 111 112 113
            } else {
                $aliases = array();
            }

            if (count($aliases) > common_config('group', 'maxaliases')) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
114
                // TRANS: Group create form validation error.
115
                // TRANS: %d is the maximum number of allowed aliases.
116
                throw new ClientException(sprintf(_m('Too many aliases! Maximum %d allowed.',
117 118 119
                                           'Too many aliases! Maximum %d allowed.',
                                           common_config('group', 'maxaliases')),
                                        common_config('group', 'maxaliases')));
120 121
            }

122 123 124 125 126 127 128 129
            if ($private) {
                $force_scope = 1;
                $join_policy = User_group::JOIN_POLICY_MODERATE;
            } else {
                $force_scope = 0;
                $join_policy = User_group::JOIN_POLICY_OPEN;
            }

130 131
            // This is set up in parent->prepare and checked in self->prepare
            assert(!is_null($this->scoped));
Evan Prodromou's avatar
Evan Prodromou committed
132

133 134 135 136 137 138
            $group = User_group::register(array('nickname' => $nickname,
                                                'fullname' => $fullname,
                                                'homepage' => $homepage,
                                                'description' => $description,
                                                'location' => $location,
                                                'aliases'  => $aliases,
139
                                                'userid'   => $this->scoped->id,
140
                                                'join_policy' => $join_policy,
141
                                                'force_scope' => $force_scope,
142
                                                'local'    => true));
Evan Prodromou's avatar
Evan Prodromou committed
143

144 145 146 147 148 149
            $this->group = $group;

            Event::handle('EndGroupSaveForm', array($this));

            common_redirect($group->homeUrl(), 303);
        }
Evan Prodromou's avatar
Evan Prodromou committed
150
    }
151
}