]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newgroup.php
NewgroupAction converted to extend FormAction
[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  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Add a new group
37  *
38  * This is the form for adding a new group
39  *
40  * @category Group
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46 class NewgroupAction extends FormAction
47 {
48     function title()
49     {
50         // TRANS: Title for form to create a group.
51         return _('New group');
52     }
53
54     /**
55      * Prepare to run
56      */
57     protected function prepare($args)
58     {
59         parent::prepare($args);
60
61         if (!common_logged_in()) {
62             // TRANS: Client error displayed trying to create a group while not logged in.
63             $this->clientError(_('You must be logged in to create a group.'));
64             return false;
65         }
66
67         // $this->scoped is the current user profile
68         if (!$this->scoped->hasRight(Right::CREATEGROUP)) {
69             // TRANS: Client exception thrown when a user tries to create a group while banned.
70             throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
71         }
72
73         return true;
74     }
75
76     public function showContent()
77     {
78         $form = new GroupEditForm($this);
79         $form->show();
80     }
81
82     public function showInstructions()
83     {
84         $this->element('p', 'instructions',
85                        // TRANS: Form instructions for group create form.
86                        _('Use this form to create a new group.'));
87     }
88
89     protected function handlePost()
90     {
91         parent::handlePost();
92
93         if (Event::handle('StartGroupSaveForm', array($this))) {
94             $nickname = Nickname::normalize($this->trimmed('newnickname'));
95
96             $fullname    = $this->trimmed('fullname');
97             $homepage    = $this->trimmed('homepage');
98             $description = $this->trimmed('description');
99             $location    = $this->trimmed('location');
100             $private     = $this->boolean('private');
101             $aliasstring = $this->trimmed('aliases');
102
103             if ($this->nicknameExists($nickname)) {
104                 // TRANS: Group create form validation error.
105                 throw new ClientException(_('Nickname already in use. Try another one.'));
106             } else if (!User_group::allowedNickname($nickname)) {
107                 // TRANS: Group create form validation error.
108                 throw new ClientException(_('Not a valid nickname.'));
109             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
110                        !Validate::uri($homepage,
111                                       array('allowed_schemes' =>
112                                             array('http', 'https')))) {
113                 // TRANS: Group create form validation error.
114                 throw new ClientException(_('Homepage is not a valid URL.'));
115             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
116                 // TRANS: Group create form validation error.
117                 throw new ClientException(_('Full name is too long (maximum 255 characters).'));
118             } else if (User_group::descriptionTooLong($description)) {
119                 // TRANS: Group create form validation error.
120                 // TRANS: %d is the maximum number of allowed characters.
121                 throw new ClientException(sprintf(_m('Description is too long (maximum %d character).',
122                                            'Description is too long (maximum %d characters).',
123                                            User_group::maxDescription()),
124                                         User_group::maxDescription()));
125             } else if (!is_null($location) && mb_strlen($location) > 255) {
126                 // TRANS: Group create form validation error.
127                 throw new ClientException(_('Location is too long (maximum 255 characters).'));
128             }
129
130             if (!empty($aliasstring)) {
131                 $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
132             } else {
133                 $aliases = array();
134             }
135
136             if (count($aliases) > common_config('group', 'maxaliases')) {
137                 // TRANS: Group create form validation error.
138                 // TRANS: %d is the maximum number of allowed aliases.
139                 throw new ClientException(sprintf(_m('Too many aliases! Maximum %d allowed.',
140                                            'Too many aliases! Maximum %d allowed.',
141                                            common_config('group', 'maxaliases')),
142                                         common_config('group', 'maxaliases')));
143                 return;
144             }
145
146             foreach ($aliases as $alias) {
147                 if (!Nickname::isValid($alias)) {
148                     // TRANS: Group create form validation error.
149                     // TRANS: %s is the invalid alias.
150                     throw new ClientException(sprintf(_('Invalid alias: "%s"'), $alias));
151                 }
152                 if ($this->nicknameExists($alias)) {
153                     // TRANS: Group create form validation error. %s is the already used alias.
154                     throw new ClientException(sprintf(_('Alias "%s" already in use. Try another one.'),
155                                             $alias));
156                 }
157                 // XXX assumes alphanum nicknames
158                 if (strcmp($alias, $nickname) == 0) {
159                     // TRANS: Group create form validation error.
160                     throw new ClientException(_('Alias cannot be the same as nickname.'));
161                 }
162             }
163
164             if ($private) {
165                 $force_scope = 1;
166                 $join_policy = User_group::JOIN_POLICY_MODERATE;
167             } else {
168                 $force_scope = 0;
169                 $join_policy = User_group::JOIN_POLICY_OPEN;
170             }
171
172             // This is set up in parent->prepare and checked in self->prepare
173             assert(!is_null($this->scoped));
174
175             $group = User_group::register(array('nickname' => $nickname,
176                                                 'fullname' => $fullname,
177                                                 'homepage' => $homepage,
178                                                 'description' => $description,
179                                                 'location' => $location,
180                                                 'aliases'  => $aliases,
181                                                 'userid'   => $this->scoped->id,
182                                                 'join_policy' => $join_policy,
183                                                 'force_scope' => $force_scope,
184                                                 'local'    => true));
185
186             $this->group = $group;
187
188             Event::handle('EndGroupSaveForm', array($this));
189
190             common_redirect($group->homeUrl(), 303);
191         }
192     }
193
194     function nicknameExists($nickname)
195     {
196         $local = Local_group::getKV('nickname', $nickname);
197
198         if (!empty($local)) {
199             return true;
200         }
201
202         $alias = Group_alias::getKV('alias', $nickname);
203
204         if (!empty($alias)) {
205             return true;
206         }
207
208         return false;
209     }
210 }