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($args)
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.'));
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);
76 public function showContent()
78 $form = new GroupEditForm($this);
82 public function showInstructions()
84 $this->element('p', 'instructions',
85 // TRANS: Form instructions for group create form.
86 _('Use this form to create a new group.'));
89 protected function handlePost()
93 if (Event::handle('StartGroupSaveForm', array($this))) {
94 $nickname = Nickname::normalize($this->trimmed('newnickname'));
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');
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).'));
130 if (!empty($aliasstring)) {
131 $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
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')));
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));
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.'),
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.'));
166 $join_policy = User_group::JOIN_POLICY_MODERATE;
169 $join_policy = User_group::JOIN_POLICY_OPEN;
172 // This is set up in parent->prepare and checked in self->prepare
173 assert(!is_null($this->scoped));
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,
186 $this->group = $group;
188 Event::handle('EndGroupSaveForm', array($this));
190 common_redirect($group->homeUrl(), 303);
194 function nicknameExists($nickname)
196 $local = Local_group::getKV('nickname', $nickname);
198 if (!empty($local)) {
202 $alias = Group_alias::getKV('alias', $nickname);
204 if (!empty($alias)) {