3 * Laconica, 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@controlyourself.ca>
25 * @author Sarven Capadisli <csarven@controlyourself.ca>
26 * @author Zach Copley <zach@controlyourself.ca>
27 * @copyright 2008-2009 Control Yourself, Inc.
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://laconi.ca/
32 if (!defined('LACONICA')) {
39 * This is the form for adding a new group
43 * @author Evan Prodromou <evan@controlyourself.ca>
44 * @author Zach Copley <zach@controlyourself.ca>
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46 * @link http://laconi.ca/
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_config('inboxes','enabled')) {
68 $this->serverError(_('Inboxes must be enabled for groups to work'));
72 if (!common_logged_in()) {
73 $this->clientError(_('You must be logged in to create a group.'));
77 $nickname_arg = $this->trimmed('nickname');
78 $nickname = common_canonical_nickname($nickname_arg);
80 // Permanent redirect on non-canonical nickname
82 if ($nickname_arg != $nickname) {
83 $args = array('nickname' => $nickname);
84 common_redirect(common_local_url('editgroup', $args), 301);
89 $this->clientError(_('No nickname'), 404);
93 $groupid = $this->trimmed('groupid');
95 $this->group = User_group::staticGet('id', $groupid);
97 $this->group = User_group::staticGet('nickname', $nickname);
101 $this->clientError(_('No such group'), 404);
105 $cur = common_current_user();
107 if (!$cur->isAdmin($this->group)) {
108 $this->clientError(_('You must be an admin to edit the group'), 403);
118 * On GET, show the form. On POST, try to save the group.
120 * @param array $args unused
125 function handle($args)
127 parent::handle($args);
128 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
135 function showForm($msg=null)
141 function showLocalNav()
143 $nav = new GroupNav($this, $this->group);
147 function showContent()
149 $form = new GroupEditForm($this, $this->group);
153 function showPageNotice()
156 $this->element('p', 'error', $this->msg);
158 $this->element('p', 'instructions',
159 _('Use this form to edit the group.'));
165 $cur = common_current_user();
166 if (!$cur->isAdmin($this->group)) {
167 $this->clientError(_('You must be an admin to edit the group'), 403);
171 $nickname = common_canonical_nickname($this->trimmed('nickname'));
172 $fullname = $this->trimmed('fullname');
173 $homepage = $this->trimmed('homepage');
174 $description = $this->trimmed('description');
175 $location = $this->trimmed('location');
176 $aliasstring = $this->trimmed('aliases');
178 if (!Validate::string($nickname, array('min_length' => 1,
180 'format' => NICKNAME_FMT))) {
181 $this->showForm(_('Nickname must have only lowercase letters '.
182 'and numbers and no spaces.'));
184 } else if ($this->nicknameExists($nickname)) {
185 $this->showForm(_('Nickname already in use. Try another one.'));
187 } else if (!User_group::allowedNickname($nickname)) {
188 $this->showForm(_('Not a valid nickname.'));
190 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
191 !Validate::uri($homepage,
192 array('allowed_schemes' =>
193 array('http', 'https')))) {
194 $this->showForm(_('Homepage is not a valid URL.'));
196 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
197 $this->showForm(_('Full name is too long (max 255 chars).'));
199 } else if (!is_null($description) && mb_strlen($description) > 140) {
200 $this->showForm(_('description is too long (max 140 chars).'));
202 } else if (!is_null($location) && mb_strlen($location) > 255) {
203 $this->showForm(_('Location is too long (max 255 chars).'));
207 if (!empty($aliasstring)) {
208 $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
213 if (count($aliases) > common_config('group', 'maxaliases')) {
214 $this->showForm(sprintf(_('Too many aliases! Maximum %d.'),
215 common_config('group', 'maxaliases')));
219 foreach ($aliases as $alias) {
220 if (!Validate::string($alias, array('min_length' => 1,
222 'format' => NICKNAME_FMT))) {
223 $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
226 if ($this->nicknameExists($alias)) {
227 $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
231 // XXX assumes alphanum nicknames
232 if (strcmp($alias, $nickname) == 0) {
233 $this->showForm(_('Alias can\'t be the same as nickname.'));
238 $this->group->query('BEGIN');
240 $orig = clone($this->group);
242 $this->group->nickname = $nickname;
243 $this->group->fullname = $fullname;
244 $this->group->homepage = $homepage;
245 $this->group->description = $description;
246 $this->group->location = $location;
247 $this->group->created = common_sql_now();
249 $result = $this->group->update($orig);
252 common_log_db_error($this->group, 'UPDATE', __FILE__);
253 $this->serverError(_('Could not update group.'));
256 $result = $this->group->setAliases($aliases);
259 $this->serverError(_('Could not create aliases.'));
262 $this->group->query('COMMIT');
264 if ($this->group->nickname != $orig->nickname) {
265 common_redirect(common_local_url('editgroup',
266 array('nickname' => $nickname)),
269 $this->showForm(_('Options saved.'));
273 function nicknameExists($nickname)
275 $group = User_group::staticGet('nickname', $nickname);
277 if (!empty($group) &&
278 $group->id != $this->group->id) {
282 $alias = Group_alias::staticGet('alias', $nickname);
284 if (!empty($alias) &&
285 $alias->group_id != $this->group->id) {