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-2011 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 GroupAction
54 // TRANS: Title for form to edit a group. %s is a group nickname.
55 return sprintf(_('Edit %s group'), $this->group->nickname);
62 protected function prepare(array $args=array())
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::getKV('id', $groupid);
94 $local = Local_group::getKV('nickname', $nickname);
96 $this->group = User_group::getKV('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.
124 protected function handle()
127 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
134 function showForm($msg=null)
140 function showContent()
142 $form = new GroupEditForm($this, $this->group);
146 function showPageNotice()
149 $this->element('p', 'error', $this->msg);
151 $this->element('p', 'instructions',
152 // TRANS: Form instructions for group edit form.
153 _('Use this form to edit the group.'));
157 function showScripts()
159 parent::showScripts();
160 $this->autofocus('newnickname');
165 $cur = common_current_user();
166 if (!$cur->isAdmin($this->group)) {
167 // TRANS: Client error displayed trying to edit a group while not being a group admin.
168 $this->clientError(_('You must be an admin to edit the group.'), 403);
171 if (Event::handle('StartGroupSaveForm', array($this))) {
173 $nickname = $this->trimmed('newnickname');
175 $nickname = Nickname::normalize($nickname, true);
176 } catch (NicknameTakenException $e) {
177 // Abort only if the nickname is occupied by _another_ group
178 if ($e->profile->id != $this->group->profile_id) {
179 $this->showForm($e->getMessage());
182 $nickname = Nickname::normalize($nickname); // without in-use check this time
183 } catch (NicknameException $e) {
184 $this->showForm($e->getMessage());
188 $fullname = $this->trimmed('fullname');
189 $homepage = $this->trimmed('homepage');
190 $description = $this->trimmed('description');
191 $location = $this->trimmed('location');
192 $aliasstring = $this->trimmed('aliases');
193 $private = $this->boolean('private');
197 $join_policy = User_group::JOIN_POLICY_MODERATE;
200 $join_policy = User_group::JOIN_POLICY_OPEN;
203 if (!is_null($homepage) && (strlen($homepage) > 0) &&
204 !common_valid_http_url($homepage)) {
205 // TRANS: Group edit form validation error.
206 $this->showForm(_('Homepage is not a valid URL.'));
208 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
209 // TRANS: Group edit form validation error.
210 $this->showForm(_('Full name is too long (maximum 255 characters).'));
212 } else if (User_group::descriptionTooLong($description)) {
213 $this->showForm(sprintf(
214 // TRANS: Group edit form validation error.
215 _m('Description is too long (maximum %d character).',
216 'Description is too long (maximum %d characters).',
217 User_group::maxDescription()),
218 User_group::maxDescription()));
220 } else if (!is_null($location) && mb_strlen($location) > 255) {
221 // TRANS: Group edit form validation error.
222 $this->showForm(_('Location is too long (maximum 255 characters).'));
226 if (!empty($aliasstring)) {
227 $aliases = array_map(array('Nickname', 'normalize'),
228 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 $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));
253 $this->group->join_policy = $join_policy;
254 $this->group->force_scope = $force_scope;
256 $result = $this->group->update($orig);
258 if ($result === false) {
259 common_log_db_error($this->group, 'UPDATE', __FILE__);
260 // TRANS: Server error displayed when editing a group fails.
261 $this->serverError(_('Could not update group.'));
264 $result = $this->group->setAliases($aliases);
267 // TRANS: Server error displayed when group aliases could not be added.
268 $this->serverError(_('Could not create aliases.'));
271 $this->group->query('COMMIT');
273 Event::handle('EndGroupSaveForm', array($this));
276 if ($this->group->nickname != $orig->nickname) {
277 common_redirect(common_local_url('editgroup',
278 array('nickname' => $nickname)),
281 // TRANS: Group edit form success message.
282 $this->showForm(_('Options saved.'));