3 * StatusNet, the distributed open-source microblogging tool
5 * Edit an existing group
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 * @author Zach Copley <zach@status.net>
27 * @copyright 2008-2009 StatusNet, 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') && !defined('LACONICA')) {
39 * This is the form for adding a new group
43 * @author Evan Prodromou <evan@status.net>
44 * @author Zach Copley <zach@status.net>
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46 * @link http://status.net/
48 class EditgroupAction extends GroupDesignAction
54 // TRANS: Title for form to edit a group. %s is a group nickname.
55 return sprintf(_('Edit %s group'), $this->group->nickname);
62 function prepare($args)
64 parent::prepare($args);
66 if (!common_logged_in()) {
67 // TRANS: Client error displayed trying to edit a group while not logged in.
68 $this->clientError(_('You must be logged in to create a group.'));
72 $nickname_arg = $this->trimmed('nickname');
73 $nickname = common_canonical_nickname($nickname_arg);
75 // Permanent redirect on non-canonical nickname
77 if ($nickname_arg != $nickname) {
78 $args = array('nickname' => $nickname);
79 common_redirect(common_local_url('editgroup', $args), 301);
84 // TRANS: Client error displayed trying to edit a group while not proving a nickname for the group to edit.
85 $this->clientError(_('No nickname.'), 404);
89 $groupid = $this->trimmed('groupid');
92 $this->group = User_group::staticGet('id', $groupid);
94 $local = Local_group::staticGet('nickname', $nickname);
96 $this->group = User_group::staticGet('id', $local->group_id);
101 // TRANS: Client error displayed trying to edit a non-existing group.
102 $this->clientError(_('No such group.'), 404);
106 $cur = common_current_user();
108 if (!$cur->isAdmin($this->group)) {
109 // TRANS: Client error displayed trying to edit a group while not being a group admin.
110 $this->clientError(_('You must be an admin to edit the group.'), 403);
120 * On GET, show the form. On POST, try to save the group.
122 * @param array $args unused
126 function handle($args)
128 parent::handle($args);
129 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
136 function showForm($msg=null)
142 function showLocalNav()
144 $nav = new GroupNav($this, $this->group);
148 function showContent()
150 $form = new GroupEditForm($this, $this->group);
154 function showPageNotice()
157 $this->element('p', 'error', $this->msg);
159 $this->element('p', 'instructions',
160 // TRANS: Form instructions for group edit form.
161 _('Use this form to edit the group.'));
165 function showScripts()
167 parent::showScripts();
168 $this->autofocus('nickname');
173 $cur = common_current_user();
174 if (!$cur->isAdmin($this->group)) {
175 // TRANS: Client error displayed trying to edit a group while not being a group admin.
176 $this->clientError(_('You must be an admin to edit the group.'), 403);
180 $nickname = common_canonical_nickname($this->trimmed('nickname'));
181 $fullname = $this->trimmed('fullname');
182 $homepage = $this->trimmed('homepage');
183 $description = $this->trimmed('description');
184 $location = $this->trimmed('location');
185 $aliasstring = $this->trimmed('aliases');
187 if (!Validate::string($nickname, array('min_length' => 1,
189 'format' => NICKNAME_FMT))) {
190 // TRANS: Group edit form validation error.
191 $this->showForm(_('Nickname must have only lowercase letters '.
192 'and numbers and no spaces.'));
194 } else if ($this->nicknameExists($nickname)) {
195 // TRANS: Group edit form validation error.
196 $this->showForm(_('Nickname already in use. Try another one.'));
198 } else if (!User_group::allowedNickname($nickname)) {
199 // TRANS: Group edit form validation error.
200 $this->showForm(_('Not a valid nickname.'));
202 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
203 !Validate::uri($homepage,
204 array('allowed_schemes' =>
205 array('http', 'https')))) {
206 // TRANS: Group edit form validation error.
207 $this->showForm(_('Homepage is not a valid URL.'));
209 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
210 // TRANS: Group edit form validation error.
211 $this->showForm(_('Full name is too long (maximum 255 characters).'));
213 } else if (User_group::descriptionTooLong($description)) {
214 $this->showForm(sprintf(
215 // TRANS: Group edit form validation error.
216 _m('Description is too long (maximum %d character).',
217 'Description is too long (maximum %d characters).',
218 User_group::maxDescription()),
219 User_group::maxDescription()));
221 } else if (!is_null($location) && mb_strlen($location) > 255) {
222 // TRANS: Group edit form validation error.
223 $this->showForm(_('Location is too long (maximum 255 characters).'));
227 if (!empty($aliasstring)) {
228 $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
233 if (count($aliases) > common_config('group', 'maxaliases')) {
234 // TRANS: Group edit form validation error.
235 // TRANS: %d is the maximum number of allowed aliases.
236 $this->showForm(sprintf(_m('Too many aliases! Maximum %d allowed.',
237 'Too many aliases! Maximum %d allowed.',
238 common_config('group', 'maxaliases')),
239 common_config('group', 'maxaliases')));
243 foreach ($aliases as $alias) {
244 if (!Validate::string($alias, array('min_length' => 1,
246 'format' => NICKNAME_FMT))) {
247 // TRANS: Group edit form validation error.
248 $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
251 if ($this->nicknameExists($alias)) {
252 // TRANS: Group edit form validation error.
253 $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
257 // XXX assumes alphanum nicknames
258 if (strcmp($alias, $nickname) == 0) {
259 // TRANS: Group edit form validation error.
260 $this->showForm(_('Alias can\'t be the same as nickname.'));
265 $this->group->query('BEGIN');
267 $orig = clone($this->group);
269 $this->group->nickname = $nickname;
270 $this->group->fullname = $fullname;
271 $this->group->homepage = $homepage;
272 $this->group->description = $description;
273 $this->group->location = $location;
274 $this->group->mainpage = common_local_url('showgroup', array('nickname' => $nickname));
276 $result = $this->group->update($orig);
279 common_log_db_error($this->group, 'UPDATE', __FILE__);
280 // TRANS: Server error displayed when editing a group fails.
281 $this->serverError(_('Could not update group.'));
284 $result = $this->group->setAliases($aliases);
287 // TRANS: Server error displayed when group aliases could not be added.
288 $this->serverError(_('Could not create aliases.'));
291 if ($nickname != $orig->nickname) {
292 common_log(LOG_INFO, "Saving local group info.");
293 $local = Local_group::staticGet('group_id', $this->group->id);
294 $local->setNickname($nickname);
297 $this->group->query('COMMIT');
299 if ($this->group->nickname != $orig->nickname) {
300 common_redirect(common_local_url('editgroup',
301 array('nickname' => $nickname)),
304 // TRANS: Group edit form success message.
305 $this->showForm(_('Options saved.'));
309 function nicknameExists($nickname)
311 $group = Local_group::staticGet('nickname', $nickname);
313 if (!empty($group) &&
314 $group->group_id != $this->group->id) {
318 $alias = Group_alias::staticGet('alias', $nickname);
320 if (!empty($alias) &&
321 $alias->group_id != $this->group->id) {