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/
49 class EditgroupAction extends GroupDesignAction
56 return sprintf(_('Edit %s group'), $this->group->nickname);
63 function prepare($args)
65 parent::prepare($args);
67 if (!common_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 $this->clientError(_('No nickname.'), 404);
88 $groupid = $this->trimmed('groupid');
91 $this->group = User_group::staticGet('id', $groupid);
93 $local = Local_group::staticGet('nickname', $nickname);
95 $this->group = User_group::staticGet('id', $local->group_id);
100 $this->clientError(_('No such group.'), 404);
104 $cur = common_current_user();
106 if (!$cur->isAdmin($this->group)) {
107 $this->clientError(_('You must be an admin to edit the group.'), 403);
117 * On GET, show the form. On POST, try to save the group.
119 * @param array $args unused
124 function handle($args)
126 parent::handle($args);
127 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
134 function showForm($msg=null)
140 function showLocalNav()
142 $nav = new GroupNav($this, $this->group);
146 function showContent()
148 $form = new GroupEditForm($this, $this->group);
152 function showPageNotice()
155 $this->element('p', 'error', $this->msg);
157 $this->element('p', 'instructions',
158 _('Use this form to edit the group.'));
162 function showScripts()
164 parent::showScripts();
165 $this->autofocus('nickname');
170 $cur = common_current_user();
171 if (!$cur->isAdmin($this->group)) {
172 $this->clientError(_('You must be an admin to edit the group.'), 403);
176 $nickname = common_canonical_nickname($this->trimmed('nickname'));
177 $fullname = $this->trimmed('fullname');
178 $homepage = $this->trimmed('homepage');
179 $description = $this->trimmed('description');
180 $location = $this->trimmed('location');
181 $aliasstring = $this->trimmed('aliases');
183 if (!Validate::string($nickname, array('min_length' => 1,
185 'format' => NICKNAME_FMT))) {
186 $this->showForm(_('Nickname must have only lowercase letters '.
187 'and numbers and no spaces.'));
189 } else if ($this->nicknameExists($nickname)) {
190 $this->showForm(_('Nickname already in use. Try another one.'));
192 } else if (!User_group::allowedNickname($nickname)) {
193 $this->showForm(_('Not a valid nickname.'));
195 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
196 !Validate::uri($homepage,
197 array('allowed_schemes' =>
198 array('http', 'https')))) {
199 $this->showForm(_('Homepage is not a valid URL.'));
201 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
202 $this->showForm(_('Full name is too long (maximum 255 characters).'));
204 } else if (User_group::descriptionTooLong($description)) {
205 $this->showForm(sprintf(_('Description is too long (max %d chars).'), User_group::maxDescription()));
207 } else if (!is_null($location) && mb_strlen($location) > 255) {
208 $this->showForm(_('Location is too long (maximum 255 characters).'));
212 if (!empty($aliasstring)) {
213 $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
218 if (count($aliases) > common_config('group', 'maxaliases')) {
219 $this->showForm(sprintf(_('Too many aliases! Maximum %d.'),
220 common_config('group', 'maxaliases')));
224 foreach ($aliases as $alias) {
225 if (!Validate::string($alias, array('min_length' => 1,
227 'format' => NICKNAME_FMT))) {
228 $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
231 if ($this->nicknameExists($alias)) {
232 $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
236 // XXX assumes alphanum nicknames
237 if (strcmp($alias, $nickname) == 0) {
238 $this->showForm(_('Alias can\'t be the same as nickname.'));
243 $this->group->query('BEGIN');
245 $orig = clone($this->group);
247 $this->group->nickname = $nickname;
248 $this->group->fullname = $fullname;
249 $this->group->homepage = $homepage;
250 $this->group->description = $description;
251 $this->group->location = $location;
252 $this->group->mainpage = common_local_url('showgroup', array('nickname' => $nickname));
254 $result = $this->group->update($orig);
257 common_log_db_error($this->group, 'UPDATE', __FILE__);
258 $this->serverError(_('Could not update group.'));
261 $result = $this->group->setAliases($aliases);
264 $this->serverError(_('Could not create aliases.'));
267 if ($nickname != $orig->nickname) {
268 common_log(LOG_INFO, "Saving local group info.");
269 $local = Local_group::staticGet('group_id', $this->group->id);
270 $local->setNickname($nickname);
273 $this->group->query('COMMIT');
275 if ($this->group->nickname != $orig->nickname) {
276 common_redirect(common_local_url('editgroup',
277 array('nickname' => $nickname)),
280 $this->showForm(_('Options saved.'));
284 function nicknameExists($nickname)
286 $group = Local_group::staticGet('nickname', $nickname);
288 if (!empty($group) &&
289 $group->group_id != $this->group->id) {
293 $alias = Group_alias::staticGet('alias', $nickname);
295 if (!empty($alias) &&
296 $alias->group_id != $this->group->id) {