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 * @copyright 2013 Free Software Foundation, Inc.
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://status.net/
32 if (!defined('STATUSNET')) {
39 * This is the form for adding a new group
43 * @author Evan Prodromou <evan@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
47 class NewgroupAction extends FormAction
51 // TRANS: Title for form to create a group.
52 return _('New group');
58 protected function prepare(array $args=array())
60 parent::prepare($args);
62 // $this->scoped is the current user profile
63 if (!$this->scoped->hasRight(Right::CREATEGROUP)) {
64 // TRANS: Client exception thrown when a user tries to create a group while banned.
65 $this->clientError(_('You are not allowed to create groups on this site.'), 403);
71 public function showContent()
73 $form = new GroupEditForm($this);
77 public function showInstructions()
79 $this->element('p', 'instructions',
80 // TRANS: Form instructions for group create form.
81 _('Use this form to create a new group.'));
84 protected function handlePost()
88 if (Event::handle('StartGroupSaveForm', array($this))) {
89 $nickname = Nickname::normalize($this->trimmed('newnickname'));
91 $fullname = $this->trimmed('fullname');
92 $homepage = $this->trimmed('homepage');
93 $description = $this->trimmed('description');
94 $location = $this->trimmed('location');
95 $private = $this->boolean('private');
96 $aliasstring = $this->trimmed('aliases');
98 if ($this->nicknameExists($nickname)) {
99 // TRANS: Group create form validation error.
100 throw new ClientException(_('Nickname already in use. Try another one.'));
101 } else if (!User_group::allowedNickname($nickname)) {
102 // TRANS: Group create form validation error.
103 throw new ClientException(_('Not a valid nickname.'));
104 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
105 !Validate::uri($homepage,
106 array('allowed_schemes' =>
107 array('http', 'https')))) {
108 // TRANS: Group create form validation error.
109 throw new ClientException(_('Homepage is not a valid URL.'));
110 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
111 // TRANS: Group create form validation error.
112 throw new ClientException(_('Full name is too long (maximum 255 characters).'));
113 } else if (User_group::descriptionTooLong($description)) {
114 // TRANS: Group create form validation error.
115 // TRANS: %d is the maximum number of allowed characters.
116 throw new ClientException(sprintf(_m('Description is too long (maximum %d character).',
117 'Description is too long (maximum %d characters).',
118 User_group::maxDescription()),
119 User_group::maxDescription()));
120 } else if (!is_null($location) && mb_strlen($location) > 255) {
121 // TRANS: Group create form validation error.
122 throw new ClientException(_('Location is too long (maximum 255 characters).'));
125 if (!empty($aliasstring)) {
126 $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
131 if (count($aliases) > common_config('group', 'maxaliases')) {
132 // TRANS: Group create form validation error.
133 // TRANS: %d is the maximum number of allowed aliases.
134 throw new ClientException(sprintf(_m('Too many aliases! Maximum %d allowed.',
135 'Too many aliases! Maximum %d allowed.',
136 common_config('group', 'maxaliases')),
137 common_config('group', 'maxaliases')));
141 foreach ($aliases as $alias) {
142 if (!Nickname::isValid($alias)) {
143 // TRANS: Group create form validation error.
144 // TRANS: %s is the invalid alias.
145 throw new ClientException(sprintf(_('Invalid alias: "%s"'), $alias));
147 if ($this->nicknameExists($alias)) {
148 // TRANS: Group create form validation error. %s is the already used alias.
149 throw new ClientException(sprintf(_('Alias "%s" already in use. Try another one.'),
152 // XXX assumes alphanum nicknames
153 if (strcmp($alias, $nickname) == 0) {
154 // TRANS: Group create form validation error.
155 throw new ClientException(_('Alias cannot be the same as nickname.'));
161 $join_policy = User_group::JOIN_POLICY_MODERATE;
164 $join_policy = User_group::JOIN_POLICY_OPEN;
167 // This is set up in parent->prepare and checked in self->prepare
168 assert(!is_null($this->scoped));
170 $group = User_group::register(array('nickname' => $nickname,
171 'fullname' => $fullname,
172 'homepage' => $homepage,
173 'description' => $description,
174 'location' => $location,
175 'aliases' => $aliases,
176 'userid' => $this->scoped->id,
177 'join_policy' => $join_policy,
178 'force_scope' => $force_scope,
181 $this->group = $group;
183 Event::handle('EndGroupSaveForm', array($this));
185 common_redirect($group->homeUrl(), 303);
189 function nicknameExists($nickname)
191 $local = Local_group::getKV('nickname', $nickname);
193 if (!empty($local)) {
197 $alias = Group_alias::getKV('alias', $nickname);
199 if (!empty($alias)) {