3 * StatusNet, the distributed open-source microblogging tool
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.
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.
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/>.
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/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
38 * This is the form for adding a new group
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/
46 class NewgroupAction extends Action
52 // TRANS: Title for form to create a group.
53 return _('New group');
59 function prepare($args)
61 parent::prepare($args);
63 if (!common_logged_in()) {
64 // TRANS: Client error displayed trying to create a group while not logged in.
65 $this->clientError(_('You must be logged in to create a group.'));
69 $user = common_current_user();
70 $profile = $user->getProfile();
71 if (!$profile->hasRight(Right::CREATEGROUP)) {
72 // TRANS: Client exception thrown when a user tries to create a group while banned.
73 throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
82 * On GET, show the form. On POST, try to save the group.
84 * @param array $args unused
88 function handle($args)
90 parent::handle($args);
91 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
98 function showForm($msg=null)
104 function showContent()
106 $form = new GroupEditForm($this);
110 function showPageNotice()
113 $this->element('p', 'error', $this->msg);
115 $this->element('p', 'instructions',
116 // TRANS: Form instructions for group create form.
117 _('Use this form to create a new group.'));
123 if (Event::handle('StartGroupSaveForm', array($this))) {
125 $nickname = Nickname::normalize($this->trimmed('newnickname'));
126 } catch (NicknameException $e) {
127 $this->showForm($e->getMessage());
130 $fullname = $this->trimmed('fullname');
131 $homepage = $this->trimmed('homepage');
132 $description = $this->trimmed('description');
133 $location = $this->trimmed('location');
134 $private = $this->boolean('private');
135 $aliasstring = $this->trimmed('aliases');
137 if ($this->nicknameExists($nickname)) {
138 // TRANS: Group create form validation error.
139 $this->showForm(_('Nickname already in use. Try another one.'));
141 } else if (!User_group::allowedNickname($nickname)) {
142 // TRANS: Group create form validation error.
143 $this->showForm(_('Not a valid nickname.'));
145 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
146 !Validate::uri($homepage,
147 array('allowed_schemes' =>
148 array('http', 'https')))) {
149 // TRANS: Group create form validation error.
150 $this->showForm(_('Homepage is not a valid URL.'));
152 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
153 // TRANS: Group create form validation error.
154 $this->showForm(_('Full name is too long (maximum 255 characters).'));
156 } else if (User_group::descriptionTooLong($description)) {
157 // TRANS: Group create form validation error.
158 // TRANS: %d is the maximum number of allowed characters.
159 $this->showForm(sprintf(_m('Description is too long (maximum %d character).',
160 'Description is too long (maximum %d characters).',
161 User_group::maxDescription()),
162 User_group::maxDescription()));
164 } else if (!is_null($location) && mb_strlen($location) > 255) {
165 // TRANS: Group create form validation error.
166 $this->showForm(_('Location is too long (maximum 255 characters).'));
170 if (!empty($aliasstring)) {
171 $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
176 if (count($aliases) > common_config('group', 'maxaliases')) {
177 // TRANS: Group create form validation error.
178 // TRANS: %d is the maximum number of allowed aliases.
179 $this->showForm(sprintf(_m('Too many aliases! Maximum %d allowed.',
180 'Too many aliases! Maximum %d allowed.',
181 common_config('group', 'maxaliases')),
182 common_config('group', 'maxaliases')));
186 foreach ($aliases as $alias) {
187 if (!Nickname::isValid($alias)) {
188 // TRANS: Group create form validation error.
189 // TRANS: %s is the invalid alias.
190 $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
193 if ($this->nicknameExists($alias)) {
194 // TRANS: Group create form validation error. %s is the already used alias.
195 $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
199 // XXX assumes alphanum nicknames
200 if (strcmp($alias, $nickname) == 0) {
201 // TRANS: Group create form validation error.
202 $this->showForm(_('Alias cannot be the same as nickname.'));
209 $join_policy = User_group::JOIN_POLICY_MODERATE;
212 $join_policy = User_group::JOIN_POLICY_OPEN;
215 $cur = common_current_user();
217 // Checked in prepare() above
219 assert(!is_null($cur));
221 $group = User_group::register(array('nickname' => $nickname,
222 'fullname' => $fullname,
223 'homepage' => $homepage,
224 'description' => $description,
225 'location' => $location,
226 'aliases' => $aliases,
227 'userid' => $cur->id,
228 'join_policy' => $join_policy,
229 'force_scope' => $force_scope,
232 $this->group = $group;
234 Event::handle('EndGroupSaveForm', array($this));
236 common_redirect($group->homeUrl(), 303);
240 function nicknameExists($nickname)
242 $local = Local_group::staticGet('nickname', $nickname);
244 if (!empty($local)) {
248 $alias = Group_alias::staticGet('alias', $nickname);
250 if (!empty($alias)) {