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') && !defined('GNUSOCIAL')) {
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 * @author Alexei Sorokin <sor.alexei@meowr.ru>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
49 class EditgroupAction extends GroupAction
51 public $message = null;
52 public $success = null;
53 protected $canPost = true;
55 public function title()
57 // TRANS: Title for form to edit a group. %s is a group nickname.
58 return sprintf(_('Edit %s group'), $this->group->nickname);
61 public function showContent()
63 $form = new GroupEditForm($this, $this->group);
67 public function showPageNoticeBlock()
69 parent::showPageNoticeBlock();
74 ($this->success) ? 'success' : 'error',
81 // TRANS: Form instructions for group edit form.
82 _('Use this form to edit the group.')
87 public function showScripts()
89 parent::showScripts();
90 $this->autofocus('fullname');
97 * @throws ClientException
98 * @throws NicknameException
101 protected function prepare(array $args = [])
103 parent::prepare($args);
105 if (!common_logged_in()) {
106 // TRANS: Client error displayed trying to edit a group while not logged in.
107 $this->clientError(_('You must be logged in to create a group.'));
110 $nickname_arg = $this->trimmed('nickname');
111 $nickname = common_canonical_nickname($nickname_arg);
113 // Permanent redirect on non-canonical nickname
115 if ($nickname_arg != $nickname) {
116 $args = ['nickname' => $nickname];
117 common_redirect(common_local_url('editgroup', $args), 301);
121 // TRANS: Client error displayed trying to edit a group while not proving a nickname for the group to edit.
122 $this->clientError(_('No nickname.'), 404);
125 $groupid = $this->trimmed('groupid');
128 $this->group = User_group::getKV('id', $groupid);
130 $local = Local_group::getKV('nickname', $nickname);
132 $this->group = User_group::getKV('id', $local->group_id);
137 // TRANS: Client error displayed trying to edit a non-existing group.
138 $this->clientError(_('No such group.'), 404);
141 $cur = common_current_user();
143 if (!$cur->isAdmin($this->group)) {
144 // TRANS: Client error displayed trying to edit a group while not being a group admin.
145 $this->clientError(_('You must be an admin to edit the group.'), 403);
151 protected function handlePost()
153 parent::handlePost();
155 $cur = common_current_user();
156 if (!$cur->isAdmin($this->group)) {
157 // TRANS: Client error displayed trying to edit a group while not being a group admin.
158 $this->clientError(_('You must be an admin to edit the group.'), 403);
161 if (Event::handle('StartGroupSaveForm', [$this])) {
163 // $nickname will only be set if this changenick value is true.
165 if (common_config('profile', 'changenick') == true) {
167 $nickname = Nickname::normalize($this->trimmed('newnickname'), true);
168 } catch (NicknameTakenException $e) {
169 // Abort only if the nickname is occupied by _another_ group
170 if ($e->profile->id != $this->group->profile_id) {
171 $this->setMessage($e->getMessage(), true);
174 $nickname = Nickname::normalize($this->trimmed('newnickname')); // without in-use check this time
175 } catch (NicknameException $e) {
176 $this->setMessage($e->getMessage(), true);
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');
186 $private = $this->boolean('private');
190 $join_policy = User_group::JOIN_POLICY_MODERATE;
193 $join_policy = User_group::JOIN_POLICY_OPEN;
196 if (!is_null($homepage) && (strlen($homepage) > 0) &&
197 !common_valid_http_url($homepage)) {
198 // TRANS: Group edit form validation error.
199 $this->setMessage(_('Homepage is not a valid URL.'), true);
201 } elseif (!is_null($fullname) && mb_strlen($fullname) > 255) {
202 // TRANS: Group edit form validation error.
203 $this->setMessage(_('Full name is too long (maximum 255 characters).'), true);
205 } elseif (User_group::descriptionTooLong($description)) {
206 $this->setMessage(sprintf(
207 // TRANS: Group edit form validation error.
209 'Description is too long (maximum %d character).',
210 'Description is too long (maximum %d characters).',
211 User_group::maxDescription()
213 User_group::maxDescription()
216 } elseif (!is_null($location) && mb_strlen($location) > 255) {
217 // TRANS: Group edit form validation error.
218 $this->setMessage(_('Location is too long (maximum 255 characters).'), true);
222 if (!empty($aliasstring)) {
223 $aliases = array_map(
224 ['Nickname', 'normalize'],
225 array_unique(preg_split('/[\s,]+/', $aliasstring))
231 if (count($aliases) > common_config('group', 'maxaliases')) {
232 // TRANS: Group edit form validation error.
233 // TRANS: %d is the maximum number of allowed aliases.
234 $this->setMessage(sprintf(
236 'Too many aliases! Maximum %d allowed.',
237 'Too many aliases! Maximum %d allowed.',
238 common_config('group', 'maxaliases')
240 common_config('group', 'maxaliases')
245 $this->group->query('BEGIN');
247 $orig = clone($this->group);
249 if (common_config('profile', 'changenick') == true && $this->group->nickname !== $nickname) {
250 assert(Nickname::normalize($nickname) === $nickname);
251 common_debug("Changing group nickname from '{$this->group->nickname}' to '{$nickname}'.");
252 $this->group->nickname = $nickname;
253 $this->group->mainpage = common_local_url('showgroup', ['nickname' => $this->group->nickname]);
255 $this->group->fullname = $fullname;
256 $this->group->homepage = $homepage;
257 $this->group->description = $description;
258 $this->group->location = $location;
259 $this->group->join_policy = $join_policy;
260 $this->group->force_scope = $force_scope;
262 $result = $this->group->update($orig);
264 if ($result === false) {
265 common_log_db_error($this->group, 'UPDATE', __FILE__);
266 // TRANS: Server error displayed when editing a group fails.
267 $this->serverError(_('Could not update group.'));
270 $result = $this->group->setAliases($aliases);
273 // TRANS: Server error displayed when group aliases could not be added.
274 $this->serverError(_('Could not create aliases.'));
277 $this->group->query('COMMIT');
279 Event::handle('EndGroupSaveForm', [$this]);
281 if ($this->group->nickname != $orig->nickname) {
282 common_redirect(common_local_url('editgroup', ['nickname' => $this->group->nickname]), 303);
286 // TRANS: Group edit form success message.
287 $this->setMessage(_('Options saved.'));
290 public function setMessage($msg, $error = false)
292 $this->message = $msg;
293 $this->success = !$error;