newgroup.php 7.96 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('STATUSNET')) {
Evan Prodromou's avatar
Evan Prodromou committed
33 34 35 36 37 38 39 40 41
    exit(1);
}

/**
 * Add a new group
 *
 * This is the form for adding a new group
 *
 * @category Group
42
 * @package  StatusNet
43
 * @author   Evan Prodromou <evan@status.net>
Evan Prodromou's avatar
Evan Prodromou committed
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/
Evan Prodromou's avatar
Evan Prodromou committed
46
 */
47
class NewgroupAction extends FormAction
Evan Prodromou's avatar
Evan Prodromou committed
48 49 50
{
    function title()
    {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
51
        // TRANS: Title for form to create a group.
Evan Prodromou's avatar
Evan Prodromou committed
52 53 54 55 56 57
        return _('New group');
    }

    /**
     * Prepare to run
     */
58
    protected function prepare(array $args=array())
Evan Prodromou's avatar
Evan Prodromou committed
59 60 61
    {
        parent::prepare($args);

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

Evan Prodromou's avatar
Evan Prodromou committed
68 69 70
        return true;
    }

71
    public function showContent()
Evan Prodromou's avatar
Evan Prodromou committed
72 73 74 75 76
    {
        $form = new GroupEditForm($this);
        $form->show();
    }

77
    public function showInstructions()
Evan Prodromou's avatar
Evan Prodromou committed
78
    {
79 80 81
        $this->element('p', 'instructions',
                       // TRANS: Form instructions for group create form.
                       _('Use this form to create a new group.'));
Evan Prodromou's avatar
Evan Prodromou committed
82 83
    }

84
    protected function handlePost()
Evan Prodromou's avatar
Evan Prodromou committed
85
    {
86 87
        parent::handlePost();

88
        if (Event::handle('StartGroupSaveForm', array($this))) {
89 90
            $nickname = Nickname::normalize($this->trimmed('newnickname'));

91 92 93 94
            $fullname    = $this->trimmed('fullname');
            $homepage    = $this->trimmed('homepage');
            $description = $this->trimmed('description');
            $location    = $this->trimmed('location');
95
            $private     = $this->boolean('private');
96
            $aliasstring = $this->trimmed('aliases');
97

98
            if ($this->nicknameExists($nickname)) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
99
                // TRANS: Group create form validation error.
100
                throw new ClientException(_('Nickname already in use. Try another one.'));
101 102
            } else if (!User_group::allowedNickname($nickname)) {
                // TRANS: Group create form validation error.
103
                throw new ClientException(_('Not a valid nickname.'));
104
            } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
105
                       !common_valid_http_url($homepage)) {
106
                // TRANS: Group create form validation error.
107
                throw new ClientException(_('Homepage is not a valid URL.'));
108 109
            } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
                // TRANS: Group create form validation error.
110
                throw new ClientException(_('Full name is too long (maximum 255 characters).'));
111 112 113
            } else if (User_group::descriptionTooLong($description)) {
                // TRANS: Group create form validation error.
                // TRANS: %d is the maximum number of allowed characters.
114
                throw new ClientException(sprintf(_m('Description is too long (maximum %d character).',
115 116 117 118 119
                                           '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.
120
                throw new ClientException(_('Location is too long (maximum 255 characters).'));
121
            }
122 123 124 125 126 127 128 129

            if (!empty($aliasstring)) {
                $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
            } else {
                $aliases = array();
            }

            if (count($aliases) > common_config('group', 'maxaliases')) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
130
                // TRANS: Group create form validation error.
131
                // TRANS: %d is the maximum number of allowed aliases.
132
                throw new ClientException(sprintf(_m('Too many aliases! Maximum %d allowed.',
133 134 135
                                           'Too many aliases! Maximum %d allowed.',
                                           common_config('group', 'maxaliases')),
                                        common_config('group', 'maxaliases')));
136 137 138
                return;
            }

139 140 141 142
            foreach ($aliases as $alias) {
                if (!Nickname::isValid($alias)) {
                    // TRANS: Group create form validation error.
                    // TRANS: %s is the invalid alias.
143
                    throw new ClientException(sprintf(_('Invalid alias: "%s"'), $alias));
144 145 146
                }
                if ($this->nicknameExists($alias)) {
                    // TRANS: Group create form validation error. %s is the already used alias.
147
                    throw new ClientException(sprintf(_('Alias "%s" already in use. Try another one.'),
148 149 150 151 152
                                            $alias));
                }
                // XXX assumes alphanum nicknames
                if (strcmp($alias, $nickname) == 0) {
                    // TRANS: Group create form validation error.
153
                    throw new ClientException(_('Alias cannot be the same as nickname.'));
154 155 156
                }
            }

157 158 159 160 161 162 163 164
            if ($private) {
                $force_scope = 1;
                $join_policy = User_group::JOIN_POLICY_MODERATE;
            } else {
                $force_scope = 0;
                $join_policy = User_group::JOIN_POLICY_OPEN;
            }

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

168 169 170 171 172 173
            $group = User_group::register(array('nickname' => $nickname,
                                                'fullname' => $fullname,
                                                'homepage' => $homepage,
                                                'description' => $description,
                                                'location' => $location,
                                                'aliases'  => $aliases,
174
                                                'userid'   => $this->scoped->id,
175
                                                'join_policy' => $join_policy,
176
                                                'force_scope' => $force_scope,
177
                                                'local'    => true));
Evan Prodromou's avatar
Evan Prodromou committed
178

179 180 181 182 183 184
            $this->group = $group;

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

            common_redirect($group->homeUrl(), 303);
        }
Evan Prodromou's avatar
Evan Prodromou committed
185 186 187 188
    }

    function nicknameExists($nickname)
    {
189
        $local = Local_group::getKV('nickname', $nickname);
190

191
        if (!empty($local)) {
192 193 194
            return true;
        }

195
        $alias = Group_alias::getKV('alias', $nickname);
196 197 198 199 200 201

        if (!empty($alias)) {
            return true;
        }

        return false;
Evan Prodromou's avatar
Evan Prodromou committed
202
    }
203
}