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('nickname'));
126 } catch (NicknameException $e) {
127 $this->showForm($e->getMessage());
129 $fullname = $this->trimmed('fullname');
130 $homepage = $this->trimmed('homepage');
131 $description = $this->trimmed('description');
132 $location = $this->trimmed('location');
133 $aliasstring = $this->trimmed('aliases');
135 if ($this->nicknameExists($nickname)) {
136 // TRANS: Group create form validation error.
137 $this->showForm(_('Nickname already in use. Try another one.'));
139 } else if (!User_group::allowedNickname($nickname)) {
140 // TRANS: Group create form validation error.
141 $this->showForm(_('Not a valid nickname.'));
143 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
144 !Validate::uri($homepage,
145 array('allowed_schemes' =>
146 array('http', 'https')))) {
147 // TRANS: Group create form validation error.
148 $this->showForm(_('Homepage is not a valid URL.'));
150 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
151 // TRANS: Group create form validation error.
152 $this->showForm(_('Full name is too long (maximum 255 characters).'));
154 } else if (User_group::descriptionTooLong($description)) {
155 // TRANS: Group create form validation error.
156 // TRANS: %d is the maximum number of allowed characters.
157 $this->showForm(sprintf(_m('Description is too long (maximum %d character).',
158 'Description is too long (maximum %d characters).',
159 User_group::maxDescription()),
160 User_group::maxDescription()));
162 } else if (!is_null($location) && mb_strlen($location) > 255) {
163 // TRANS: Group create form validation error.
164 $this->showForm(_('Location is too long (maximum 255 characters).'));
168 if (!empty($aliasstring)) {
169 $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
174 if (count($aliases) > common_config('group', 'maxaliases')) {
175 // TRANS: Group create form validation error.
176 // TRANS: %d is the maximum number of allowed aliases.
177 $this->showForm(sprintf(_m('Too many aliases! Maximum %d allowed.',
178 'Too many aliases! Maximum %d allowed.',
179 common_config('group', 'maxaliases')),
180 common_config('group', 'maxaliases')));
184 foreach ($aliases as $alias) {
185 if (!Nickname::isValid($alias)) {
186 // TRANS: Group create form validation error.
187 // TRANS: %s is the invalid alias.
188 $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
191 if ($this->nicknameExists($alias)) {
192 // TRANS: Group create form validation error. %s is the already used alias.
193 $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
197 // XXX assumes alphanum nicknames
198 if (strcmp($alias, $nickname) == 0) {
199 // TRANS: Group create form validation error.
200 $this->showForm(_('Alias cannot be the same as nickname.'));
205 $cur = common_current_user();
207 // Checked in prepare() above
209 assert(!is_null($cur));
211 $group = User_group::register(array('nickname' => $nickname,
212 'fullname' => $fullname,
213 'homepage' => $homepage,
214 'description' => $description,
215 'location' => $location,
216 'aliases' => $aliases,
217 'userid' => $cur->id,
220 $this->group = $group;
222 Event::handle('EndGroupSaveForm', array($this));
224 common_redirect($group->homeUrl(), 303);
228 function nicknameExists($nickname)
230 $local = Local_group::staticGet('nickname', $nickname);
232 if (!empty($local)) {
236 $alias = Group_alias::staticGet('alias', $nickname);
238 if (!empty($alias)) {