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')) {
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 FormAction
50 // TRANS: Title for form to create a group.
51 return _('New group');
57 protected function prepare(array $args=array())
59 parent::prepare($args);
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.'), 403);
66 // $this->scoped is the current user profile
67 if (!$this->scoped->hasRight(Right::CREATEGROUP)) {
68 // TRANS: Client exception thrown when a user tries to create a group while banned.
69 $this->clientError(_('You are not allowed to create groups on this site.'), 403);
75 public function showContent()
77 $form = new GroupEditForm($this);
81 public function showInstructions()
83 $this->element('p', 'instructions',
84 // TRANS: Form instructions for group create form.
85 _('Use this form to create a new group.'));
88 protected function handlePost()
92 if (Event::handle('StartGroupSaveForm', array($this))) {
93 $nickname = Nickname::normalize($this->trimmed('newnickname'));
95 $fullname = $this->trimmed('fullname');
96 $homepage = $this->trimmed('homepage');
97 $description = $this->trimmed('description');
98 $location = $this->trimmed('location');
99 $private = $this->boolean('private');
100 $aliasstring = $this->trimmed('aliases');
102 if ($this->nicknameExists($nickname)) {
103 // TRANS: Group create form validation error.
104 throw new ClientException(_('Nickname already in use. Try another one.'));
105 } else if (!User_group::allowedNickname($nickname)) {
106 // TRANS: Group create form validation error.
107 throw new ClientException(_('Not a valid nickname.'));
108 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
109 !Validate::uri($homepage,
110 array('allowed_schemes' =>
111 array('http', 'https')))) {
112 // TRANS: Group create form validation error.
113 throw new ClientException(_('Homepage is not a valid URL.'));
114 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
115 // TRANS: Group create form validation error.
116 throw new ClientException(_('Full name is too long (maximum 255 characters).'));
117 } else if (User_group::descriptionTooLong($description)) {
118 // TRANS: Group create form validation error.
119 // TRANS: %d is the maximum number of allowed characters.
120 throw new ClientException(sprintf(_m('Description is too long (maximum %d character).',
121 'Description is too long (maximum %d characters).',
122 User_group::maxDescription()),
123 User_group::maxDescription()));
124 } else if (!is_null($location) && mb_strlen($location) > 255) {
125 // TRANS: Group create form validation error.
126 throw new ClientException(_('Location is too long (maximum 255 characters).'));
129 if (!empty($aliasstring)) {
130 $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
135 if (count($aliases) > common_config('group', 'maxaliases')) {
136 // TRANS: Group create form validation error.
137 // TRANS: %d is the maximum number of allowed aliases.
138 throw new ClientException(sprintf(_m('Too many aliases! Maximum %d allowed.',
139 'Too many aliases! Maximum %d allowed.',
140 common_config('group', 'maxaliases')),
141 common_config('group', 'maxaliases')));
145 foreach ($aliases as $alias) {
146 if (!Nickname::isValid($alias)) {
147 // TRANS: Group create form validation error.
148 // TRANS: %s is the invalid alias.
149 throw new ClientException(sprintf(_('Invalid alias: "%s"'), $alias));
151 if ($this->nicknameExists($alias)) {
152 // TRANS: Group create form validation error. %s is the already used alias.
153 throw new ClientException(sprintf(_('Alias "%s" already in use. Try another one.'),
156 // XXX assumes alphanum nicknames
157 if (strcmp($alias, $nickname) == 0) {
158 // TRANS: Group create form validation error.
159 throw new ClientException(_('Alias cannot be the same as nickname.'));
165 $join_policy = User_group::JOIN_POLICY_MODERATE;
168 $join_policy = User_group::JOIN_POLICY_OPEN;
171 // This is set up in parent->prepare and checked in self->prepare
172 assert(!is_null($this->scoped));
174 $group = User_group::register(array('nickname' => $nickname,
175 'fullname' => $fullname,
176 'homepage' => $homepage,
177 'description' => $description,
178 'location' => $location,
179 'aliases' => $aliases,
180 'userid' => $this->scoped->id,
181 'join_policy' => $join_policy,
182 'force_scope' => $force_scope,
185 $this->group = $group;
187 Event::handle('EndGroupSaveForm', array($this));
189 common_redirect($group->homeUrl(), 303);
193 function nicknameExists($nickname)
195 $local = Local_group::getKV('nickname', $nickname);
197 if (!empty($local)) {
201 $alias = Group_alias::getKV('alias', $nickname);
203 if (!empty($alias)) {