]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newgroup.php
Type-hint is array here.
[quix0rs-gnu-social.git] / actions / newgroup.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Add a new group
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  Group
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @copyright 2013 Free Software Foundation, 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('GNUSOCIAL')) { exit(1); }
33
34 /**
35  * Add a new group
36  *
37  * This is the form for adding a new group
38  *
39  * @category Group
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
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/
44  */
45 class NewgroupAction extends FormAction
46 {
47     protected $group;
48
49     protected $form = 'GroupEdit';
50
51     function getGroup() {
52         return $this->group;
53     }
54
55     function title()
56     {
57         // TRANS: Title for form to create a group.
58         return _('New group');
59     }
60
61     protected function doPreparation()
62     {
63         // $this->scoped is the current user profile
64         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         }
68     }
69
70     protected function getInstructions()
71     {
72         // TRANS: Form instructions for group create form.
73         return _('Use this form to create a new group.');
74     }
75
76     protected function doPost()
77     {
78         if (Event::handle('StartGroupSaveForm', array($this))) {
79             $nickname = Nickname::normalize($this->trimmed('newnickname'), true);
80
81             $fullname    = $this->trimmed('fullname');
82             $homepage    = $this->trimmed('homepage');
83             $description = $this->trimmed('description');
84             $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             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
93                 // TRANS: Group create form validation error.
94                 throw new ClientException(_('Full name is too long (maximum 255 characters).'));
95             } else if (User_group::descriptionTooLong($description)) {
96                 // TRANS: Group create form validation error.
97                 // TRANS: %d is the maximum number of allowed characters.
98                 throw new ClientException(sprintf(_m('Description is too long (maximum %d character).',
99                                            'Description is too long (maximum %d characters).',
100                                            User_group::maxDescription()),
101                                         User_group::maxDescription()));
102             } else if (!is_null($location) && mb_strlen($location) > 255) {
103                 // 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             } else {
110                 $aliases = array();
111             }
112
113             if (count($aliases) > common_config('group', 'maxaliases')) {
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                                            'Too many aliases! Maximum %d allowed.',
118                                            common_config('group', 'maxaliases')),
119                                         common_config('group', 'maxaliases')));
120             }
121
122             if ($private) {
123                 $force_scope = 1;
124                 $join_policy = User_group::JOIN_POLICY_MODERATE;
125             } else {
126                 $force_scope = 0;
127                 $join_policy = User_group::JOIN_POLICY_OPEN;
128             }
129
130             // This is set up in parent->prepare and checked in self->prepare
131             assert(!is_null($this->scoped));
132
133             $group = User_group::register(array('nickname' => $nickname,
134                                                 'fullname' => $fullname,
135                                                 'homepage' => $homepage,
136                                                 'description' => $description,
137                                                 'location' => $location,
138                                                 'aliases'  => $aliases,
139                                                 'userid'   => $this->scoped->id,
140                                                 'join_policy' => $join_policy,
141                                                 'force_scope' => $force_scope,
142                                                 'local'    => true));
143
144             $this->group = $group;
145
146             Event::handle('EndGroupSaveForm', array($this));
147
148             common_redirect($group->homeUrl(), 303);
149         }
150     }
151 }